How to Set Up RAID in Linux

This article is part of the RAID series:

While a hardware RAID would be set up in the BIOS and automatically used by the Linux kernel, a software RAID will require somewhat more work on Linux.

These instructions will suit someone with a home system who would prefer their files be safe or those running small self-hosted servers. For larger, production-grade systems, careful planning and more elaborate implementation is advisable.

On Linux, RAID disks do not follow the usual /dev/sdX naming, but will be represented as md (multidisk) files, such as md0, md1, md2, stc.

An important file you need to remember is /proc/mdstat, which will provide information about any RAID setups on your system. Typing

cat /proc/mdstat

will tell you all about any existing RAID setups.

The command you will need to use to set up raid is mdadm. Most systems should come with this pre-installed. If not, such as on some Debian systems, you can get it with

sudo apt-get install mdadm

Once you have all the required tools, it is time to decide what your implementation will be like.

You can find out more about the different RAID levels here. Theoretically, you could apply any combination of RAID arrays to your disks and partitions, though there are some common practices and considerations that are worth thinking about.

  • RAID 0 would suit non-critical disks where speed is imperative. A good use for RAID 0 is for the swap partition, as it can considerably improve its speed.
  • For two disks only, on small systems like a home computer where you need redundancy and capacity is no concern, use RAID 1.
  • If you have four or more disks and you want speed and redundancy, RAID 10 is a good choice.
  • RAID 5 needs a minimum of three disks, introduces an overhead for small random disk writes, and it under-performs on large or slow drives. Do not use RAID 5 if your disk is slow (less than 7200 RPM) and/or large (over 1TB).
  • For larger disks, RAID 6 is preferred, but you will lose two disks for parity (as compared to one in RAID 5), and the write overhead is greater than with RAID 5.
  • For more elaborate setups, it is worth considering to use RAID alongside LVM, but that would need expert knowledge and careful planning.

There are two ways you can implement RAID on Linux. The simplest method is to add a new RAID array to an existing system for storage purposes. For a more elaborate setup and greater protection, RAID could (and should) be implemented at install time, but of course this is not always an option.

There is also the possibility to migrate an already installed system onto a newly implemented RAID array, but that is a rather advanced process and will not be covered in this simple tutorial. We will now focus on adding a RAID array to your existing setup, for storing sensitive data or whatever it is you would like to ensure a recovery option exists for.

In this example we will set up a RAID 1 array across two disks that will be used for storing important data files.

Note: Partitioning and configuring disks can easily lead to damage or loss of data. Always create a backup before trying such operations, and only proceed if you feel absolutely confident that you understand the process. Proceed at your own risk!

First you will need to prepare your partitions. Use your favorite partitioning tool and create two partitions, one on each drive, that will be used as the RAID array. The new partitions should be identical in size and with the type “fd” (RAID autodetect) (If your system partition needs resizing, you can use a Live CD, like Parted Magic).

Assuming your partitions now are sda1, sda2 on the first disk and sdb1, sdb2 on the second disk where

  • sda1 is your original system partition, (ext4 mounted as /)
  • sda2 is your new partition that would be used in the RAID array with a size of 7.7GB
  • sdb1 is the other partition to be used in the RAID array with the size of 7.7GB
  • sdb2 is the remainder of the second hard drive (as the size of the partitions making up RAID must be identical, the excess cannot be meaningfully used in this case.)

linux-raid-lsblk

What is interesting now is the sda2 and sdb1 partitions that would make up the RAID array.

Making a RAID 1 array is relatively simple using the mdadm command which allows for fine control for managing RAID. To see all your options, type:

mdadm --help

For our example, use the command like this:

sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda2 /dev/sdb1 --verbose

To break down the above command

--create /dev/md0 will create the new RAID array and call it md0. If you have existing RAID setups, make sure you use a number that is unused until this point.

--level=1 This is to make sure it is RAID 1.

--raid devices=2 basically tells mdadm that there will be two partitions used which will be specified right after (/dev/sada2 and /dev/sdb1 in this case).

--verbose will make more output, so when you monitor the command you will get more information.

- p f2 would use the “far” rotation plan, meaning that the data is not stored in the same sequence allowing for greater protection against simultaneous drive failures. (If the drive fails for manufacturing errors, writing the same sequence of data in the same way all the time could mean that the drives could fail more or less at the same time. Useful with RAID 10)

Just accept the confirmation dialogue, and you are done.

linux-raid-confirmation

To see what happens during setup, you can use the watch command from another terminal window to monitor the /proc/mdstat file:

watch -d cat /proc/mdstat

linux-raid-progress

When ready, the progress bars will change into the usual layout of the file’s contents.

linux-raid-done

Although not strictly required on all systems, as most would automatically scan for active RAID arrays, it is best to make sure the /etc/mdadm/mdadm.conf file is created. The sudo command will not have enough “power” for this. You’ll need to log in as root or use su. On Ubuntu systems, the root password is usually left unconfigured as a security precaution. To give it a password, type:

sudo passwd root

and type the new root password twice. (This password will work when you use su. For sudo you would still use your usual user password.)

Now become root:

su

and configure mdadm.conf:

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

Unfortunately mdadm adds a name parameter by default which could lead to problems when mounting the RAID array at boot time. To resolve this, open your freshly updated mdadm.conf file with:

sudo nano /etc/mdadm/mdadm.conf

and remove the name=[devicename]:[x] bit, which in our case was:

name=ubuntu-test:0

Now save and exit.

You should then update initramfs to use the modified mdadm.conf

sudo updtate-initramfs -u

Check you new RAID array with:

mdadm --examine --scan

The command should return no error.

linux-raid-examine-scan

Now format your new RAID array to the file system of your choice. In this example we will use ext4.

sudo mkfs.ext4 /dev/md0

To mount your new array, create the mount point. This could be anywhere. In this example we will use ~/failsafe

mkdir ~/failsafe

Then open /etc/fstab file for writing

sudo nano /etc/fstab

and add the following line:

/dev/md0 [path_to_mount_point]  ext4 defaults 1 2

Of course you will have to substitute [path_to_mount_point] with the full path where you want to mount your new RAID 1 array. In our case, the line looked like:

/dev/md0 /home/test/failsafe ext4 defaults 1 2

Now mount it without rebooting:

sudo mount -a

And you are ready to use your new RAID 1 array.

linux-raid-lsblk-final

If your RAID has become read-only, check permissions and ownership on the mount directory. If it’s root, you can change this with chown:

sudo chown [username] [path]

after which your RAID array would be writable.

That’s all you need to do to setup RAID in Linux. Let us know in the comments if you face any problems during the setup.