How to set up a RAID 1 under GNU/Linux

2012-03-29
•
RAIDhardwareGNU/Linuxmdadmfdisksysadmin

This article explains how to set up a software RAID 1 (simple mirroring) under
GNU/Linux with existing data.

Rationale: Let’s say you’ve got two hard drives of the same size and want to
make some redundancy because the currently installed disk is starting to make
scary noises. You know, that kind of “I’m gonna die soonish!” sound in hard
drive parlance. Also, you don’t want to format this first drive so we’ll see
how to build a RAID 1 array on our already running system without losing data or
using a third drive.

This how-to should be suitable for any GNU/Linux distro (the only
distro-specific step is explained for Fedora and Debian families).

First of all, let’s make sure we’ve got the package mdadm installed:

sudo yum install mdadm # or sudo apt-get install mdadm

sdd and sde will be the two drives that we’re going to use for our RAID.
sde is the one containing the existing precious data and sdd is the spare
drive with nothing important on it (it’ll be formatted). Bare in mind to adapt
it for your configuration if different.

Pro tip: An array of two drives can read in two different places at the same
time. Therefore, to maximize performance benefits of RAID 1, you should use
independent disk controllers, i.e. one for each disk.

Start by unmounting both drives to be sure they are not currently used:

sudo umount /dev/sd{d,e}?

The next step is to change the partition type to Linux raid autodetect for
every partition on sde.

Then, we create the actual RAID 1 using the placeholder missing for our
partition with existing data. That will allow us to mount it outside from the
RAID to copy our data onto the new RAID.

sudo mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdd1
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? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

Since my partition is a simple dumb “data” partition with no boot flag,
mdadm warns us but that’s fine in this case.

Before we can actually use the RAID, we need to format it. I’ve chosen XFS here
but you may want something else depending of your needs.

sudo mkfs.xfs -L data /dev/md0
[...]

Now, you need to create a new file to store your RAID configuration. Otherwise,
your OS will not know about it after reboot.
man 5 mdadm.conf should tell you its name but it’s usually /etc/mdadm.conf
on Fedora/CentOs/Redhat and /etc/mdadm/mdadm.conf on Debian/Ubuntu.

Go make you a coffee… If you have to interrupt the process half-way through,
just hit Ctrl-C and you’ll be able to run rsync again later to finish
transferring the missing files. rsync’s such a great tool! Personally, I had
to do it over two days to copy the whole 932 gigs…

As soon as you enter the following command, your RAID should switch to recovery
mode and start mirroring files to sde1:

sudo mdadm --add /dev/md0 /dev/sde1

You can monitor it with /proc/mdstat. Once again, it can take ages (although
it should be slightly faster than the rsync step above). And once again, you
can reboot during the process as you wish, the recovery will resume
automagically until it reaches 100%.