[systemd-devel] [PATCH] fstab-overmount

John Lane systemd at jelmail.com
Fri Jan 4 06:09:10 PST 2013


From: John Lane <john at lane.uk.net>

---
 src/core/mount.c                      | 13 +++++++-
 src/fstab-generator/fstab-generator.c | 58 +++++++++++++++++++++++------------
 2 files changed, 51 insertions(+), 20 deletions(-)

diff --git a/src/core/mount.c b/src/core/mount.c
index f263d9b..b0a06e9 100644
--- a/src/core/mount.c
+++ b/src/core/mount.c
@@ -507,6 +507,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)
@@ -518,7 +521,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 a0173d8..1f693fe 100644
--- a/src/fstab-generator/fstab-generator.c
+++ b/src/fstab-generator/fstab-generator.c
@@ -214,6 +214,9 @@ static int add_mount(const char *what, const char *where, const char *type, cons
                      int passno, bool wait, bool noauto, bool nofail, bool automount, bool isbind, bool isnetwork,
                      const char *source) {
         char *name = NULL, *unit = NULL, *lnk = NULL, *device = NULL, *automount_name = NULL, *automount_unit = NULL;
+        char uniq_suffix[10] = ".mount";
+        int uniq_num = 0;
+        char *prior_unit = NULL;
         FILE *f = NULL;
         int r;
         const char *post, *pre;
@@ -244,27 +247,37 @@ 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) {
-                r = log_oom();
-                goto finish;
-        }
+        do {
 
-        unit = strjoin(arg_dest, "/", name, NULL);
-        if (!unit) {
-                r = log_oom();
-                goto finish;
-        }
+                name = unit_name_from_path(where, uniq_suffix);
+                if (!name) {
+                        r = log_oom();
+                        goto finish;
+                }
 
-        f = fopen(unit, "wxe");
-        if (!f) {
-                r = -errno;
-                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);
-                goto finish;
-        }
+                unit = strjoin(arg_dest, "/", name, NULL);
+                if (!unit) {
+                        r = log_oom();
+                        goto finish;
+                }
+
+                f = fopen(unit, "wxe");
+                if (!f) {
+                        r = -errno;
+                        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);
+                                goto finish;
+                        }
+                }
+        } while (!f);
 
         fprintf(f,
               "# Automatically generated by systemd-fstab-generator\n\n"
@@ -288,6 +301,13 @@ static int add_mount(const char *what, const char *where, const char *type, cons
                         "Before=%s\n",
                         post);
 
+        if (prior_unit) {
+                fprintf(f,
+                        "After=%s\n",
+                        prior_unit);
+                free(prior_unit);
+        }
+
         fprintf(f,
                 "\n"
                 "[Mount]\n"
-- 
1.8.0



More information about the systemd-devel mailing list