Linux 磁盘开机自动挂载_使用systemctl方法替代fstab

为什么?

fstab 是专门处理磁盘分区的开机自动挂载的解决方案,
那为什么还要使用 systemctl, 难为自己写service脚本?

第一瓢凉水:
当磁盘供电不足时(磁盘反复掉盘), fstab方案, 重启机器, Linux能开机吗?

第二瓢凉水:
当磁盘故障时, fstab方案, 重启机器, Linux能开机吗?

解决思路, 不管磁盘如何变化, Linux主机开机必须完成初始化.

插入新盘

dmesg 查看盘符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[chunli@localhost blog]$ dmesg
[10653688.163026] usb 4-3: new SuperSpeed USB device number 3 using xhci_hcd
[10653688.174849] usb 4-3: New USB device found, idVendor=174c, idProduct=55aa
[10653688.174853] usb 4-3: New USB device strings: Mfr=2, Product=3, SerialNumber=1
[10653688.174856] usb 4-3: Product: ASMT1153e
[10653688.174859] usb 4-3: Manufacturer: asmedia
[10653688.174870] usb 4-3: SerialNumber: 1234567891C7
[10653688.176552] scsi host12: uas
[10653688.276057] scsi 12:0:0:0: Direct-Access ST950042 0AS 0 PQ: 0 ANSI: 6
[10653688.287390] sd 12:0:0:0: Attached scsi generic sg5 type 0
[10653688.287993] sd 12:0:0:0: [sdh] 976773168 512-byte logical blocks: (500 GB/465 GiB)
[10653688.291822] sd 12:0:0:0: [sdh] Write Protect is off
[10653688.291827] sd 12:0:0:0: [sdh] Mode Sense: 43 00 00 00
[10653688.292055] sd 12:0:0:0: [sdh] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[10653688.321809] sdh:
[10653688.322746] sd 12:0:0:0: [sdh] Attached SCSI disk

格式化分区

1
[root@localhost data]# mkfs.xfs /dev/sdh

获取 uuid

1
2
3
[root@localhost data]# blkid
/dev/sdh: UUID="e46f958a-9bba-4abb-b9d1-8660478c86fa" TYPE="xfs"
[root@localhost data]#

创建service 脚本

关机需要等待 umount超时, 约4分钟才能关机

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost data]# cat /lib/systemd/system/disk_mine.service
[Unit]
Description=mine
After=network.target

[Service]
Type=oneshot
User=root
ExecStartPre=/bin/sleep 45
ExecStart=/bin/mount -U "e46f958a-9bba-4abb-b9d1-8660478c86fa" /mnt/mine/

[Install]
WantedBy=multi-user.target

设置开机自启

1
2
3
4
5
6
7
[root@localhost data]# systemctl enable disk_mine
Created symlink from /etc/systemd/system/multi-user.target.wants/disk_mine.service to /usr/lib/systemd/system/disk_mine.service.
[root@localhost data]#

# 设置挂载点
[root@localhost data]# mkdir /mnt/mine
[root@localhost data]#

启动验证

1
2
3
4
5
6
7
8
9
10

# 启动服务
[root@localhost data]# systemctl start disk_mine


# 验证
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sdh 466G 86G 381G 19% /mnt/mine
[root@localhost ~]#