chiark / gitweb /
generators: add Documentation= fields that point to the generator man pages
[elogind.git] / src / fstab-generator / fstab-generator.c
index 6ebe8aa67300016a901171f3c52cd89f18df232c..248a4ceb90d4d8af4aeace4f31cb1f88c18583a1 100644 (file)
 #include "mount-setup.h"
 #include "special.h"
 #include "mkdir.h"
-#include "virt.h"
 #include "fileio.h"
+#include "generator.h"
+#include "strv.h"
 
 static const char *arg_dest = "/tmp";
-static bool arg_enabled = true;
+static bool arg_fstab_enabled = true;
 
 static int mount_find_pri(struct mntent *me, int *ret) {
         char *end, *pri;
@@ -64,8 +65,9 @@ static int mount_find_pri(struct mntent *me, int *ret) {
 }
 
 static int add_swap(const char *what, struct mntent *me) {
-        _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL, *device = NULL;
+        _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        bool noauto;
         int r, pri = -1;
 
         assert(what);
@@ -77,6 +79,8 @@ static int add_swap(const char *what, struct mntent *me) {
                 return pri;
         }
 
+        noauto = !!hasmntopt(me, "noauto");
+
         name = unit_name_from_path(what, ".swap");
         if (!name)
                 return log_oom();
@@ -98,7 +102,7 @@ static int add_swap(const char *what, struct mntent *me) {
                 "# Automatically generated by systemd-fstab-generator\n\n"
                 "[Unit]\n"
                 "SourcePath=/etc/fstab\n"
-                "\n"
+                "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
                 "[Swap]\n"
                 "What=%s\n",
                 what);
@@ -114,6 +118,18 @@ static int add_swap(const char *what, struct mntent *me) {
                 return -errno;
         }
 
+        if (!noauto) {
+                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
+                if (!lnk)
+                        return log_oom();
+
+                mkdir_parents_label(lnk, 0755);
+                if (symlink(unit, lnk) < 0) {
+                        log_error("Failed to create symlink %s: %m", lnk);
+                        return -errno;
+                }
+        }
+
         return 0;
 }
 
@@ -144,10 +160,12 @@ static int add_mount(
                 bool automount,
                 const char *post,
                 const char *source) {
+
         _cleanup_free_ char
-                *name = NULL, *unit = NULL, *lnk = NULL, *device = NULL,
+                *name = NULL, *unit = NULL, *lnk = NULL,
                 *automount_name = NULL, *automount_unit = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        int r;
 
         assert(what);
         assert(where);
@@ -167,6 +185,12 @@ static int add_mount(
             mount_point_ignore(where))
                 return 0;
 
+        if (path_equal(where, "/")) {
+                automount = false;
+                noauto = false;
+                nofail = false;
+        }
+
         name = unit_name_from_path(where, ".mount");
         if (!name)
                 return log_oom();
@@ -185,30 +209,34 @@ static int add_mount(
         }
 
         fprintf(f,
-              "# Automatically generated by systemd-fstab-generator\n\n"
-              "[Unit]\n"
-              "SourcePath=%s\n",
-              source);
+                "# Automatically generated by systemd-fstab-generator\n\n"
+                "[Unit]\n"
+                "SourcePath=%s\n"
+                "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
+                source);
 
         if (post && !noauto && !nofail && !automount)
                 fprintf(f,
                         "Before=%s\n",
                         post);
 
+        if (passno != 0) {
+                r = generator_write_fsck_deps(f, arg_dest, what, where, type);
+                if (r < 0)
+                        return r;
+        }
+
         fprintf(f,
                 "\n"
                 "[Mount]\n"
                 "What=%s\n"
                 "Where=%s\n"
-                "Type=%s\n"
-                "FsckPassNo=%i\n",
+                "Type=%s\n",
                 what,
                 where,
-                type,
-                passno);
+                type);
 
