]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super0.c
Support --uuid= with --create to choose your own UUID.
[thirdparty/mdadm.git] / super0.c
index fbb7eb9eb53cd17d175e0d833ab4eeaec0425917..757d9056672b2f1110acbdae5ea2d08da19fde73 100644 (file)
--- a/super0.c
+++ b/super0.c
@@ -116,7 +116,8 @@ static void examine_super0(void *sbv, char *homehost)
        printf("     Raid Level : %s\n", c?c:"-unknown-");
        if ((int)sb->level >= 0) {
                int ddsks=0;
-               printf("    Device Size : %d%s\n", sb->size, human_size((long long)sb->size<<10));
+               printf("  Used Dev Size : %d%s\n", sb->size,
+                      human_size((long long)sb->size<<10));
                switch(sb->level) {
                case 1: ddsks=1;break;
                case 4:
@@ -320,6 +321,7 @@ static void getinfo_super0(struct mdinfo *info, void *sbv)
        info->array.ctime = sb->ctime;
        info->array.utime = sb->utime;
        info->array.chunk_size = sb->chunk_size;
+       info->array.state = sb->state;
        info->component_size = sb->size*2;
 
        info->disk.state = sb->this_disk.state;
@@ -503,11 +505,11 @@ static int update_super0(struct mdinfo *info, void *sbv, char *update,
 
 
 static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
-                      unsigned long long size, char *ignored_name, char *homehost)
+                      unsigned long long size, char *ignored_name, char *homehost,
+                      int *uuid)
 {
        mdp_super_t *sb = malloc(MD_SB_BYTES + sizeof(bitmap_super_t));
        int spares;
-       int rfd;
        memset(sb, 0, MD_SB_BYTES + sizeof(bitmap_super_t));
 
        if (info->major_version == -1) {
@@ -523,14 +525,11 @@ static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
                return 0;
        }
 
-       rfd = open("/dev/urandom", O_RDONLY);
        sb->md_magic = MD_SB_MAGIC;
        sb->major_version = 0;
        sb->minor_version = 90;
        sb->patch_version = 0;
        sb->gvalid_words = 0; /* ignored */
-       if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
-               sb->set_uuid0 = random();
        sb->ctime = time(0);
        sb->level = info->level;
        if (size != info->size)
@@ -540,13 +539,23 @@ static int init_super0(struct supertype *st, void **sbp, mdu_array_info_t *info,
        sb->raid_disks = info->raid_disks;
        sb->md_minor = info->md_minor;
        sb->not_persistent = 0;
-       if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
-               sb->set_uuid1 = random();
-               sb->set_uuid2 = random();
-               sb->set_uuid3 = random();
+       if (uuid) {
+               sb->set_uuid0 = uuid[0];
+               sb->set_uuid1 = uuid[1];
+               sb->set_uuid2 = uuid[2];
+               sb->set_uuid3 = uuid[3];
+       } else {
+               int rfd = open("/dev/urandom", O_RDONLY);
+               if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
+                       sb->set_uuid0 = random();
+               if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
+                       sb->set_uuid1 = random();
+                       sb->set_uuid2 = random();
+                       sb->set_uuid3 = random();
+               }
+               if (rfd >= 0)
+                       close(rfd);
        }
-       if (rfd >= 0)
-               close(rfd);
        if (homehost) {
                char buf[20];
                char *hash = sha1_buffer(homehost,
@@ -586,20 +595,12 @@ static void add_to_super0(void *sbv, mdu_disk_info_t *dinfo)
 
 static int store_super0(struct supertype *st, int fd, void *sbv)
 {
-       unsigned long size;
        unsigned long long dsize;
        unsigned long long offset;
        mdp_super_t *super = sbv;
-    
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return 1;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
+
+       if (!get_dev_size(fd, NULL, &dsize))
+               return 1;
 
        if (dsize < MD_RESERVED_SECTORS*2*512)
                return 2;
@@ -706,20 +707,10 @@ static int load_super0(struct supertype *st, int fd, void **sbp, char *devname)
        int uuid[4];
        struct bitmap_super_s *bsb;
     
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size)) {
-                       if (devname)
-                               fprintf(stderr, Name ": cannot find device size for %s: %s\n",
-                                       devname, strerror(errno));
-                       return 1;
-               } else
-                       dsize = size << 9;
-       }
+       if (!get_dev_size(fd, devname, &dsize))
+               return 1;
 
-       if (dsize < MD_RESERVED_SECTORS*2) {
+       if (dsize < MD_RESERVED_SECTORS*512 * 2) {
                if (devname)
                        fprintf(stderr, Name ": %s is too small for md: size is %ld sectors.\n",
                                devname, size);
@@ -875,19 +866,12 @@ static int add_internal_bitmap0(struct supertype *st, void *sbv, int *chunkp,
 void locate_bitmap0(struct supertype *st, int fd, void *sbv)
 {
        unsigned long long dsize;
-       unsigned long size;
        unsigned long long offset;
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
 
-       if (dsize < MD_RESERVED_SECTORS*2)
+       if (!get_dev_size(fd, NULL, &dsize))
+               return;
+
+       if (dsize < MD_RESERVED_SECTORS*512 * 2)
                return;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);
@@ -901,7 +885,6 @@ void locate_bitmap0(struct supertype *st, int fd, void *sbv)
 
 int write_bitmap0(struct supertype *st, int fd, void *sbv)
 {
-       unsigned long size;
        unsigned long long dsize;
        unsigned long long offset;
        mdp_super_t *sb = sbv;
@@ -911,17 +894,11 @@ int write_bitmap0(struct supertype *st, int fd, void *sbv)
        int towrite, n;
        char buf[4096];
 
-#ifdef BLKGETSIZE64
-       if (ioctl(fd, BLKGETSIZE64, &dsize) != 0)
-#endif
-       {
-               if (ioctl(fd, BLKGETSIZE, &size))
-                       return 1;
-               else
-                       dsize = ((unsigned long long)size)<<9;
-       }
+       if (!get_dev_size(fd, NULL, &dsize))
+               return 1;
+
 
-       if (dsize < MD_RESERVED_SECTORS*2)
+       if (dsize < MD_RESERVED_SECTORS*512 * 2)
        return -1;
 
        offset = MD_NEW_SIZE_SECTORS(dsize>>9);