Date

Purpose

This tutorial aims to help you setup a software RAID-1 (mirror) under ubuntu 13.04

Lets assume the following mapping

/dev/sda1    /
/dev/sda2    /home
/dev/sda3    swap
/dev/sda4    /spare

We also assume that you have a second (empty) hard disk drive larger or equal to sda .

We are going to create 4 new partitions (md1,md2,md3,md4) which will act as RAID-1 using sda1 & sdb1, sda2 & sdb2, sda3 & sdb3, and sda4 & sdb4 respectively to store the data.  This way all our data will be available on both disks.

Dependencies

$ apt-get install initramfs-tools mdadm

Now run

$ modprobe raid1

and

$ more /proc/mdstat

This should print something like this:

Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
unused devices:

Setup sdb

sdb's partition table must be a replica of sda's.  Execute:

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

to replicate it.  Next we need to set the partitions' types.  Execute:

# fdisk /dev/sdb

you will see a prompt like this:

# Command (m for help):

type t and enter. Then you will see a prompt like this:

# Partition number (1-4):

type 1 and enter. Then you will see a prompt like this:

Hex code (type L to list codes):

type fd and enter.

Do this for every partition (1-4) only changing the partition number at the second prompt.

Finally type w and enter to write the changes.

The output should be like this:

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): fd
Changed system type of partition 4 to fd (Linux raid autodetect)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

Create the RAID arrays

# for i in 1 2 3 4; do mdadm --create /dev/md$i --level=1 --raid-disks=2 missing /dev/sdb$i; done

If you see the following message type y.

mdadm: Note: this array has metadata at the start and
    may not be suitable as a boot device.  If you plan to
    store '/boot' on this device please ensure that
    your boot-loader understands md/v1.x metadata, or use
    --metadata=0.90
Continue creating array?

Now setup the filesystem on the arrays.

# for i in 1 2 4; do mkfs.ext4 /dev/md$i; done
# mkswap /dev/md3

Configure mdadm

# cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm.conf_orig
# mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Update the fstab

Open /etc/fstab and change the /dev/sdX uuids with those of the corresponding /dev/mdX uuids.  To see the mapping between /dev/mdX and the uuids use:

$ ls -l /dev/disk/by-uuid/

Temporarily set GRUB2

Create /etc/grub.d/09_temp with the following content

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'temp' --class ubuntu --class gnu-linux --class gnu --class os {
        insmod raid
        insmod mdraid
        insmod part_msdos
        insmod ext2
        set root='(md/1)'
        echo    'Loading Linux...'
        linux   /boot/vmlinuz-3.8.0-25-generic root=UUID=the_UUID_of_/dev/md1 ro  quiet
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initrd.img-3.8.0-25-generic
}

Use

$ uname-r

to get the appropriate kernel version or just

$ ls /boot/

to see the available files.

Make /etc/grub.d/09_temp executable

# chmod +x /etc/grub.d/09_temp

Now we need to update GRUB

# update-grub

and the ramdisk

# update-initramfs -u

Finally mount /dev/md1, /dev/md2 and /dev/md3 and

# cp -dpRx / /mnt/md1
# rm -rf /mnt/md1/home /mnt/md1/spare
# cp -dpRx /home/* /mnt/md2
# cp -dpRx /spare/* /mnt/md4

Install GRUB on both disks

# grub-install /dev/sda
# grub-install /dev/sdb
# reboot

If during the startup it asks you something about booting in degraded mode type y. Then you might also need to type S. When the system finally start, execute

$ df

and you should see /dev/md1, /dev/md2 and /dev/md4 in the listing.

Update partitions' type of /dev/sda and add them to the array

Execute:

# fdisk /dev/sda

you will see a prompt like this:

# Command (m for help):

type t and enter. Then you will see a prompt like this:

# Partition number (1-4):

type 1 and enter. Then you will see a prompt like this:

Hex code (type L to list codes):

type fd and enter.

Do this for every partition (1-4) only changing the partition number at the second prompt.

Finally type w and enter to write the changes.

The output should be like this:

Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): fd
Changed system type of partition 4 to fd (Linux raid autodetect)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

Finally add them.

# for i in 1 2 3 4; do mdadm --add /dev/md$i /dev/sda$i; done

Executing:

$ cat /proc/mdstat

should print something like this

Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10] 
md3 : active raid1 sda3[2] sdb3[1]
      8384448 blocks super 1.2 [2/1] [_U]
        resync=DELAYED

md4 : active raid1 sda4[2] sdb4[1]
      162277248 blocks super 1.2 [2/1] [_U]
        resync=DELAYED

md2 : active raid1 sda2[2] sdb2[1]
      41910144 blocks super 1.2 [2/1] [_U]
        resync=DELAYED

md1 : active raid1 sda1[2] sdb1[1]
      31440512 blocks super 1.2 [2/1] [_U]
      [=>...................]  recovery =  9.8% (3111488/31440512) finish=13.5min speed=34736K/sec

unused devices:

Wait for it to finish

$ watch cat /proc/mdstat

Now we must reconfigure mdadm

# cp /etc/mdadm/mdadm.conf_orig /etc/mdadm/mdadm.conf
# mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Final setup of GRUB2

Delete the temporary entry

# rm -f /etc/grub.d/09_temp

Now update GRUB

# update-grub

and the ramdisk

# update-initramfs -u

Re-install and reboot

# grub-install /dev/sda
# grub-install /dev/sdb
# reboot

Test

To test your new RAID-1 setup follow these instructions.

Source: http://www.howtoforge.com/how-to-set-up-software-raid1-on-a-running-system-incl-grub2-configuration-debian-squeeze