Linux Software Raid 1 Setup

RAID 1 is a hard disk configuration where the contents from one hard disk are mirrored onto another. This provides the user with some redundancy in case a disk fails. On your Linux system, the two hard drives are represented as a single file system. But in the background, making changes to your files is actually writing the changes to two disks at the same time. You can also add more than two disks to the configuration, as long as you keep the number even. Otherwise, something like RAID 5 will be more suitable.

There are many ways to configure a RAID setup. One of the easiest and most accessible ways is through the mdadm software package, which can be installed and used on any major Linux distribution. This is easier than some other RAID setups, since it doesn’t require any special hardware (like a RAID controller) and isn’t that hard to configure.

In this guide, we’ll go through the step by step instructions to install and setup mdadm on Linux, and create a RAID 1 configuration for two hard disks. Our example scenario will consist of two empty hard disks that are each 10 GB in size. This is in addition to our main hard disk, which is just used for the operating system.

Strictly speaking, RAID 1 is not a proper backup solution. It does provide some protection from disk failure, but what if you accidentally delete a file or a virus corrupts multiple files? Those undesirable changes are instantly written to both disks. RAID 1 provides high availability, but you shouldn’t use it as your only backup solution.

In this tutorial you will learn:

  • How to install mdadm on major Linux distros
  • How to partition hard disks for RAID setup
  • How to create a new RAID device in mdadm and mount it
  • How to keep the RAID array mount persistent
Using mdadm to create a software RAID 1 array on Linux

Using mdadm to create a software RAID 1 array on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software mdadm
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Install mdadm on major Linux distros

If mdadm is not already installed by default on your Linux distro, you can use the appropriate command below to install it through your system’s package manager.

To install mdadm on Ubuntu, Debian, and Linux Mint:

$ sudo apt install mdadm

To install mdadm on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install mdadm

To install mdadm on Arch Linux and Manjaro:

$ sudo pacman -S mdadm

After installation, you should reboot your system so that all modules get loaded into the kernel correctly.

$ reboot

Partition hard disks

The first thing we’ll need to do to configure our hard disks is to partition them as Linux RAID auto.

  1. We can see our hard disks by using the fdisk command. This will show us how they are named, which we will need for future commands. As you can see in the screenshot below, our disks are called /dev/sdb and /dev/sdc. These disks are just raw storage at the moment – they don’t have a partition table or anything else configured.
    # fdisk -l
  2. fdisk shows our two disks that we plan to use for our RAID 1 setup

    fdisk shows our two disks that we plan to use for our RAID 1 setup

  3. Use the following command to begin partitioning the first disk. This will open up the fdisk menu. Substitute your own disk name if yours is different.
    # fdisk /dev/sdb
  4. We will enter the following commands into the fdisk prompts in order to create a new partition and configure it as Linux RAID autodetect.

    1. Enter n to create a new partition.
    2. Enter p to mark this as a primary partition.
    3. Enter 1 for the partition number.
    4. For first and last sector (2 prompts), just press the enter key for default response.
    5. Enter t to select the partition we’ve just created.
    6. Enter fd to configure Linux RAID autodetect on the partition.
    7. Enter w to write all these changes to the disk.

  5. Partitioning the hard disk

    Partitioning the hard disk

  6. We now need to do the exact same steps for our second disk. In our case, that would be disk /dev/sdc. Repeat Step 2 and Step 3 for your second disk. Afterwards, you should be able to see your newly configured RAID partitions with the fdisk command.
    # fdisk -l

  7. Both drives have been partitioned as Linux RAID autodetect

    Both drives have been partitioned as Linux RAID autodetect

Create RAID device

Now that we have our hard drives properly partitioned, we can use mdadm to create a RAID device with the following command. Remember that even though we have two hard drives, the system will see them as a single device and mirroring will happen in the background.

  1. Create a RAID array called /dev/md0 with this command, substituting your own drive names as necessary.
    # mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/sd[b-c]1
  2. Next, put a file system on the device. We’ll use ext4 in this example.
    # mkfs.ext4 /dev/md0
  3. Now, make a directory to where you can mount the newly created RAID device. And then mount the device there.
    # mkdir -p /mnt/raid1
    # mount /dev/md0 /mnt/raid1
  4. Your RAID array should now be accessible at the mount point you defined.
    $ cd /mnt/raid1
  5. We can access our mounted RAID array and also use the df command to view details about it

    We can access our mounted RAID array and also use the df command to view details about it

Configure persistent RAID mount

The only issue now is that your RAID mount will not survive a reboot. To avoid needing to manually mount it every time, we can configure the /etc/fstab file. We’ll also save our mdadm configuration in the following steps.

  1. Edit the fstab file with nano or your favorite text editor, and add the following line.
    /dev/md0        /mnt/raid1      ext4    defaults        0       0
  2. Adding the RAID mount to fstab file

    Adding the RAID mount to fstab file

  3. Next, use the following command to save your current mdadm configuration.
    # mdadm --detail --scan --verbose >> /etc/mdadm.conf

That’s all there is to it. If you’ve made it this far, you should now have a working RAID 1 array that stays mounted even after a system reboot.

Closing Thoughts

In this tutorial, we saw how to create a RAID 1 mirror array using mdadm on Linux. To help guide you through the steps, we used two empty 10 GB drives and configured our own software RAID. At the end, we also learned how to keep the RAID array mounted after a system reboot.

Even if you have a slightly different environment (i.e. more than 2 disks in your array), these instructions are easy to adapt to different scenarios and will help you to reliably create new RAID configurations.

Comments and Discussions
Linux Forum