Linux产生大量的临时文件和目录,例如/tmp、/run 。RHEL7或者CentOS7中,systemd提供了一个结构化的可配置方法来管理临时文件和目录,即systemd-tmpfiles,可以创建、删除和管理临时文件的服务。
服务
Linux的systemd提供了tmpfiles相关的服务:
➜ systemctl -a|grep tmpfiles
systemd-tmpfiles-clean.service loaded inactive dead Cleanup of Temporary Directories
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories
tmpfiles服务一般分成两类:
- 开机启动时
- systemd-tmpfiles-setup.service
- systemd-tmpfiles-setup-dev.service
- Linux正常运行期间
- systemd-tmpfiles-clean.timer
systemd-tmpfiles-setup.service
开机时,创建和清理临时目录
➜ ~ systemctl cat systemd-tmpfiles-setup.service
[Unit]
Description=Create Volatile Files and Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target systemd-sysusers.service
Before=sysinit.target shutdown.target
RefuseManualStop=yes
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev
SuccessExitStatus=65 73
systemd-tmpfiles-setup-dev.service
开机时,在/dev/中创建静态设备节点
➜ ~ systemctl cat systemd-tmpfiles-setup-dev.service
[Unit]
Description=Create Static Device Nodes in /dev
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-sysusers.service
Before=sysinit.target local-fs-pre.target systemd-udevd.service shutdown.target
ConditionCapability=CAP_SYS_MODULE
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/systemd-tmpfiles --prefix=/dev --create --boot
SuccessExitStatus=65 73
systemd-tmpfiles-clean.timer
从timer具体内容可以知道系统启动15分钟后和每天会运行一次服务。
那么timer定时运行的是哪些服务呢? 默认是同名的带有.service
后缀的单元,也可以在配置中指定。
➜ ~ systemctl cat systemd-tmpfiles-clean.timer
[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
systemd-tmpfiles-clean.service
systemd-tmpfiles-clean.service就是systemd-tmpfiles-clean.timer定时执行的服务,用于清理目录。
➜ ~ systemctl cat systemd-tmpfiles-clean.service
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
SuccessExitStatus=65
IOSchedulingClass=idle
配置
对于不同目录下的同名配置文件,仅以优先级最高的目录中的那一个为准。具体说来就是: /etc/tmpfiles.d
的优先级最高、 /run/tmpfiles.d
的优先级居中、 /usr/lib/tmpfiles.d
的优先级最低。 软件包应该将自带的配置文件安装在 /usr/lib/tmpfiles.d
目录中, 而 /etc/tmpfiles.d
目录仅供系统管理员使用。 所有的配置文件,无论其位于哪个目录中,都统一按照文件名的字典顺序处理。 如果在多个配置文件中设置了同一个路径(文件或目录),那么仅以文件名最靠前(字典顺序)的那一个为准, 其他针对同一个路径的配置项将会作为警告信息记录到错误日志中。 如果有两行的路径互为前后缀,那么始终是先创建前缀行、再创建后缀行, 如果还需要删除,那么顺序正好相反,始终是先删除后缀行、再删除前缀行。 所有带有shell风格通配符的行,都在所有不带通配符的行之后处理。如果有多个操作符应用于同一个文件(例如 ACL, xattr, 文件属性调整),那么将始终按固定的顺序操作。除上述清空之外,对于其他情况, 文件与目录总是按照它们在配置文件中出现的顺序处理。
➜ ~ cat /usr/lib/tmpfiles.d/tmp.conf
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
q /tmp 1777 root root 10d
q /var/tmp 1777 root root 30d
# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp
# Remove top-level private temporary directories on each boot
R! /tmp/systemd-private-*
R! /var/tmp/systemd-private-*
调试
env SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --clean
# 模拟开机启动时,创建临时目录、清理逻辑
env SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --create --remove --boot
env SYSTEMD_LOG_LEVEL=debug systemd-tmpfiles --remove
这种方式比较详细,会把中间的判断过程也打印出来。
参考文献
最后修改于 2023-01-03
此篇文章的评论功能已经停用。