选项 1:永久更改服务

选项 1:永久更改服务

假设我正在运行一些冗长的过程,包括两个步骤:a生成一个大的中间文件,例如/tmp/aoutput,然后b读取该文件并对其执行进一步的操作。

现在系统在执行该b步骤时崩溃了。是否可以以某种方式重新启动系统不是删除 的内容/tmp(通常在启动时会发生这种情况),以便我们可以立即重新启动b而无需先完全运行a

这当然假设/tmp没有安装在tmpfs.

我想排除涉及物理取出 HDD/SSD 或使用实时 USB 介质启动的解决方案。在我现在的具体情况中,它实际上是一个具有全盘加密的 SSD,但我也对仅适用于非加密驱动器的解决方案感兴趣。


编辑现在仍然保留这个问题,因为这两个答案都没有给出一个无需实时媒体或事先调整配置即可工作的解决方案。也许确实不可能有其他方式,但看起来很奇怪:GRUB 不是已经挂载分区来引导 Linux 了吗?如果是这样,有没有办法手动移动文件,曾经推出过吗systemd?也许我错了,我对引导加载程序知之甚少;但我想听听为什么这是不可能的。


systemd配置信息:

$ grep -s . /*/tmpfiles.d/*.conf /*/*/tmpfiles.d/*.conf
/run/tmpfiles.d/kmod.conf:c! /dev/fuse 0600 - - - 10:229
/run/tmpfiles.d/kmod.conf:c! /dev/cuse 0600 - - - 10:203
/run/tmpfiles.d/kmod.conf:c! /dev/btrfs-control 0600 - - - 10:234
/run/tmpfiles.d/kmod.conf:d /dev/net 0755 - - -
/run/tmpfiles.d/kmod.conf:c! /dev/net/tun 0600 - - - 10:200
/run/tmpfiles.d/kmod.conf:c! /dev/ppp 0600 - - - 108:0
/run/tmpfiles.d/kmod.conf:c! /dev/uinput 0600 - - - 10:223
/run/tmpfiles.d/kmod.conf:d /dev/mapper 0755 - - -
/run/tmpfiles.d/kmod.conf:c! /dev/mapper/control 0600 - - - 10:236
/run/tmpfiles.d/kmod.conf:c! /dev/uhid 0600 - - - 10:239
/run/tmpfiles.d/kmod.conf:c! /dev/vhci 0600 - - - 10:137
/run/tmpfiles.d/kmod.conf:d /dev/snd 0755 - - -
/run/tmpfiles.d/kmod.conf:c! /dev/snd/timer 0600 - - - 116:33
/run/tmpfiles.d/kmod.conf:d /dev/snd 0755 - - -
/run/tmpfiles.d/kmod.conf:c! /dev/snd/seq 0600 - - - 116:1
/usr/lib/tmpfiles.d/00rsyslog.conf:# Override systemd's default tmpfiles.d/var.conf to make /var/log writable by
/usr/lib/tmpfiles.d/00rsyslog.conf:# the syslog group, so that rsyslog can run as user.
/usr/lib/tmpfiles.d/00rsyslog.conf:# See tmpfiles.d(5) for details.
/usr/lib/tmpfiles.d/00rsyslog.conf:# Type Path    Mode UID  GID  Age Argument
/usr/lib/tmpfiles.d/00rsyslog.conf:d /var/log 0775 root syslog -
/usr/lib/tmpfiles.d/dbus.conf:# Type Path                     Mode    UID     GID     Age     Argument
/usr/lib/tmpfiles.d/dbus.conf:L /var/lib/dbus/machine-id      -       -       -       -       /etc/machine-id
/usr/lib/tmpfiles.d/debian.conf:#  This file is part of the debianisation of systemd.
/usr/lib/tmpfiles.d/debian.conf:#
/usr/lib/tmpfiles.d/debian.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/debian.conf:#  under the terms of the GNU General Public License as published by
/usr/lib/tmpfiles.d/debian.conf:#  the Free Software Foundation; either version 2 of the License, or
/usr/lib/tmpfiles.d/debian.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/debian.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/debian.conf:# Type Path    Mode UID  GID  Age Argument
/usr/lib/tmpfiles.d/debian.conf:L /run/initctl -    -    -    -   /run/systemd/initctl/fifo
/usr/lib/tmpfiles.d/debian.conf:L /run/shm     -    -    -    -   /dev/shm
/usr/lib/tmpfiles.d/debian.conf:d /run/sendsigs.omit.d 0755 root root -
/usr/lib/tmpfiles.d/debian.conf:L+ /etc/mtab   -    -    -    -  ../proc/self/mounts
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# This is a systemd tmpfiles.d configuration file
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:#
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# tmpfiles.d defaults are set to clean /run/user every now and then
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# which includes our gvfs-fuse mount being mounted in /run/user/<id>/gvfs
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:#
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# This file adds an exclusion rule so that user data don't get automatically
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# cleaned up (i.e. destroyed).
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:#
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# Due to our fuse mount restrictions root can't access nor stat the mountpoint
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# resulting in warning spitted out by the systemd-tmpfiles process. Please
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# ignore it for the time being until proper solution is found:
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:# https://bugzilla.gnome.org/show_bug.cgi?id=560658
/usr/lib/tmpfiles.d/gvfsd-fuse-tmpfiles.conf:x /run/user/*/gvfs
/usr/lib/tmpfiles.d/home.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/home.conf:#
/usr/lib/tmpfiles.d/home.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/home.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/home.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/home.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/home.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/home.conf:Q /home 0755 - - -
/usr/lib/tmpfiles.d/home.conf:q /srv 0755 - - -
/usr/lib/tmpfiles.d/journal-nocow.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/journal-nocow.conf:#
/usr/lib/tmpfiles.d/journal-nocow.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/journal-nocow.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/journal-nocow.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/journal-nocow.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/journal-nocow.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/journal-nocow.conf:# Set the NOCOW attribute for directories of journal files. This flag
/usr/lib/tmpfiles.d/journal-nocow.conf:# is inheredited by their new files and sub-directories. Matters only
/usr/lib/tmpfiles.d/journal-nocow.conf:# for btrfs filesystems.
/usr/lib/tmpfiles.d/journal-nocow.conf:#
/usr/lib/tmpfiles.d/journal-nocow.conf:# WARNING: Enabling the NOCOW attribute improves journal performance
/usr/lib/tmpfiles.d/journal-nocow.conf:#     substantially, but also disables the btrfs checksum logic. In
/usr/lib/tmpfiles.d/journal-nocow.conf:#     btrfs RAID filesystems the checksums are needed for rebuilding
/usr/lib/tmpfiles.d/journal-nocow.conf:#     corrupted files. Without checksums such rebuilds are not
/usr/lib/tmpfiles.d/journal-nocow.conf:#     possible.
/usr/lib/tmpfiles.d/journal-nocow.conf:#
/usr/lib/tmpfiles.d/journal-nocow.conf:# In a single-disk filesystem (or a filesystem without redundancy)
/usr/lib/tmpfiles.d/journal-nocow.conf:# enabling the NOCOW attribute for journal files is safe, because
/usr/lib/tmpfiles.d/journal-nocow.conf:# they have their own checksums and a rebuilding wouldn't be possible
/usr/lib/tmpfiles.d/journal-nocow.conf:# in any case.
/usr/lib/tmpfiles.d/journal-nocow.conf:h /var/log/journal - - - - +C
/usr/lib/tmpfiles.d/journal-nocow.conf:h /var/log/journal/%m - - - - +C
/usr/lib/tmpfiles.d/journal-nocow.conf:h /var/log/journal/remote - - - - +C
/usr/lib/tmpfiles.d/legacy.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/legacy.conf:#
/usr/lib/tmpfiles.d/legacy.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/legacy.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/legacy.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/legacy.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/legacy.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/legacy.conf:# These files are considered legacy and are unnecessary on legacy-free
/usr/lib/tmpfiles.d/legacy.conf:# systems.
/usr/lib/tmpfiles.d/legacy.conf:L /var/lock - - - - ../run/lock
/usr/lib/tmpfiles.d/legacy.conf:# /run/lock/subsys is used for serializing SysV service execution, and
/usr/lib/tmpfiles.d/legacy.conf:# hence without use on SysV-less systems.
/usr/lib/tmpfiles.d/legacy.conf:d /run/lock/subsys 0755 root root -
/usr/lib/tmpfiles.d/legacy.conf:# /forcefsck, /fastboot and /forcequotacheck are deprecated in favor of the
/usr/lib/tmpfiles.d/legacy.conf:# kernel command line options 'fsck.mode=force', 'fsck.mode=skip' and
/usr/lib/tmpfiles.d/legacy.conf:# 'quotacheck.mode=force'
/usr/lib/tmpfiles.d/legacy.conf:r! /forcefsck
/usr/lib/tmpfiles.d/legacy.conf:r! /fastboot
/usr/lib/tmpfiles.d/legacy.conf:r! /forcequotacheck
/usr/lib/tmpfiles.d/man-db.conf:d /var/cache/man 2755 man root 1w
/usr/lib/tmpfiles.d/passwd.conf:# If a password operation is in progress and we lose power, stale lockfiles
/usr/lib/tmpfiles.d/passwd.conf:# can be left behind.  Clear them on boot.
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/gshadow.lock
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/shadow.lock
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/passwd.lock
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/group.lock
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/subuid.lock
/usr/lib/tmpfiles.d/passwd.conf:r! /etc/subgid.lock
/usr/lib/tmpfiles.d/samba.conf:d /run/samba 0755 root root -
/usr/lib/tmpfiles.d/sshd.conf:d /var/run/sshd 0755 root root
/usr/lib/tmpfiles.d/sudo.conf:# Create an empty sudo time stamp directory on OSes using systemd.
/usr/lib/tmpfiles.d/sudo.conf:# Sudo will create the directory itself but this can cause problems
/usr/lib/tmpfiles.d/sudo.conf:# on systems that have SELinux enabled since the directories will be
/usr/lib/tmpfiles.d/sudo.conf:# created with the user's security context.
/usr/lib/tmpfiles.d/sudo.conf:d /var/run/sudo 0711 root root
/usr/lib/tmpfiles.d/sudo.conf:D /var/run/sudo/ts 0700 root root
/usr/lib/tmpfiles.d/systemd.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/systemd.conf:#
/usr/lib/tmpfiles.d/systemd.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/systemd.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/systemd.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/systemd.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/systemd.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/systemd.conf:d /run/user 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:F! /run/utmp 0664 root utmp -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/ask-password 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/seats 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/sessions 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/users 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/machines 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/shutdown 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/netif 0755 systemd-network systemd-network -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/netif/links 0755 systemd-network systemd-network -
/usr/lib/tmpfiles.d/systemd.conf:d /run/systemd/netif/leases 0755 systemd-network systemd-network -
/usr/lib/tmpfiles.d/systemd.conf:d /run/log 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:z /run/log/journal 2755 root systemd-journal - -
/usr/lib/tmpfiles.d/systemd.conf:Z /run/log/journal/%m ~2750 root systemd-journal - -
/usr/lib/tmpfiles.d/systemd.conf:a+ /run/log/journal/%m - - - - d:group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:A+ /run/log/journal/%m - - - - group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:z /var/log/journal 2755 root systemd-journal - -
/usr/lib/tmpfiles.d/systemd.conf:z /var/log/journal/%m 2755 root systemd-journal - -
/usr/lib/tmpfiles.d/systemd.conf:z /var/log/journal/%m/system.journal 0640 root systemd-journal - -
/usr/lib/tmpfiles.d/systemd.conf:a+ /var/log/journal    - - - - d:group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:a+ /var/log/journal    - - - - group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:a+ /var/log/journal/%m - - - - d:group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:a+ /var/log/journal/%m - - - - group:adm:r-x
/usr/lib/tmpfiles.d/systemd.conf:a+ /var/log/journal/%m/system.journal - - - - group:adm:r--
/usr/lib/tmpfiles.d/systemd.conf:d /var/lib/systemd 0755 root root -
/usr/lib/tmpfiles.d/systemd.conf:d /var/lib/systemd/coredump 0755 root root 3d
/usr/lib/tmpfiles.d/systemd-nologin.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/systemd-nologin.conf:#
/usr/lib/tmpfiles.d/systemd-nologin.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/systemd-nologin.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/systemd-nologin.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/systemd-nologin.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/systemd-nologin.conf:# See tmpfiles.d(5), systemd-user-session.service(5) and pam_nologin(8).
/usr/lib/tmpfiles.d/systemd-nologin.conf:# This file has special suffix so it is not run by mistake.
/usr/lib/tmpfiles.d/systemd-nologin.conf:F! /run/nologin 0644 - - - "System is booting up. See pam_nologin(8)"
/usr/lib/tmpfiles.d/tmp.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/tmp.conf:#
/usr/lib/tmpfiles.d/tmp.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/tmp.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/tmp.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/tmp.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/tmp.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/tmp.conf:# Clear tmp directories separately, to make them easier to override
/usr/lib/tmpfiles.d/tmp.conf:D /tmp 1777 root root -
/usr/lib/tmpfiles.d/tmp.conf:#q /var/tmp 1777 root root 30d
/usr/lib/tmpfiles.d/tmp.conf:# Exclude namespace mountpoints created with PrivateTmp=yes
/usr/lib/tmpfiles.d/tmp.conf:x /tmp/systemd-private-%b-*
/usr/lib/tmpfiles.d/tmp.conf:X /tmp/systemd-private-%b-*/tmp
/usr/lib/tmpfiles.d/tmp.conf:x /var/tmp/systemd-private-%b-*
/usr/lib/tmpfiles.d/tmp.conf:X /var/tmp/systemd-private-%b-*/tmp
/usr/lib/tmpfiles.d/var.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/var.conf:#
/usr/lib/tmpfiles.d/var.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/var.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/var.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/var.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/var.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/var.conf:q /var 0755 - - -
/usr/lib/tmpfiles.d/var.conf:L /var/run - - - - ../run
/usr/lib/tmpfiles.d/var.conf:d /var/log 0755 - - -
/usr/lib/tmpfiles.d/var.conf:f /var/log/wtmp 0664 root utmp -
/usr/lib/tmpfiles.d/var.conf:f /var/log/btmp 0600 root utmp -
/usr/lib/tmpfiles.d/var.conf:d /var/cache 0755 - - -
/usr/lib/tmpfiles.d/var.conf:d /var/lib 0755 - - -
/usr/lib/tmpfiles.d/var.conf:d /var/spool 0755 - - -
/usr/lib/tmpfiles.d/x11.conf:#  This file is part of systemd.
/usr/lib/tmpfiles.d/x11.conf:#
/usr/lib/tmpfiles.d/x11.conf:#  systemd is free software; you can redistribute it and/or modify it
/usr/lib/tmpfiles.d/x11.conf:#  under the terms of the GNU Lesser General Public License as published by
/usr/lib/tmpfiles.d/x11.conf:#  the Free Software Foundation; either version 2.1 of the License, or
/usr/lib/tmpfiles.d/x11.conf:#  (at your option) any later version.
/usr/lib/tmpfiles.d/x11.conf:# See tmpfiles.d(5) for details
/usr/lib/tmpfiles.d/x11.conf:# Make sure these are created by default so that nobody else can
/usr/lib/tmpfiles.d/x11.conf:d /tmp/.X11-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:d /tmp/.ICE-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:d /tmp/.XIM-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:d /tmp/.font-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:d /tmp/.Test-unix 1777 root root 10d
/usr/lib/tmpfiles.d/x11.conf:# Unlink the X11 lock files
/usr/lib/tmpfiles.d/x11.conf:r! /tmp/.X[0-9]*-lock
/var/run/tmpfiles.d/kmod.conf:c! /dev/fuse 0600 - - - 10:229
/var/run/tmpfiles.d/kmod.conf:c! /dev/cuse 0600 - - - 10:203
/var/run/tmpfiles.d/kmod.conf:c! /dev/btrfs-control 0600 - - - 10:234
/var/run/tmpfiles.d/kmod.conf:d /dev/net 0755 - - -
/var/run/tmpfiles.d/kmod.conf:c! /dev/net/tun 0600 - - - 10:200
/var/run/tmpfiles.d/kmod.conf:c! /dev/ppp 0600 - - - 108:0
/var/run/tmpfiles.d/kmod.conf:c! /dev/uinput 0600 - - - 10:223
/var/run/tmpfiles.d/kmod.conf:d /dev/mapper 0755 - - -
/var/run/tmpfiles.d/kmod.conf:c! /dev/mapper/control 0600 - - - 10:236
/var/run/tmpfiles.d/kmod.conf:c! /dev/uhid 0600 - - - 10:239
/var/run/tmpfiles.d/kmod.conf:c! /dev/vhci 0600 - - - 10:137
/var/run/tmpfiles.d/kmod.conf:d /dev/snd 0755 - - -
/var/run/tmpfiles.d/kmod.conf:c! /dev/snd/timer 0600 - - - 116:33
/var/run/tmpfiles.d/kmod.conf:d /dev/snd 0755 - - -
/var/run/tmpfiles.d/kmod.conf:c! /dev/snd/seq 0600 - - - 116:1

