| |

LFCS 95 ๐Ÿง RAID with mdadm โ€” Managing and Recovering

โฑ๏ธ Estimated read: 9 min | |

An array that is created and forgotten is a liability. Drives fail, sectors go bad, and arrays silently degrade into a state where a second failure means total data loss. Managing an mdadm array is not about reacting to catastropheโ€”it is about routine monitoring, deliberate failure testing, and a recovery procedure that you have practiced before the disk actually dies. The LFCS exam tests the sequence: identify the failed disk, mark it as failed, remove it, add a replacement, and monitor the rebuild.

Key point: The recovery procedure is --fail, --remove, --add. The --fail step is not optional when a disk is still responding but corruptedโ€”it forces the array to stop trusting the device before you remove it.


Why management and recovery exist

The silent degradation problem. When a disk fails in a RAID 1 or RAID 5 array, the array continues to operate. This is the point of RAID. But the array is now degraded: performance is reduced, and a second failure is unrecoverable. If no one is monitoring /proc/mdstat, the degraded state can persist for months until the second disk fails and the data is gone. Monitoring is the first line of defense .

The fail-first rule. A disk that is returning I/O errors but not completely dead will continue to be used by the array unless explicitly marked as failed. The kernel’s md driver may not immediately recognize that the disk is unreliable. mdadm --fail forces the array to stop using the device, allowing a replacement to be added without waiting for the disk to fail completely .

The rebuild risk. When a replacement disk is added, the array begins rebuilding. For RAID 1, this is a simple copy from the surviving mirror. For RAID 5, the array must read all surviving data and parity blocks to reconstruct the lost information. This places heavy I/O load on the remaining disks, increasing the chance of a second failure during the rebuild window. Monitoring the rebuild and adjusting speed limits are part of recovery .

The scrub problem. Even without a disk failure, RAID arrays can develop silent data corruptionโ€”mismatches between mirror copies or parity inconsistencies. A periodic scrub reads all data and verifies consistency. The kernel exposes this through /sys/block/mdX/md/sync_action, where writing check initiates a read-only verification and repair attempts to correct mismatches .

The configuration persistence problem. An array that is not listed in /etc/mdadm.conf may not assemble automatically on boot. After any change to the arrayโ€”adding a disk, replacing a memberโ€”the configuration should be regenerated with mdadm --detail --scan .


a. Monitoring array health

The first command in any management workflow is cat /proc/mdstat. It shows the array device, the RAID level, the member disks with their slot numbers, and the state of each disk. The notation [UU] means both disks are up; [U_] means the second disk has failed. During a rebuild, the progress is shown with a percentage and estimated time .

# Quick status
cat /proc/mdstat

# Detailed view
sudo mdadm --detail /dev/md0

mdadm --detail provides the array UUID, chunk size, RAID level, and the state of each member disk. It is the command to run when /proc/mdstat shows a problem, because it identifies which specific device has failed .

For automated monitoring, mdadm --monitor can run in daemon mode and send alerts when events occur. The --oneshot flag checks the array once and exits, which is suitable for cron jobs .

# Check once and report degraded arrays
sudo mdadm --monitor --scan --oneshot

b. Replacing a failed disk

The recovery sequence has four steps. First, mark the disk as failed if the kernel has not already done so. Second, remove the failed disk from the array. Third, physically replace the disk (or prepare the new device). Fourth, add the new disk to the array and monitor the rebuild.

# Mark as failed (required if the disk still responds)
sudo mdadm /dev/md0 --fail /dev/sdc

# Remove from array
sudo mdadm /dev/md0 --remove /dev/sdc

# Add replacement (after physical replacement or if using a spare)
sudo mdadm /dev/md0 --add /dev/sdd

The --add command triggers the rebuild automatically. The array continues to serve data during the rebuild, but performance is reduced. The progress can be monitored with cat /proc/mdstat or watch -n 5 cat /proc/mdstat .

The rebuild speed can be adjusted through the kernel parameters /proc/sys/dev/raid/speed_limit_min and /proc/sys/dev/raid/speed_limit_max. Increasing the minimum speed accelerates the rebuild at the cost of foreground I/O performance .


c. Scrubbing and verifying integrity

