]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Allow overriding /etc/fstab with $SYSTEMD_FSTAB
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Nov 2019 16:36:46 +0000 (17:36 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Nov 2019 21:04:51 +0000 (22:04 +0100)
docs/ENVIRONMENT.md
src/cryptsetup/cryptsetup.c
src/fstab-generator/fstab-generator.c
src/remount-fs/remount-fs.c
src/shared/fstab-util.c
src/shared/fstab-util.h

index 5ad18c9e97ba5d52136f8a362b303a8654234ba7..4882cab6005cb2b51e3f4f7ad361ccb61d7a6eb1 100644 (file)
@@ -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.
 
index dd8c178fcbe13e22864b0fc746453b51ae608be9..dd1504dc9daf7a311ccb7711399b38290333a348 100644 (file)
@@ -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;
 
index abe4413f929dd6374efba748ed27163053ef5033..6fd6fbdf1ccdf542b8cf6ae6b7ac3a16d36ff723 100644 (file)
@@ -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)
index 589101342703bd203ff42e1852530d2b4ea61dd0..7386f705291c5b17660e2d04e07a7983baa75af3 100644 (file)
@@ -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 */
index 75e4784c3879a38f7c569b470e7b9791b8fa94c4..f90501eb9288b6ffe8ff35933ebd45bbc52bedfa 100644 (file)
@@ -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;
 
index 08622565113b82420b06e297ab82fc22ae8e0ca2..f575ed0bb2996962e10d14b5d28fc5004e740a18 100644 (file)
@@ -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";
+}