This guide will detail how to setup a RAID1 array using MD in Ubuntu 16.04, while also providing a demonstration of how to swap disks in the event of a failure.
First install mdadm:
$ sudo apt install mdadm
Create partitions for each device first (don't format)
$ sudo cfdisk /dev/sdX
Follow the terminal interface to create the relevant partitions on your drives. (you can use any tool for this)
Create a RAID1 array by using the –level=mirror setting, then specify your drives. (It's that simple).
$ sudo mdadm --create --verbose /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 /dev/sdc1
Now that the array is set-up, we can format the drives. This is as easy as formatting the /dev/mdX raid “block device” we created.
$ sudo mkfs.ext4 /dev/mdX
By default, most of mdadm.conf is commented out, and it contains just the following:
/etc/mdadm.conf
DEVICE partitions
This directive tells mdadm to examine the devices referenced by /proc/partitions and assemble as many arrays as possible. A more precise approach is to explicitly add the arrays to /etc/mdadm.conf:
$ sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf
Once the configuration file has been updated the array can be assembled using mdadm:
$ sudo mdadm --assemble --scan
In order for the arrays to be detected by the initial root filesystem, the initramfs needs to be updated:
$ sudo update-initramfs -u
If this is note done, the system will hang at boot, if the md devices were added to /etc/fstab, as they will be assigned random device names such as /dev/md127.
Mounting an md array at boot simply requires the /dev/mdX device to be specified, such as:
/etc/fstab
/dev/md0 /media/drive1 ext4 defaults 0 2 /dev/md1 /media/drive2 ext4 defaults 0 2
You can check the status of any ongoing operations of your md raid anytime.
$ cat /proc/mdstat
The output may look something like this:
Personalities : [raid1] [linear] [multipath] [raid0] [raid6] [raid5] [raid4] [raid10]
md0 : active raid1 sdd1[0] sde1[1] 1953382464 blocks super 1.2 [2/2] [UU]
[============>........] resync = 63.6% (1242973952/1953382464)
finish=83.5min speed=141708K/sec bitmap: 8/15 pages [32KB], 65536KB chunk
md1 : active raid1 sdb1[0] sdc1[1] 976630464 blocks super 1.2 [2/2] [UU]
[===================>.] resync = 95.3% (930960832/976630464)
finish=8.6min speed=88130K/sec bitmap: 4/8 pages [16KB], 65536KB chunk