Convert a single drive system to RAID

From ArchWiki

Revision as of 09:07, 22 April 2009 by Nickj(Talk | contribs)(→‎Update GRUB: Some further updates that make kernetl security updates a lot easier (at least on Debian & Ubuntu), Arch may be similar. These two fields are used when menu.lst is regenerated.)

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:

init 1

To see the current partitions:

fisk -l

To partition the new disk

fdisk

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

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 parition
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"
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:

Mount the Array

Copy the Data

Alternatively, you can use tar if you prefer instead of the above rsync command. However rsync will 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.

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, irrespective of the linux variant).

For Ubuntu or Debian: Rebuild initramfs

... 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 bootup:

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

Then rebuild initramfs, incorporating the above two changes:

update-initramfs -k `uname -r` -c -t

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. you should have a line similar to the following in the output:

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

Take the output of Template:Codeline on your new disk, and make the partitions on your original disk look the same.