Linux Software Raid 1 Setup


From Linuxconfig.org

Jump to: navigation, search

Contents

Image:raid.png

Introduction

This article describes step by step setup of Linux software RAID 1 on Linux Platform. Although this software RAID 1 configuration has been accomplished on Debian ( Ubuntu ) it also can guide you if you are running some other Linux distributions such as RedHat, Fedora , Suse, PCLinux0S, Sabayon etc. RAID-1 can be setup on two or more disks. This mode of RAID creates exact mirror of all data on one disk to the other disk.

Any comments are highly appreciated: lubos(at)linuxconfig(dot)org

System Info

OS: Debian Etch ( basic installation on /dev/sda)

Kernel: Linux raid 2.6.18-5-686 #1 SMP Fri Jun 1 00:47:00 UTC 2007 i686 GNU/Linux

Hard Drives: /dev/sda -> 4GB , /dev/sdb -> 5GB

The Plan

We start with running linux system Debian Etch and the following partition scheme and hard drives ( sda , sdb ):

Image:raid1_1.gif

Where our plan is show on the figure at the beginning of this document.

Software Installation

There is only 1 package ( + prerequisites ) needed by software raid 1 on debian. This package is mdadm. Simply use apt-get tool to install mdadm package it into your system. You may need to answer couple question.

apt-get install mdadm

Configure Kernel Modules

Load modules at boot time

No we need to make sure that raid kernel modules are loaded at the boot time. To accomplish this task we need to edit /etc/modules files and add couple lines. Open up you favorite text editor ( Here is a tutorial where you can learn how to use vi / vim text editor. ) or just simply append lines by echo command.

echo raid1 >> /etc/modules
echo md >> /etc/modules

Image:raid1_2.gif

Load modules to the Kernel

At this stage if we want to use raid modules we have two options. First one is to reboot our system and the other option is to use modprobe ( or insmode ) to load modules to the Kernel. I find second option easier:

modprobe raid1

Image:raid1_3.gif

NOTE: if you do not have md module already loaded as shown on the figure above use modprobe to load it:

modprobe md

There are two ways to confirm that our raid modules are loaded into kernel:

lsmod | grep raid1
cat /proc/mdstat

You should have similar output as shown on the figure below:

Image:raid1_4.gif

Prepare Second Hard Drive ( sdb ) for RAID

At this stage we need to prepare our second hard drive sdb to act as RAID 1 arrays. First we need to copy partition table from sda => sdb1. For this task we need to use sfdisk command. This step can also be done manually if you prefer. Make sure that you do NOT have any data on disk sdb because this sfdisk command will erase them permanently !!! This command will copy partition table from /dev/sda to /dev/sdb.

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

Image:raid1_5.gif

Second Hard Drive sdb is almost ready. All we need to do is to change partition ID's for partitions: sdb1, sdb5, sdb6, sdb8 and sdb9 to fd (Linux raid autodetect). Also in this taks sfdisk command comes very handy:

NOTE: We are not interested in mirroring swap sdb7 partition which is swap partition.

for partition in 1 5 6 8 9; do sfdisk --change-id /dev/sdb $partition fd; done

Image:raid1_6.gif

Setting Up RAID 1 Arrays

Create RAID 1 Arrays

Its time to create RAID 1 arrays. We first create RAID 1 array only from /dev/sdb. Once this is ready we include the first hard drive /dev/sda to this array as well.

for partition in 1 5 6 8 9; do mdadm --create /dev/md$partition --level=1 \
--raid-disks=2 missing /dev/sdb$partition; done

Image:raid1_7.gif

Create a filesystem on RAID 1 Arrays

Now we can create file system on our new RAID 1 arrays:

for partition in 1 5 6 8 9; do mkfs.ext3 /dev/md$partition; done

Edit mdadm.conf file

Please make a copy of your /etc/mdadm/mdadm.conf file as we will use later. Lets edit mdadm.conf file. Make sure that you use append ">>" to avoid accidental overwrite of mdadm.conf file.

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

Image:raid1_8.gif

