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
raid_mdadm_config
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
raid_building
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,