Mirror Disks


Table of Contents


Mirroring disks on Linux is simple and prudent to ensure your data survives hardware, software and human failures. All data from one disk is duplicated on another disk and if any one disk fails, you will never know it. So it is important after setting up to monitor the disk array.

Install mdadm

$ sudo apt-get install mdadm

Assembling your arrays

If you had disks before but lost them due to a boot disk rebuild, you can bring them back by using the assemble command:

$ sudo mdadm --assemble --scan

This is the command that runs in the background at boot, and assembles and runs all your arrays (unless something goes wrong, when you usually end up with a partially assembled array. This can be a right pain if you don't realise that's what's happened).

Status of array

Schedule this to run every day and send you the output, probably by e-mail. As you can see below, on disk is missing, so this array is degraded. If you find yourself in this position, add another disk back as soon as possible!

 $ sudo mdadm -D /dev/md/0
/dev/md/0:
           Version : 1.2
     Creation Time : Fri Nov 26 18:39:56 2021
        Raid Level : raid1
        Array Size : 976629440 (931.39 GiB 1000.07 GB)
     Used Dev Size : 976629440 (931.39 GiB 1000.07 GB)
      Raid Devices : 2
     Total Devices : 1
       Persistence : Superblock is persistent

     Intent Bitmap : Internal

       Update Time : Mon Sep  4 13:16:39 2023
             State : clean, degraded 
    Active Devices : 1
   Working Devices : 1
    Failed Devices : 0
     Spare Devices : 0

Consistency Policy : bitmap

              Name : beaglebone:0
              UUID : 5b7b53b5:778a2dbf:be80ae03:938abef
            Events : 33884

    Number   Major   Minor   RaidDevice State
       -       0        0        0      removed
       1       8        1        1      active sync   /dev/sda1

removed is keyword for failed!

Adding a drive to a mirror

This will add a new drive to your mirror. The "--grow and --raid-devices" are optional, if you increase the number of raid devices, the new drive will become an active part of the array and the existing drives will mirror across. If you don't increase the number of raid devices, the new drive will be a spare, and will only become part of the active array if one of the other drives fails.

$ sudo  mdadm [--grow] /dev/md/mirror --add /dev/sdc1 [--raid-devices=3]
$ sudo  mdadm /dev/md/0 --add /dev/sdb1

Creating a mirror raid

The simplest example of creating an array, is creating a mirror.

$ sudo mdadm --create /dev/md/name /dev/sda1 /dev/sdb1 --level=1 --raid-devices=2

This will copy the contents of sda1 to sdb1 and give you a clean array. There is no reason why you can't use the array while it is copying (resyncing). This can be suppressed with the "--assume-clean" option, but you should only do this if you know the partitions have been wiped to null beforehand. Otherwise, the dead space will not be a mirror, and any check command will moan blue murder.

Configuration File

Run update-initramfs -u after updating this file.

File: /etc/mdadm/mdadm.conf

# mdadm.conf
#
# !NB! Run update-initramfs -u after updating this file.
# !NB! This will ensure that initramfs has an uptodate copy.
#
# Please refer to mdadm.conf(5) for information about this file.
#

# by default (built-in), scan all partitions (/proc/partitions) and all
# containers for MD superblocks. alternatively, specify devices to scan, using
# wildcards if desired.
#DEVICE partitions containers

# automatically tag new arrays as belonging to the local system
HOMEHOST <system>

# instruct the monitoring daemon where to send mail alerts
MAILADDR root

# definitions of existing MD arrays
ARRAY /dev/md/0  metadata=1.2 UUID=5b7b53b5:778a2dbf:be80ae03:938abef name=beaglebone:0

# This configuration was auto-generated on Mon, 04 Sep 2023 14:42:26 +0000 by mkconf

Hint: To re-create the ARRAY definition line above, use this:

$ sudo mdadm --detail --scan

Reference:

Auto Mount Array

I usually put the mount as a seperate command, running through /etc/rc.local, in case it does not build the array at boot for some reason. This way you can still log in, fix the error, and mount the array. Otherwise you have the run through boot recovery to fix it.

Note the delay to allow time for mdadm to assemble the array

File: /etc/rc.local

#!/bin/bash
#
# Mount mdadm array
#
sleep 60
#
mount /dev/md/0 /mnt/raid1
#
exit 0

Make /etc/rc.local executable, and start rc-local systemd process and it will run /etc/rc.local on every boot.

$ sudo chmod 755 /etc/rc.local
$ sudo systemctl start rc-local

Monitor Array

Minimal monitoring could be a status script in e-mail every morning.

File: /mnt/raid9/raid.sh

#!/bin/bash
TMP=$(mktemp)
sudo /sbin/mdadm -D /dev/md0 >$TMP
/bin/cat $TMP | /usr/bin/mail -s "Raid status" bob@bob.com
rm $TMP

Schedule in cron.

File: /etc/cron.d/mdadm

#
# cron.d/mdadm -- schedules periodic redundancy checks of MD devices
#
# Copyright © martin f. krafft <madduck@madduck.net>
# distributed under the terms of the Artistic Licence 2.0
#

# By default, run at 00:57 on every Sunday, but do nothing unless the day of
# the month is less than or equal to 7. Thus, only run on the first Sunday of
# each month. crontab(5) sucks, unfortunately, in this regard; therefore this
# hack (see #380425).
57 0 * * 0 root if [ -x /usr/share/mdadm/checkarray ] && [ $(date +\%d) -le 7 ]; then /usr/share/mdadm/checkarray --cron --all --idle --quiet; fi
# Don - email status every morning
11 7 * * * root /mnt/raid9/raid.sh


Mirror Disks - Linux in the House - https://linux-in-the-house.org Creative Commons License