LFCS 94 ๐ง RAID with mdadm โ Creating Arrays
The previous chapter established the theory of RAID levels. This chapter translates that theory into practice. mdadm is the userspace tool that communicates with the Linux kernel’s multiple device (md) driver to assemble physical block devices into a logical RAID array. The command syntax is denseโa single invocation can specify the array name, RAID level, number of devices, metadata format, chunk size, and spare disks. On the LFCS exam, you must construct this command correctly, create a filesystem on the resulting array, and ensure it persists across reboots.
Key point: The --create command is a single atomic operation that configures the array, writes metadata to every member disk, and starts the initial synchronization. If the command is malformed, the disks may be left with partial metadata that must be cleared before retrying.
Why mdadm exists
The kernel interface problem. The Linux kernel implements software RAID through the md driver. The driver accepts configuration through ioctl calls and sysfs, but these interfaces are not designed for human use. mdadm translates a readable command line into the low-level operations the driver requires. Without it, creating a RAID array would require writing a C program that calls ioctl on /dev/md* .
The metadata problem. Every disk in a RAID array carries a superblockโa small block of metadata that identifies the array UUID, the disk’s role, the RAID level, and the chunk size. When the system boots, the kernel reads these superblocks to assemble the array automatically. mdadm writes this metadata during creation and can update it when the array configuration changes. The metadata format matters: version 1.2 (the modern default) stores the superblock 4 KiB from the start of the device, while version 0.90 (the legacy format) stores it at the end and limits arrays to 2 TB per device .
The naming problem. The array device is named /dev/mdX or /dev/md/name. The name is not arbitraryโit is stored in the metadata and used by the kernel to identify the array during assembly. If two arrays have the same name, the assembly process may attach the wrong disks. mdadm provides the --name option to set a persistent name, but the default naming scheme (/dev/md0, /dev/md1) is usually sufficient for exam scenarios .
The sync problem. When a RAID 1 or RAID 5 array is created, the member disks may contain different data. The kernel begins a background synchronization (resync) that copies the data from one disk to the others or computes the initial parity. During this process, the array is functional but degradedโperformance is lower, and a disk failure at this stage is unrecoverable. mdadm --detail and /proc/mdstat show the sync progress .
The trade-off. mdadm is flexible but unforgiving. A typo in the device list can include the wrong disk and destroy existing data. The --create command does not prompt for confirmation if the disks already contain a filesystem signature. The safe workflow is to verify device names with lsblk and clear existing signatures with wipefs before creating the array.
a. Creating a RAID 1 array
RAID 1 is the simplest array to create and the most common exam scenario. It requires exactly two devices (or more, if you want additional mirrors) of equal or near-equal size. The command specifies the array device, the RAID level, the number of devices, and the member disks.
# Clear any existing filesystem or RAID signatures
sudo wipefs -a /dev/sdb
sudo wipefs -a /dev/sdc
# Create the RAID 1 array
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
The --level=1 (or -l 1) specifies mirroring. The --raid-devices=2 (or -n 2) declares that the array uses two active disks. The member disks follow as positional arguments. After the command completes, mdadm writes the superblock to both disks and the kernel begins the initial resync .
The resync can take minutes or hours depending on disk size. The array is usable during this time, but cat /proc/mdstat shows the progress:
md0 : active raid1 sdc[1] sdb[0]
976630464 blocks super 1.2 [2/2] [UU]
[>....................] resync = 0.5% (5000000/976630464) finish=120.0min speed=135000K/sec
The [UU] notation indicates that both disks are up. If one disk fails, it becomes [U_] and the array is degraded .
b. Creating a RAID 5 array
RAID 5 requires at least three devices. The command is structurally identical to RAID 1, but the level changes and the device count increases.
# Create a three-disk RAID 5 array
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd
The initial parity computation is more intensive than RAID 1 mirroring. The resync process reads every block and computes the parity, which can take longer on large drives. The array is functional during this period, but performance is degraded .
RAID 5 also supports spare disks. A spare is not part of the active array but is automatically used for reconstruction if an active disk fails. The --spare-devices (or -x) option adds spares at creation time:
# RAID 5 with three active disks and one spare
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 --spare-devices=1 /dev/sdb /dev/sdc /dev/sdd /dev/sde
The total number of devices on the command line must equal the number of active devices plus the number of spares. In this example, four disks are listed: three active, one spare .
c. Metadata format and chunk size
The default metadata format for new arrays is version 1.2. This format stores the superblock 4 KiB from the start of the device and supports arrays larger than 2 TB. The legacy 0.90 format is limited to 2 TB per device and is only needed for compatibility with very old bootloaders. For exam purposes, the default is correct unless the question specifically requires 0.90 .
The chunk size matters for RAID 0, 5, 6, and 10. It defines the granularity of data distribution across disks. The default is 512 KiB. For large sequential workloads, a larger chunk size reduces overhead. For random workloads, a smaller chunk size distributes I/O more evenly. The --chunk (or -c) option sets it in kilobytes:
# RAID 5 with 64 KiB chunk size
sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 --chunk=64 /dev/sdb /dev/sdc /dev/sdd
The chunk size is recorded in the metadata and cannot be changed without recreating the array. For exam scenarios that do not specify a chunk size, the default is acceptable .
Complete Example Session
# ============================================
# PART 1: IDENTIFY AVAILABLE DISKS
# ============================================
# List block devices with sizes and types.
lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINT
# sdb and sdc are available, no filesystem.
# ============================================
# PART 2: CLEAR EXISTING SIGNATURES
# ============================================
# Remove any filesystem or RAID metadata.
sudo wipefs -a /dev/sdb
sudo wipefs -a /dev/sdc
# ============================================
# PART 3: CREATE RAID 1 ARRAY
# ============================================
# Two-disk mirror.
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
# Output: mdadm: array /dev/md0 started.
# ============================================
# PART 4: CHECK SYNC PROGRESS
# ============================================
# The array syncs in the background.
cat /proc/mdstat
# md0 : active raid1 sdc[1] sdb[0]
# [UU]
# [=>...] resync = 10% ...
# ============================================
# PART 5: CREATE FILESYSTEM
# ============================================
# Format the array with ext4.
sudo mkfs.ext4 /dev/md0
# ============================================
# PART 6: MOUNT AND VERIFY
# ============================================
sudo mkdir -p /mnt/raid1
sudo mount /dev/md0 /mnt/raid1
df -h /mnt/raid1
# ============================================
# PART 7: SAVE CONFIGURATION
# ============================================
# Persist array definition for boot assembly.
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm.conf
# ============================================
# PART 8: CREATE RAID 5 WITH SPARE
# ============================================
# Three active disks, one spare.
sudo mdadm --create /dev/md1 --level=5 --raid-devices=3 --spare-devices=1 /dev/sdd /dev/sde /dev/sdf /dev/sdg
# ============================================
# PART 9: DETAILED ARRAY VIEW
# ============================================
# Show array state, UUID, chunk size, and disk roles.
sudo mdadm --detail /dev/md0
The nine parts trace the creation workflow: identifying disks, clearing signatures, creating the array, monitoring sync, formatting, mounting, persisting configuration, creating a more complex array with a spare, and inspecting the result.
Quick Reference
mdadm Create Flags
| Flag | Purpose |
|---|---|
--create or -C | Create a new array |
--level or -l | RAID level (0, 1, 5, 6, 10) |
--raid-devices or -n | Number of active disks |
--spare-devices or -x | Number of spare disks |
--chunk or -c | Chunk size in KiB (default 512) |
--metadata or -e | Metadata format (default 1.2) |
--name | Persistent array name |
Minimum Disks by Level
| Level | Minimum Active | Spares Allowed |
|---|---|---|
| 0 | 2 | No |
| 1 | 2 | Yes |
| 5 | 3 | Yes |
| 6 | 4 | Yes |
| 10 | 4 | Yes |
Verification Commands
| Command | Purpose |
|---|---|
cat /proc/mdstat | Quick status |
mdadm --detail /dev/mdX | Full details |
lsblk | Block device tree |
Best Practices
โ Do This:
sudo wipefs -a /dev/sdb
sudo wipefs -a /dev/sdc
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb /dev/sdc
- โ Clear signatures, then create
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm.conf
- โ Persist configuration for boot assembly
sudo mkfs.ext4 /dev/md0
- โ Create a filesystem after the array is active
โ Don’t Do This:
sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sda /dev/sdb
- โ Never use a disk containing existing data without verifying
sudo mdadm --create /dev/md0 --level=5 --raid-devices=2 /dev/sdb /dev/sdc
- โ RAID 5 requires at least three active disks
sudo mount /dev/md0 /mnt
- โ Mount only after the filesystem is created
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Device already in use | Disk has existing signature | wipefs -a before creation |
| Array not assembled on boot | mdadm.conf not updated | mdadm --detail --scan >> /etc/mdadm.conf |
| RAID 5 creation fails | Fewer than 3 devices | Use at least 3 for level 5 |
| Wrong device names | Typo in device list | Verify with lsblk before running |
| Sync takes too long | Large disks | Monitor with /proc/mdstat; array is usable during sync |
Real-World Examples
1. RAID 1 Mirror
sudo mdadm -C /dev/md0 -l 1 -n 2 /dev/sdb /dev/sdc
2. RAID 5 with Spare
sudo mdadm -C /dev/md0 -l 5 -n 3 -x 1 /dev/sdb /dev/sdc /dev/sdd /dev/sde
3. RAID 6 Array
sudo mdadm -C /dev/md0 -l 6 -n 4 /dev/sdb /dev/sdc /dev/sdd /dev/sde
4. RAID 10 Array
sudo mdadm -C /dev/md0 -l 10 -n 4 /dev/sdb /dev/sdc /dev/sdd /dev/sde
5. RAID 0 Stripe
sudo mdadm -C /dev/md0 -l 0 -n 2 /dev/sdb /dev/sdc
6. Custom Chunk Size
sudo mdadm -C /dev/md0 -l 5 -n 3 -c 64 /dev/sdb /dev/sdc /dev/sdd
7. Save Configuration
sudo mdadm --detail --scan >> /etc/mdadm.conf
8. Check Sync
watch cat /proc/mdstat
9. Format Array
sudo mkfs.xfs /dev/md0
10. Mount Array
sudo mount /dev/md0 /mnt/raid
Visual
RAID 1 Creation Flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ wipefs -a /dev/sdb /dev/sdc โ
โ (clear existing signatures) โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mdadm --create /dev/md0 --level=1 โ
โ --raid-devices=2 /dev/sdb /dev/sdc โ
โ โ
โ 1. Write superblock to both disks โ
โ 2. Start kernel md driver โ
โ 3. Begin background resync โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ /proc/mdstat โ
โ md0 : active raid1 sdc[1] sdb[0] โ
โ [UU] โ
โโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ mkfs.ext4 /dev/md0 โ
โ mount /dev/md0 /mnt/raid1 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The array is usable immediately after creation, but the resync runs in the background.
Device Roles in RAID 5
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โ /dev/sdb โ โ /dev/sdc โ โ /dev/sdd โ
โ Disk 0 โ โ Disk 1 โ โ Disk 2 โ
โ โ โ โ โ โ
โ Active โ โ Active โ โ Active โ
โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโ
โผ
โโโโโโโโโโโโโโโโ
โ /dev/md0 โ
โ RAID 5 โ
โ (N-1) cap โ
โโโโโโโโโโโโโโโโ
For RAID 5, the minimum is three active disks. A spare is optional but recommended.
Summary
| Item | Value |
|---|---|
| Create command | mdadm --create /dev/mdX --level=L --raid-devices=N disks |
| RAID 1 minimum | 2 devices |
| RAID 5 minimum | 3 devices |
| RAID 6 minimum | 4 devices |
| RAID 10 minimum | 4 devices |
| Default metadata | 1.2 |
| Default chunk | 512 KiB |
| Persist config | mdadm --detail --scan >> /etc/mdadm.conf |
| Status check | cat /proc/mdstat |
Key takeaways:
mdadm --createis atomic. It configures the array, writes metadata, and starts the sync in one command. A malformed command may leave disks in an inconsistent state .- RAID levels have minimum disk requirements. RAID 1 requires two, RAID 5 requires three, RAID 6 and RAID 10 require four. The
--raid-devicesvalue must match the number of active disks . - Spares are declared separately. The
--spare-devicesoption adds disks that are not active but can replace failed members. The total disk count is active plus spare . - The array is usable during initial sync. Performance is degraded, but the filesystem can be created and mounted immediately after
--createreturns . - Configuration must be persisted. Without
/etc/mdadm.conf, the array may not assemble automatically on boot.mdadm --detail --scangenerates the correct entry. - Metadata format matters for large disks. Version 1.2 supports arrays larger than 2 TB per device. Version 0.90 is legacy and should be avoided unless required .
Remember: Creating a RAID array with mdadm is a single command that does three things: writes metadata to each disk, starts the kernel driver, and begins the initial synchronization. The command must be correct on the first attemptโthere is no confirmation prompt, and an incorrect device list can overwrite existing data. Verify device names with lsblk, clear signatures with wipefs, and persist the configuration with mdadm --detail --scan.
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!