Linux md-raid / mdadm - Cheat Sheet

June 14, 2025

MDRaid Banner


Guides and commands tried and tested with Debian and Ubuntu (Ubuntu 12.04 all the way through to 24.04), although I do believe they will carry over quite easily to other distros

For Proxmox may need to “apt install mdadm”

Removal of old mdraid array and partitions

md-raid arrays have a habit of lingering around and re-appearing randomly as /dev/md127 if not completely removed from the system These are the methods I use successfully to stop, remove and purge any unwanted mdraid configuration

Stop an existing array

mdadm –stop /dev/md#

:~# mdadm --stop /dev/md1
mdadm: stopped /dev/md1

Wipe disks old superblock

mdadm –zero-superblock /dev/sdX#

:~# mdadm --zero-superblock /dev/sda1
(no output)

Remove mdadm config line

Edit /etc/mdadm/mdadm.conf

Remove or comment the corresponding ARRAY line such as

ARRAY /dev/md/1  metadata=1.2 UUID=33b7dc52:f781b390:b6506437:5f059397 name=debian:1

Update initramfs

This step is required to copy the latest version of mdadm.conf to the initramfs, as it’s one of those things that need to load before the full system loads, so it needs a copy in the initramfs too

:~# update-initramfs -u

Clear the entry from fstab

Edit /etc/fstab

Remove or comment the corresponding line so the system does not attempt to mount the old array at next boot

Remove partitions from old disk

wipefs -a /dev/sdX

:~# wipefs -a /dev/sda
/dev/sda: 8 bytes were erased at offset 0x00000200 (gpt): 45 46 49 20 50 41 52 54
/dev/sda: 8 bytes were erased at offset 0x1bf1fc55e00 (gpt): 45 46 49 20 50 41 52 54
/dev/sda: 2 bytes were erased at offset 0x000001fe (PMBR): 55 aa
/dev/sda: calling ioctl to re-read partition table: Success

Forcibly wipe the first 1GB of any pre-used disk

Useful if the previous commands aren’t playing ball

dd if=/dev/zero of=/dev/sdX bs=1M count=1000 status=progress

:~# dd if=/dev/zero of=/dev/sda bs=1M count=1000 status=progress
771751936 bytes (772 MB, 736 MiB) copied, 1 s, 771 MB/s
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB, 1000 MiB) copied, 4.32454 s, 242 MB/s

Reboot

At this point I would usually do a reboot to ensure the old array is safely gone

Create a new raid array

These are the steps I use to create a new array and ensure it persists after reboot. If steps such as updating the init-ramfs are missed, it may come back as /dev/md127 after a reboot

Ensure the first part of this guide was followed so that you’re starting with lovely clean disks

Create the partition table

You may use this pipe of characters into fdisk to create the required partitions - but there’s no harm to do it manually either!

It creates a GPT partition table, then creates a new raid partition and sets the type to “42 Linux RAID”, and then writes the table and exits

echo -e “g\nn\n\n\n\nt\n42\nw\n” | fdisk /dev/sdX

:~# echo -e "g\nn\n\n\n\nt\n42\nw\n" | fdisk /dev/sda

Welcome to fdisk (util-linux 2.38.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS (MBR) disklabel with disk identifier 0xc9485127.

Command (m for help): Created a new GPT disklabel (GUID: BF2669A7-8F80-524E-9BAA-9C2C6AC452F1).

Command (m for help): Partition number (1-128, default 1): First sector (2048-3750748814, default 2048): Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3750748814, default 3750748159):
Created a new partition 1 of type 'Linux filesystem' and of size 1.7 TiB.

Command (m for help): Selected partition 1
Partition type or alias (type L to list all): Changed type of partition 'Linux filesystem' to 'Linux RAID'.

Command (m for help): The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Repeat it for all the disks to be involved in the array

Create the array

Adjust array name, level and raid devices to your needs

mdadm –create /dev/mdX –level=# –raid-devices=# /dev/sdX1 /dev/sdX1 /dev/sdX1

root@rikpve:~# mdadm --create /dev/md1 --level=5 --raid-devices=6 /dev/sda1 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 /dev/sdf1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

Update mdadm.conf to persist the new array

Get the details of the new array

mdadm -Es

:~# mdadm -Es
ARRAY /dev/md/1  metadata=1.2 UUID=ae62dce6:24d8548f:31b9d42b:61bfd6ce name=rikpve:1

Copy the output (ARRAY line), put it in /etc/mdadm/mdadm.conf and save the file

Create the partition table

Create a new partition on the array. It will be, for example, dev/md1p1

fdisk /dev/md#

Create the partition, write and exit

Format the partition with a filesystem

Example is to create a simple ext4 partition on top of the new array but should work just as well with other filesystems

mkfs.ext4 /dev/md#p1

:~# mkfs.ext4 /dev/md1p1
mke2fs 1.47.0 (5-Feb-2023)
Creating filesystem with 2344049920 4k blocks and 293007360 inodes
Filesystem UUID: 82cea1de-a521-4333-9d9e-58ec59fa2de3
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544, 1934917632

Allocating group tables: done
Writing inode tables: done
Creating journal (262144 blocks): done
Writing superblocks and filesystem accounting information: done

Retrieve the UUID of the new partition

You will need this UUID for the fstab entry

blkid -s UUID -o value /dev/md#p1

~# blkid -s UUID -o value /dev/md1p1
ba6bc1d2-060a-4b78-851d-d877cdcb6912

Add the entry in fstab

Create the mount point

mkdir /your/mount/point

:~# mkdir /mnt/data

Edit /etc/fstab, enter the mount point details on a new line

UUID=uuid-of-partition /your/mount/point ext4 errors=remount-ro 0 1

UUID=ba6bc1d2-060a-4b78-851d-d877cdcb6912 /mnt/data ext4 errors=remount-ro 0 1

Mount the new filesystem

mount /your/mount/point

:~# mount /mnt/data

Update initramfs

This step is required to copy the latest version of mdadm.conf to the initramfs, as it’s one of those things that need to load before the full system loads, so it needs a copy in the initramfs too

update-initramfs -u

Reboot

At this point I would usually do a reboot to ensure the new array correctly mounts on startup