答案1

长话短说:如果崩溃刚刚发生,请使用实时媒体。否则,通过/usr/lib/tmpfiles.d/tmp.conf复制/etc/tmpfiles.d/tmp.conf并更改文件来配置您的系统etc

-D /tmp 1777 root root -
+d /tmp 1777 root root 1s
+x /tmp/*output

如果您的文件不遵循特定模式,请改用以下设置:

-D /tmp 1777 root root -
+d /tmp 1777 root root 12h

现在系统在执行该b步骤时崩溃了。是否可以以一种不会删除 /tmp 内容的方式重新启动系统(通常在启动时会发生这种情况),以便我们可以立即重新启动b而无需先完全运行?

好吧,有点,如果你可以配置系统崩溃发生了。如果此时系统已经崩溃了,那么你必须使用实时媒体,因为每个常见的systemd.targetrescue、、 )multi-usergraphical包括删除临时文件,甚至rescue.target.

话虽这么说,为了为下一次崩溃做好准备,我们可以改变您的tmpfiles行为。大多数 Debian 衍生系统定义了/tmpin 的行为/usr/lib/tmpfiles.d/tmp.conf

$ 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
D /tmp 1777 root root -
#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-*

负责清理的线路是

D /tmp 1777 root root -

根据tmpfiles.d(5),大写字母D表示该目录应在 上删除systemd-tmpfiles --remove。不幸的是,该命令几乎在每个目标上运行,因为systemd-tmpfiles-setup.service

$ systemctl cat systemd-tmpfiles-setup.service | grep Exec
ExecStart=/bin/systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev

$ systemctl list-dependencies --reverse systemd-tmpfiles-setup.service | grep target
● └─sysinit.target
●   ├─basic.target
●   │ └─multi-user.target
●   │   └─graphical.target
●   └─rescue.target

此外,还有几个计时器可以触发--clean操作,从而删除旧文件。

根据您的情况,您手头有多种解决方案,但没有一个是完美的。

警告:无论你做什么,请勿更改/usr/lib/tmpfiles.d/内容。相反,在 中创建一个同名的文件/etc/tmpfiles.d,例如

sudo cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/tmp.conf

tmpfiles.d遵循通常的 systemd 规则:/etc>> /run/usr/lib/etc/获得优先权。

选项 1:永久更改服务

/tmp由于--remove上述服务而被删除。您可以通过systemctl edit参数ExecStart删除参数--remove

$ systemctl edit systemd-tmpfiles-setup.service
ExecStart=
ExecStart=/bin/systemd-tmpfiles --create --boot --exclude-prefix=/dev

ExecStart需要额外的空来删除原始配置。或者运行systemctl edit --full ...并编辑生成的文件。

由于--remove缺少,D所有配置文件都被有效禁用,这可能没有帮助,因此我不鼓励。因此,您可能想更改规则的定义/tmp

选项 2:仅针对特定年龄的文件清理 /tmp

复制并替换/usr/lib/tmpfiles.d/tmp.conf该行/etc/tmpfiles.d/tmp.confD

-D /tmp 1777 root root -
+d /tmp 1777 root root 12h

这只会删除超过 12 小时的文件,而不是所有文件。如果您在崩溃后立即重新启动,那么这对您来说可能就足够了。然而,我们甚至可以更进一步。

选项 3:排除项目文件

如果您的文件始终具有相同的名称/模式,我们可以通过排除来防止它们被删除x

-D /tmp 1777 root root -
+d /tmp 1777 root root 1s
+x /tmp/*output

请注意,调用1s时将有效删除所有文件,systemd-tmpfiles除了 /tmp/*output文件,例如/tmp/aoutput.您还可以排除整个文件夹。

最后一个变体应该足以解决您的问题,并防止您遇到/tmp/太大的问题。

检查配置

更改配置后,就可以对其进行测试了。无论您使用哪种变体,此测试都是相同的。在您的文件中创建具有相似名称的文件/tmp,然后运行 ​​systemd 服务:

$ touch /tmp/my-project-files-{a,b,c,d,e,f} /tmp/some-test-garbage{1..10}
$ systemctl start systemd-tmpfiles-{clean,setup}.service

如果一切设置正确,您的“项目”文件应该仍然存在。如果您使用了排除x变体,那么其他垃圾/tmp/应该消失。

参考

  • tmpfiles.d(5)tmpfiles.d/*文件的语法和优先级
  • systemd.service(5)为了ExecStart
  • systemd.unit(5)用于ExecStart=覆盖
  • systemd-tmpfiles(8)概览tmpfiles景观
  • 文件systemd-tmpfiles-*.service通过systemctl cat systemd-tmpfiles-{clean,setup,setup-dev}.service

答案2

如果您只想挽救 /tmp 下的一些文件,可以按照以下步骤操作:

  1. 在 grub 菜单项中按“e”进入 grub 编辑模式
  2. 在以“linux”开头的行末尾添加“break=mount”
  3. 按 F10 启动,您应该最终进入 (initramfs) 控制台
  4. 运行blkid以获取可用文件系统的概述
  5. mkdir /mnt然后运行modprobe ext4,或者类似的操作mount /dev/nvme0n1p1 /mnt,将您感兴趣的分区安装在/mnt
  6. 使用类似的方法将您感兴趣的文件移动到永久位置mv /mnt/tmp/important_file /mnt/home/myuser/
  7. 重新启动reboot -f并正常启动系统

答案3

您可以添加init=/bin/bash到内核命令行以引导到 shell,而无需启动 systemd。以读写方式重新挂载文件系统,将文件移出 /tmp,再次以只读方式挂载文件系统,然后exec /sbin/init启动系统。

请注意,shell 不会有完全设置的终端,因此某些功能(制表符完成、control-Z、control-C...)可能无法工作。

我建议首先在测试系统上尝试此操作,以确保它适合您,而不会给您的文件带来风险。

PS:通常/var/tmp在引导过程中保留。

答案4

是的。

有两种情况可能会导致文件丢失/tmp

  1. 临时文件系统:许多现代系统都会安装临时文件系统(例如 RAM 磁盘),/tmp这意味着关闭时内容总是会丢失。

  2. 旧文件清理/tmp。这是一个可配置的脚本,用于清理/tmp.

请检查命令的输出df /tmp。在情况 1 中,它看起来类似于:

$ df /tmp/
Filesystem     1K-blocks  Used Available Use% Mounted on
tmpfs           12290040   276  12289764   1% /tmp

(重要的是第一栏。. tmpfs

/etc/tmpfiles.d在情况 2(您的情况)中,您通常可以通过查看-- 请参阅中的文件来了解更多信息man tmpfiles.d。请添加输出

grep -s . /*/tmpfiles.d/*.conf /*/*/tmpfiles.d/*.conf

对于你的问题。 ⟹ 您的文件是/usr/lib/tmpfiles.d/tmp.conf您需要编辑的文件,例如sudo nano /usr/lib/tmpfiles.d/tmp.conf根据以下示例应用更改。

这是一个示例文件/etc/tmpfiles.d/tmp.conf

# see tmpfiles.d(5)
# always enable /tmp folder cleaning
D! /tmp 1777 root root 0

# remove files in /var/tmp older than 10 days
D /var/tmp 1777 root root 10d

第一个条目D! /tmp 1777 root root 0意味着该/tmp文件夹将在每次启动时被删除并重新创建。您可以注释掉该行,并可能添加类似于该文件中下一个非注释行的行:

# see tmpfiles.d(5)

# # always enable /tmp folder cleaning
# D! /tmp 1777 root root 0

# Keep files in /tmp but not very old ones
D! /var/tmp 1777 root root 30d

# remove files in /var/tmp older than 10 days
D /var/tmp 1777 root root 10d

由于系统现在尚未启动,您需要从 Live DVD/USB 介质启动,然后挂载您的系统分区,例如挂载到/mnt.然后/mnt/usr/lib/tmpfiles.d/tmp.conf按照上述方法编辑该文件。

相关内容