X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Ffstab-generator%2Ffstab-generator.c;h=496657bdc176a25b170788d8304bf306ceff5b1b;hb=00b4ffdecbb39d849af65c0ab741be482325a4a9;hp=94cbc3a5fe8f64d7c9e4d7fd63420e0782ccc8d6;hpb=3f85ef0f05ffc51e19f86fb83a1c51e8e3cd6817;p=elogind.git diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 94cbc3a5f..496657bdc 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -74,16 +74,24 @@ static int mount_find_pri(struct mntent *me, int *ret) { return 1; } -static int add_swap(const char *what, struct mntent *me) { +static int add_swap( + const char *what, + struct mntent *me, + bool noauto, + bool nofail) { + _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL; _cleanup_fclose_ FILE *f = NULL; - - bool noauto; int r, pri = -1; assert(what); assert(me); + if (access("/proc/swaps", F_OK) < 0) { + log_info("Swap not supported, ignoring fstab swap entry for %s.", what); + return 0; + } + if (detect_container(NULL) > 0) { log_info("Running in a container, ignoring fstab swap entry for %s.", what); return 0; @@ -95,8 +103,6 @@ static int add_swap(const char *what, struct mntent *me) { return r; } - noauto = !!hasmntopt(me, "noauto"); - name = unit_name_from_path(what, ".swap"); if (!name) return log_oom(); @@ -110,7 +116,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; } @@ -132,10 +138,8 @@ static int add_swap(const char *what, struct mntent *me) { fprintf(f, "Options=%s\n", me->mnt_opts); r = fflush_and_check(f); - if (r < 0) { - log_error("Failed to write unit file %s: %s", unit, strerror(-r)); - return r; - } + 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); @@ -143,15 +147,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; @@ -229,7 +232,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; } @@ -268,10 +271,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); @@ -279,10 +280,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) { @@ -296,10 +295,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" @@ -319,10 +316,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); @@ -330,10 +325,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; @@ -351,12 +344,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)) @@ -366,7 +360,7 @@ static int parse_fstab(bool initrd) { if (!what) return log_oom(); - if (detect_container(NULL) > 0 && is_device_path(what)) { + if (is_device_path(what) && path_is_read_only_fs("sys") > 0) { log_info("Running in a container, ignoring fstab device entry for %s.", what); continue; } @@ -378,16 +372,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"); @@ -485,7 +481,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); @@ -494,7 +490,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, @@ -593,8 +595,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()) { @@ -625,6 +628,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; }