文章目录
一、挂载本地磁盘
二、挂载网络文件系统
三、自动挂载
四、开启关停
五、重要事项:
为什么使用systemd来挂载文件系统而不使用fstab?使用fstab的时候如果设备被移除会导致系统启动失败,无法进入系统(只能进入临时修复系统),systemd则没有这个问题。
一、挂载本地磁盘
#vim /etc/systemd/system/xxx-xxx.mount
以下为配置内容:
[Unit]
Description = mount disk
[Mount]
What = /dev/sdb4
Where = /mnt/data
Type = ext4
Options = defaults
[Install]
WantedBy = local-fs.target
二、挂载网络文件系统
#vim /etc/systemd/system/xxx-xxx.mount
[Unit]
Description = Mount NFS disk
[Mount]
What = nfs.example.com:/export/scratch
Where = /mnt/data/nfs
Type = nfs
Options = defaults
[Install]
WantedBy = multi-user.target
三、自动挂载
#vim /etc/systemd/system/xxx-xxx-xxx.automount
[Unit]
Description = Auto Mount NFS disk
[Automount]
Where = /mnt/data/nfs
[Install]
WantedBy = multi-user.target
四、开启关停
# systemctl start xxx-xxx-xxx.mount
# systemctl stop xxx-xxx-xxx.mount
# systemctl disable xxx-xxx-xxx.mount
# systemctl statu xxx-xxx-xxx.mount
如果使用automount,则必须使用:
# systemctl enable xxx-xxx-xxx.automount
没用使用automount,则使用:
# systemctl enable xxx-xxx-xxx.mount
# systemctl is-enabled mnt-scratch.mount
disabled
# systemctl is-enabled mnt-scratch.automount
enabled
# systemctl start mnt-scratch.automount
# ls /mnt/scratch >/dev/null
# systemctl status mnt-scratch.automount
● mnt-scratch.automount - Automount Scratch
Loaded: loaded (/etc/systemd/system/mnt-scratch.automount; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-04-18 10:49:04 CEST; 4h 33min ago
Where: /mnt/scratch
Apr 18 10:49:04 oat systemd[1]: Set up automount Automount Scratch.
Apr 18 10:49:14 oat systemd[1]: mnt-scratch.automount: Got automount request for /mnt/scratch, triggered by 20266 (zsh)
# systemctl status mnt-scratch.mount
● mnt-scratch.mount - Scratch
Loaded: loaded (/proc/self/mountinfo; disabled; vendor preset: disabled)
Active: active (mounted) since Mon 2016-04-18 10:49:16 CEST; 4h 33min ago
Where: /mnt/scratch
What: nfs.example.com:/export/scratch
Apr 18 10:49:14 oat systemd[1]: Mounting Scratch...
Apr 18 10:49:16 oat systemd[1]: Mounted Scratch.
五、重要事项:
(以下内容非常重要,可能直接导致挂载失败。)
mount文件名称必须要与你挂载路径一致,把路径里的「/」换成「 -」,例如本例要挂的目录为/mnt/data,则文件名称必须为mnt-data.mount。可以多层目录如xxx-xxx-xxx.mount。
如果挂载nfs的话需要安装nfs客户端:
服务器端:
$sudo apt install nfs-kernel-server
$sudo apt install portmap(如果安装上一个,可以不用执行此操作)
客户端:
#apt install nfs-common
Automatic mounts with systemd:
https://blog.tomecek.net/post/automount-with-systemd/
Systemd target mount automount介绍:
https://codingbee.net/rhcsa/rhcsa-understanding-systemd