Edit /etc/fstab

As we want to make our system to mount new RAID 1 arrays after reboot we need to edit /etc/fstab file.

OLD /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/sda1       /               ext3    defaults,errors=remount-ro 0       1
/dev/sda9       /home           ext3    defaults        0       2
/dev/sda8       /tmp            ext3    defaults        0       2
/dev/sda5       /usr            ext3    defaults        0       2
/dev/sda6       /var            ext3    defaults        0       2
/dev/sda7       none            swap    sw              0       0
/dev/hdc        /media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto  0       0

NEW /etc/fstab

# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/md1       /               ext3    defaults,errors=remount-ro 0       1
/dev/md9       /home           ext3    defaults        0       2
/dev/md8       /tmp            ext3    defaults        0       2
/dev/md5       /usr            ext3    defaults        0       2
/dev/md6       /var            ext3    defaults        0       2
/dev/sda7       none            swap    sw              0       0
/dev/hdc        /media/cdrom0   udf,iso9660 user,noauto     0       0
/dev/fd0        /media/floppy0  auto    rw,user,noauto  0       0

Configure GRUB boot manager

First we make a copy of /boot/grub/menu.lst.

cp /boot/grub/menu.lst /boot/grub/menu.lst_orig

lets engage sed command with this job:

sed 's/sda1/md1/' < /boot/grub/menu.lst_orig > /boot/grub/menu.lst

Image:raid1_9.gif

Update GRUB:

update-grub

Image:raid1_10.gif

Copy data from sda => sdb

reboot in single mode:

init 1

Copy data from each partition to new RAID 1 arrays. For this operation we use rsync command which will clone both partitions. If you do not have rsync installed on your system issue command:

apt-get install rsync

"/" copy root partition:

mount /dev/md1 /media; rsync -aqxP / /media; umount /media

copy /usr/ partition:

mount /dev/md5 /media; rsync -aqxP /usr/* /media; umount /media

copy /var partition:

mount /dev/md6 /media; rsync -aqxP /var/* /media; umount /media

copy /tmp/ partition:

NOTE: You can omit /tmp if you like !!

mount /dev/md8 /media; rsync -aqxP /tmp/* /media; umount /media

copy /home partition:

mount /dev/md9 /media; rsync -aqxP /home/* /media; umount /media

Setup boot manager

grub

Once in GRUB type the following commands:

device (hd0) /dev/sdb
root (hd0,0)
setup (hd0)
quit

Image:raid1_11.gif

Reboot

We are ready to reboot the system. To confirm that we are running system from RAID 1 arrays ( I guess we are on the good path since our system has started ) we can check for mounted devices with mount command:

mount

Image:raid1_12.gif

Add first hard drive ( sda ) to the RAID 1 array

Change Partition Id with sfdisk

Same as we did with /dev/sdb we need to first change partition ID's:

for partition in 1 5 6 8 9; do sfdisk --change-id /dev/sda $partition fd; done

Image:raid1_13.gif

Add partitions with mdadm to RAID 1 array

for partition in 1 5 6 8 9; do mdadm --add /dev/md$partition /dev/sda$partition; done

Image:raid1_14.gif

Since we have added new devices to a RAID 1 array the system started to resync each /dev/md* . You can see this process with watch command:

watch cat /proc/mdstat

Image:raid1_15.gif

Edit /etc/mdadm/mdadm.conf

Now we need to again edit /etc/mdadm/mdadm.conf file. We use our backup copy of this file as a template.

cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig1
cp /etc/mdadm/mdadm.conf_orig /etc/mdadm/mdadm.conf
mdadm --detail --scan >> /etc/mdadm/mdadm.conf

Image:raid1_16.gif

Finish

  • Everything is now ready. You may want to reboot your system and confirm your successful setup of Linux software RAID 1.
  • To confirm that raid 1 is running use cat /proc/mdstat
cat /proc/mdstat
  • Since we have used /dev/sda7 as a swap we have now unused partition /dev/sdb7.

References

Other Topics

Personal tools