BLOG POSTS
How to Create RAID Arrays with mdadm on Ubuntu

How to Create RAID Arrays with mdadm on Ubuntu

Setting up RAID arrays on Ubuntu is a crucial skill for anyone managing server infrastructure or looking to improve storage reliability and performance. Whether you’re a developer building high-availability applications or a sysadmin protecting against hardware failures, understanding RAID configuration with mdadm can save you from catastrophic data loss and performance bottlenecks. This guide will walk you through creating various RAID levels using mdadm, troubleshooting common issues, and optimizing for real-world scenarios.

How RAID and mdadm Work

RAID (Redundant Array of Independent Disks) combines multiple physical drives into logical storage units for redundancy, performance, or both. Linux’s mdadm (multiple device administration) is the go-to tool for software RAID management, offering flexibility without expensive hardware controllers.

mdadm operates at the block device level, creating virtual devices (typically /dev/md0, /dev/md1, etc.) that your filesystem sees as single drives. The kernel’s MD (Multiple Device) driver handles the actual I/O distribution and redundancy calculations in real-time.

RAID Level Min Drives Redundancy Performance Storage Efficiency Best Use Case
RAID 0 2 None Excellent read/write 100% High-performance temp storage
RAID 1 2 1 drive failure Good read, normal write 50% Critical data, boot drives
RAID 5 3 1 drive failure Good read, slower write 67-90% General purpose storage
RAID 6 4 2 drive failures Good read, slow write 50-88% Long-term archival
RAID 10 4 1 drive per mirror Excellent all-around 50% Database servers

Step-by-Step RAID Setup Guide

Before starting, install mdadm and prepare your drives. Never use drives with existing data unless you want to lose it.

sudo apt update
sudo apt install mdadm -y

# Check available drives
lsblk
fdisk -l

Creating RAID 1 (Mirror)

RAID 1 is perfect for system drives and critical data. Here’s how to set up a two-drive mirror:

# Wipe existing partition tables (DESTRUCTIVE!)
sudo wipefs -a /dev/sdb /dev/sdc

# Create the RAID 1 array
sudo mdadm --create --verbose /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

# Check array status
cat /proc/mdstat
sudo mdadm --detail /dev/md0

Creating RAID 5 (Striped with Parity)

RAID 5 offers good balance between performance, redundancy, and storage efficiency:

# Create RAID 5 with 3 drives
sudo mdadm --create --verbose /dev/md1 --level=5 --raid-devices=3 /dev/sdd /dev/sde /dev/sdf

# Monitor build progress
watch cat /proc/mdstat

Creating RAID 10 (Striped Mirrors)

For database servers and high-performance applications:

# RAID 10 requires minimum 4 drives
sudo mdadm --create --verbose /dev/md2 --level=10 --raid-devices=4 /dev/sdg /dev/sdh /dev/sdi /dev/sdj

Making RAID Arrays Persistent

Without proper configuration, your arrays won’t survive reboots:

# Save RAID configuration
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf

# Update initramfs
sudo update-initramfs -u

# Create filesystem and mount
sudo mkfs.ext4 /dev/md0
sudo mkdir /mnt/raid1
sudo mount /dev/md0 /mnt/raid1

# Add to fstab for automatic mounting
echo '/dev/md0 /mnt/raid1 ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab

Real-World Examples and Use Cases

Web Server Setup

For a typical web server, you might want RAID 1 for the OS and RAID 5 for data:

# OS drives (RAID 1)
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda /dev/sdb

# Data drives (RAID 5)
sudo mdadm --create /dev/md1 --level=5 --raid-devices=3 /dev/sdc /dev/sdd /dev/sde

# Format and mount
sudo mkfs.ext4 /dev/md0
sudo mkfs.ext4 /dev/md1
sudo mount /dev/md0 /
sudo mount /dev/md1 /var/www

Database Server Configuration

Database workloads benefit from RAID 10’s performance characteristics:

# High-performance RAID 10 for database files
sudo mdadm --create /dev/md0 --level=10 --raid-devices=4 /dev/nvme0n1 /dev/nvme1n1 /dev/nvme2n1 /dev/nvme3n1

# Optimize for database workload
sudo mkfs.ext4 -E stride=128,stripe-width=256 /dev/md0
sudo mount -o noatime,nodiratime /dev/md0 /var/lib/mysql

Backup Server Setup

For backup servers, RAID 6 provides maximum protection:

# RAID 6 for backup storage
sudo mdadm --create /dev/md0 --level=6 --raid-devices=6 /dev/sd{b,c,d,e,f,g}

