]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - mdopen.c
--wait or -W will wait for resync activity to finish on the given devices.
[thirdparty/mdadm.git] / mdopen.c
index 5cca17922243d50a339f79d85beb7818e6187e9d..9f3dfb8465ec31a10b72e8816ea7c93ef7b81321 100644 (file)
--- a/mdopen.c
+++ b/mdopen.c
 #include "md_p.h"
 #include <ctype.h>
 
-void make_parts(char *dev, int cnt)
+
+void make_dev_symlink(char *dev)
+{
+       char *new = strdup(dev);
+
+       if (!new) return;
+       /* /dev/md/0 -> /dev/md0
+        * /dev/md/d0 -> /dev/md_d0
+        */
+       if (isdigit(new[8]))
+               strcpy(new+7, new+8);
+       else
+               new[7] = '_';
+       if (symlink(dev+5, new))
+               perror(new);
+}
+
+
+void make_parts(char *dev, int cnt, int symlinks)
 {
        /* make 'cnt' partition devices for 'dev'
         * We use the major/minor from dev and add 1..cnt
@@ -71,11 +89,14 @@ void make_parts(char *dev, int cnt)
                        perror("chown");
                if (chmod(name, stb2.st_mode & 07777))
                        perror("chmod");
+               if (symlinks && strncmp(name, "/dev/md/", 8) == 0)
+                       make_dev_symlink(name);
                stat(name, &stb2);
                add_dev(name, &stb2, 0, NULL);
        }
 }
 
+
 /*
  * Open a given md device, and check that it really is one.
  * If 'autof' is given, then we need to create, or recreate, the md device.
@@ -94,7 +115,7 @@ int open_mddev(char *dev, int autof)
        int must_remove = 0;
        struct mdstat_ent *mdlist;
        int num;
-       struct createinfo *ci = conf_get_create_info(NULL);
+       struct createinfo *ci = conf_get_create_info();
        int parts;
 
        if (autof == 0)
@@ -114,10 +135,6 @@ int open_mddev(char *dev, int autof)
                                dev);
                        return -1;
                }
-               if (autof == 2 && stb.st_mode == 0 && !is_standard(dev, NULL)) {
-                       fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
-                       return -1;
-               }
                /* check major number is correct */
                num = -1;
                std = is_standard(dev, &num);
@@ -125,19 +142,23 @@ int open_mddev(char *dev, int autof)
                switch(autof) {
                case 2: /* only create is_standard names */
                        if (!std && !stb.st_mode) {
-                               fprintf(stderr, Name ": --auto=yes requires a 'standard' md device name, not %s\n", dev);
+                               fprintf(stderr, Name
+                       ": %s does not exist and is not a 'standard' name "
+                       "so it cannot be created\n", dev);
                                return -1;
                        }
                        break;
                case 3: /* create md, reject std>0 */
                        if (std > 0) {
-                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+                               fprintf(stderr, Name ": that --auto option "
+                               "not compatable with device named %s\n", dev);
                                return -1;
                        }
                        break;
                case 4: /* create mdp, reject std<0 */
                        if (std < 0) {
-                               fprintf(stderr, Name ": that --auto option not compatable with device named %s\n", dev);
+                               fprintf(stderr, Name ": that --auto option "
+                               "not compatable with device named %s\n", dev);
                                return -1;
                        }
                        break;
@@ -151,7 +172,6 @@ int open_mddev(char *dev, int autof)
                if (stb.st_mode && major(stb.st_rdev) != major)
                        must_remove = 1;
                if (stb.st_mode && !must_remove) {
-                       mdu_array_info_t array;
                        /* looks ok, see if it is available */
                        mdfd = open(dev, O_RDWR, 0);
                        if (mdfd < 0) {
@@ -164,15 +184,9 @@ int open_mddev(char *dev, int autof)
                                close(mdfd);
                                return -1;
                        }
-                       if (ioctl(mdfd, GET_ARRAY_INFO, &array)==0) {
-                               /* already active */
-                               must_remove = 1;
-                               close(mdfd);
-                       } else {
-                               if (major != MD_MAJOR && parts > 0)
-                                       make_parts(dev, parts);
-                               return mdfd;
-                       }
+                       if (major != MD_MAJOR && parts > 0)
+                               make_parts(dev, parts, ci->symlinks);
+                       return mdfd;
                }
                /* Ok, need to find a minor that is not in use.
                 * If the device name is in a 'standard' format,
@@ -234,6 +248,14 @@ int open_mddev(char *dev, int autof)
                        if (must_remove)
                                unlink(dev);
 
+                       if (strncmp(dev, "/dev/md/", 8) == 0) {
+                               if (mkdir("/dev/md",0700)==0) {
+                                       if (chown("/dev/md", ci->uid, ci->gid))
+                                               perror("chown /dev/md");
+                                       if (chmod("/dev/md", ci->mode| ((ci->mode>>2) & 0111)))
+                                               perror("chmod /dev/md");
+                               }
+                       }
                        if (mknod(dev, S_IFBLK|0600, makedev(major, minor))!= 0) {
                                fprintf(stderr, Name ": failed to create %s\n", dev);
                                return -1;
@@ -251,8 +273,10 @@ int open_mddev(char *dev, int autof)
                        }
                        stat(dev, &stb);
                        add_dev(dev, &stb, 0, NULL);
+                       if (ci->symlinks && strncmp(dev, "/dev/md/", 8) == 0)
+                               make_dev_symlink(dev);
                        if (major != MD_MAJOR)
-                               make_parts(dev,parts);
+                               make_parts(dev,parts, ci->symlinks);
                }
        }
        mdfd = open(dev, O_RDWR, 0);