You already have a fully functional system setup on a single drive, but you would like to add some redundancy to the setup by using RAID-1 to mirror your data across 2 drives. This guide follows the following steps to make the required changes, without losing data.

You already have a fully functional system setup on a single drive, but you would like to add some redundancy to the setup by using RAID-1 to mirror your data across 2 drives. This guide follows the following steps to make the required changes, without losing data.

* Create a single-disk RAID-1 array with our new disk

* Create a single-disk RAID-1 array with our new disk

Revision as of 16:02, 16 March 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

You already have a fully functional system setup on a single drive, but you would like to add some redundancy to the setup by using RAID-1 to mirror your data across 2 drives. This guide follows the following steps to make the required changes, without losing data.

Create a single-disk RAID-1 array with our new disk

Move all your data from the old-disk to the new RAID-1 array

Verify the data move was successful

Wipe the old disk and add it to the new RAID-1 array

Warning: Make a backup first. Even though our aim is to convert to a RAID setup without losing data, there's no guarantees the process will be perfect, and there is a high risk of accidents happening.

The swap partitions will not be in a RAID array as having swap on RAID serves no purpose. Refer to this article for reasons why.

To minimize the risk of Data on Disk (DoD) changing in the middle of our changes, I suggest you drop to single user mode before you start by using the Template:Codeline command.

You will need to be the root user for the entire process.

Create new RAID array

First we need to create a single-disk RAID array using the new disk.

Partition the Disk

Use fdisk or your partitioning program of choice to setup 2 primary partitions on your new disk. Make the swap partition half the size of the total swap you want (the other half will go on the other disk).

Drop to single user mode:

telinit 1

To see the current partitions:

fdisk -l

To partition the new disk

fdisk /dev/sdb

Then the fdisk commands to partition the new disk. Note that everything after the "#" is an explanation of what the command is doing:

c # Turn off DOS compatibility (optional).
n # new
p # primary
1 # first partition
1 # start at first cylinder
101 # end cylinder, 0.1% of the disk. Note: update this number as appropriate for your disk.
n # new
p # primary
2 # second partition
press enter # Uses the default start from the end of the first partition
press enter # Uses the default of using all the remain space on the disk.
t # set the partition type
1 # for partition number 1
82 # ... and set it to be swap
t # set the partition type
2 # for partition number 2 ...
fd # ... and set it to be "linux raid auto"
a # Toggle the bootable flag to be "on"
2 # for partition number 2.
p # print what the partition table will look like
w # now write all of the above changes to disk

At the end of partitioning, your partitions should look something like this:

Note: If the above command causes mdadm to say "no such device /dev/sdb2", then reboot, and run the command again.

If you want to use Grub 0.97 (default in the Arch Linux 2010.05 release) 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.

Mount the array

Copy the data

[root@arch ~]# rsync -avxHAXS --delete --progress / /mnt/new-raid

Note that by using the -x option you are limiting rsync to a single file system. If you have a more traditional file system layout, with different partitions for /boot, /home, and perhaps others, you will need to rsync those file systems separately. For example:

Alternatively, you can use tar instead of the above rsync command if you prefer. rsync will, however, be quicker if you are only copying over changes. The tar command is: tar -C / -clspf - . | tar -xlspvf -

Alter fstab

You need to tell fstab on the new disk where to find the new devices. It's better to use UUID codes here, which should not change, even if our partition detection order changes or a drive gets removed.

Look for the partition labeled "NEW-SWAP", on /dev/sdb1, that we created above. Copy your swap partition's UUID into the new fstab, as shown below. Of course we also add /dev/md0, as our root mount point.

If the chroot command gives you an error like chroot: failed to run command `/bin/zsh': No such file or directory, then use chroot /mnt/new-raid/ /bin/bash instead.

You are now chrooted in what will become the root of your RAID-1 system. Complete the appropriate section below for your distribution (almost every other step is identical, regardless of the Linux variant).

Record mdadm's config

For Arch Linux, use "/etc/mdadm.conf", for Ubuntu or Debian, use "/etc/mdadm/mdadm.conf"

nano /etc/mdadm.conf

... and change the "MAILADDR" line to be your email address, if you want emailed alerts of problems with the RAID-1.

Then save the array configuration with UUIDs to make it easier for the system to find /dev/md0 on boot-up. If you don't do this, you can get an "ALERT! /dev/md0 does not exist" error when booting :

Then substitute/script in these others so that all are available for use with the new RAID setup.

Install GRUB on the RAID array

Start grub:

[root@arch ~]# grub --no-floppy

Then we find our two partitions - the current one (hd0,0) (I.e. first disk, first partition), and (hd1,1) (i.e. the partition we just added above, on the second partition of the second drive). Check you get two results here:

grub> find /boot/grub/stage1
(hd0,0)
(hd1,1)

Then we tell grub to assume the new second drive is (hd0), i.e. the first disk in the system (when it is not currently the case). If your first disk fails, however, and you remove it, or you change the order disks are detected in the BIOS so that you can boot from your second disk, then your second disk will become the first disk in the system. The MBR will then be correct, your new second drive will have become your first drive, and you will be able to boot from this disk.

grub> device (hd0) /dev/sdb

Then we install GRUB onto the MBR of our new second drive. Check that the "partition type" is detected as "0xfd", as shown below, to make sure you have the right partition:

Verify success

Reboot your computer, making sure it boots from the new RAID disk (Template:Filename) and not the original disk (Template:Filename). You may need to change the boot device priorities in your BIOS to do this.

Once the GRUB on the new disk loads, make sure you select to boot the new entry you created in Template:Filename earlier.

Verify you have booted from the RAID array by looking at the output of mount. Also check mdstat again only to confirm which disk is in the array.

If system boots fine, and the output of the above commands is correct, then congratulations! You're now running off the degraded RAID array. We can add the original disk to the array now to bring it up to full performance.

Add original disk to array

Partition original disk

Copy the partition table from /dev/sdb (newly implemented RAID disk) to /dev/sda (second disk we are adding to the array) so that both disks have exactly the same layout.

[root@arch ~]# sfdisk -d /dev/sdb | sfdisk /dev/sda

Alternate method - this will output the /dev/sdb partition layout to a file, then it's used as input for partitioning /dev/sda.

Verify that email alerts are working

If you run this command, then you should get a notification email showing the contents of /proc/mdstat :

[root@arch ~]# mdadm --monitor --test --oneshot /dev/md0

Check that you get the test email notification. This way you can be aware if one of the disks in the array fails (otherwise it may fail silently, putting you at risk of data loss if another drive should also fail). If you don't get an email notification, check the "MAILADDR" line in mdadm.conf, and also check that sending an email to this address from the command line (e.g. using the "mail" command) works.