genimage
genimage copied to clipboard
Create partition with MDRAID (mdadm) array metadata
I have buildscript which creates harddisk image with partitions and some of them have MDRAID metadata on them. I would like to migrate it to genimage, but there is no such feature.
Side note: All i need is very simple setup. RAID1 with single(!) drive in it. It seems stupid to have RAID1 with only single drive. But it is much easier to grow such array later to desired state, when compared to converting plain non-RAID filesystem partition to RAID. I am usualy using this on routers which get deployed from the image in bulk. user can later easily add second SSD to the most important machines and grow the RAID across it once the deployment becomes mission-critical.
I've already tried to figure out how mdadm creates the metadata, but it seems it leaves metadata creation to kernel, which is bit annoying, because there is probably no userspace utility which can be called to do that offline without starting the array.
But there is struct mdp_superblock_s
in header file #include <md_p.h>
, so that can be included and populated from userspace. There seems to be mild complexity to it, but it should be possible under 200 lines of C code.
https://docs.huihoo.com/doxygen/linux/kernel/3.7/md__p_8h_source.html https://docs.huihoo.com/doxygen/linux/kernel/3.7/structmdp__superblock__s.html
Hmm, this is not exactly trivial and I have no idea about raid
this is not exactly trivial
Not trivial, but not hard either.
Basicaly we just need to populate previously mentioned metadata structs with correct values and then write that at 4K offset from the beginning of the partition:
https://raid.wiki.kernel.org/index.php/RAID_superblock_formats
Only problem is that the struct is bit complex, but still reasonable. Another approach might be that i wil prepare external tool to create raid partitions from userspace and then we just call it from genimage.
OK, so i've made bit of progress. I've put together following code that is able to generate "valid" mdraid RAID1 superblock, that is recognized by mdadm.
https://github.com/Harvie/Programs/blob/master/c/mdraid-gen/mdraid.c
It seems that now we just need to clean up few small things, set the data offsets correctly and append data to generated superblock with these offsets.
This is the current output:
$ mdadm --examine test.img
test.img:
Magic : a92b4efc
Version : 1.2
Feature Map : 0x0
Array UUID : f0a13849:e1e5172d:dc424607:ffb79b1a
Name : localh
Creation Time : Thu Jan 1 01:00:00 1970
Raid Level : raid1
Raid Devices : 1
Avail Dev Size : 0 sectors
Array Size : 0 KiB
Data Offset : 4352 sectors
Super Offset : 8 sectors
Unused Space : before=4272 sectors, after=18446744073709547352 sectors
State : active
Device UUID : d96f1f41:4466c20b:e636544f:3fb25e78
Update Time : Thu Jan 1 01:00:00 1970
Checksum : b75679d2 - correct
Events : 0
Device Role : Active device 0
Array State : A ('A' == active, '.' == missing, 'R' == replacing)