Difference between revisions of "Basic usage of Software Raid (MDADM)"

From Pulsed Media Wiki
(Created page with "== Creating and Using Software RAID with mdadm on Debian == Software RAID on Debian is commonly configured using the <code>mdadm</code> utility. This guide provides a step-by...")
 
(Information)
 
Line 14: Line 14:
 
Install the RAID management tool:
 
Install the RAID management tool:
  
<syntaxhighlight lang="bash">
+
 
sudo apt update
+
  sudo apt update
sudo apt install mdadm -y
+
  sudo apt install mdadm -y
</syntaxhighlight>
+
 
  
 
During installation, you may be prompted to configure mdadm to email alerts. You can skip or configure as preferred.
 
During installation, you may be prompted to configure mdadm to email alerts. You can skip or configure as preferred.
Line 25: Line 25:
 
List all attached storage devices:
 
List all attached storage devices:
  
<syntaxhighlight lang="bash">
+
 
lsblk
+
  lsblk
</syntaxhighlight>
+
 
  
 
Or use:
 
Or use:
  
<syntaxhighlight lang="bash">
+
 
sudo fdisk -l
+
  sudo fdisk -l
</syntaxhighlight>
+
 
  
 
Ensure the devices are **unmounted** and **do not contain any important data**.
 
Ensure the devices are **unmounted** and **do not contain any important data**.
Line 43: Line 43:
 
==== RAID 1 (Mirroring, 2 Disks) ====
 
==== RAID 1 (Mirroring, 2 Disks) ====
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
+
  sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
</syntaxhighlight>
+
 
  
 
==== RAID 0 (Striping, 2+ Disks) ====
 
==== RAID 0 (Striping, 2+ Disks) ====
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc
+
  sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdb /dev/sdc
</syntaxhighlight>
+
 
  
 
==== RAID 5 (Striped with Parity, 3+ Disks) ====
 
==== RAID 5 (Striped with Parity, 3+ Disks) ====
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd
+
  sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd
</syntaxhighlight>
+
 
  
 
You may need to confirm with '''yes''' to proceed.
 
You may need to confirm with '''yes''' to proceed.
Line 65: Line 65:
 
Check array status:
 
Check array status:
  
<syntaxhighlight lang="bash">
+
 
cat /proc/mdstat
+
  cat /proc/mdstat
</syntaxhighlight>
+
 
  
 
To view detailed status:
 
To view detailed status:
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --detail /dev/md0
+
  sudo mdadm --detail /dev/md0
</syntaxhighlight>
+
 
  
 
=== Step 5: Create Filesystem on RAID Array ===
 
=== Step 5: Create Filesystem on RAID Array ===
Line 79: Line 79:
 
After creation completes, format the RAID array:
 
After creation completes, format the RAID array:
  
<syntaxhighlight lang="bash">
+
 
sudo mkfs.ext4 /dev/md0
+
  sudo mkfs.ext4 /dev/md0
</syntaxhighlight>
+
 
  
 
=== Step 6: Mount the RAID Array ===
 
=== Step 6: Mount the RAID Array ===
Line 87: Line 87:
 
Create mount point and mount it:
 
Create mount point and mount it:
  
<syntaxhighlight lang="bash">
+
 
sudo mkdir /mnt/raid
+
  sudo mkdir /mnt/raid
sudo mount /dev/md0 /mnt/raid
+
  sudo mount /dev/md0 /mnt/raid
</syntaxhighlight>
+
 
  
 
Optional: Set up ownership or permissions:
 
Optional: Set up ownership or permissions:
  
<syntaxhighlight lang="bash">
+
 
sudo chown user:user /mnt/raid
+
  sudo chown user:user /mnt/raid
</syntaxhighlight>
+
 
  
 
=== Step 7: Save mdadm Configuration ===
 
=== Step 7: Save mdadm Configuration ===
Line 102: Line 102:
 
Save the array details for boot persistence:
 
Save the array details for boot persistence:
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
+
  sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u
+
  sudo update-initramfs -u
</syntaxhighlight>
+
 
  
 
=== Step 8: Automount on Boot ===
 
=== Step 8: Automount on Boot ===
Line 111: Line 111:
 
Add entry to <code>/etc/fstab</code>:
 
Add entry to <code>/etc/fstab</code>:
  
<syntaxhighlight lang="bash">
+
 
sudo nano /etc/fstab
+
  sudo nano /etc/fstab
</syntaxhighlight>
+
 
  
 
Add:
 
Add:
  
<syntaxhighlight lang="text">
+
 
/dev/md0  /mnt/raid  ext4  defaults,nofail  0  0
+
  /dev/md0  /mnt/raid  ext4  defaults,nofail  0  0
</syntaxhighlight>
+
 
  
 
Save and close.
 
Save and close.
Line 127: Line 127:
 
If RAID is not automatically detected:
 
If RAID is not automatically detected:
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --assemble --scan
+
  sudo mdadm --assemble --scan
</syntaxhighlight>
+
 
  
 
=== Optional: Remove or Stop a RAID Array ===
 
=== Optional: Remove or Stop a RAID Array ===
Line 135: Line 135:
 
To stop the array:
 
To stop the array:
  
<syntaxhighlight lang="bash">
+
 
sudo umount /mnt/raid
+
  sudo umount /mnt/raid
sudo mdadm --stop /dev/md0
+
  sudo mdadm --stop /dev/md0
</syntaxhighlight>
+
 
  
 
To remove the array:
 
To remove the array:
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --remove /dev/md0
+
  sudo mdadm --remove /dev/md0
</syntaxhighlight>
+
 
  
 
To zero out superblocks:
 
To zero out superblocks:
  
<syntaxhighlight lang="bash">
+
 
sudo mdadm --zero-superblock /dev/sdb
+
  sudo mdadm --zero-superblock /dev/sdb
sudo mdadm --zero-superblock /dev/sdc
+
  sudo mdadm --zero-superblock /dev/sdc
</syntaxhighlight>
+
 
  
 
=== Summary Table ===
 
=== Summary Table ===

Latest revision as of 14:17, 19 May 2025

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