How-To Create Software RAID 1 (mirror) on Debian Linux

By | 2014/12/27

A RAID1 array on Linux is fairly straightforward to setup. Here is a quick guide!

Note: In this example, the two disk for the array are: /dev/sda and /dev/sdb

1. Install mdadm

On Debian, install mdadm:

sudo apt-get install mdadm

If prompted to define the array, leave the answer blank for now.

2. Run fdisk on each disk and create a new partition, type fd (Linux raid auto)
sudo fdisk /dev/sda

sudo fdisk /dev/sdb

3. Edit /etc/mdamd/mdadm.conf and define disks in the array
ARRAY /dev/md0 devices=/dev/sda1,/dev/sdb1 level=1 num-devices=2 auto=yes
4. Zero out the superblock to remove any previous raid installation from the disks
sudo mdadm --zero-superblock /dev/sda /dev/sdb

If output provides an error such as ‘Unrecognised md component device’ this is ok and expected behavior for a disk that has never been part of RAID before.

5. Next, create the RAID1 array
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda1 /dev/sdb1

6. Format the array with a desired file system. Here I am using ext4.
sudo mkfs.ext4 /dev/md0

The array will now take some time to build. To view the progress of the array, use the watch command like so:

watch cat /proc/mdstat
7. Mount array

The RAID1 can be mounted as desired. For example to use as your /home, mount with:

sudo mount /dev/md0 /home

Running df -H will show the RAID1 like a regular disk:

stmiller@brahms:~$ df -H
Filesystem      Size  Used Avail Use% Mounted on
/dev/sde1        30G   21G  7.4G  74% /
udev             11M     0   11M   0% /dev
tmpfs           3.4G   19M  3.4G   1% /run
tmpfs           8.5G   34M  8.4G   1% /dev/shm
tmpfs           5.3M  4.1k  5.3M   1% /run/lock
tmpfs           8.5G     0  8.5G   0% /sys/fs/cgroup
tmpfs           8.5G  2.9M  8.5G   1% /tmp
tmpfs           1.7G     0  1.7G   0% /run/user/121
tmpfs           1.7G  8.2k  1.7G   1% /run/user/1000
/dev/md0        246G   63M  234G   1% /home

8. Mount array at boot

To have the array be mounted and available at boot, add a line to /etc/fstab to define the desired mount point:

/dev/md0 /home ext4 noatime,rw 0 0 

Rock on,

2 thoughts on “How-To Create Software RAID 1 (mirror) on Debian Linux

  1. gvn

    Is there anything special you need to do when using two 4TB drives?

    After following these instructions I ended up with only 2.2 TB showing up when I run `df -H`

  2. cat1092

    gyn, you need a UEFI Firmware (what used to be called BIOS) & format as GPT.

    That way, you’ll be able to use your entire drives, all of them. Of course that provided you have UEFI Firmware (mid to late 2012 onwards, before then, most computers were BIOS only. If you were close to the range, you may want to see if there’s a UEFI Firmware update that applies to you, it’s a longshot option, yet any shot is better than none.

    Two last option(s), if your MB by chance is the ASRock brand, there’s a tool that they offer to unlock HDD’s over 2TB in size. If that’s not possible, then I’d suggest looking for a modern MB for your CPU, if DDR2 RAM, forget UEFI, you’ll need at least a Sandy Bridge (2nd gen ‘i’ series) CPU to get a UEFI MB.

    A rule of thumb when asking these questions is to list system components. That’ll help us help you in determining what can be done.



Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.