正常情况下挂载磁盘,我们可以直接写在 /etc/fstab
中,然而在某些特殊情况下,数据盘挂载需要在操作系统正常启动之后才允许挂载。
## check_mount.sh
#!/bin/bash
MOUNT_DIR="/data1"
DISK_UUID="6fff8524-d014-4e34-9b93-fbf60fdb03cd"
cmd="mountpoint $MOUNT_DIR"
echo $cmd
# run command
$cmd
# get command return code
code=$?
# 检测磁盘是否已经挂载
if [ $code -eq 0 ]; then
echo "disk already mounted"
else
# 如果磁盘没有正常挂载,则执行挂载命令
mount_cmd="mount -t xfs UUID=\"$DISK_UUID\" /data1"
echo $mount_cmd
$mount_cmd
fi
编写启动服务,开机执行一次完成自动挂载
## mount_once.service
[Unit]
Description=Mount data disk to /data1
After=local-fs.target
After=network.target
[Service]
ExecStart=/etc/data1/check_mount.sh
RemainAfterExit=true
Type=oneshot
[Install]
WantedBy=multi-user.target
配置服务
cp mount_once.service /etc/systemd/system
systemctl enable mount_once.service
systemctl start mount_once.service