关于linux挂载fstab和systemd.mount使用场景的一些笔记(代码片段)

山河已无恙 山河已无恙     2022-11-28     432

关键词:

写在前面


不知归路,宁愿一世无悔追逐。——王小波


一些介绍

对于不熟悉的小伙伴这里简单介绍下:

systemd.mount 用于封装一个文件系统挂载点(也向后兼容传统的 /etc/fstab 文件)。 系统中所有的 ".mount" 为后缀的单元文件, 封装了一个由 systemd 管理的文件系统挂载点。类似Service unit 一样,可以配置自动挂载

┌──[root@vms82.liruilongs.github.io]-[~]
└─$systemctl list-unit-files -t mount
UNIT FILE                     STATE
........
tmp.mount                     disabled
var-lib-nfs-rpc_pipefs.mount  static

就拿 tmp.mount来讲, 通过 systemctl cat tmp.mount 可以查看 mount 的单元文件/usr/lib/systemd/system/tmp.mount

可以看到挂载信息,当前的文件系统类型为 Type=tmpfs, 挂载位置为 Where=/tmp 系统临时文件夹,挂载的存储设备为 What=tmpfs 基于内存的存储

┌──[root@vms82.liruilongs.github.io]-[~]
└─$systemctl cat tmp.mount
[Unit]
Description=Temporary Directory
Documentation=man:hier(7)
Documentation=http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime

# Make 'systemctl enable tmp.mount' work:
[Install]
WantedBy=local-fs.target

systemctl status tmp.mount 命令可以查看状态

┌──[root@vms82.liruilongs.github.io]-[~]
└─$systemctl status tmp.mount
● tmp.mount - Temporary Directory
   Loaded: loaded (/usr/lib/systemd/system/tmp.mount; disabled; vendor preset: disabled)
   Active: inactive (dead)
    Where: /tmp
     What: tmpfs
     Docs: man:hier(7)
           http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
┌──[root@vms82.liruilongs.github.io]-[~]
└─$

但是传统的挂载方式我们一般是使用 mount 或者直接写到/etc/fstab文件下。类似下面这样

┌──[root@vms81.liruilongs.github.io]-[~]
└─$cat /etc/fstab
UUID=9875fa5e-2eea-4fcc-a83e-5528c7d0f6a5 /                       xfs     defaults        0 0

然后来看下这个问题

原问题

To have /tmp on tmpfs, I know I can use an entry in /etc/fstab, but I do not understand the role of /etc/default/tmpfs mentioned sometimes, and in what case I need to create or modify it.

Recently, I often see suggested to use systemd tmp.mount confuguration. For example, on Debian:

为了在tmpfs上有/tmp,我知道我可以使用/etc/fstab中的条目,但我不明白/etc/default/tmpfs的作用提到,在什么情况下我需要创建或修改它。

$ sudo cp /usr/share/systemd/tmp.mount /etc/systemd/system/
$ sudo systemctl enable tmp.mount

Which of the two methods is more appropriate for everyday use? In what situations one is better than the other? When do I need to deal with /etc/default/tmpfs?

这两种方法哪一种更适合日常使用?在什么情况下一种比另一种更好?


答案

On some systems, /tmp is a tmpfs by default, and this is the configuration provided by systemd’s “API File Systems”. Fedora-based systems follow this pattern to various extents; Fedora itself ships /usr/lib/systemd/system/tmp.mount and enables it, but RHEL 8 ships it without enabling it. On such systems, masking and unmasking the unit is the appropriate way of disabling or enabling a tmpfs /tmp, as documented in the API File Systems documentation.

在某些系统上,/tmp 默认是 tmpfs,这是 systemdAPI文件系统提供的配置。基于 Fedora 的系统在不同程度上遵循这种模式。; Fedora 本身发布了 /usr/lib/systemd/system/tmp.mount 并启用它,但是 RHEL 8 没有启用它。在这样的系统上,屏蔽和解密单元是禁用或启用 tmpfs /tmp 的适当方法,如API文件系统文档中所述。

这里的 API文件系统 即指:https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems/

