Basic usage of Software Raid (MDADM)

From Pulsed Media Wiki
Revision as of 14:17, 19 May 2025 by Gallogeta (talk | contribs) (Information)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Creating and Using Software RAID with mdadm on Debian

Software RAID on Debian is commonly configured using the mdadm utility. This guide provides a step-by-step approach for creating, configuring, and managing a basic RAID array (RAID 0, RAID 1, or RAID 5), suitable for both virtual and dedicated servers.

Prerequisites

  • Two or more storage devices (e.g., /dev/sdb, /dev/sdc)
  • Root or sudo privileges
  • Debian 10, 11, 12 or Ubuntu derivative
  • Non-mounted and unformatted target disks

Step 1: Install mdadm

Install the RAID management tool:


 sudo apt update
 sudo apt install mdadm -y


During installation, you may be prompted to configure mdadm to email alerts. You can skip or configure as preferred.

Step 2: Identify Available Disks

List all attached storage devices:


 lsblk


Or use:


 sudo fdisk -l


Ensure the devices are **unmounted** and **do not contain any important data**.

Step 3: Create the RAID Array

Examples:

RAID 1 (Mirroring, 2 Disks)

 sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc


RAID 0 (Striping, 2+ Disks)

 sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc


RAID 5 (Striped with Parity, 3+ Disks)

 sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd


You may need to confirm with yes to proceed.

Step 4: Monitor the RAID Creation

Check array status:


 cat /proc/mdstat


To view detailed status:


 sudo mdadm --detail /dev/md0


Step 5: Create Filesystem on RAID Array

After creation completes, format the RAID array:


 sudo mkfs.ext4 /dev/md0


Step 6: Mount the RAID Array

Create mount point and mount it:


 sudo mkdir /mnt/raid
 sudo mount /dev/md0 /mnt/raid


Optional: Set up ownership or permissions:


 sudo chown user:user /mnt/raid


Step 7: Save mdadm Configuration

Save the array details for boot persistence:


 sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
 sudo update-initramfs -u


Step 8: Automount on Boot

Add entry to /etc/fstab:


 sudo nano /etc/fstab


Add:


 /dev/md0   /mnt/raid   ext4   defaults,nofail   0   0


Save and close.

Optional: Assemble RAID After Reboot

If RAID is not automatically detected:


 sudo mdadm --assemble --scan


Optional: Remove or Stop a RAID Array

To stop the array:


 sudo umount /mnt/raid
 sudo mdadm --stop /dev/md0


To remove the array:


 sudo mdadm --remove /dev/md0


To zero out superblocks:


 sudo mdadm --zero-superblock /dev/sdb
 sudo mdadm --zero-superblock /dev/sdc


Summary Table

Task Command Description
Install mdadm apt install mdadm Install RAID tool
Create RAID 1 mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdX /dev/sdY Create mirrored array
Monitor RAID cat /proc/mdstat View sync/progress
Format array mkfs.ext4 /dev/md0 Prepare filesystem
Mount array mount /dev/md0 /mnt/raid Use RAID volume
Auto-mount /etc/fstab Persistent configuration
Save config mdadm --detail --scan >> /etc/mdadm/mdadm.conf Boot-time detection

See Also