How To Create A RAID-1 Setup On A CentOS/RedHat 6.0 Server

1
945

 

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

)

sync
umount /mnt/raid
mount /dev/md1 /mnt/raid
cd / ; find . -depth -xdev | grep -v '^\./tmp/' | cpio -pmd /mnt/raid
sync
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
sync
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.

Originally posted 2016-02-28 00:25:01.

1 COMMENT

  1. Hi, thanks for the explanation , it was really useful for me. I’m trying to do the part of edit fstab and grub.conf (i am working on a centos 6.2) but the information that i found about it is a little bit confusing. Is in your plans to write something about that soon? it would be really nice, thanks again 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here