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

+124 -27
View File
@@ -69,6 +69,35 @@ int cat_config = 0;
/* File the ones on the command line are read in place of, --replace */
static char *replace;
/* Directory all paths are below, --root */
static char *root;
/*
* Path of an open @fd, for what cannot be done on an fd opened with
* O_PATH. With --root there is no /proc, see change_root().
*/
static char *fd_path(int fd, char *buf, size_t len)
{
snprintf(buf, len, "%s%d", root ? "" : "/proc/self/fd/", fd);
return buf;
}
/*
* No /proc below @dir, most likely, so the work dir is left in the
* /proc/self/fd of the old root. Nothing else may be relative to it,
* and nothing from below @dir may be run.
*/
static void change_root(const char *dir)
{
int fd;
fd = open("/proc/self/fd", O_PATH | O_DIRECTORY | O_CLOEXEC);
if (fd < 0 || chroot(dir) || fchdir(fd))
err(1, "Failed changing root to %s", dir);
close(fd);
}
#define MAX_EXCLUDES 32
static const char *exclude_prefixes[MAX_EXCLUDES];
static int num_excludes;
@@ -193,9 +222,7 @@ static int fd_chmod(int fd, mode_t mode)
if (errno != EBADF)
return -1;
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
return chmod(proc, mode);
return chmod(fd_path(fd, proc, sizeof(proc)), mode);
}
static int copy_errno;
@@ -636,6 +663,49 @@ static int parse_age(const char *str, struct age *age)
return 0;
}
/* With --root the files are read, NSS is that of the old root */
static int file_uid(const char *user)
{
struct passwd *pw;
int uid = -1;
FILE *fp;
fp = fopen("/etc/passwd", "r");
if (!fp)
return -1;
while ((pw = fgetpwent(fp))) {
if (strcmp(pw->pw_name, user))
continue;
uid = pw->pw_uid;
break;
}
fclose(fp);
return uid;
}
static int file_gid(const char *group)
{
struct group *gr;
int gid = -1;
FILE *fp;
fp = fopen("/etc/group", "r");
if (!fp)
return -1;
while ((gr = fgetgrent(fp))) {
if (strcmp(gr->gr_name, group))
continue;
gid = gr->gr_gid;
break;
}
fclose(fp);
return gid;
}
/**
* Parse user string - supports both names and numeric UIDs.
* Returns UID on success, -1 on failure or when no user is given.
@@ -653,7 +723,7 @@ static int parse_uid(const char *user)
return val;
/* Not numeric, look up by name */
return getuser(user, NULL);
return root ? file_uid(user) : getuser(user, NULL);
}
/**
@@ -673,7 +743,7 @@ static int parse_gid(const char *group)
return val;
/* Not numeric, look up by name */
return getgroup(group);
return root ? file_gid(group) : getgroup(group);
}
/* Mode and owner of an entry, mode 0 and id -1 when not given */
@@ -989,10 +1059,7 @@ static int acl_parse_entry(char *str, struct acl *access, struct acl *def)
if (type != ACL_USER && type != ACL_GROUP)
return -1;
if (type == ACL_USER)
val = parse_uid(name);
else if ((val = atonum(name)) < 0)
val = getgroup(name);
val = type == ACL_USER ? parse_uid(name) : parse_gid(name);
if (val < 0)
return -1;
id = val;
@@ -1166,7 +1233,7 @@ static int acl_path(const char *path)
if (fd < 0)
return -1;
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
fd_path(fd, proc, sizeof(proc));
if (fstat(fd, &st)) {
rc = -1;
} else if (!S_ISLNK(st.st_mode)) {
@@ -2333,7 +2400,7 @@ static void tmpfiles(struct entry *e)
} else if (perm_path(p, &perm, 0, O_PATH)) {
warn("Failed setting mode/owner on %s", p);
}
if (whichp("restorecon"))
if (!root && whichp("restorecon"))
systemf("restorecon %s %s", opts, p);
}
globfree(&gl);
@@ -2383,26 +2450,20 @@ static void run_entries(int create)
}
}
static void process_file(const char *fn)
static void read_file(const char *fn, FILE *fp)
{
FILE *fp = stdin;
if (strcmp(fn, "-")) {
fp = fopen(fn, "r");
if (!fp) {
warn("Failed to open %s", fn);
return;
}
}
if (!fp)
return;
conf = fn;
if (cat_config) {
printf("# %s\n", fn);
fcopyfile(fp, stdout);
puts("");
return;
}
while (!cat_config && !feof(fp)) {
while (!feof(fp)) {
/* No comment character, a # is only a comment at the start of a line */
const char delim[3] = { '\\', '\\', 0 };
char *line;
@@ -2414,8 +2475,18 @@ static void process_file(const char *fn)
if (parse(line))
free(line);
}
}
if (fp != stdin)
static void process_file(const char *fn)
{
FILE *fp;
fp = fopen(fn, "r");
if (!fp)
warn("Failed to open %s", fn);
read_file(fn, fp);
if (fp)
fclose(fp);
}
@@ -2456,7 +2527,7 @@ static int by_name(const void *a, const void *b)
* name in a directory before its own, and what is left is read in
* order of the file names, whatever directory they are in.
*/
static void process(int num, char *file[])
static void process(int num, char *file[], FILE *fp[])
{
int flags = GLOB_NOESCAPE;
size_t i, j, cnt = 0;
@@ -2465,7 +2536,7 @@ static void process(int num, char *file[])
if (num && !replace) {
for (int k = 0; k < num; k++)
process_file(file[k]);
read_file(file[k], fp[k]);
return;
}
@@ -2508,7 +2579,7 @@ static void process(int num, char *file[])
continue;
}
for (int k = 0; k < num; k++)
process_file(file[k]);
read_file(file[k], fp[k]);
}
free(list);
@@ -2533,6 +2604,7 @@ static int usage(int rc)
" --prefix=PFX Only apply rules for paths starting with PFX\n"
" --purge Remove all that the entries marked with $ in CONFIGFILE make\n"
" --replace=PATH Read all files, with CONFIGFILE in place of PATH\n"
" --root=DIR Run on the system below DIR, with its tmpfiles.d files\n"
" -r, --remove Remove files and directories marked for removal\n"
" -h, --help This help text\n"
"\n"
@@ -2552,6 +2624,7 @@ enum {
OPT_GRACEFUL,
OPT_REPLACE,
OPT_PURGE,
OPT_ROOT,
};
int main(int argc, char *argv[])
@@ -2569,10 +2642,12 @@ int main(int argc, char *argv[])
{ "purge", 0, NULL, OPT_PURGE },
{ "remove", 0, NULL, 'r' },
{ "replace", 1, NULL, OPT_REPLACE },
{ "root", 1, NULL, OPT_ROOT },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 }
};
FILE **fp;
int c;
while ((c = getopt_long(argc, argv, "CcdnrEh?", long_options, NULL)) != EOF) {
@@ -2623,6 +2698,10 @@ int main(int argc, char *argv[])
purge_flag = 1;
break;
case OPT_ROOT:
root = optarg;
break;
case OPT_EXCLUDE_PREFIX:
add_exclude_prefix(optarg);
break;
@@ -2654,7 +2733,25 @@ int main(int argc, char *argv[])
return 1;
}
process(argc - optind, &argv[optind]);
/* Those on the command line are of the old root, also with --root */
argc -= optind;
argv += optind;
fp = calloc(argc + 1, sizeof(FILE *));
if (!fp)
err(1, "Failed opening files");
for (c = 0; c < argc; c++) {
fp[c] = strcmp(argv[c], "-") ? fopen(argv[c], "r") : stdin;
if (!fp[c])
warn("Failed to open %s", argv[c]);
}
if (root && strcmp(root, "/"))
change_root(root);
else
root = NULL;
process(argc, argv, fp);
free(fp);
if (purge_flag) {
purging = 1;