Caution: We are not responsible for data loss or any damage that might occur from following these instructions.
Summary
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)
Directions
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.
- Run blkid, lsblk, 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.
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
fdisk /dev/sdb
or gdisk /dev/sdb
- Change the partition type for the partitions of interest to "Linux RAID" (fd00).
mdadm --zero-superblock /dev/sdb1; mdadm --zero-superblock /dev/sdb2; mdadm --zero-superblock /dev/sdb5
- An "Unrecognized md component device" message is not an error.
mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb1
mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb2
mdadm --create /dev/md2 --level=1 --raid-disks=2 missing /dev/sdb5
mkfs.xfs /dev/md0; mkfs.xfs /dev/md1; mkfs.xfs /dev/md2
mkdir /mnt/md0 /mnt/md1 /mnt/md2
mount /dev/md0 /mnt/md0; cd /boot; find . -depth | cpio -pmd /mnt/md0
mount /dev/md1 /mnt/md1; cd / ; find . -depth -xdev | grep -v '^\./tmp/' | cpio -pmd /mnt/md1
mount /dev/md2 /mnt/md2; cd /home; find . -depth | cpio -pmd /mnt/md2
umount /mnt/md0 /mnt/md1 /mnt/md2
rmdir /mnt/md0 /mnt/md1 /mnt/md2
mount /dev/md1 /mnt
mount /dev/md0 /mnt/boot
mount /dev/md2 /mnt/home
mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys
mount --bind /run /mnt/run
touch /mnt/.autorelabel
blkid |grep /dev/md
- Note the UUIDs. You need them in Step 26.
ls -l /dev/disk/by-id/md-uuid*
- Note the value for the root filesystem (md1). We will need it when configuring boot parameters.
chroot /mnt
vim /etc/fstab
- Replace the current UUIDs with the ones gathered in Step 23.
mdadm --detail --scan >> /etc/mdadm.conf
mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.old
dracut --mdadmconf --force /boot/initramfs-$(uname -r).img $(uname -r)
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"
grub2-mkconfig -o /boot/grub2/grub.cfg
vim /boot/grub2/grub.cfg
- If necessary, set the value of "root=" to the mduuid/... collected in Step 24.
grub2-install /dev/sdb
- For EFI/UEFI boots, first install:
yum install grub2-efi-modules
- 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
fdisk /dev/sda
- Change the partition type for the partitions of interest to "Linux RAID"
mdadm --manage /dev/md0 --add /dev/sda1
mdadm --manage /dev/md1 --add /dev/sda2
mdadm --manage /dev/md2 --add /dev/sda5
watch -n1 "cat /proc/mdstat"
grub2-install /dev/sda
Monitoring
echo "MAILADDR root" >> /etc/mdadm.conf
vim /etc/sysconfig/raid-check
Notes
Thanks to the author of this article that provided a useful reference.