[systemd-commits] 2 commits - Makefile.am src/fstab-generator units/initrd-cleanup.service units/initrd-parse-etc.service units/initrd-switch-root.service units/initrd-switch-root.target units/initrd-udevadm-cleanup-db.service

Tom Gundersen tomegun at kemper.freedesktop.org
Fri Mar 1 13:49:53 PST 2013


 Makefile.am                             |    7 ++++-
 src/fstab-generator/fstab-generator.c   |   39 ++++++++++++++++++++++----------
 units/initrd-cleanup.service            |   18 ++++++++++++++
 units/initrd-parse-etc.service          |   19 +++++++++++++++
 units/initrd-switch-root.service        |   19 +++++++++++++++
 units/initrd-switch-root.target         |   16 +++++++++++++
 units/initrd-udevadm-cleanup-db.service |   18 ++++++++++++++
 7 files changed, 124 insertions(+), 12 deletions(-)

New commits:
commit cf843477946451fabf9b5d17eec8ec81515057b6
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Feb 24 17:40:36 2013 +0100

    initrd: add unit files needed for basic systemd-in-initrd support
    
    This will:
     * mount all configured filesystems (typically the rootfs on /sysroot)
     * reload the configuration to pick up anything from the mounted fs (typically
       /sysroot/etc/fstab)
     * mount any newly configured filesystems (typically /usr on /sysroot/usr, if
       applicable)
     * shut-down and clean-up any daemons running in the initramfs (typically udevd)
     * switch-root to /sysroot and start the real init
    
    For an example of what files should be included in an initramfs based on this
    see
    <https://mailman.archlinux.org/pipermail/arch-projects/2013-February/003628.html>.
    
    Cc: Harald Hoyer <harald.hoyer at gmail.com>
    Cc: Dave Reisner <d at falconindy.com>

diff --git a/Makefile.am b/Makefile.am
index 790e501..b697949 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -379,7 +379,12 @@ dist_systemunit_DATA = \
 	units/systemd-ask-password-console.path \
 	units/systemd-udevd-control.socket \
 	units/systemd-udevd-kernel.socket \
-	units/system-update.target
+	units/system-update.target \
+	units/initrd-parse-etc.service \
+	units/initrd-cleanup.service \
+	units/initrd-switch-root.target \
+	units/initrd-udevadm-cleanup-db.service \
+	units/initrd-switch-root.service
 
 nodist_systemunit_DATA = \
 	units/getty at .service \
