Linux Software RAID

Image by Thomas Ulrich on Pixabay

Image by Thomas Ulrich on Pixabay

RAID (Redundant Array of Inexpensive Disks) is a fantastic method of ensuring that your precious files are stored in a way that can survive a disc failure. There are hardware alternatives available that are faster and perhaps a bit less error-prone than software RAID, but in turn, they are a lot more expensive.

In this article, I will provide a step-by-step guide on how to create software RAID5 for your Linux box using MDadm.

Partitioning

Creating a logical volume on the disks

In this article, we will be using three virtual hard drives that have been connected to a VM (Virtual Machine). Each drive has a storage capacity of 5 GB. When the RAID5 is created we will have a logical drive that can store 10GB. This is because there will be a parity block spread across the drives which will reduce the overall storage capacity.

RAID5 requires a minimum of 3 disks but can handle one failed disk regardless of big the array is. In an 8 disk RAID5, the storage capacity will be equal to 7 disks in the logical volume.

RAID6 is very similar to RAID5 but requires a minimum of 4 disks. RAID6 can handle the failure of two disks at the same time. Because of more parity blocks, the usable storage capacity of an 8 disk RAID6 will equal 6 disks in the logical volume.

To check what device names the OS kernel gave our drives during startup, we can run lsblk. This will list block devices registered on the host.

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   80G  0 disk 
├─sda1   8:1    0   79G  0 part /
├─sda2   8:2    0    1K  0 part 
└─sda5   8:5    0  975M  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
sdc      8:32   0    5G  0 disk 
sdd      8:48   0    5G  0 disk 
sr0     11:0    1 1024M  0 rom

As we can read from the terminal dump above, the three drives got assigned the device names. sdb, sdc, and sdd.

The first step is to create a partition on the drives that can be used to create the RAID. This can be done with the fdisk utility

sudo fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.38).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xdd61b6a9.

Command (m for help):

Once the interactive fdisk utility has started we can type n, and press enter to create a new partition. Accept the default values for partition type, partition number, and first- and last sector to create a portion that spans the entire disk.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-10485759, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-10485759, default 10485759): 

Created a new partition 1 of type 'Linux' and of size 5 GiB.

Command (m for help):

As you can read from the terminal output above the partition was successfully defined. Next, the partition type must be defined. This is done by typing t and pressing enter.

Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'.

Command (m for help):

This defined the partition to be of a RAID-friendly variant. To complete the partitioning process write the changes to the disk by typing w, and pressing enter.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

This will write the partition table to disk, and take you back to your regular shell. To verify that the partition was created, lsblk can be run again.

lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda      8:0    0   80G  0 disk 
├─sda1   8:1    0   79G  0 part /
├─sda2   8:2    0    1K  0 part 
└─sda5   8:5    0  975M  0 part [SWAP]
sdb      8:16   0    5G  0 disk 
└─sdb1   8:17   0    5G  0 part 
sdc      8:32   0    5G  0 disk 
sdd      8:48   0    5G  0 disk 
sr0     11:0    1 1024M  0 rom

As we can read from the terminal dump above the partition was successfully created.

To prepare the last two disks, run fdisk and repeat the process on them as well.

sudo fdisk /dev/sdc
sudo fdisk /dev/sdd

Create RAID

Combining disks into a Redundant Array

To create a logical RAID volume the MDadm utility has to be used. If you don't have it installed on your box, install it before continuing.

sudo apt update && sudo apt upgrade
sudo apt install mdadm -y

MDadm is a utility to create Mirrored Devices and is perhaps the easiest way to create software RAID on Linux hosts.

sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sd[bcd]1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.

This created the software RAID5 device /dev/md0. To verify the creation of the RAID5, the fdisk utility can be used.

sudo fdisk -l /dev/md0
Disk /dev/md0: 9.99 GiB, 10724835328 bytes, 20946944 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 524288 bytes / 1048576 bytes

Format

Creating a filesystem on the RAID

Now that we have a functional RAID it must be formatted with a filesystem. In this example, we will use the standard ext4 filesystem.

sudo mkfs -t ext4 /dev/md0                                           
mke2fs 1.46.5 (30-Dec-2021)
Creating filesystem with 2618368 4k blocks and 655360 inodes
Filesystem UUID: b53b8e29-b825-459f-93d7-17719ab0bab6
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This successfully formated the RAID.

Mount

Make the RAID available for use

Lastly, the RAID has to be mounted on the virtual filesystem to make it possible to use it for storage. In this example, I mounted it in my home directory on a new directory called raidTest

cd
makedir raidTest
sudo mount -t ext4 /dev/md0 raidTest
sudo chown stanley:stanley raidTest

The last line in the terminal dump above made my user the owner of the directory that the RAID was mounted to.

Congratulations, you now have a fully functioning software RAID5! 🎉

Remove RAID

Thow the data in the garbage bin, and restart?

To remove the RAID from your filesystem you first have to unmount it.

sudo umount raidTest

Next, the RAID has to be deleted

sudo mdadm --stop /dev/md0
mdadm: stopped /dev/md0

Now you can create a new RAID, repartition the drives, remove them from the system, or do whatever you want to do with them.