]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - util.c
Util: get device size from id
[thirdparty/mdadm.git] / util.c
diff --git a/util.c b/util.c
index 57b7e1f57d91a71294d489e05fce40d602e14793..1ecce3b8fd73d598051955bd8c6343b65bf29a9b 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1086,14 +1086,30 @@ struct supertype *super_by_fd(int fd, char **subarrayp)
                sysfs_free(sra);
        if (st) {
                st->sb = NULL;
-               st->subarray[0] = 0;
-               *subarrayp = subarray;
+               if (subarrayp)
+                       *subarrayp = subarray;
        } else
                free(subarray);
        return st;
 }
 #endif /* !defined(MDASSEMBLE) || defined(MDASSEMBLE) && defined(MDASSEMBLE_AUTO) */
 
+int dev_size_from_id(unsigned int id, unsigned long long *size)
+{
+       char buf[20];
+       int fd;
+
+       sprintf(buf, "%d:%d", major(id), minor(id));
+       fd = dev_open(buf, O_RDONLY);
+       if (fd < 0)
+               return 0;
+       if (get_dev_size(fd, NULL, size)) {
+               close(fd);
+               return 1;
+       }
+       close(fd);
+       return 0;
+}
 
 struct supertype *dup_super(struct supertype *orig)
 {
@@ -1108,7 +1124,6 @@ struct supertype *dup_super(struct supertype *orig)
        st->ss = orig->ss;
        st->max_devs = orig->max_devs;
        st->minor_version = orig->minor_version;
-       strcpy(st->subarray, orig->subarray);
        st->sb = NULL;
        st->info = NULL;
        return st;
@@ -1190,6 +1205,20 @@ int get_dev_size(int fd, char *dname, unsigned long long *sizep)
        return 1;
 }
 
+/* Return true if this can only be a container, not a member device.
+ * i.e. is and md device and size is zero
+ */
+int must_be_container(int fd)
+{
+       unsigned long long size;
+       if (md_get_version(fd) < 0)
+               return 0;
+       if (get_dev_size(fd, NULL, &size) == 0)
+               return 1;
+       if (size == 0)
+               return 1;
+       return 0;
+}
 
 /* Sets endofpart parameter to the last block used by the last GPT partition on the device.
  * Returns: 1 if successful
@@ -1499,17 +1528,17 @@ int open_subarray(char *dev, char *subarray, struct supertype *st, int quiet)
                goto free_sysfs;
        }
 
-       if (st->ss->load_super(st, fd, NULL)) {
+       if (!st->ss->load_container) {
                if (!quiet)
-                       fprintf(stderr, Name ": Failed to load metadata for %s\n",
-                               dev);
+                       fprintf(stderr, Name ": %s is not a container\n", dev);
                goto free_name;
        }
 
-       if (!st->loaded_container) {
+       if (st->ss->load_container(st, fd, NULL)) {
                if (!quiet)
-                       fprintf(stderr, Name ": %s is not a container\n", dev);
-               goto free_super;
+                       fprintf(stderr, Name ": Failed to load metadata for %s\n",
+                               dev);
+               goto free_name;
        }
 
        info = st->ss->container_content(st, subarray);