-        if (!isempty(opts) &&
-            !streq(opts, "defaults"))
+        if (!isempty(opts) && !streq(opts, "defaults"))
                 fprintf(f,
                         "Options=%s\n",
                         opts);
@@ -233,7 +261,7 @@ static int add_mount(
                 }
         }
 
-        if (automount && !path_equal(where, "/")) {
+        if (automount) {
                 automount_name = unit_name_from_path(where, ".automount");
                 if (!automount_name)
                         return log_oom();
@@ -252,12 +280,13 @@ static int add_mount(
                 fprintf(f,
                         "# Automatically generated by systemd-fstab-generator\n\n"
                         "[Unit]\n"
-                        "SourcePath=%s\n",
+                        "SourcePath=%s\n"
+                        "Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
                         source);
 
                 if (post)
                         fprintf(f,
-                                "Before= %s\n",
+                                "Before=%s\n",
                                 post);
 
                 fprintf(f,
@@ -286,22 +315,19 @@ static int add_mount(
         return 0;
 }
 
-static int parse_fstab(const char *prefix, bool initrd) {
-        _cleanup_free_ char *fstab_path = NULL;
-        FILE *f;
-        int r = 0;
+static int parse_fstab(bool initrd) {
+        _cleanup_endmntent_ FILE *f;
+        const char *fstab_path;
         struct mntent *me;
+        int r = 0;
 
-        fstab_path = strjoin(strempty(prefix), "/etc/fstab", NULL);
-        if (!fstab_path)
-                return log_oom();
-
-        f = setmntent(fstab_path, "r");
+        fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
+        f = setmntent(fstab_path, "re");
         if (!f) {
                 if (errno == ENOENT)
                         return 0;
 
-                log_error("Failed to open %s/etc/fstab: %m", strempty(prefix));
+                log_error("Failed to open %s: %m", fstab_path);
                 return -errno;
         }
 
@@ -313,11 +339,12 @@ static int parse_fstab(const char *prefix, bool initrd) {
                         continue;
 
                 what = fstab_node_to_udev_node(me->mnt_fsname);
-                where = strjoin(strempty(prefix), me->mnt_dir, NULL);
-                if (!what || !where) {
-                        r = log_oom();
-                        goto finish;
-                }
+                if (!what)
+                        return log_oom();
+
+                where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir);
+                if (!where)
+                        return log_oom();
 
                 if (is_path(where))
                         path_kill_slashes(where);
@@ -336,15 +363,14 @@ static int parse_fstab(const char *prefix, bool initrd) {
                                   hasmntopt(me, "comment=systemd.automount") ||
                                   hasmntopt(me, "x-systemd.automount");
 
-                        if (initrd) {
+                        if (initrd)
                                 post = SPECIAL_INITRD_FS_TARGET;
-                        } else if (mount_in_initrd(me)) {
+                        else if (mount_in_initrd(me))
                                 post = SPECIAL_INITRD_ROOT_FS_TARGET;
-                        } else if (mount_is_network(me)) {
+                        else if (mount_is_network(me))
                                 post = SPECIAL_REMOTE_FS_TARGET;
-                        } else {
+                        else
                                 post = SPECIAL_LOCAL_FS_TARGET;
-                        }
 
                         k = add_mount(what, where, me->mnt_type, me->mnt_opts,
                                       me->mnt_passno, noauto, nofail, automount,
@@ -355,23 +381,21 @@ static int parse_fstab(const char *prefix, bool initrd) {
                         r = k;
         }
 
-finish:
-        endmntent(f);
         return r;
 }
 
 static int parse_new_root_from_proc_cmdline(void) {
         _cleanup_free_ char *what = NULL, *type = NULL, *opts = NULL, *line = NULL;
+        bool noauto, nofail;
         char *w, *state;
-        int r;
         size_t l;
-        bool noauto, nofail;
+        int r;
 
-        r = read_one_line_file("/proc/cmdline", &line);
-        if (r < 0) {
-                log_error("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+        r = proc_cmdline(&line);
+        if (r < 0)
+                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+        if (r <= 0)
                 return 0;
-        }
 
         opts = strdup("ro");
         type = strdup("auto");
@@ -435,63 +459,31 @@ static int parse_new_root_from_proc_cmdline(void) {
         }
 
         log_debug("Found entry what=%s where=/sysroot type=%s", what, type);
-        r = add_mount(what, "/sysroot", type, opts, 0, noauto, nofail, false,
+        r = add_mount(what, "/sysroot", type, opts, 1, noauto, nofail, false,
                       SPECIAL_INITRD_ROOT_FS_TARGET, "/proc/cmdline");
 
         return (r < 0) ? r : 0;
 }
 
-static int parse_proc_cmdline(void) {
-        _cleanup_free_ char *line = NULL;
-        char *w, *state;
+static int parse_proc_cmdline_item(const char *key, const char *value) {
         int r;
-        size_t l;
 
-        if (detect_container(NULL) > 0)
-                return 0;
+        if (STR_IN_SET(key, "fstab", "rd.fstab") && value) {
 
-        r = read_one_line_file("/proc/cmdline", &line);
-        if (r < 0) {
-                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
-                return 0;
-        }
-
-        FOREACH_WORD_QUOTED(w, l, line, state) {
-                _cleanup_free_ char *word = NULL;
-
-                word = strndup(w, l);
-                if (!word)
-                        return log_oom();
-
-                if (startswith(word, "fstab=")) {
-                        r = parse_boolean(word + 6);
-                        if (r < 0)
-                                log_warning("Failed to parse fstab switch %s. Ignoring.", word + 6);
-                        else
-                                arg_enabled = r;
-
-                } else if (startswith(word, "rd.fstab=")) {
-
-                        if (in_initrd()) {
-                                r = parse_boolean(word + 9);
-                                if (r < 0)
-                                        log_warning("Failed to parse fstab switch %s. Ignoring.", word + 9);
-                                else
-                                        arg_enabled = r;
-                        }
-
-                } else if (startswith(word, "fstab.") ||
-                           (in_initrd() && startswith(word, "rd.fstab."))) {
+                r = parse_boolean(value);
+                if (r < 0)
+                        log_warning("Failed to parse fstab switch %s. Ignoring.", value);
+                else
+                        arg_fstab_enabled = r;
 
-                        log_warning("Unknown kernel switch %s. Ignoring.", word);
-                }
-        }
+        } else if (startswith(key, "fstab.") || startswith(key, "rd.fstab."))
+                log_warning("Unknown kernel switch %s. Ignoring.", key);
 
         return 0;
 }
 
 int main(int argc, char *argv[]) {
-        int r = 0, k, l = 0;
+        int r = 0;
 
         if (argc > 1 && argc != 4) {
                 log_error("This program takes three or no arguments.");
@@ -507,19 +499,29 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (parse_proc_cmdline() < 0)
+        if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
                 return EXIT_FAILURE;
 
+        /* Always honour root= in the kernel command line if we are in an initrd */
         if (in_initrd())
                 r = parse_new_root_from_proc_cmdline();
 
-        if (!arg_enabled)
-                return (r < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+        /* Honour /etc/fstab only when that's enabled */
+        if (arg_fstab_enabled) {
+                int k;
 
-        k = parse_fstab(NULL, false);
+                /* Parse the local /etc/fstab, possibly from the initrd */
+                k = parse_fstab(false);
+                if (k < 0)
+                        r = k;
 
-        if (in_initrd())
-                l = parse_fstab("/sysroot", true);
+                /* If running in the initrd also parse the /etc/fstab from the host */
+                if (in_initrd()) {
+                        k = parse_fstab(true);
+                        if (k < 0)
+                                r = k;
+                }
+        }
 
-        return (r < 0) || (k < 0) || (l < 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }