使用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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: systemd 在引导过程中会挂载文件系统。在大多数情况下,这是在内核完成初始化并运行第一个用户空间进程之后发生的。 具体来说,systemd 会在内核完成设备枚举和初始化后,并且在执行第一个用户空间进程之前,挂载文件系统。这个进程通常是 `systemd-initctl`。 ### 回答2: systemd是一个Linux系统的init系统和服务管理器。它在系统启动的过程中,负责控制各个进程的启动和管理。 对于挂载文件系统systemd在整个启动过程中的早期阶段就会进行。具体来说,systemd会在系统引导和加载内核之后立即进行根文件系统挂载。 要实现这一过程,systemd会读取/boot目录下的配置文件,例如bootloader(如GRUB)提供的配置文件,或者systemd专用的配置文件。通过这些配置文件,systemd可以确定正确的根文件系统分区,并将其挂载到对应的挂载点上。 在根文件系统挂载之前,systemd还会进行一些其他重要的操作,如加载必要的驱动程序、激活关键系统服务等。这些操作的成功与否对系统的正常启动起着至关重要的作用。 总的来说,systemd在Linux系统引导的早期阶段就会挂载文件系统。通过合理的配置文件和有序的操作,systemd可以确保根文件系统的正确挂载,从而保证系统能够正常启动并提供各种服务。 ### 回答3: systemd是一个负责管理和控制Linux操作系统的init系统和系统管理守护进程。在Linux系统启动过程中,systemd负责挂载文件系统。 具体来说,在Linux系统启动时,BIOS或UEFI引导加载程序会启动initramfs(init ramdisk),这是一个压缩文件系统,包含了用于挂载文件系统所需的初始化程序、驱动程序和工具等。initramfs会被加载到内存中,并运行其中的init程序。 init程序的任务之一就是在启动阶段挂载文件系统。它会根据配置文件中的指示,查找并识别根文件系统所在的设备,并将其挂载到指定的挂载点。 systemd在这个过程中发挥重要作用。init程序会调用systemd的相关功能,以实现根文件系统挂载systemd会在系统启动过程中检测可用的存储设备,并识别出根文件系统所在的设备。然后,它会根据挂载点的配置信息,在指定的挂载点将根文件系统成功挂载上。 需要说明的是,systemd不仅仅在启动阶段挂载文件系统,它还负责其他相关的系统初始化和管理任务,如进程管理、服务管理和日志记录等。它的设计目标是提供更加高效、可靠和可扩展的系统启动和管理方式,以满足现代化Linux系统的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值