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`