Create uuid and label symlinks when mounting devices
authorJeremy Kerr <jk@ozlabs.org>
Mon, 7 Jan 2008 10:25:22 +0000 (21:25 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 7 Jan 2008 10:25:22 +0000 (21:25 +1100)
When we discover a device with ID_FS_UUID or ID_FS_LABEL properties,
create a symlink in the mount tree. This will end up with a mount
tree structure mirroring /dev, and allow devices to be referenced
by uuid when booting is attempted.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
devices/paths.c
devices/paths.h
devices/udev-helper.c

index e9bf6cbcc9c7c49fcb6f4ab03ad9a64e89eba61c..2373c28f560555025d7b522733551b5cb3dffe22 100644 (file)
@@ -15,7 +15,7 @@ struct device_map {
 #define DEVICE_MAP_SIZE 32
 static struct device_map device_map[DEVICE_MAP_SIZE];
 
 #define DEVICE_MAP_SIZE 32
 static struct device_map device_map[DEVICE_MAP_SIZE];
 
-static char *encode_label(const char *label)
+char *encode_label(const char *label)
 {
        char *str, *c;
        int i;
 {
        char *str, *c;
        int i;
index a6f01c4ca56454e0db6032ea992327f1cb01bfb1..26d4ce41eb2a946c8bc9f13b5e5ba7b60637f4a6 100644 (file)
@@ -45,5 +45,9 @@ void set_mount_base(const char *path);
  */
 char *join_paths(const char *a, const char *b);
 
  */
 char *join_paths(const char *a, const char *b);
 
+/**
+ * encode a disk label (or uuid) for use in a symlink.
+ */
+char *encode_label(const char *label);
 
 #endif /* PATHS_H */
 
 #endif /* PATHS_H */
index a55d3780906e15d5274fec996b2bf6ce8906bd5d..f77777a0ccaf51a5f08d93cdf50e2ce5a8967c63 100644 (file)
@@ -184,6 +184,97 @@ int connect_to_socket()
 #endif
 }
 
 #endif
 }
 
+static int mkdir_recursive(const char *dir)
+{
+       char *str, *sep;
+       int mode = 0755;
+       struct stat statbuf;
+
+       pb_log("mkdir_recursive(%s)\n", dir);
+
+       if (!*dir)
+               return 0;
+
+       if (!stat(dir, &statbuf)) {
+               if (!S_ISDIR(statbuf.st_mode)) {
+                       pb_log("%s: %s exists, but isn't a directory\n",
+                                       __func__, dir);
+                       return -1;
+               }
+               return 0;
+       }
+
+       str = strdup(dir);
+       sep = strchr(*str == '/' ? str + 1 : str, '/');
+
+       while (1) {
+
+               /* terminate the path at sep */
+               if (sep)
+                       *sep = '\0';
+               pb_log("mkdir(%s)\n", str);
+
+               if (mkdir(str, mode) && errno != EEXIST) {
+                       pb_log("mkdir(%s): %s\n", str, strerror(errno));
+                       return -1;
+               }
+
+               if (!sep)
+                       break;
+
+               /* reset dir to the full path */
+               strcpy(str, dir);
+               sep = strchr(sep + 1, '/');
+       }
+
+       free(str);
+
+       return 0;
+}
+
+static void setup_device_links(const char *device)
+{
+       struct link {
+               char *env, *dir;
+       } *link, links[] = {
+               {
+                       .env = "ID_FS_UUID",
+                       .dir = "disk/by-uuid"
+               },
+               {
+                       .env = "ID_FS_LABEL",
+                       .dir = "disk/by-label"
+               },
+               {
+                       .env = NULL
+               }
+       };
+
+       for (link = links; link->env; link++) {
+               char *value, *dir, *path;
+
+               value = getenv(link->env);
+               if (!value)
+                       continue;
+
+               value = encode_label(value);
+               dir = join_paths(TMP_DIR, link->dir);
+               path = join_paths(dir, value);
+
+               if (!mkdir_recursive(dir)) {
+                       unlink(path);
+                       if (symlink(mountpoint_for_device(device), path)) {
+                               pb_log("symlink(%s): %s\n",
+                                               path, strerror(errno));
+                       }
+               }
+
+               free(path);
+               free(dir);
+               free(value);
+       }
+}
+
 int mount_device(const char *dev_path)
 {
        const char *dir;
 int mount_device(const char *dev_path)
 {
        const char *dir;
@@ -224,8 +315,10 @@ int mount_device(const char *dev_path)
                goto out;
        }
 
                goto out;
        }
 
-       if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+       if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
+               setup_device_links(dev_path);
                rc = 0;
                rc = 0;
+       }
 
 out:
        return rc;
 
 out:
        return rc;