使用systemd挂载文件系统

文章目录

为什么使用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.

 

五、重要事项:

(以下内容非常重要,可能直接导致挂载失败。)

  1. mount文件名称必须要与你挂载路径一致,把路径里的「/」换成「 -」,例如本例要挂的目录为/mnt/data,则文件名称必须为mnt-data.mount。可以多层目录如xxx-xxx-xxx.mount
  2. 如果挂载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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值