Converting Fedora Linux install to MDRaid

This has been taken from various blogs around the net.  The example written below is taken from my system.  My initial disk is /dev/sda and has 2 partitions: /dev/sda1 formatted ext4 and mounted as /boot which contains the kernels, initramfs images and grub; /dev/sda2 which is the rest of the disk and contains LVM.  The LVM volume group is called fedora and has 3 logical volumes – root, swap, and home (these show as /dev/fedora/home etc.).

Make sure you have a good backup first!

1. Install new disk into computer (same size or larger than the existing) and use fdisk -l to verify it is being seen.  Save the contents of the fdisk -l output for future reference.

2. Partition the second drive identically to the first – an example using /dev/sdb as the new drive is:
fdisk /dev/sdb
n to create new partition
p for primary partition
it should automatically default to 1 as the partition number
start at the first cylinder shown from the saved output of the fdisk -l command
type in the last cylinder from the saved output
t to set the partition type
it should auto select partition 1
fd to set the partition type to Linux auto-raid

repeat the steps for any additional partitions (on most setups there will only be a second partition)

w! to write changes and exit fdisk

If you receive a message about the kernel using the old tables, run modprobe to force a re-read

Re-run fdisk -l to verify that the sectors start and end at the same place

3. Zero out the superblocks on the new partitions
mdadm –zero-superblock /dev/sdb1
mdadm –zero-superblock /dev/sdb2

4. Create the new RAID devices, the “missing” word is used to indicate there will be another device in the set but it is not yet available
mdadm –create /dev/md0 –verbose –level=1 –raid-devices=2 missing /dev/sdb1
mdadm –create /dev/md1 –verbose –level=1 –raid-devices=2 missing /dev/sdb2

5. Build the /etc/mdadm.conf file (if one already exists then you may need to review it first).
echo “DEVICE partitions” >> /etc/mdadm.conf
echo “MAILADDR root” >> /etc/mdadm.conf
mdadm –examine –scan >> /etc/mdadm.conf

6. Format the first partition with the same filesystem as the existing one:
mkfs.ext4 /dev/md0

7. Copy the existing data from the first partition to the second:
mkdir /mnt/boot
mount /dev/md0 /mnt/boot
cp -dpRx /boot/* /mnt/boot/
umount /mnt/boot

8. Replace the existing boot mount with the new one
umount /boot
mount /dev/md0 /boot

9. Rebuild the initramfs image
dracut –force

10. Edit the /etc/default/grub file to include the directive rd.auto to the GRUB_CMDLINE_LINUX line, e.g.
GRUB_CMDLINE_LINUX=”rd.lvm.lv=fedora/swap rd.lvm.lv=fedora/root rd.auto”
(this caused me all sorts of trouble until I found out that newer Dracut versions do not automatically assemble device mapper items unless told to explicitly by adding the rd.auto entry, if you’re nervous about adding this because of extra entries in your mdadm.conf file, then it is possible to just add the dm items you want at boot by specifying the rd.md.uuid option (Google for more info))

11. Re-install grub (to both disks since if the first one fails, you still need grub on the second to boot…)
grub-install –recheck /dev/sdb
grub-install –recheck /dev/sda

12. Change the /etc/fstab file to set the /dev/md0 as the /boot mount (the existing mount may specify LABEL=boot instead of a device, it should look like this (of course replace the filesystem type with the appropriate one if not using ext4):
/dev/md0 /boot ext4 defaults 1 2

13. Cross your fingers and reboot.  If after rebooting, you are dropped into a Dracut emergency shell then you can try to run

mdadm –assemble –scan
lvm vgscan
lvm vgchange -ay
ln -s /dev/mapper/fedora-root /dev/root
exit (at this point the boot should continue)

(replace /dev/mapper/fedora-root with the correct volume group name and logical volume for your install, you can usually find this by the output of the lvm vgchange command)

14. After reboot, use mount to verify that the /dev/md0 device is mounted at /boot. 

15. Change the first partition to Linux auto raid
fdisk /dev/sda
t to change partition type
1 to select /dev/sda1
fd for Linux autoraid
w! to write and quit

16. Remove the old label from the sda1 partition and add it to the raid set
e2label /dev/sda1 “”
mdadm –add /dev/md0 /dev/sda1

You can then monitor the progress of the add by doing watch cat /proc/mdstat

Done with partition 1, now onto partition 2 and the LVM groups.

17. Create /dev/md1 as a physical volume using pvcreate -v /dev/md1

18. Use the command vgs to show the volume group and then use vgextend to extend the lvm group onto /dev/md1 like this:
vgextend -v VolGroup /dev/md1  (where VolGroup came from the output of vgs, on my system it was fedora so I ran vgextend -v fedora /dev/md1)

19. Use lvs to get the volume names and the use pvmove to move the extents to the md1 device: pvmove -v /dev/sda2 /dev/md1 , this should output the status every 15 seconds and could take quite a while depending on your volume size.

20. Run vgreduce to remove the sda2 partition from the phycial lvm volume: vgreduce fedora /dev/sda2 (replace fedora with the name of your volume group)

21. Run pvremove to remove the physical volume attributes from /dev/sda2: pvremove /dev/sda2

At this point your /boot volume should be mirrored across both drives and the rest of your volumes should exist only on the second disk.

22. Change the second partition to Linux auto raid
fdisk /dev/sda
t to change partition type
2 to select /dev/sda2
fd for Linux autoraid
w! to write and quit

23. Remove the old label from the sda2 partition and add it to the raid set
e2label /dev/sda2 “”
mdadm –add /dev/md1 /dev/sda2

You can then monitor the progress of the add by doing watch cat /proc/mdstat (use CTRL+C to exit when done)

24. Once this has completed, re-run the dracut –force command again.  Visually verify that the /boot/grub2/grub.cfg has the rd.auto line appended to the kernel line and you can then reboot to verify everything is working. 

At this point your disks should be mirrored. Yay!

SBS 2011

SBS 2011

Considering an upgrade of your Small Business Server?  Contact Us and let us help you with your migration…. Some things we have […]