[systemd-devel] [PATCH 1/2] fstab-generator: initrd - mount the entries in /etc/fstab.sys from the initramfs

Tom Gundersen teg at jklm.no
Mon Feb 25 07:00:31 PST 2013


This (together with the right unit files) is needed in the initramfs in order to
natively support mounting /usr (and friends) from the initramfs.

The concept of /etc/fstab.sys is taken from dracut, but might be used elsewhere
too.

The idea should be that /etc/fstab.sys should contain all filesystems containing
"system resources" needed by early boot (i.e. fs' that cannot be mounted or
unmounted by the real init). The canonical example would be /usr, but one could
imagine also keeping /usr/local or other subdirectories on separate partitions.

Notably, /etc (obviously) cannot be mounted from fstab.sys, but must still
reside on the rootfs.

The way it is meant to work is:
 * wait for sysroot.mount to be mounted
 * do a daemon-reload to generate sysroot-usr.mount from /sysroot/etc/fstab.sys
 * wait for sysroot-usr.mount to be mounted
 * switch-root

Two alternatives were considered, but decided against:
 * configure usr= on the kernel commandline in the same way as root= is.
   However, this would be restricted to only the /usr mountpoint, and the
   kernelcommandline is crowded enough as it is, so it seems more reasonable to
   store the configuration in /etc (as we can). Moreover, this logic would allow
   us to use more configuration sources from /etc in the future should that be
   necessary.
 * configure the 'sys-mounts' in /etc/fstab. This would require a heuristic to
   decide which mounts sohuld be taken care of by the initramfs and which to be
   taken care of by the real init. I could not come up with a non-hacky way of
   doing that. Perhaps there is use-case for mounting with one set of options in
   the initrd and remounting with a different set of options in the real init
   (like can be done for the rootfs)? Keeping everything in fstab would make
   that impossible.

Cc: Harald Hoyer <harald.hoyer at gmail.com>
Cc: Dave Reisner <d at falconindy.com>
---
 src/fstab-generator/fstab-generator.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index bfedded..05833ba 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -344,13 +344,14 @@ static int add_mount(const char *what, const char *where, const char *type, cons
                 fprintf(f,
                         "# Automatically generated by systemd-fstab-generator\n\n"
                         "[Unit]\n"
-                        "SourcePath=/etc/fstab\n"
+                        "SourcePath=%s\n"
                         "DefaultDependencies=no\n"
                         "Conflicts=" SPECIAL_UMOUNT_TARGET "\n"
                         "Before=" SPECIAL_UMOUNT_TARGET " %s\n"
                         "\n"
                         "[Automount]\n"
                         "Where=%s\n",
+                        source,
                         post,
                         where);
 
@@ -375,18 +376,20 @@ static int add_mount(const char *what, const char *where, const char *type, cons
         return 0;
 }
 
-static int parse_fstab(void) {
+static int parse_fstab(const char *prefix, const char *postfix) {
         FILE *f;
+        _cleanup_free_ char *fstab_path = NULL;
         int r = 0;
         struct mntent *me;
 
         errno = 0;
-        f = setmntent("/etc/fstab", "r");
+        fstab_path = strjoin(prefix, "/etc/fstab", postfix, NULL);
+        f = setmntent(fstab_path, "r");
         if (!f) {
                 if (errno == ENOENT)
                         return 0;
 
-                log_error("Failed to open /etc/fstab: %m");
+                log_error("Failed to open %s/etc/fstab%s: %m", prefix, postfix);
                 return -errno;
         }
 
@@ -395,7 +398,7 @@ static int parse_fstab(void) {
                 int k;
 
                 what = fstab_node_to_udev_node(me->mnt_fsname);
-                where = strdup(me->mnt_dir);
+                where = strjoin(prefix, me->mnt_dir, NULL);
                 if (!what || !where) {
                         r = log_oom();
                         goto finish;
@@ -422,7 +425,7 @@ static int parse_fstab(void) {
                         k = add_mount(what, where, me->mnt_type, me->mnt_opts,
                                      me->mnt_passno, false, noauto, nofail,
                                      automount, isbind, isnetwork,
-                                     "/etc/fstab");
+                                     fstab_path);
                 }
 
                 if (k < 0)
@@ -557,7 +560,7 @@ static int parse_proc_cmdline(void) {
 }
 
 int main(int argc, char *argv[]) {
-        int r, k = 0;
+        int r = 0, k, l = 0;
 
         if (argc > 1 && argc != 4) {
                 log_error("This program takes three or no arguments.");
@@ -577,12 +580,15 @@ int main(int argc, char *argv[]) {
                 return EXIT_FAILURE;
 
         if (in_initrd())
-                k = parse_new_root_from_proc_cmdline();
+                r = parse_new_root_from_proc_cmdline();
 
         if (!arg_enabled)
-                return EXIT_SUCCESS;
+                return (r < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
 
-        r = parse_fstab();
+        k = parse_fstab("", "");
 
-        return (r < 0) || (k < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+        if (in_initrd())
+                l = parse_fstab("/sysroot", ".sys");
+
+        return (r < 0) || (k < 0) || (l < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
 }
-- 
1.8.1.4



More information about the systemd-devel mailing list