How to Create a Bootable Linux Software RAID 1 (mdadm) Array for Root Filesystem with GRUB Installation


5 views

When setting up a mirrored root filesystem using Linux's software RAID (mdadm), the primary obstacle occurs during GRUB installation. The bootloader needs to access RAID metadata before the array is assembled, creating a chicken-and-egg problem. Here's why most installations fail:

grub-install /dev/sda
# Error: filesystem md0' doesn't support blocklists
# (RAID arrays require special handling)

There are two fundamentally sound methods to solve this:

  • Separate /boot partition (non-RAID) on one drive
  • Full RAID boot using initramfs and GRUB modules

For a clean 500GB dual-drive setup, use this partition structure:

# On BOTH drives (/dev/sda and /dev/sdb):
# 1. 500MB boot partition (standard ext4)
# 2. Remaining space for RAID
# 3. Optional swap partition outside RAID

sfdisk /dev/sda << EOF
,500M,83,*
,499.5G,fd
EOF

sfdisk -d /dev/sda | sfdisk /dev/sdb

Modern metadata (1.2+) won't work for boot partitions. Use legacy format:

mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 \\
--metadata=0.90 /dev/sda2 /dev/sdb2

mkfs.ext4 /dev/md0

Install GRUB to both drives' MBR with RAID support:

mount /dev/md0 /mnt
mount /dev/sda1 /mnt/boot
grub-install --root-directory=/mnt /dev/sda
grub-install --root-directory=/mnt /dev/sdb

Update your initramfs to include mdraid modules:

echo "mdadm" >> /etc/initramfs-tools/modules
update-initramfs -u

After installation, verify the setup:

mdadm --detail /dev/md0
grub-probe /boot/grub
lsinitramfs /boot/initrd.img-$(uname -r) | grep md

When creating a bootable RAID 1 array, the critical challenge lies in the bootloader installation phase. The GRUB bootloader needs to access the RAID metadata before the kernel and initramfs are loaded, which creates a chicken-and-egg problem.

Here's the optimal partitioning approach for two 500GB drives:

# On /dev/sda:
sgdisk -n 1:0:+512M -t 1:fd00 -c 1:"RAID boot" /dev/sda
sgdisk -n 2:0:+2G -t 2:fd00 -c 2:"RAID swap" /dev/sda
sgdisk -n 3:0:0 -t 3:fd00 -c 3:"RAID root" /dev/sda

# Clone to /dev/sdb:
sgdisk -R /dev/sdb /dev/sda
sgdisk -G /dev/sdb

Create separate arrays for boot and root partitions:

# For /boot (using older metadata format for GRUB compatibility)
mdadm --create /dev/md0 --level=1 --raid-devices=2 \
      --metadata=0.90 /dev/sda1 /dev/sdb1

# For swap
mdadm --create /dev/md1 --level=1 --raid-devices=2 \
      --metadata=1.2 /dev/sda2 /dev/sdb2

# For root
mdadm --create /dev/md2 --level=1 --raid-devices=2 \
      --metadata=1.2 /dev/sda3 /dev/sdb3

Format and mount the partitions:

mkfs.ext4 /dev/md0
mkswap /dev/md1
mkfs.ext4 /dev/md2

mount /dev/md2 /mnt
mkdir /mnt/boot
mount /dev/md0 /mnt/boot

The key to successful GRUB installation is ensuring the boot partition uses metadata version 0.90 and running these commands:

chroot /mnt
grub-install --no-floppy /dev/sda
grub-install --no-floppy /dev/sdb
update-grub

Add these lines to /etc/mdadm/mdadm.conf:

ARRAY /dev/md0 metadata=0.90 UUID=
ARRAY /dev/md1 metadata=1.2 UUID=
ARRAY /dev/md2 metadata=1.2 UUID=

Update your initramfs to include mdadm support:

update-initramfs -u -k all

After reboot, verify with:

cat /proc/mdstat
mdadm --detail /dev/md0
lsblk -o NAME,MAJ:MIN,RM,SIZE,RO,FSTYPE,MOUNTPOINT