tmpfiles: add --root, run on the system below a directory

There was no way to run the tmpfiles.d files of a root file system
that is not the running one, for example an image that is being put
together.

systemd-tmpfiles has --root=DIR for that.  Add the same:

    tmpfiles --create --root=/mnt/target

The tmpfiles.d files, users, and groups are those below DIR, and so is
every path, also what an absolute link there points to.  A file on the
command line is opened before the root is changed, as systemd does.

It is done with chroot(2), so tmpfiles must be run as root.  The new
root most likely has no /proc, which is needed to set the mode, ACL,
or attributes of a path opened with O_PATH.  The work directory is
therefore left in the /proc/self/fd of the old root.  Users and groups
are read from /etc/passwd and /etc/group, NSS would be that of the old
root.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 19:23:51 +01:00
1 parent 26568bc0fd
commit 225b2594b7
5 files changed
+205 -41

No files matched your search

+5
View File
@@ -67,6 +67,11 @@ All relevant changes are documented in this file.
- tmpfiles: add `--purge` and the `$` modifier, as in systemd-tmpfiles.
`tmpfiles --purge FILE` removes what the entries of `FILE` that are
marked with a `$`, as in `d$`, make. For when a package is removed
- tmpfiles: add `--root=DIR`, to run on the system below `DIR`: its
tmpfiles.d files, its `/etc/passwd` and `/etc/group`, and its paths.
For a root file system that is being put together. Finit's tmpfiles
does this with chroot(2), so unlike systemd-tmpfiles it must be run
as root
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
it. `--prefix` and `--exclude-prefix` match whole path components,
`--exclude-prefix=/dev` no longer skips `/devices`
+11 -14
View File
@@ -1,20 +1,17 @@
tmpfiles TODO
=============
What `tmpfiles` lacks compared to [tmpfiles.d(5)][], from a read of
`src/tmpfiles.c` in November 2026. Ranked: first what Finit's own
files or common packages already rely on, last what few will miss.
What `tmpfiles` lacks compared to [tmpfiles.d(5)][] and
systemd-tmpfiles(8), from a read of `src/tmpfiles.c` in November 2026.
[tmpfiles.d(5)]: https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html
1. Remaining types and modifiers
--------------------------------
* `z` and `Z` only set the SELinux label when `restorecon` is installed
2. Command line
---------------
Missing option: `--root`.
* `z` and `Z` only set the SELinux label when `restorecon` is installed,
and no other entry sets a label on what it creates. Doing it without
the program needs libselinux
* `--root` needs chroot(2), so it cannot be run as a regular user, or
under fakeroot when a root file system is built. The `%b` specifier
does not work there, the root has no `/proc`
* No `--image`, `--user`, or `--tldr`, and no btrfs subvolumes: `v`, `q`,
and `Q` make a plain directory
* An entry whose argument comes from a credential, `^`, is skipped