Convert Linux standard partitions to software RAID

Tags linux how-to
 Caution

We are not responsible for data loss or any damage that might occur from following these instructions

Objective

You want to mirror your drive to create a RAID-1 configuration, using Linux software RAID, without loss of data.

Environment

  • Red Hat Enterprise Linux 7
  • CentOS 7
  • Terminal/Command Line Interface (CLI)

Procedure

This article assumes you have a single hard drive with standard Linux partitions (such as sda1, sda2, etc.) and a second blank drive of the same size.

We will assume our original drive is sda.

  1. Run blkid and fdisk to confirm the identification of the drive.
    • In this article we will assume the original drive is sda, and we will assume we want to mirror sda1 (/boot), sda2 (/), and sda5 (/home).
    • Adapt it to your own situation.
  2. sfdisk -d /dev/sda | sfdisk /dev/sdb
    • sfdisk does not work with GPT partitions on RHEL/CentOS 6. However, you can accomplish the same thing with sgdisk from the gptfdisk package, which can be found online.
      • wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/kalyaka/CentOS_CentOS-6/x86_64/gptfdisk-0.8.6-13.4.x86_64.rpm
      • yum localinstall gptfdisk-0.8.6-13.4.x86_64.rpm
      • sgdisk --backup=table /dev/sda
      • sgdisk --load-backup=table /dev/sdb
      • sgdisk -G /dev/sdb
  3. fdisk /dev/sdb or gdisk /dev/sdb
    • Change the partition type for the partitions of interest to "Linux RAID" (fd00).
  4. mdadm --zero-superblock /dev/sdb1; mdadm --zero-superblock /dev/sdb2; mdadm --zero-superblock /dev/sdb5
  5. mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
  6. mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
  7. mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sdb5
  8. mkfs.xfs /dev/md0; mkfs.xfs /dev/md1; mkfs.xfs /dev/md2
  9. mkdir /mnt/md0 /mnt/md1 /mnt/md2
  10. mount /dev/md0 /mnt/md0; cd /boot; find . -depth | cpio -pmd /mnt/md0
  11. mount /dev/md1 /mnt/md1; cd / ; find . -depth -xdev | grep -v '^\./tmp/' | cpio -pmd /mnt/md1
  12. mount /dev/md2 /mnt/md2; cd /home; find . -depth | cpio -pmd /mnt/md2
  13. umount /mnt/md0 /mnt/md1 /mnt/md2
  14. rmdir /mnt/md0 /mnt/md1 /mnt/md2
  15. mount /dev/md1 /mnt
  16. mount /dev/md0 /mnt/boot
  17. mount /dev/md2 /mnt/home
  18. mount --bind /proc /mnt/proc
  19. mount --bind /dev /mnt/dev
  20. mount --bind /sys /mnt/sys
  21. mount --bind /run /mnt/run
  22. touch /mnt/.autorelabel
  23. blkid |grep /dev/md
    • Note the UUIDs. You need them in Step 26.
  24. ls -l /dev/disk/by-id/md-uuid*
    • Note the value for the root filesystem (md1). We will need it when configuring boot parameters.
  25. chroot /mnt
  26. vim /etc/fstab
    • Replace the current UUIDs with the ones gathered in Step 23.
  27. mdadm --detail --scan >> /etc/mdadm.conf
  28. mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.old
  29. dracut --mdadmconf --force /boot/initramfs-$(uname -r).img $(uname -r)
  30. vi /etc/default/grub
    #GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
    GRUB_CMDLINE_LINUX="rd.auto rd.auto=1 rhgb quiet"
    GRUB_PRELOAD_MODULES="mdraidlx"
  31. grub2-mkconfig -o /boot/grub2/grub.cfg
  32. vim /boot/grub2/grub.cfg
    • If necessary, set the value of "root=" to the mduuid/... collected in Step 24.
  33. grub2-install /dev/sdb
    • For EFI/UEFI boots, first install: yum install grub2-efi-modules
  34. Reboot to sdb.

If this reboot is successful and everything still works, you are ready to add sda to your RAID. If there are any problems, you can still boot to sda, which is untouched, and fix any problems.

Add sda to the RAID

  1. fdisk /dev/sda
    • Change the partition type for the partitions of interest to "Linux RAID"
  2. mdadm --manage /dev/md0 --add /dev/sda1
  3. mdadm --manage /dev/md1 --add /dev/sda2
  4. mdadm --manage /dev/md2 --add /dev/sda5
  5. watch -n1 "cat /proc/mdstat"
  6. grub2-install /dev/sda

Monitoring

  1. echo "MAILADDR root" >> /etc/mdadm.conf
  2. vim /etc/sysconfig/raid-check

Additional notes

  • Thanks to the author of this article that provided a useful reference.

Details

Article ID: 1748
Created
Wed 5/27/20 10:42 AM
Modified
Tue 6/23/20 11:33 AM