Files
finit/test/tmpfiles-root.sh
T
Joachim Wiberg 225b2594b7 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>
2026-11-21 19:23:51 +01:00

64 lines
1.9 KiB
Bash
Executable File

#!/bin/sh
# Verify tmpfiles --root, which runs on the system below a directory:
# its tmpfiles.d files, its users, and its paths
set -eu
TEST_DIR=$(dirname "$0")
ROOT=/tmp/root
CONF=/tmp/root.conf
assert_stat()
{
assert "$1 is $2" "$(texec stat -c '%a %u %g' "$ROOT$1")" = "$2"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $ROOT $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $ROOT/etc/tmpfiles.d $ROOT/usr/share/factory/etc"
run "echo 'app:x:1:1::/:/bin/sh' > $ROOT/etc/passwd"
run "echo 'grp:x:2:' > $ROOT/etc/group"
run "echo factory > $ROOT/usr/share/factory/etc/issue"
run "cat > $ROOT/etc/tmpfiles.d/root.conf" <<CONF
d /var/lib/root-test 0750 app grp
f /var/lib/root-test/file 0600 - - - inside
f /var/lib/root-test/other 0600
z /var/lib/root-test/other 0644
L /link - - - - /var/lib/root-test
f /link/through 0640
C /etc/issue
d /skipped 0700 daemon
CONF
run "echo 'd /from/cmdline 0711' > $CONF"
# The leak check of a sanitizer build needs /proc, the root has none
tmpfiles()
{
run "ASAN_OPTIONS=detect_leaks=0 /libexec/finit/tmpfiles --create --root=$ROOT $*"
}
tmpfiles
say 'The tmpfiles.d files, users, and groups are those below the root'
assert_stat /var/lib/root-test "750 1 2"
assert "File content" "$(texec cat $ROOT/var/lib/root-test/file)" = "inside"
assert "No user daemon there" "$(texec sh -c "ls $ROOT | grep -c skipped")" = "0"
sep
say 'So are the paths, also what a link points to'
assert_stat /var/lib/root-test/other "644 0 0"
assert_stat /var/lib/root-test/through "640 0 0"
assert "Copied from its factory" "$(texec cat $ROOT/etc/issue)" = "factory"
assert "Nothing outside it" "$(texec sh -c "ls /var/lib | grep -c root-test")" = "0"
sep
say 'A file on the command line is not below the root'
tmpfiles $CONF
assert_stat /from/cmdline "711 0 0"