1 /* MDRAID Superblock generator
2 This should create valid mdraid superblock for raid1 with 1 device.
3 It is still work in progress, but following seems to be recognized:
7 mdadm --examine test.img
9 losetup /dev/loop1 test.img
10 mdadm --assemble md /dev/loop1
19 #include <linux/raid/md_p.h>
21 void random_uuid(__u8
*buf
)
24 for (int i
= 0; i
< 4; i
++)
29 static unsigned int calc_sb_1_csum(struct mdp_superblock_1
* sb
)
31 unsigned int disk_csum
, csum
;
32 unsigned long long newcsum
;
33 int size
= sizeof(*sb
) + __le32_to_cpu(sb
->max_dev
)*2;
34 unsigned int *isuper
= (unsigned int*)sb
;
36 /* make sure I can count... (needs include cstddef) */
38 if (offsetof(struct mdp_superblock_1,data_offset) != 128 ||
39 offsetof(struct mdp_superblock_1, utime) != 192 ||
40 sizeof(struct mdp_superblock_1) != 256) {
41 fprintf(stderr, "WARNING - superblock isn't sized correctly\n");
45 disk_csum
= sb
->sb_csum
;
48 for (; size
>=4; size
-= 4 ) {
49 newcsum
+= __le32_to_cpu(*isuper
);
54 newcsum
+= __le16_to_cpu(*(unsigned short*) isuper
);
56 csum
= (newcsum
& 0xffffffff) + (newcsum
>> 32);
57 sb
->sb_csum
= disk_csum
;
58 return __cpu_to_le32(csum
);
62 size_t data_size
= 10000; //512B sectors
64 srand(time(NULL
)); //FIXME: Seed UUID properly
66 struct mdp_superblock_1 sb
= {0};
68 /* constant array information - 128 bytes */
69 sb
.magic
= 0xa92b4efc; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
70 sb
.major_version
= 1; /* 1 */
71 sb
.feature_map
= 0; /* bit 0 set if 'bitmap_offset' is meaningful */
72 sb
.pad0
= 0; /* always set to 0 when writing */
75 random_uuid(sb
.set_uuid
); /* user-space generated. U8[16]*/
76 memcpy(sb
.set_name
, "localhost:7", 12); /* set and interpreted by user-space. CHAR[32] */
77 sb
.ctime
=0; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
79 sb
.level
=1; /* -4 (multipath), -1 (linear), 0,1,4,5 */
80 //sb.layout=2; /* only for raid5 and raid10 currently */
81 sb
.size
=data_size
+128; /* used size of component devices, in 512byte sectors */
83 sb
.chunksize
=0; /* in 512byte sectors - not used in raid 1 */
85 sb
.bitmap_offset
=8; /* sectors after start of superblock that bitmap starts
86 * NOTE: signed, so bitmap can be before superblock
87 * only meaningful of feature_map[0] is set.
90 /* constant this-device information - 64 bytes */
91 sb
.data_offset
=128; /* sector start of data, often 0 */
92 sb
.data_size
=data_size
; /* sectors in this device that can be used for data */
93 sb
.super_offset
=8; /* sector start of this superblock */
95 sb
.dev_number
=0; /* permanent identifier of this device - not role in raid */
96 sb
.cnt_corrected_read
=0; /* number of read errors that were corrected by re-writing */
97 random_uuid(sb
.device_uuid
); /* user-space setable, ignored by kernel U8[16] */
98 sb
.devflags
=0; /* per-device flags. Only two defined...*/
99 //#define WriteMostly1 1 /* mask for writemostly flag in above */
100 //#define FailFast1 2 /* Should avoid retries and fixups and just fail */
102 /* array state information - 64 bytes */
103 sb
.utime
=0; /* 40 bits second, 24 bits microseconds */
104 sb
.events
=0; /* incremented when superblock updated */
105 sb
.resync_offset
=0; /* data before this offset (from data_offset) known to be in sync */
106 sb
.max_dev
=1; /* size of devs[] array to consider */
107 //__u8 pad3[64-32]; /* set to 0 when writing */
109 /* device state information. Indexed by dev_number.
111 * Note there are no per-device state flags. State information is rolled
112 * into the 'roles' value. If a device is spare or faulty, then it doesn't
113 * have a meaningful role.
115 //__le16 dev_roles[]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
118 sb
.sb_csum
=calc_sb_1_csum(&sb
);
120 //printf("Superblock\n");
121 for(int i
=0;i
<(8*512);i
++) putc(0, stdout
);
122 fwrite(&sb
, sizeof(sb
), 1, stdout
);
123 for(int i
=0;i
<((120*512)-sizeof(sb
));i
++) putc(0, stdout
);
125 for(int i
=0;i
<(data_size
*512);i
++) putc(0, stdout
);
This page took 1.200911 seconds and 4 git commands to generate.