A scrub reads every block in the array and verifies that mirrors match or that parity is consistent. It does not require unmounting the array. The kernel exposes this through sysfs.

# Start a read-only check
echo check | sudo tee /sys/block/md0/md/sync_action

# Monitor progress
cat /proc/mdstat

# After completion, check for mismatches
cat /sys/block/md0/md/mismatch_cnt

# If mismatches are found, repair them
echo repair | sudo tee /sys/block/md0/md/sync_action

A mismatch_cnt of zero means the array is consistent. A non-zero value indicates that mirror copies diverged or parity did not match the data. The repair action attempts to correct these inconsistencies . Scrubbing is not a substitute for backupsโ€”it detects and fixes consistency errors, not user mistakes or malware.


Complete Example Session

# ============================================
# PART 1: CHECK ARRAY STATUS
# ============================================

cat /proc/mdstat
# md0 : active raid1 sdc[1] sdb[0]
#       976630464 blocks super 1.2 [2/2] [UU]
# ============================================
# PART 2: IDENTIFY A FAILED DISK
# ============================================

# Simulate a failure (for testing)
sudo mdadm /dev/md0 --fail /dev/sdc

# Verify the array is degraded
cat /proc/mdstat
# [2/1] [U_]
# ============================================
# PART 3: REMOVE THE FAILED DISK
# ============================================

sudo mdadm /dev/md0 --remove /dev/sdc
# The slot is now empty.
# ============================================
# PART 4: ADD A REPLACEMENT
# ============================================

# Physically replace the disk, then:
sudo mdadm /dev/md0 --add /dev/sdd
# Rebuild starts automatically.
# ============================================
# PART 5: MONITOR REBUILD
# ============================================

watch -n 5 cat /proc/mdstat
# [=>...] recovery = 15% ...
# ============================================
# PART 6: ADJUST REBUILD SPEED
# ============================================

# Increase minimum speed for faster rebuild
echo 50000 | sudo tee /proc/sys/dev/raid/speed_limit_min
# ============================================
# PART 7: SCRUB THE ARRAY
# ============================================

echo check | sudo tee /sys/block/md0/md/sync_action
# Wait for completion, then:
cat /sys/block/md0/md/mismatch_cnt
# ============================================
# PART 8: REPAIR MISMATCHES
# ============================================

# If mismatch_cnt > 0:
echo repair | sudo tee /sys/block/md0/md/sync_action
# ============================================
# PART 9: PERSIST CONFIGURATION
# ============================================

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

The nine parts trace the management lifecycle: status check, simulated failure, removal, replacement, rebuild monitoring, speed tuning, scrubbing, repair, and configuration persistence.


Quick Reference

Management Commands

CommandPurpose
mdadm --detail /dev/md0Full array details
mdadm /dev/md0 --fail /dev/sdXMark disk as failed
mdadm /dev/md0 --remove /dev/sdXRemove failed disk
mdadm /dev/md0 --add /dev/sdXAdd replacement, start rebuild
cat /proc/mdstatQuick status
mdadm --monitor --scan --oneshotSingle check for cron

Scrubbing Commands

CommandPurpose
echo check > /sys/block/md0/md/sync_actionRead-only verification
echo repair > /sys/block/md0/md/sync_actionCorrect mismatches
cat /sys/block/md0/md/mismatch_cntCount inconsistencies

Rebuild Speed

ParameterDefaultPurpose
speed_limit_min1000 KB/sMinimum rebuild rate
speed_limit_max200000 KB/sMaximum rebuild rate

Best Practices

โœ… Do This:

sudo mdadm /dev/md0 --fail /dev/sdc
sudo mdadm /dev/md0 --remove /dev/sdc
  • โœ… Fail first, then remove
watch -n 5 cat /proc/mdstat
  • โœ… Monitor rebuild progress
echo check | sudo tee /sys/block/md0/md/sync_action
  • โœ… Periodic scrub for silent corruption

โŒ Don’t Do This:

sudo mdadm /dev/md0 --remove /dev/sdc
  • โŒ Remove without failing if the disk still responds
# Ignore degraded array
  • โŒ A degraded array is one failure from total loss
