From ed4ad4889723a9acdf75ed86f10cee0024bbbcbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Wed, 13 Nov 2019 17:36:46 +0100 Subject: [PATCH] Allow overriding /etc/fstab with $SYSTEMD_FSTAB --- docs/ENVIRONMENT.md | 3 +++ src/cryptsetup/cryptsetup.c | 3 ++- src/fstab-generator/fstab-generator.c | 28 ++++++++++++++------------- src/remount-fs/remount-fs.c | 5 +++-- src/shared/fstab-util.c | 4 ++-- src/shared/fstab-util.h | 4 ++++ 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/docs/ENVIRONMENT.md b/docs/ENVIRONMENT.md index 5ad18c9e97..4882cab600 100644 --- a/docs/ENVIRONMENT.md +++ b/docs/ENVIRONMENT.md @@ -41,6 +41,9 @@ All tools: debugging, in order to test generators and other code against specific kernel command lines. +* `$SYSTEMD_FSTAB` — if set, use this path instead of /etc/fstab. Only useful + for debugging. + * `$SYSTEMD_CRYPTTAB` — if set, use this path instead of /etc/crypttab. Only useful for debugging. Currently only supported by systemd-cryptsetup-generator. diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index dd8c178fcb..dd1504dc9d 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -15,6 +15,7 @@ #include "device-util.h" #include "escape.h" #include "fileio.h" +#include "fstab-util.h" #include "log.h" #include "main-func.h" #include "mount-util.h" @@ -302,7 +303,7 @@ static char *disk_mount_point(const char *label) { if (asprintf(&device, "/dev/mapper/%s", label) < 0) return NULL; - f = setmntent("/etc/fstab", "re"); + f = setmntent(fstab_path(), "re"); if (!f) return NULL; diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index abe4413f92..6fd6fbdf1c 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -112,14 +112,16 @@ static int add_swap( if (r < 0) return log_error_errno(r, "Failed to generate unit name: %m"); - r = generator_open_unit_file(arg_dest, "/etc/fstab", name, &f); + r = generator_open_unit_file(arg_dest, fstab_path(), name, &f); if (r < 0) return r; - fputs("[Unit]\n" - "SourcePath=/etc/fstab\n" - "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" - "[Swap]\n", f); + fprintf(f, + "[Unit]\n" + "SourcePath=%s\n" + "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" + "[Swap]\n", + fstab_path()); r = write_what(f, what); if (r < 0) @@ -340,7 +342,7 @@ static int add_mount( if (r < 0) return log_error_errno(r, "Failed to generate unit name: %m"); - r = generator_open_unit_file(dest, "/etc/fstab", name, &f); + r = generator_open_unit_file(dest, fstab_path(), name, &f); if (r < 0) return r; @@ -463,7 +465,7 @@ static int add_mount( f = safe_fclose(f); - r = generator_open_unit_file(dest, "/etc/fstab", automount_name, &f); + r = generator_open_unit_file(dest, fstab_path(), automount_name, &f); if (r < 0) return r; @@ -515,19 +517,19 @@ static int add_mount( static int parse_fstab(bool initrd) { _cleanup_endmntent_ FILE *f = NULL; - const char *fstab_path; + const char *fstab; struct mntent *me; int r = 0; - fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab"; - log_debug("Parsing %s...", fstab_path); + fstab = initrd ? "/sysroot/etc/fstab" : fstab_path(); + log_debug("Parsing %s...", fstab); - f = setmntent(fstab_path, "re"); + f = setmntent(fstab, "re"); if (!f) { if (errno == ENOENT) return 0; - return log_error_errno(errno, "Failed to open %s: %m", fstab_path); + return log_error_errno(errno, "Failed to open %s: %m", fstab); } while ((me = getmntent(f))) { @@ -606,7 +608,7 @@ static int parse_fstab(bool initrd) { me->mnt_passno, makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT, post, - fstab_path); + fstab); } if (r >= 0 && k < 0) diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 5891013427..7386f70529 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -9,6 +9,7 @@ #include "env-util.h" #include "exit-status.h" +#include "fstab-util.h" #include "log.h" #include "main-func.h" #include "mount-setup.h" @@ -86,10 +87,10 @@ static int run(int argc, char *argv[]) { umask(0022); - f = setmntent("/etc/fstab", "re"); + f = setmntent(fstab_path(), "re"); if (!f) { if (errno != ENOENT) - return log_error_errno(errno, "Failed to open /etc/fstab: %m"); + return log_error_errno(errno, "Failed to open %s: %m", fstab_path()); } else while ((me = getmntent(f))) { /* Remount the root fs, /usr, and all API VFSs */ diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 75e4784c38..f90501eb92 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -19,7 +19,7 @@ int fstab_has_fstype(const char *fstype) { _cleanup_endmntent_ FILE *f = NULL; struct mntent *m; - f = setmntent("/etc/fstab", "re"); + f = setmntent(fstab_path(), "re"); if (!f) return errno == ENOENT ? false : -errno; @@ -39,7 +39,7 @@ int fstab_is_mount_point(const char *mount) { _cleanup_endmntent_ FILE *f = NULL; struct mntent *m; - f = setmntent("/etc/fstab", "re"); + f = setmntent(fstab_path(), "re"); if (!f) return errno == ENOENT ? false : -errno; diff --git a/src/shared/fstab-util.h b/src/shared/fstab-util.h index 0862256511..f575ed0bb2 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -31,3 +31,7 @@ static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no } char *fstab_node_to_udev_node(const char *p); + +static inline const char* fstab_path(void) { + return secure_getenv("SYSTEMD_FSTAB") ?: "/etc/fstab"; +}