chiark / gitweb /
fstab-generator: Allow mount.usr without mount.usrflags, honor rw/ro
[elogind.git] / src / fstab-generator / fstab-generator.c
index 32a8f9bd514685432f785619a1715b67e32d8b43..1f345949289902270a98adaa96b478702a689db9 100644 (file)
@@ -47,7 +47,6 @@ static char *arg_usr_what = NULL;
 static char *arg_usr_fstype = NULL;
 static char *arg_usr_options = NULL;
 
-
 static int mount_find_pri(struct mntent *me, int *ret) {
         char *end, *opt;
         unsigned long r;
@@ -60,7 +59,6 @@ static int mount_find_pri(struct mntent *me, int *ret) {
                 return 0;
 
         opt += strlen("pri");
-
         if (*opt != '=')
                 return -EINVAL;
 
@@ -76,45 +74,14 @@ static int mount_find_pri(struct mntent *me, int *ret) {
         return 1;
 }
 
-static int mount_find_discard(struct mntent *me, char **ret) {
-        char *opt, *ans;
-        size_t len;
-
-        assert(me);
-        assert(ret);
-
-        opt = hasmntopt(me, "discard");
-        if (!opt)
-                return 0;
-
-        opt += strlen("discard");
-
-        if (*opt == ',' || *opt == '\0')
-                ans = strdup("all");
-        else {
-                if (*opt != '=')
-                        return -EINVAL;
-
-                len = strcspn(opt + 1, ",");
-                if (len == 0)
-                        return -EINVAL;
-
-                ans = strndup(opt + 1, len);
-        }
-
-        if (!ans)
-                return -ENOMEM;
-
-        *ret = ans;
-        return 1;
-}
+static int add_swap(
+                const char *what,
+                struct mntent *me,
+                bool noauto,
+                bool nofail) {
 
-static int add_swap(const char *what, struct mntent *me) {
         _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
         _cleanup_fclose_ FILE *f = NULL;
-        _cleanup_free_ char *discard = NULL;
-
-        bool noauto;
         int r, pri = -1;
 
         assert(what);
@@ -131,14 +98,6 @@ static int add_swap(const char *what, struct mntent *me) {
                 return r;
         }
 
-        r = mount_find_discard(me, &discard);
-        if (r < 0) {
-                log_error("Failed to parse discard");
-                return r;
-        }
-
-        noauto = !!hasmntopt(me, "noauto");
-
         name = unit_name_from_path(what, ".swap");
         if (!name)
                 return log_oom();
@@ -152,7 +111,7 @@ static int add_swap(const char *what, struct mntent *me) {
                 if (errno == EEXIST)
                         log_error("Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
                 else
-                        log_error("Failed to create unit file %s: %m", unit);
+                        log_error_errno(errno, "Failed to create unit file %s: %m", unit);
                 return -errno;
         }
 
@@ -165,17 +124,17 @@ static int add_swap(const char *what, struct mntent *me) {
                 "What=%s\n",
                 what);
 
+        /* Note that we currently pass the priority field twice, once
+         * in Priority=, and once in Options= */
         if (pri >= 0)
                 fprintf(f, "Priority=%i\n", pri);
 
-        if (discard)
-                fprintf(f, "Discard=%s\n", discard);
+        if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults"))
+                fprintf(f, "Options=%s\n", me->mnt_opts);
 
-        fflush(f);
-        if (ferror(f)) {
-                log_error("Failed to write unit file %s: %m", unit);
-                return -errno;
-        }
+        r = fflush_and_check(f);
+        if (r < 0)
+                return log_error_errno(r, "Failed to write unit file %s: %m", unit);
 
         /* use what as where, to have a nicer error message */
         r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
@@ -183,15 +142,14 @@ static int add_swap(const char *what, struct mntent *me) {
                 return r;
 
         if (!noauto) {
-                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
+                lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET,
+                              nofail ? ".wants/" : ".requires/", 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;
-                }
+                if (symlink(unit, lnk) < 0)
+                        return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
         }
 
         return 0;
@@ -269,7 +227,7 @@ static int add_mount(
                 if (errno == EEXIST)
                         log_error("Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
                 else
-                        log_error("Failed to create unit file %s: %m", unit);
+                        log_error_errno(errno, "Failed to create unit file %s: %m", unit);
                 return -errno;
         }
 
@@ -308,10 +266,8 @@ static int add_mount(
                 fprintf(f, "Options=%s\n", filtered);
 
         fflush(f);
-        if (ferror(f)) {
-                log_error("Failed to write unit file %s: %m", unit);
-                return -errno;
-        }
+        if (ferror(f))
+                return log_error_errno(errno, "Failed to write unit file %s: %m", unit);
 
         if (!noauto && post) {
                 lnk = strjoin(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL);
@@ -319,10 +275,8 @@ static int add_mount(
                         return log_oom();
 
                 mkdir_parents_label(lnk, 0755);
-                if (symlink(unit, lnk) < 0) {
-                        log_error("Failed to create symlink %s: %m", lnk);
-                        return -errno;
-                }
+                if (symlink(unit, lnk) < 0)
+                        return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
         }
 
         if (automount) {
@@ -336,10 +290,8 @@ static int add_mount(
 
                 fclose(f);
                 f = fopen(automount_unit, "wxe");
-                if (!f) {
-                        log_error("Failed to create unit file %s: %m", automount_unit);
-                        return -errno;
-                }
+                if (!f)
+                        return log_error_errno(errno, "Failed to create unit file %s: %m", automount_unit);
 
                 fprintf(f,
                         "# Automatically generated by systemd-fstab-generator\n\n"
@@ -359,10 +311,8 @@ static int add_mount(
                         where);
 
                 fflush(f);
-                if (ferror(f)) {
-                        log_error("Failed to write unit file %s: %m", automount_unit);
-                        return -errno;
-                }
+                if (ferror(f))
+                        return log_error_errno(errno, "Failed to write unit file %s: %m", automount_unit);
 
                 free(lnk);
                 lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL);
@@ -370,10 +320,8 @@ static int add_mount(
                         return log_oom();
 
                 mkdir_parents_label(lnk, 0755);
-                if (symlink(automount_unit, lnk) < 0) {
-                        log_error("Failed to create symlink %s: %m", lnk);
-                        return -errno;
-                }
+                if (symlink(automount_unit, lnk) < 0)
+                        return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
         }
 
         return 0;
@@ -391,12 +339,13 @@ static int parse_fstab(bool initrd) {
                 if (errno == ENOENT)
                         return 0;
 
-                log_error("Failed to open %s: %m", fstab_path);
+                log_error_errno(errno, "Failed to open %s: %m", fstab_path);
                 return -errno;
         }
 
         while ((me = getmntent(f))) {
                 _cleanup_free_ char *where = NULL, *what = NULL;
+                bool noauto, nofail;
                 int k;
 
                 if (initrd && !mount_in_initrd(me))
@@ -418,16 +367,18 @@ static int parse_fstab(bool initrd) {
                 if (is_path(where))
                         path_kill_slashes(where);
 
-                log_debug("Found entry what=%s where=%s type=%s", what, where, me->mnt_type);
+                noauto = !!hasmntopt(me, "noauto");
+                nofail = !!hasmntopt(me, "nofail");
+                log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
+                          what, where, me->mnt_type,
+                          yes_no(noauto), yes_no(nofail));
 
                 if (streq(me->mnt_type, "swap"))
-                        k = add_swap(what, me);
+                        k = add_swap(what, me, noauto, nofail);
                 else {
-                        bool noauto, nofail, automount;
+                        bool automount;
                         const char *post;
 
-                        noauto = !!hasmntopt(me, "noauto");
-                        nofail = !!hasmntopt(me, "nofail");
                         automount =
                                   hasmntopt(me, "comment=systemd.automount") ||
                                   hasmntopt(me, "x-systemd.automount");
@@ -465,7 +416,7 @@ static int add_root_mount(void) {
         const char *opts;
 
         if (isempty(arg_root_what)) {
-                log_debug("Could not find a root= entry on the kernel commandline.");
+                log_debug("Could not find a root= entry on the kernel command line.");
                 return 0;
         }
 
@@ -525,7 +476,7 @@ static int add_usr_mount(void) {
                         return log_oom();
         }
 
-        if (!arg_usr_what || !arg_usr_options)
+        if (!arg_usr_what)
                 return 0;
 
         what = fstab_node_to_udev_node(arg_usr_what);
@@ -534,7 +485,13 @@ static int add_usr_mount(void) {
                 return -1;
         }
 
-        opts = arg_usr_options;
+        if (!arg_usr_options)
+                opts = arg_root_rw > 0 ? "rw" : "ro";
+        else if (!mount_test_option(arg_usr_options, "ro") &&
+                 !mount_test_option(arg_usr_options, "rw"))
+                opts = strappenda(arg_usr_options, ",", arg_root_rw > 0 ? "rw" : "ro");
+        else
+                opts = arg_usr_options;
 
         log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
         return add_mount(what,
@@ -633,8 +590,9 @@ int main(int argc, char *argv[]) {
 
         umask(0022);
 
-        if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
-                return EXIT_FAILURE;
+        r = parse_proc_cmdline(parse_proc_cmdline_item);
+        if (r < 0)
+                log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
 
         /* Always honour root= and usr= in the kernel command line if we are in an initrd */
         if (in_initrd()) {
@@ -665,6 +623,12 @@ int main(int argc, char *argv[]) {
         }
 
         free(arg_root_what);
+        free(arg_root_fstype);
+        free(arg_root_options);
+
+        free(arg_usr_what);
+        free(arg_usr_fstype);
+        free(arg_usr_options);
 
         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
 }