如何在CentOS/rhel7&8中使用systemd unit file自动挂载文件系统

时间:2020-01-09 10:37:16  来源:igfitidea点击:

如何在Linux下自动挂载文件系统?

一种单元配置文件,其名称以".automount"结尾,它对由systemd控制和监督的文件系统自动装入点的信息进行编码。

自动装载单元可用于实现文件系统的按需装载和并行装载。如果一个自动装入点位于文件系统层次结构中的另一个装入点之下,则会自动创建两个单元之间的依赖关系。

与/etc/fstab的依赖关系

可以通过单元文件或者通过/etc/fstab配置自动装载单元如果在/etc/fstab和单元文件中都配置了自动装载点,则后者中的配置优先。

安装系统单元文件示例

在上一篇文章中,我们创建了systemd mount unit文件,现在我们将在创建"tmp_dir"的相同位置创建自定义的automount systemd unit文件。

挂载文件

[root@rhel-8 system]# pwd
/usr/lib/systemd/system

创建tmp_dir.mount

[root@rhel-8 system]# cat tmp_dir.mount
#  This file is part of systemd.
[Unit]
Description=Test Directory (/tmp_dir)
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target
[Mount]
What=/dev/disk/by-uuid/cea0757d-6329-4bf8-abbf-03f9c313b07f
Where=/tmp_dir
Type=ext4
Options=defaults
[Install]
WantedBy=multi-user.target

重要提示:

要使用systemd unit file自动装载文件系统,systemd mount unit file也可用并且处于工作状态非常重要。

由于.automount服务将始终为各自的分区查找映射.mount服务。

automount systemd单位文件示例

自动装载单元必须以它们控制的自动装载目录命名。示例:必须在单位文件"home"中配置自动装入点"/home/lennart"-自动安装. 这里我的pount point在根目录下,因此我的automount systemd unit文件将是tmp_方向自动装载 .

下面是我的示例automount systemd unit文件到automount文件系统

[root@rhel-8 system]# cat tmp_dir.automount
[Unit]
Description=Sample automount partition
ConditionPathExists=/tmp_dir
[Automount]
Where=/tmp_dir
TimeoutIdleSec=10
[Install]
WantedBy=multi-user.target

其中:

Where=
   Takes an absolute path of a directory of the automount point. If the automount point does not exist at time that the automount point is installed, it is created. This string must be reflected in the unit filename. This option is mandatory.
DirectoryMode=
   Directories of automount points (and any parent directories) are automatically created if needed. This option specifies the file system access mode used when creating these directories. Takes an access mode in octal notation. Defaults to 0755.
TimeoutIdleSec=
   Configures an idleness timeout. Once the mount has been idle for the specified time, systemd will attempt to unmount. Takes a unit-less value in seconds, or a time span value such as "5min 20s". Pass 0 to disable the timeout logic. The timeout is disabled by default.
   
With ConditionPathExists= a file existence condition is checked before a unit is started. If the specified absolute path name does not exist, the condition will fail. If the absolute path name passed to ConditionPathExists= is prefixed with an exclamation mark ("!"), the test is negated, and the unit is only started if the path does not exist.

提示:

我已经给了10秒作为超时时间,这样我就不必再等待更长的时间来向我们演示这种行为。

一旦我们将automount systemd unit文件创建到automount file system,请重新加载systemd守护进程

[root@rhel-8 system]# systemctl daemon-reload

提示:

确保"/tmp_dir"未处于装入状态,否则为"tmp"_方向自动装载服务将无法启动自动装载文件系统的服务。

自动装载文件系统(启动systemd服务)

现在让我们开始tmp_方向自动装载服务

[root@rhel-8 system]# systemctl start tmp_dir.automount

让我们检查一下服务状态。如图所示,服务正在运行,但处于(waiting)状态,因为我们为空闲会话提供了10秒的超时值

[root@rhel-8 system]# systemctl status tmp_dir.automount
● tmp_dir.automount - Sample automount partition
   Loaded: loaded (/usr/lib/systemd/system/tmp_dir.automount; disabled; vendor preset: disabled)
   Active: active (waiting) since Mon 2019-09-16 18:45:59 IST; 6s ago
    Where: /tmp_dir
Sep 16 18:45:59 rhel-8.example systemd[1]: Set up automount Sample automount partition.

说明:

dformount命令仍不会将/tmp_dir显示为已装入,因为只有当有人试图访问装入点时,automount才会装入此文件系统,这是此服务的优点。

如我们所见,"df"命令还没有显示"/tmp_dir"分区

[root@rhel-8 ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               900M     0  900M   0% /dev
tmpfs                  915M     0  915M   0% /dev/shm
tmpfs                  915M  8.5M  907M   1% /run
tmpfs                  915M     0  915M   0% /sys/fs/cgroup
/dev/mapper/rhel-root   15G  2.1G   12G  16% /
/dev/sdc1              976M  2.6M  907M   1% /second_part
/dev/sda1              483M  258M  225M  54% /boot
tmpfs                  183M     0  183M   0% /run/user/0

现在如果我显式地使用'df'命令访问这个分区

[root@rhel-8 ~]# df -h /tmp_dir/
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       976M  2.6M  907M   1% /tmp_dir

然后分区会自动挂载。

另外,如果我们检查服务状态,输出片段将提示我们后端发生了什么

[root@rhel-8 ~]# systemctl status tmp_dir.automount
● tmp_dir.automount - Sample automount partition
   Loaded: loaded (/usr/lib/systemd/system/tmp_dir.automount; disabled; vendor preset: disabled)
   Active: active (waiting) since Mon 2019-09-16 18:45:59 IST; 17min ago
    Where: /tmp_dir
Sep 16 18:45:59 rhel-8.example systemd[1]: Set up automount Sample automount partition.
Sep 16 19:00:54 rhel-8.example systemd[1]: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 3868 (df)
Sep 16 19:01:09 rhel-8.example systemd[1]: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 1318 (bash)
Sep 16 19:02:04 rhel-8.example systemd[1]: tmp_dir.automount: Got automount request for /tmp_dir, triggered by 3903 (df)