How To Create A RAID1 Setup On An Existing CentOS/RedHat 6.0 System

Want to support HowtoForge? Become a subscriber!
Submitted by mauriceh (Contact Author) (Forums) on Fri, 2011-10-21 15:09. :: CentOS | Storage

How To Create A RAID1 Setup On An Existing CentOS/RedHat 6.0 System

By: Maurice Hilarius - Hard Data Ltd. - October 12, 2011

This tutorial is for turning a single disk CentOS 6 system into a two disk RAID1 system. The GRUB bootloader will be configured in such a way that the system will still be able to boot if one of the hard drives fails (no matter which one).

NOTE: Everything has to be done as root:

su -
enter root password

In this example the initial layout for the hard disks was:

Disk with installed OS. "Original"

Device Mountpoint Size

/dev/sdb ~1002GB
/dev/sdb1 /boot 256MB
/dev/sdb2 / 24GB
/dev/sdb3 swap 4GB
/dev/sdb5 /var 4GB
/dev/sdb6 /home ~900GB

And we will be adding the other hard disk: /dev/sda (~1002GB). "Target disk".

1. Back everything up! You might want to get your data back after you crashed the conversion. Trust me on this!

2. Verify Backup! See above.

3. Create partitions on /dev/sda identical to the partitions on /dev/sdb:

sfdisk -d /dev/sdb | sfdisk /dev/sda

4. We load a few kernel modules (to avoid a reboot):

modprobe linear
modprobe raid0
modprobe raid1

5. Now run:

cat /proc/mdstat

The output should look as follows:

root@server:~# cat /proc/mdstat
Personalities : [linear] [multipath] [raid0] [raid1]
unused devices: <none>

Here we see now that the RAID kernel modules are working, but there are no RAID sets yet.

6. Run the following commands:

mdadm --create /dev/md0 --level=1 --raid-disks=2 /dev/sda1 missing
mdadm --create /dev/md1 --level=1 --raid-disks=2 /dev/sda2 missing
mdadm --create /dev/md2 --level=1 --raid-disks=2 /dev/sda5 missing
mdadm --create /dev/md3 --level=1 --raid-disks=2 /dev/sda6 missing

This generates the raid devices 0 to 3 in a degenerated state because the second drive is missing.

7. If you want to use Grub 0.97 (default in CentOS 5 or 6)) on RAID 1, you need to specify an older version of metadata than the default. Add the option "--metadata=0.90" to the above command. Otherwise Grub will respond with "Filesystem type unknown, partition type 0xfd" and refuse to install. This is supposedly not necessary with Grub 2.

Like this:

mdadm --create /dev/md0 --metadata=0.90 --level=1 --raid-devices=2 /dev/sda1 missing

8. Check the output of

cat /proc/mdstat

#cat /proc/mdstat
Personalities : [raid1]
md1 : active raid1 sdb2[1]
473792 blocks [2/2] [U_]

md2 : active raid1 sdb5[1]
4980032 blocks [2/2] [U_]

md3 : active raid1 sdb6[1]
3349440 blocks [2/2] [U_]

md0 : active raid1 sdb1[1]
80192 blocks [2/2] [U_]

unused devices: <none>

9. Create a mdadm.conf from your current configuration:

mdadm --detail --scan > /etc/mdadm.conf

10. Display the contents of the file:

cat /etc/mdadm.conf

At the bottom of the file you should now see details about our (degraded) RAID arrays.

11. We use dracut to rebuild the initramfs with the new mdadm.conf:

mv /boot/initramfs-$(uname -r).img /boot/initramfs-$(uname -r).img.old

dracut --mdadmconf --force /boot/initramfs-$(uname -r).img $(uname -r)

12. Create the filesystems on these new software raid devices:

mkfs.ext2 /dev/md0 # For /boot ext2 is good
mkfs.ext4 /dev/md1 # For / ext4 is good
mkfs.ext4 /dev/md2 # For /home ext4 is good
mkfs.ext4 /dev/md3 # For /var ext4 is good
mkswap -c /dev/sda2 #We want swap partitions on both drives for performance

13. Copy the data from the existing (and still running) partitions to the newly created raid partitions:

mkdir /mnt/raid
mount /dev/md0 /mnt/raid
cd /boot; find . -depth | cpio -pmd /mnt/raid

(If SELinux is in use also do this:

touch /mnt/raid/.autorelabel


umount /mnt/raid

mount /dev/md1 /mnt/raid
cd / ; find . -depth -xdev | grep -v '^\./tmp/' | cpio -pmd /mnt/raid
umount /mnt/raid

NOTES: You really do not want to copy files in /tmp and /var/tmp.
This command will create empty mount points like 'proc' or 'dev' and will not forget things like /.autofsck.

mount /dev/md2 /mnt/raid
cd /var; find . -depth | cpio -pmd /mnt/raid sync umount /mnt/raid

mount /dev/md3 /mnt/raid
cd /home; find . -depth | cpio -pmd /mnt/raid
umount /mnt/raid

At this point we have our raid system created and the existing data was mirrored manually onto the new devices.
To make sure that the system will boot from the raid devices we have to change some entries in /etc/fstab and /boot/grub/menu.lst.

Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by dipps (registered user) on Fri, 2012-10-19 07:37.

Thanks for this HOWTO! I've used it on two centos 6.3 systems. I had trouble until I worked out that the command in step 9,

# mdadm --detail --scan > /etc/mdadm.conf

put wrong UUIDs into mdadm.conf, and those went into the initramfs: so the initramfs wouldn't find its root volume, and couldn't boot. The actual UUIDs come from # blkid | grep md

I edited in the UUIDs I got from running blkid, repeated things after step 9 and it worked. I found it's best to always use the UUID in grub.conf, fstab, etc.

Another topic: step 17 talks about setting the partition type to 0xfd for the second drive. That also needs to be done for the first drive (/dev/sda in these instructions) sometime before the reboot in step 16.

Submitted by Carlos Vela (not registered) on Fri, 2012-08-31 14:20.

Congratulations for your work and thanks for sharing.

 In point 6 you create raid disk with /dev/sda disk (original), but in point 8 mdadm shows /dev/sdb as devices used for raid disk.

 Is there anything wrong or am I missing anything?

Thanks again.