# mdadm renames devices

## [n00b@localhost]

I recently ran out of space on my /home partition so decided to repartition my PC to claim some space back from the system partitions.  I was using mdadm before and am continuing to use it.

I have 2 identical 640GB hard drives (/dev/sda and /dev/sdb) partitioned identically into 32MB, 3GB and 1GB primary partitions (/dev/sdX1, 2 and 3) and an extended partition for the rest of the drives (/dev/sdX4) containing a 30GB partition (/dev/sdX5) and a partition for the rest of the available space (about 600GB on /dev/sdX6).

I am using mdadm to mirror /dev/sda1 and /dev/sdb1 for my boot partition, to mirror /dev/sda3 and /dev/sdb3 for my root partition, to stripe /dev/sda5 and /dev/sdb5 for my system partition (making it 60GB, containing volumes managed by lvm) and to mirror /dev/sda6 and /dev/sdb6 for my home partition.  After reading the man pages about the deprecation of in-kernel auto-detection of raid arrays I decided to follow the raid kernel wiki, disabled in-kernel auto-detection and put a version 1.0 superblock on my boot partition (/dev/sda1 and /dev/sdb1) and version 1.2 superblocks on the rest and give them names corresponding to their function (boot, root, system and home).  Using the new superblocks means that I need to have an initramfs to assemble and mount the root partition (which isn't a big deal as I already have an initramfs for uvesafb and fbcondecor).  The rest of the arrays are assembled by the mdraid init script before localmount mounts them during the boot process.

The new version of the mdadm superblock stores the hostname of the system that created the array as well as a name for the array.  This is so that if a new drive added to the PC from another doesn't accidentally get added to an existing array and screw it up (and also if another drive gets added as /dev/sda then the array will still be created correctly as mdadm looks for the name rather than the device).  If an array doesn't contain a "homehost" entry that matches the current hostname then the hostname contained in the superblock is prepended to the name when creating the device.  This stuff is all contained in the man page for mdadm and from https://raid.wiki.kernel.org.

I create my root array in my initramfs using mdadm --assemble root /dev/sda3 /dev/sdb3 then mount it using mount /dev/md/root /root and switch_root into it.  However when the mdraid init script attempts to create the other arrays for boot, system and home it always prepends the hostname to them and then mount cannot find the devices (it is looking for /dev/md/home rather than /dev/md/<hostname>:home).  The command it uses for this is mdadm --assemble --scan in /lib64/rcscripts/addons/raid-start.sh.  When I run this command myself after the rest of the boot process is complete (or as much can be without a home partition mounted) it creates the arrays correctly (/dev/md/boot, /dev/md/system and /dev/md/home).

I did think that maybe the hostname wasn't set when mdraid was creating the devices as this would mean the current hostname wouldn't match that stored in the superblocks so I altered the initscript to make hostname run before it and turned on boot logging to verify the hostname was set first.  I also added a line to /lib64/rcscripts/addons/raid-start.sh to print out the hostname before assembling the arrays then, to be even more sure, changed a couple of lines in the source of mdadm to print out the hostname it gets from gethostname to assemble the devices.  It is getting the correct hostname which matches that stored in the superblock of the raid devices but still insists on prepending the name of the devices with the host name.  How do I get it not to?

----------

## trueserve

I also had this problem.

```
mdadm --examine --scan
```

Put the results in your /etc/mdadm.conf. I'm not sure if removing the "name=" portion is necessary as I believe it is just another way to look up, like with the UUID, but I removed it in mine. Change the /dev/md device to what you want it to be.

The initscript just runs 'mdadm -As' and without a defined /etc/mdadm.conf, seems to include the hostname saved in the RAID in the device name.

----------

## [n00b@localhost]

I don't have an /etc/mdadm.conf in my initramfs and don't want another config file (yet alone a copy in my initramfs) to keep up to date.  I also don't need one as despite what it says in the man page mdadm -As scans disks as well as the config file to find arrays to assemble.  Try it for yourself - delete mdadm.conf and run mdadm -As.  Any array that has been stopped and removed will be found, assembled and started.  Or boot into an initramfs shell and run it by hand without an mdadm.conf.

The problem is that mdadm is prepending the hostname to the name of the arrays because when it is run at boot (by openrc after the initramfs has created the root array and mounted it) the hostname hasn't been set yet (or it is picking up the wrong hostname) and so won't match that stored in the superblock of the arrays.

The lvm2 script also has no dependency on mdraid but should.  I have a volume group on a raid array and lvm2 gets started before mdraid causing the physical volumes not to be found.

The order should be:

hostname (to set the hostname before the raid arrays are assembled to make sure they are assembled with the right name),

mdraid (to assemble and start the raid arrays before scanning for lvm2 volumes),

lvm2 (to scan for volume groups that may or may not be on a raid array),

localmount, etc.

Currently the first three services above are started in any order whether doing a parallel or serial startup.

As a workaround in my initramfs I am using the root=LABEL=root parameter to find the root filesystem by label.  This means the array can be assembled with whatever name it wants although I do have to remember to label my filesystems.

----------

## cach0rr0

 *[n00b@localhost] wrote:*   

> 
> 
> The lvm2 script also has no dependency on mdraid but should.

 

so what happens to lvm users who don't use mdraid? 

i dont know if there's a one-size-fits-all solution here. I don't use mdraid, so i cant check myself, does the init script have a line that says "before lvm2" ?

----------

## [n00b@localhost]

 *cach0rr0 wrote:*   

>   *[n00b@localhost] wrote:*   The lvm2 script also has no dependency on mdraid but should. 
> 
> so what happens to lvm users who don't use mdraid? 
> 
> i dont know if there's a one-size-fits-all solution here. I don't use mdraid, so i cant check myself, does the init script have a line that says "before lvm2" ?

 

I believe there is a kind of dependency for initscripts ("use") that means "start the service if it is installed and would run in the current runlevel, but if not don't bother".  That is the kind I was meaning.

My /etc/init.d/mdraid contains "depend lvm" among others but I think I've butchered it trying to fix this.

I think there can be a one-size-fits-all solution here:

make mdraid depend on hostname so that the hostname gets set before the raid arrays are assembled since mdadm checks the hostname in the superblock against the current hostname when deciding the name for the device;

make lvm2 "use" mdraid so that if mdadm is installed it is started before lvm2 so that any lvm2 volumes on raid arrays can be detected.

I'm not sure exactly how to implement this myself as I have tried playing about with the depend block in the init scripts but it doesn't seem to make a difference (to the order or the outcome).

----------

## trueserve

My solution wouldn't fix the root array, but would fix the others, or at least be a workaround for the hostname not being set. I'm not booting from mdraid root, only mounting fixed storage volumes on boot, so this was enough to solve the problem for me.

----------