# Use XFS for large files
sudo mkfs.xfs /dev/md0
sudo mount -o noatime /dev/md0 /backup

Monitoring and Maintenance

Regular monitoring prevents nasty surprises. Set up automated checks and notifications:

# Check array health
sudo mdadm --detail /dev/md0

# Monitor all arrays
cat /proc/mdstat

# Set up email notifications
echo 'MAILADDR admin@yourdomain.com' | sudo tee -a /etc/mdadm/mdadm.conf

# Enable monitoring daemon
sudo systemctl enable mdmonitor
sudo systemctl start mdmonitor

# Manual consistency check (run during off-peak hours)
echo check | sudo tee /sys/block/md0/md/sync_action

Performance Optimization

Default settings aren’t always optimal. Here are some tuning parameters:

# Increase stripe cache size for RAID 5/6
echo 8192 | sudo tee /sys/block/md0/md/stripe_cache_size

# Adjust read-ahead for sequential workloads
sudo blockdev --setra 65536 /dev/md0

# Set appropriate scheduler
echo deadline | sudo tee /sys/block/md0/queue/scheduler

# Make permanent by adding to /etc/rc.local or systemd service

Common Issues and Troubleshooting

Drive Failure Simulation and Recovery

Testing failure scenarios before they happen is crucial:

# Simulate drive failure
sudo mdadm --manage /dev/md0 --fail /dev/sdb

# Check degraded status
cat /proc/mdstat

# Remove failed drive
sudo mdadm --manage /dev/md0 --remove /dev/sdb

# Add replacement drive
sudo mdadm --manage /dev/md0 --add /dev/sdx

# Monitor rebuild
watch cat /proc/mdstat

Assembly Issues After Reboot

Sometimes arrays don’t come up automatically:

# Manually assemble array
sudo mdadm --assemble /dev/md0 /dev/sdb /dev/sdc

# Scan and assemble all arrays
sudo mdadm --assemble --scan

# Force assembly (use carefully)
sudo mdadm --assemble --force /dev/md0 /dev/sdb /dev/sdc

Superblock Issues

Corrupted superblocks can prevent array assembly:

# Check superblock information
sudo mdadm --examine /dev/sdb

# Zero superblock (DESTRUCTIVE - backup first!)
sudo mdadm --zero-superblock /dev/sdb

# Create array with specific metadata version
sudo mdadm --create /dev/md0 --metadata=1.2 --level=1 --raid-devices=2 /dev/sdb /dev/sdc

Best Practices and Security Considerations

  • Always use drives from different manufacturers or batches to avoid simultaneous failures
  • Set up monitoring and alerting before deploying to production
  • Test backup and recovery procedures regularly
  • Keep spare drives on hand for quick replacements
  • Use UPS systems to prevent corruption during power failures
  • Consider encryption with LUKS for sensitive data
  • Document your RAID configuration and recovery procedures
  • Monitor drive health with smartmontools
# Enable SMART monitoring
sudo apt install smartmontools
sudo smartctl -a /dev/sdb
sudo smartctl -t short /dev/sdb

Comparing mdadm with Hardware RAID

Feature Software RAID (mdadm) Hardware RAID
Cost Free $200-$2000+
Performance Excellent with modern CPUs Varies by controller
Flexibility High – any combination Limited by controller
Portability Move drives to any Linux system Tied to specific controller
Monitoring Built into Linux tools Vendor-specific utilities
CPU Usage 2-5% for parity calculations Minimal

Advanced Configuration Tips

For specialized workloads, consider these advanced options:

# Create RAID with specific chunk size for databases
sudo mdadm --create /dev/md0 --level=10 --chunk=64 --raid-devices=4 /dev/sd{b,c,d,e}

# Use write-intent bitmap for faster recovery
sudo mdadm --grow /dev/md0 --bitmap=internal

# Hot spare configuration  
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sd{b,c,d,e}

# Growing arrays online
sudo mdadm --grow /dev/md0 --raid-devices=4 --add /dev/sdf

RAID with mdadm provides enterprise-level storage reliability without the hardware costs. The key is understanding your workload requirements and monitoring your arrays proactively. While software RAID does consume some CPU resources, modern processors handle this easily, and the flexibility and cost savings usually outweigh the minimal performance impact.

For more detailed information, check the official Linux RAID documentation and the mdadm manual pages.



This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.

This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.

Leave a reply

Your email address will not be published. Required fields are marked