sudo mdadm --detail --scan > /etc/mdadm.conf
  • โŒ Overwrites existing entries; use >>

Common Pitfalls

PitfallWhy It HappensFix
Remove failsDisk still active in array--fail first, then --remove
Array not assembled on bootmdadm.conf stalemdadm --detail --scan >> /etc/mdadm.conf
Rebuild too slowDefault speed limitIncrease speed_limit_min
Silent corruption undetectedNo scrub scheduledPeriodic echo check > sync_action
Second disk fails during rebuildRebuild stress on remaining disksMonitor; consider RAID 6 or 10

Real-World Examples

1. Check Status

cat /proc/mdstat

2. Detailed View

sudo mdadm --detail /dev/md0

3. Mark Failed

sudo mdadm /dev/md0 --fail /dev/sdc

4. Remove Disk

sudo mdadm /dev/md0 --remove /dev/sdc

5. Add Replacement

sudo mdadm /dev/md0 --add /dev/sdd

6. Monitor Rebuild

watch -n 5 cat /proc/mdstat

7. Start Scrub

echo check | sudo tee /sys/block/md0/md/sync_action

8. Repair Mismatches

echo repair | sudo tee /sys/block/md0/md/sync_action

9. Check Mismatches

cat /sys/block/md0/md/mismatch_cnt

10. Persist Config

sudo mdadm --detail --scan >> /etc/mdadm.conf

Visual

Recovery Sequence

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Disk failure detected                       โ”‚
โ”‚  /proc/mdstat shows [U_]                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  mdadm /dev/md0 --fail /dev/sdc              โ”‚
โ”‚  (if disk still responds)                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  mdadm /dev/md0 --remove /dev/sdc            โ”‚
โ”‚  (slot becomes empty)                        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Physically replace disk                     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  mdadm /dev/md0 --add /dev/sdd               โ”‚
โ”‚  (rebuild starts automatically)              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Monitor rebuild                             โ”‚
โ”‚  watch -n 5 cat /proc/mdstat                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

The sequence is deliberate. Failing first prevents the array from using a device that may return corrupted data.

Scrub Decision

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Periodic check (cron or manual)             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  echo check > /sys/block/md0/md/sync_action  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  Wait for completion                         โ”‚
โ”‚  cat /proc/mdstat shows progress             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                   โ”‚
                   โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  mismatch_cnt > 0?                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
           โ”‚ YES                   โ”‚ NO
           โ–ผ                       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  echo repair         โ”‚  โ”‚  Array is consistent โ”‚
โ”‚  Correct mismatches  โ”‚  โ”‚  No action needed    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

A scrub does not require unmounting. It runs in the background and reports mismatches when complete.


Summary

ItemValue
Status checkcat /proc/mdstat
Detailed viewmdadm --detail /dev/md0
Mark failedmdadm /dev/md0 --fail /dev/sdX
Remove diskmdadm /dev/md0 --remove /dev/sdX
Add replacementmdadm /dev/md0 --add /dev/sdX
Start scrubecho check > /sys/block/md0/md/sync_action
Repair mismatchesecho repair > /sys/block/md0/md/sync_action
Persist configmdadm --detail --scan >> /etc/mdadm.conf

Key takeaways:

  • Monitoring is the first responsibility. A degraded array is one failure from data loss. Check /proc/mdstat regularly and configure mdadm --monitor for alerts .
  • Fail before removing. A disk that is responding but corrupted will continue to be used unless explicitly failed. mdadm --fail forces the array to stop trusting it .
  • The rebuild starts automatically. After --add, the kernel begins reconstruction. The array remains usable, but performance is reduced until the rebuild completes .
  • Scrubbing detects silent corruption. Periodic checks via sync_action find mismatches that would otherwise go unnoticed until a disk fails and the parity cannot reconstruct the data .
  • Configuration must be persisted. After any array change, regenerate /etc/mdadm.conf with mdadm --detail --scan to ensure the array assembles on boot .

Remember: Managing an mdadm array is a discipline of verification. Check the status, fail the disk before removing it, monitor the rebuild, and scrub periodically. The commands are simple, but the sequence matters. An array that is not monitored is an array that will fail silently.

Was this helpful?


Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!