published on in Tech Tips & Tricks

Change UUID of a mdadm partition

Today I got screwed, because the raid did not boot up, so I had a look and blkid showed me several partitions with the same UUID. UUID, shouldn’t that be unique? Well I figured I screwed something up using dd while setting up the raid, so I got the (bad, I repeat: bad) idea of changing the partitions’ uuids, which seemed impossible (for good reason!). It turns out, these UUIDs are stored in the md_raid superblock at the end of the partition. All partitions of a raid need to have that superblock and thus need to have the same uuid, otherwise mdadm won’t assemble the devices.

Anyways, in case someone really wants to change these UUIDs for whatever reason, I searched the information from e2fsprogs (details here) and here you go:

1. Locate the superblock in the partition

It is at its end and the offset is calculated as follows:

offset = partition_size & ~65535 - 65536

I’ve not found a smart method to get the exact partition size, so I use a fdisk estimate and bruteforce from there for the md_raid header:

dd if=$device bs=1 skip=$estimated_start | hexdump -C | grep N+

It should be located at a 16byte boundary and the line will contain a bunch of zeros. Don’t worry, if you chose the wrong one, you won’t be able to find the old UUID values in step 3.

2. Copy the block

dd if=$device bs=1 skip=$header_offset count=512 of=superblock</blockquote>

3. Edit the block

Change the UUID (use blkid to find the old UUIDs value): hexedit superblock

4. Write the block back:

dd if=superblock of=$device bs=1 seek=$header_offset count=512

Oh btw be warned, that mdadm fails to assemble raids if the UUID doesn’t fit (even if you adjust all partitions), there’s probably a superblock checksum involved.