diff --git a/units/initrd-cleanup.service b/units/initrd-cleanup.service
new file mode 100644
index 0000000..8998696
--- /dev/null
+++ b/units/initrd-cleanup.service
@@ -0,0 +1,18 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Cleaning Up and Shutting Down Daemons
+DefaultDependencies=no
+ConditionPathExists=/etc/initrd-release
+OnFailure=emergency.target
+Requires=local-fs.target swap.target
+After=local-fs.target swap.target
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/systemctl --no-block isolate initrd-switch-root.target
diff --git a/units/initrd-parse-etc.service b/units/initrd-parse-etc.service
new file mode 100644
index 0000000..4bfbb0f
--- /dev/null
+++ b/units/initrd-parse-etc.service
@@ -0,0 +1,19 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Reload Configuration from the Real Root
+DefaultDependencies=no
+Requires=local-fs.target swap.target
+After=local-fs.target swap.target
+OnFailure=emergency.target
+ConditionPathExists=/etc/initrd-release
+
+[Service]
+Type=oneshot
+ExecStartPre=/usr/bin/systemctl daemon-reload
+ExecStart=/usr/bin/systemctl --no-block start initrd-cleanup.service
diff --git a/units/initrd-switch-root.service b/units/initrd-switch-root.service
new file mode 100644
index 0000000..e076b39
--- /dev/null
+++ b/units/initrd-switch-root.service
@@ -0,0 +1,19 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Switch Root
+DefaultDependencies=no
+ConditionPathExists=/etc/initrd-release
+OnFailure=emergency.target
+AllowIsolate=yes
+
+[Service]
+Type=oneshot
+# we have to use "--force" here, otherwise systemd would umount /run
+ExecStart=/usr/bin/systemctl --no-block --force switch-root /sysroot
+KillMode=none
diff --git a/units/initrd-switch-root.target b/units/initrd-switch-root.target
new file mode 100644
index 0000000..f706d29
--- /dev/null
+++ b/units/initrd-switch-root.target
@@ -0,0 +1,16 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Switch Root
+ConditionPathExists=/etc/initrd-release
+DefaultDependencies=no
+Requires=initrd-switch-root.service
+Before=initrd-switch-root.service
+AllowIsolate=yes
+Wants=initrd-udevadm-cleanup-db.service local-fs.target swap.target systemd-journald.service
+After=initrd-udevadm-cleanup-db.service local-fs.target swap.target emergency.service emergency.target
diff --git a/units/initrd-udevadm-cleanup-db.service b/units/initrd-udevadm-cleanup-db.service
new file mode 100644
index 0000000..983189e
--- /dev/null
+++ b/units/initrd-udevadm-cleanup-db.service
@@ -0,0 +1,18 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU Lesser General Public License as published by
+#  the Free Software Foundation; either version 2.1 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Cleanup udevd DB
+DefaultDependencies=no
+ConditionPathExists=/etc/initrd-release
+Conflicts=systemd-udevd.service systemd-udevd-control.socket systemd-udevd-kernel.socket
+After=systemd-udevd.service systemd-udevd-control.socket systemd-udevd-kernel.socket
+Before=initrd-switch-root.target
+
+[Service]
+Type=oneshot
+ExecStart=-/usr/bin/udevadm info --cleanup-db

commit 3d22d1ab57bf44c92e4d9ca95d9728105dd3fb0d
Author: Tom Gundersen <teg at jklm.no>
Date:   Fri Feb 22 11:52:36 2013 +0100

    fstab-generator: initrd - mount selected entries from /sysroot/etc/fstab
    
    We only mount "/usr" and entries marked with "x-initrd.mount".
    
    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 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
     * wait for sysroot-usr.mount to be mounted
     * switch-root
    
    Cc: Harald Hoyer <harald.hoyer at gmail.com>
    Cc: Dave Reisner <d at falconindy.com>

diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c
index 23a8d79..1a1fe86 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -191,6 +191,14 @@ static bool mount_is_network(struct mntent *me) {
                 fstype_is_network(me->mnt_type);
 }
 
+static bool mount_in_initrd(struct mntent *me) {
+        assert(me);
+
+        return
+                hasmntopt(me, "x-initrd.mount") ||
+                streq(me->mnt_dir, "/usr");
+}
+
 static int add_mount(const char *what, const char *where, const char *type, const char *opts,
                      int passno, bool noauto, bool nofail, bool automount, bool isbind, bool isnetwork,
                      const char *source) {
@@ -342,13 +350,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);
 
@@ -373,18 +382,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, bool initrd) {
         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", 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: %m", prefix);
                 return -errno;
         }
 
@@ -392,8 +403,11 @@ static int parse_fstab(void) {
                 char _cleanup_free_ *where = NULL, *what = NULL;
                 int k;
 
+                if (initrd && !mount_in_initrd(me))
+                        continue;
+
                 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;
@@ -419,7 +433,7 @@ static int parse_fstab(void) {
 
                         k = add_mount(what, where, me->mnt_type, me->mnt_opts,
                                      me->mnt_passno, noauto, nofail, automount,
-                                     isbind, isnetwork, "/etc/fstab");
+                                     isbind, isnetwork, fstab_path);
                 }
 
                 if (k < 0)
@@ -552,7 +566,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.");
@@ -572,12 +586,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("", false);
+
+        if (in_initrd())
+                l = parse_fstab("/sysroot", true);
 
-        return (r < 0) || (k < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+        return (r < 0) || (k < 0) || (l < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
 }



More information about the systemd-commits mailing list