From d0f59d4190a9f1e0e6db4b22b5e87bec2db4f7fb Mon Sep 17 00:00:00 2001 From: Frantisek Sumsal Date: Mon, 27 Nov 2023 15:20:47 +0100 Subject: [PATCH] fstab-generator: allow overriding /etc/fstab with $SYSTEMD_FSTAB Based on: ed4ad4889723a9acdf75ed86f10cee0024bbbcbc Related: RHEL-1087 rhel-only --- 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 ++++ 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 11162eb722..de4bc9579c 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -13,6 +13,7 @@ #include "device-util.h" #include "escape.h" #include "fileio.h" +#include "fstab-util.h" #include "log.h" #include "mount-util.h" #include "parse-util.h" @@ -318,7 +319,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 f24c1d29da..105ddd2fd0 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -105,15 +105,17 @@ 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("# Automatically generated by systemd-fstab-generator\n\n" - "[Unit]\n" - "SourcePath=/etc/fstab\n" - "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n" - "[Swap]\n", f); + fprintf(f, + "# Automatically generated by systemd-fstab-generator\n\n" + "[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) @@ -334,7 +336,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; @@ -451,7 +453,7 @@ static int add_mount( 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; @@ -503,17 +505,17 @@ 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"; - f = setmntent(fstab_path, "re"); + fstab = initrd ? "/sysroot/etc/fstab" : fstab_path(); + 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))) { @@ -592,7 +594,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 9220a00215..5bcee999cc 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -9,6 +9,7 @@ #include #include "exit-status.h" +#include "fstab-util.h" #include "log.h" #include "mount-setup.h" #include "mount-util.h" @@ -39,14 +40,14 @@ int main(int argc, char *argv[]) { umask(0022); - f = setmntent("/etc/fstab", "re"); + f = setmntent(fstab_path(), "re"); if (!f) { if (errno == ENOENT) { r = 0; goto finish; } - r = log_error_errno(errno, "Failed to open /etc/fstab: %m"); + r = log_error_errno(errno, "Failed to open %s: %m", fstab_path()); goto finish; } diff --git a/src/shared/fstab-util.c b/src/shared/fstab-util.c index 6fd9866c00..bc0c047509 100644 --- a/src/shared/fstab-util.c +++ b/src/shared/fstab-util.c @@ -21,7 +21,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; @@ -41,7 +41,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 9820f78ca8..9ec6db068c 100644 --- a/src/shared/fstab-util.h +++ b/src/shared/fstab-util.h @@ -33,3 +33,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"; +}