APIFileSystems : Linux 内核为用户空间与它通信提供了多种不同的方式。许多设施都有系统调用,其他的隐藏在 Netlink 接口之后,甚至还有一些通过虚拟文件系统(例如/proc或/sys. 这些文件系统是编程接口,它们实际上并没有真正的持久存储支持。他们只是使用内核的文件系统接口作为各种不相关机制的接口。类似地,用户空间将一些文件系统用于其自身的 API 目的,用于存储共享内存段、共享临时文件或套接字。

Fedora: 是由Fedora项目社区开发、红帽公司赞助,目标是创建一套新颖、多功能并且自由(开放源代码)的操作系统。Fedora是商业化的Red Hat Enterprise Linux发行版的上游源码。

Other systems such as Debian don’t ship tmp.mount in a directly-usable location; this is why you need to copy it to /etc/systemd/system if you want to use it. This has the unfortunate side-effect of creating a full override of tmp.mount in /etc, which means that if the systemd package ships a different version of tmp.mount in /lib/systemd/system in the future, it will be ignored. On such systems I would recommend using /etc/fstab instead.

其他系统,如Debian,并没有将 tmp.mount 放在可直接使用的位置,因此如果你想使用它,需要将其复制到 /etc/systemd/system 。这样做的副作用是在 /etc中创建了一个完全覆盖的tmp.mount,这意味着如果将来 systemd 软件包在 /lib/systemd/system 中提供不同版本的 tmp.mount,它将被忽略。在这样的系统中,我建议使用 /etc/fstab代替

这里被忽略是因为单元文件的优先级问题,优先级从高到底

  • 本地配置的系统单元: /etc/systemd/system
  • 运行时配置的系统单元: /run/systemd/system
  • 软件包安装的系统单元: /usr/lib/systemd/system

In both setups, /etc/fstab is still the recommended way of customising /tmp mounts, e.g. to change their size; man systemd.mount says

在这两种设置中,/etc/fstab 仍然是自定义/tmp挂载的推荐方式,例如改变其大小;man systemd.mount

In general, configuring mount points through /etc/fstab is the preferred approach to manage mounts for humans.

通常,通过配置挂载点 /etc/fstab 是为人类管理挂载的首选方法。

and the API File Systems documentation concurs.
API文件系统文档也推荐这种

Using mount units is recommended for tooling, i.e. for automated configuration:
建议使用 mount units 作为工具,即用于自动配置。

For tooling, writing mount units should be preferred over editing /etc/fstab.
对于工具来说,编写挂载单元应该比编辑/etc/fstab更合适。

(This means that tools which want to automatically set up a mount shouldn’t try to edit /etc/fstab, which is error-prone, but should instead install a mount unit, which can be done atomically and can also be overridden by a system administrator using systemd features.)

(这意味着想要自动设置挂载的工具不应该尝试编辑/etc/fstab,这很容易出错,而是应该安装一个 mount unit,这可以原子化地完成,也可以由系统管理员使用systemd功能覆盖。)

/etc/default/tmpfs is used by Debian’s sysvinit, so it’s irrelevant with systemd.
/etc/default/tmpfsDebiansysvinit 使用,所以它与 systemd 没有关系。

博文引用资源


tmp on tmpfs: fstab vs tmp.mount with systemd: https://unix.stackexchange.com/questions/722496/tmp-on-tmpfs-fstab-vs-tmp-mount-with-systemd

API File Systems :https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems/

linux初学者求助,更改/etc/fstab后仍无法自动挂载

如图,想要把/dev/sdb1和/dev/sdb5设置成自动挂载,但重启后并没有成功,但是使用名利mount-a和手动挂载均能成功挂载,求助,用的是rhel7linux在读取/etc/fstab文件时,是按照从上至下的顺序进行读取的,在你的/etc/fstab中已经将/dev/cdr... 查看详情

linux文件系统挂载fstab(代码片段)

/etc/fstab文件描述了系统可以挂载的文件系统的信息,应用程序读取这个文件,然后根据其内容进行自动挂载的工作。比如我们执行mount-a命令就会按序挂载/etc/fstab中指定的每条文件系统(除非添加了noauto选项)。fstab文件中记录... 查看详情

8.29_linux磁盘管理

挂载点和/etc/fstab/etc/fstab配置文件系统体系被mount、fsck和其它程序使用写入配置文件的挂载路径,系统重启时保留文件系统体系可以在设备栏使用文件系统卷标使用mount-a命令挂载/etc/fstab中的所有文件系统文件挂载配置文件/etc/fst... 查看详情

/etc/fstab坏了导致linux无法启动解决

...ab坏了导致linux无法启动解决/etc/fstab由于/etc/fstab中原来的挂载设备不存在了,导致linux因无法挂载无法启动(当然也可能是你写的/etc/fstab语法有错儿导致的)。这时linux会提示你输入root密码来进入系统解决问题。但当进入系统修... 查看详情

linux入门之磁盘管理/etc/fstab与交换分区

Linux入门之磁盘管理(4)/etc/fstab与交换分区在linux通过挂载命令可以使用各种选项进行不同文件系统的设备文件进行挂载,当然不仅限于块设备,但是无论怎么挂载,一般默认新建的文件系统进行挂载开机是不会自动挂载的,每次... 查看详情

linux挂载磁盘怎么开机

在Linux系统中挂载磁盘,可以使用mount命令。在开机后可以检查fstab文件,如果有相应的设定,系统会自动挂载到指定的位置。另外也可以在/etc/rc.local中定义挂载磁盘的shell脚本,以实现开机时自动生效。参考技术A要想在开机时... 查看详情

linux如何取消开机自动挂载

因为我的是sata硬盘,我的Fedora8每次开机都会自动挂载Windows分区到Media下,个人用起来很不习惯,想把Windows分区挂在MNT里。可是当我辛苦地把它自动挂载的分区删除然后挂在MNT里,只要一重启,我手动挂载的分区就没了,而Media... 查看详情

linux为啥写入fstab里面没法自动挂载,每次重启需要手动挂载

参考技术A请检查fstab里面的挂载格式LABEL=/                /                      ext3  ... 查看详情

linux自动挂载分区(/etc/fstab|uuid)(代码片段)

摘要本文探讨如何在Linux开机的时候自动挂载分区。引言在上一篇文章中,我们讲述了如何在Linux中使用mount命令手动挂载分区,但这样的挂载不是永久的,只要重启系统,原先手动挂载好的分区就没了,你还... 查看详情

linux多路径软件在/dev/mapper/下生成的硬盘可以通过修改/etc/fstab挂载么?

...错,进系统后mount是没有问题的。好像不行。因为fstab的挂载是在整个系统启动的最前面,但这种东西都需要很多软件环境的支持才能挂载。所以防止fstab里面经常有问题。而且fstab其实只是一个配置文件,具体的挂载过程是启动... 查看详情

linux磁盘挂载

...默认不会识别外部的设备(磁盘),所有我们需要将设备挂载到系统已经存在的目录下才能进行访问。这个过程就叫做挂载,使用的命令为mount。(1)根文件系统是必须最先挂载的(2)挂载点必须存在(3)挂载设备和挂载点同... 查看详情

linux下/etc/fstab文件详解

...考技术A我们在linux中常常用mount命令把硬盘分区或者光盘挂载到文件系统中。/etc/fstab就是在开机引导的时候自动挂载到linux的文件系统。在linux中/etc/fstab的数据项如下所示:/dev/devicemountpointtyperules0order例如这是一个普通的/etc/fstab... 查看详情

关于vi/etc/fstab的问题

我在linux重新建立个分区/dev/sdb5,然后用mkfs格式化,然后挂载在/mnt/sdb5,用vi编辑器编辑/dec/sdb5/mnt/sdb5ext3defaults,usrquote,grpquote12wq保存后退出vi,但是我再用mount查看的时候,就少了usrquote,grpquote/dev/sdb5on/mnt/sdb5typeext3(rw)怎么解决?首... 查看详情

linux设好fstab要开机自动挂载其他服务器上的nfs共享,为啥没有自动挂载?

...。所以主要原因应该是启动时网络没准备好,就开始试图挂载nfs共享,所以无法自动挂载。可以这样,设个开机启动。命令为mount-a,挂载fstab内容,但是为了等网络准备好再挂载免得失败,mount前加条命令sleep30(睡眠30秒),就... 查看详情

linux磁盘挂载

1、使用root用户查看磁盘挂载情况:fdisk-l2、使用df查看当前磁盘挂载情况,根据和fdisk-l的结果进行对比,查看还有那些磁盘未使用3、挂载:mount磁盘挂载路径取消挂载 umount/dev/sdb1重新挂载mount/dev/sdb1/home/msgplusdata让linux系统... 查看详情

linux的挂载的问题,重启后就挂载就没有了

...k命令,分一个/dev/sda6出来,然后用mkfs格式化为ext3,然后挂载到根目录下的PPP文件夹中,挂载是成功了,但是用reboot和shutdown重启或关机后挂载就没有了,我是在VM上做的,请问这是什么问题?(注:第3张图片是重启后的挂载情... 查看详情

请教如何修改linux的/etc/fstab使u盘自己挂载?

...的....现在还用4已经很土了嘿嘿而且fedora11标准EXT4了自动挂载可以把挂载mount写到fstab里面..系统启动的时候就去读了,不过告诉你哦重启的时候是不会卸载的..架设普通的fstab/dev/hda2/ext3defaults01/dev/hda3swapswapdefaults00/dev/hda5/usrext3defaul... 查看详情

linux磁盘信息查看和挂载新硬盘

参考技术Alinux查看磁盘的信息和挂载磁盘是非常常见的操作。在购买硬盘插到服务器上后,需要将硬盘挂载到文件系统上,不然是无法使用的。具体可以这样理解,在Windows上的硬盘没有分区也是无法直接使用的。效果如下之前sd... 查看详情