Linux multiple disk mirror
Last update
2025-09-01
2025
09-01
«software raid 1 with mdadm»

creation

We want to create two distinct raid1 arrays for two partitions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
apt install mdadm

# set partition type "fd" aka "Linux raid autodetect" on the two partitions
fdisk /dev/sda

# clone msdos partition table from sda to sdb
sfdisk -d /dev/sda | sfdisk /dev/sdb

# create a degraded array with only one disk
mdadm --create pidata  --level=1 --raid-devices=2 /dev/sda1 missing
mdadm --create pidata2 --level=1 --raid-devices=2 /dev/sda2 missing

# array status
mdadm --detail /dev/md/pidata
mdadm --detail /dev/md/pidata2

cat /proc/mdstat

# device status
mdadm --examine /dev/sda1
mdadm --examine /dev/sda2

# format array
mkfs.ext4 /dev/md/pidata
mkfs.ext4 /dev/md/pidata2

# add the second disk
mdadm /dev/md/pidata  --add /dev/sdb1
mdadm /dev/md/pidata2 --add /dev/sdb2

# OPTIONAL: save conf (old cmd: mdadm --detail --scan --verbose)
/usr/share/mdadm/mkconf > /etc/mdadm/mdadm.conf

# stop array and release resources
mdadm -S /dev/md/pidata

monitoring

Ensure monitoring is running and configure email alerts:

1
2
# /etc/mdadm/mdadm.conf
MAILADDR root,my_user@domain.ext

recover from a soft failure

Having an occasional *HCI module problem will spawn an USB drive failure, for example cat /proc/mdstat may show [U_] (an underscore) meaning a failing drive has been removed from the array:

1
2
3
4
# re-adding the failed drive re-syncs the array
mdadm /dev/md/pidata --add /dev/sdb1
mdadm --detail /dev/md/pidata # shows "spare rebuilding" and %
cat /proc/mdstat              # shows recovery %

replacement

How to replace a failing drive (eg. sdb):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# remove drive from the array
mdadm /dev/md/pidata  --fail /dev/sdb1 --remove /dev/sdb1
mdadm /dev/md/pidata2 --fail /dev/sdb2 --remove /dev/sdb2

# unplug the drive & plug a new one (same or greater size)
eject /dev/sdb

# copy partitions
sfdisk -d /dev/sda | sfdisk /dev/sdb

# add the new disk
mdadm /dev/md/pidata  --add /dev/sdb1
mdadm /dev/md/pidata2 --add /dev/sdb2

# see rebuilding in progress
cat /proc/mdstat

Source: kernel.org, existing drive, debian.org, howtoforge: replacing, linuxbabe