mdadm / software raid
create raid
raid 1:
mdadm --create /dev/md0 --level=1 --raid=devices=2 /dev/sda /dev/sdbif you get an error like mdadm: invalid number of raid devices: devices=2 run the command like so (with -l for level and -n for devices):
mdadm --verbose --create /dev/md0 -l 1 -n 2 /dev/sdc /dev/sdbcreate a file system:
mkfs.ext4 /dev/md0or whatever filesystem you want.
check raid status
mdadm --detail /dev/md0check also: /proc/mdstat
save raid config
well, that’s somewhat important, right?
mdadm --detail --scan --verbose | sudo tee -a /etc/mdadm/mdadm.confRun update-initramfs -u after updating this file.
also, back up the mdadm.conf file to restore it onto a new system
add and remove devices to/from raid
if they’re in a faulty state for example
mdadm --remove /dev/md0 /dev/nvme0n1
mdadm --add /dev/md0 /dev/nvme0n1replace disk
check how to backup and copy your partition table
remove disk:
mdadm /dev/md0 -r /dev/sdbcopy partition table from backup or primary drive:
sfdisk -d /dev/sda | sfdisk /dev/sdbadd disk:
mdadm /dev/md0 -a /dev/sdbconvert single disk to raid 1
… hopefully without losing data!
- create raid from 2nd disk with one missing disk
- copy data from 1st disk to 2nd disk (now in raid)
- add 1st (original) disk to raid config
- rebuild raid
- profit (took 5 steps, I’m sorry)
create raid:
notice the missing bit?
mdadm --create /dev/md0 --level=1 --raid-devices=2 missing /dev/sdbnow copy the data from the original disk to the newly created raid, so create and mount your filesystem on /dev/md0
[…]
prepare original disk:
let’s dump (-d the disk layout from the raid disk to the original disk):
sfdisk -d /dev/sdb | sfdisk /dev/sdaadd original disk to raid:
mdadm /dev/md0 -a /dev/sdacheck raid rebuild status:
…in minutes
grep -oP 'finish\=\d+\.\d+' /proc/mdstat | cut -d'=' -f2increase mdadm rebuild / resync speed
you can check the current speed limits with:
sysctl dev.raid.speed_limit_min
sysctl dev.raid.speed_limit_maxand, now just increase that number to make it faster (it’s displayed in kb)
sysctl -w dev.raid.speed_limit_min=500000debugging things
if you messed things up, you can try and restore things with testdisk