Pridany badblocky
[mirrors/Programs.git] / c / mdraid-gen / mdraid.c
CommitLineData
96422adb
TM
1/* MDRAID Superblock generator
2This should create valid mdraid superblock for raid1 with 1 device.
3It is still work in progress, but following seems to be recognized:
4
5make mdraid
6./mdraid > test.img
7mdadm --examine test.img
8
30d358cd
TM
9losetup /dev/loop1 test.img
10mdadm --assemble md /dev/loop1
11
96422adb
TM
12*/
13
14//#include <cstddef>
15#include <string.h>
16#include <time.h>
17#include <stdlib.h>
18#include <stdio.h>
19#include <linux/raid/md_p.h>
20
21void random_uuid(__u8 *buf)
22{
23 __u32 r[4];
24 for (int i = 0; i < 4; i++)
25 r[i] = random();
26 memcpy(buf, r, 16);
27}
28
29static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
30{
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;
35
36/* make sure I can count... (needs include cstddef) */
37/*
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");
42 }
43*/
44
45 disk_csum = sb->sb_csum;
46 sb->sb_csum = 0;
47 newcsum = 0;
48 for (; size>=4; size -= 4 ) {
49 newcsum += __le32_to_cpu(*isuper);
50 isuper++;
51 }
52
53 if (size == 2)
54 newcsum += __le16_to_cpu(*(unsigned short*) isuper);
55
56 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
57 sb->sb_csum = disk_csum;
58 return __cpu_to_le32(csum);
59}
60
61int main() {
f636a245
TM
62 //printf("Superblock\n");
63
e7456ece 64 size_t data_size = 8192; //512B sectors (should be divisible by 8 sectors to keep 4kB alignment)
30d358cd 65
96422adb
TM
66 srand(time(NULL)); //FIXME: Seed UUID properly
67
68 struct mdp_superblock_1 sb = {0};
69
70 /* constant array information - 128 bytes */
71 sb.magic = 0xa92b4efc; /* MD_SB_MAGIC: 0xa92b4efc - little endian */
72 sb.major_version = 1; /* 1 */
f636a245 73 sb.feature_map = 0; //MD_FEATURE_BITMAP_OFFSET; /* bit 0 set if 'bitmap_offset' is meaningful */ //FIXME: internal bitmap bit is not seen by mdadm????
96422adb
TM
74 sb.pad0 = 0; /* always set to 0 when writing */
75
76 //TODO: set these
77 random_uuid(sb.set_uuid); /* user-space generated. U8[16]*/
30d358cd 78 memcpy(sb.set_name, "localhost:7", 12); /* set and interpreted by user-space. CHAR[32] */
96422adb
TM
79 sb.ctime=0; /* lo 40 bits are seconds, top 24 are microseconds or 0*/
80
81 sb.level=1; /* -4 (multipath), -1 (linear), 0,1,4,5 */
30d358cd 82 //sb.layout=2; /* only for raid5 and raid10 currently */
e7456ece 83 sb.size=data_size; /* used size of component devices, in 512byte sectors */
96422adb
TM
84
85 sb.chunksize=0; /* in 512byte sectors - not used in raid 1 */
86 sb.raid_disks=1;
30d358cd 87 sb.bitmap_offset=8; /* sectors after start of superblock that bitmap starts
96422adb
TM
88 * NOTE: signed, so bitmap can be before superblock
89 * only meaningful of feature_map[0] is set.
90 */
91
92 /* constant this-device information - 64 bytes */
e7456ece 93 sb.data_offset=2048; /* sector start of data, often 0 */
30d358cd 94 sb.data_size=data_size; /* sectors in this device that can be used for data */
96422adb
TM
95 sb.super_offset=8; /* sector start of this superblock */
96
97 sb.dev_number=0; /* permanent identifier of this device - not role in raid */
98 sb.cnt_corrected_read=0; /* number of read errors that were corrected by re-writing */
99 random_uuid(sb.device_uuid); /* user-space setable, ignored by kernel U8[16] */
100 sb.devflags=0; /* per-device flags. Only two defined...*/
101 //#define WriteMostly1 1 /* mask for writemostly flag in above */
102 //#define FailFast1 2 /* Should avoid retries and fixups and just fail */
103
f636a245
TM
104 /* Bad block log. If there are any bad blocks the feature flag is set.
105 * If offset and size are non-zero, that space is reserved and available
106 */
107 sb.bblog_shift=9; /* shift from sectors to block size */ //FIXME: not sure with this!
108 sb.bblog_size=8; /* number of sectors reserved for list */
109 sb.bblog_offset=16; /* sector offset from superblock to bblog,
110 * signed - not unsigned */
111
96422adb
TM
112 /* array state information - 64 bytes */
113 sb.utime=0; /* 40 bits second, 24 bits microseconds */
114 sb.events=0; /* incremented when superblock updated */
115 sb.resync_offset=0; /* data before this offset (from data_offset) known to be in sync */
e7456ece 116 sb.max_dev=sb.raid_disks; /* size of devs[] array to consider */
96422adb
TM
117 //__u8 pad3[64-32]; /* set to 0 when writing */
118
119 /* device state information. Indexed by dev_number.
120 * 2 bytes per device
121 * Note there are no per-device state flags. State information is rolled
122 * into the 'roles' value. If a device is spare or faulty, then it doesn't
123 * have a meaningful role.
124 */
125 //__le16 dev_roles[]; /* role in array, or 0xffff for a spare, or 0xfffe for faulty */
126
127
f636a245 128 //Calculate checksum
96422adb
TM
129 sb.sb_csum=calc_sb_1_csum(&sb);
130
e7456ece
TM
131 //Empty space before metadata (sector 0 - 7)
132 for(int i=0;i<(sb.super_offset*512);i++) putc(0, stdout);
133
134 //Superblock and padding (sector 8 - 2048)
96422adb 135 fwrite(&sb, sizeof(sb), 1, stdout);
e7456ece 136 for(int i=0;i<(((sb.data_offset-sb.super_offset)*512)-sizeof(sb));i++) putc(0, stdout);
30d358cd 137
e7456ece 138 //Data (N sectors)
30d358cd 139 for(int i=0;i<(data_size*512);i++) putc(0, stdout);
96422adb 140}
This page took 0.281086 seconds and 4 git commands to generate.