From ff194540782b9330d3154ee152d76b50779c4cc0 Mon Sep 17 00:00:00 2001 From: John Lane Date: Tue, 15 Jan 2013 10:21:54 +0000 Subject: [PATCH] fstab overmount --- src/core/mount.c | 13 +++++++++- src/fstab-generator/fstab-generator.c | 49 +++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/core/mount.c b/src/core/mount.c index 25bc7e1..8f35f68 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -518,6 +518,9 @@ static int mount_fix_timeouts(Mount *m) { static int mount_verify(Mount *m) { bool b; char *e; + char *ext, *suf; + int name_len; + char cm[strlen(UNIT(m)->id)]; assert(m); if (UNIT(m)->load_state != UNIT_LOADED) @@ -529,7 +532,15 @@ static int mount_verify(Mount *m) { if (!(e = unit_name_from_path(m->where, ".mount"))) return -ENOMEM; - b = unit_has_name(UNIT(m), e); + /* clean mount file name with any overmount suffix removed */ + ext = strrchr(UNIT(m)->id,'.'); + suf = strrchr(UNIT(m)->id,'_'); + name_len = (suf ? suf : ext ) - UNIT(m)->id; + strncpy(cm,UNIT(m)->id, name_len); + strcpy(cm + name_len, ext); + log_debug("clean mount file name (with any overmount suffix removed): %s", cm); + + b = unit_has_name(UNIT(m), e) || !strcmp(e,cm); free(e); if (!b) { diff --git a/src/fstab-generator/fstab-generator.c b/src/fstab-generator/fstab-generator.c index 2b67bb6..aad2d49 100644 --- a/src/fstab-generator/fstab-generator.c +++ b/src/fstab-generator/fstab-generator.c @@ -198,6 +198,10 @@ static int add_mount(const char *what, const char *where, const char *type, cons int r; const char *post, *pre; + char uniq_suffix[10] = ".mount"; + int uniq_num = 0; + char *prior_unit = NULL; + assert(what); assert(where); assert(type); @@ -224,22 +228,33 @@ static int add_mount(const char *what, const char *where, const char *type, cons pre = SPECIAL_LOCAL_FS_PRE_TARGET; } - name = unit_name_from_path(where, ".mount"); - if (!name) - return log_oom(); + do { - unit = strjoin(arg_dest, "/", name, NULL); - if (!unit) - return log_oom(); + name = unit_name_from_path(where, uniq_suffix); + if (!name) + return log_oom(); - f = fopen(unit, "wxe"); - if (!f) { - 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); - return -errno; - } + unit = strjoin(arg_dest, "/", name, NULL); + if (!unit) + return log_oom(); + + f = fopen(unit, "wxe"); + if (!f) { + if (errno == EEXIST) + { + free(prior_unit); + prior_unit = strdup(name); + snprintf(uniq_suffix, 10, "_%d.mount", uniq_num++); + log_warning("Failed to create mount unit file %s because it already exists (overmount entry in /etc/fstab?). Will retry with suffix %s", unit, uniq_suffix); + } + else + { + log_error("Failed to create unit file %s: %m", unit); + return -errno; + } + } + + } while (!f); fprintf(f, "# Automatically generated by systemd-fstab-generator\n\n" @@ -257,6 +272,12 @@ static int add_mount(const char *what, const char *where, const char *type, cons pre, pre); + if (prior_unit) { + fprintf(f, + "After=%s\n", + prior_unit); + free(prior_unit); + } if (!noauto && !nofail && !automount) fprintf(f, -- 1.8.0