In my previous post I explained on the most common used RAID levels and their write penalties. In this post I will show how you can easily create a software RAID array. These type of solutions are very common in a home NAS or it can also be used as an extension to a hardware RAID solution since not all RAID controllers support RAID 5+0 which is striped volume of two or more RAID 5 volumes to increase the availability, some speed and still deliver a large(r) usable volume.
Home NAS
The one RAID level I only mentioned but did not compare against the other RAID levels (1, 5 and 1+0) was level 0 itself. I did mention that RAID level 0 should not be used in production environments but I do see an exception when it comes to a home NAS. I use RAID 0 myself as partition to store my downloads which can be re-downloaded again in case of disk failure. Typically these files are large such as .iso files containing a variety of Linux distributions and backups of my DVD collection. Next to this volume I also have a volume which is a RAID 1 array to hold backups of my photo's, video's and other documents.mdadm in short
The Linux utility used is called mdadm and stands for Multiple Device
Administration. In CentOS 7 minimal (which I used for this demo) this
utility is not installed by default so lets install it first.
sudo yum -y install mdadm
When you issue the mdadm --help command you will find that there are not over a million possible options. In fact, the options listed are all you need. However, for this post I will only focus on how to create an array. You will need to create raid partitions in the drives in order to create an array out of them. But, hey, wait, that means that you can actually mix different RAID levels on the physical disks...
Partitioning the drives
I have a machine with 4 hard drives. Looking at the output the following fdisk command we can see three empty hard drives. Namely /dev/sdb, /dev/sdc and /dev/sdd.[user@demo ~]$ sudo fdisk -l | grep dev
Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 16777215 7875584 8e Linux LVM
Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 sectors
Disk /dev/sdd: 8589 MB, 8589934592 bytes, 16777216 sectors
Disk /dev/mapper/centos-root: 7159 MB, 7159676928 bytes, 13983744 sectors
Disk /dev/mapper/centos-swap: 859 MB, 859832320 bytes, 1679360 sectors
In order to be able to create an MD (software raid) device you will need to partition your drives so that they contain so called raid partitions. These partitions will be the input partitions for the MD device. In this example we have three physical HDDs. We will only use a part of the physically available space so that we can add another type of RAID level next to this first example.
Here we will create a 2GB raid partition on each of the drives. My favorite tool is still fdisk to accomplish this partitioning task.
[user@demo ~]$ sudo fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
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 Building a new DOS disklabel with disk identifier 0xf302fdde.
Command (m for help):
In short you would need to create a new partition using:
- n (as in "new")
- p (as in "primary"
- 1 (as in partition number 1)
- [enter] (as in leave the default first sector)
- +2G (as in add a 2GB partition)
- t (to into the change partition type id selector)
- fd (to set the partition type to "Linux raid auto")
- w (as in write the changes to the disk and exit fdisk)
Execute the above steps for all the physical drives. Just change the /dev/sd* name every time you execute the fdisk command.
Now issue the "sudo partprobe" command in order to scan the physical drives for their partitions.
[user@demo ~]$ sudo fdisk -l | grep dev
Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 16777215 7875584 8e Linux LVM
Disk /dev/sdb: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sdb1 2048 4196351 2097152 fd Linux raid autodetect
Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sdc1 2048 4196351 2097152 fd Linux raid autodetect
Disk /dev/sdd: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sdd1 2048 4196351 2097152 fd Linux raid autodetect
Disk /dev/mapper/centos-root: 7159 MB, 7159676928 bytes, 13983744 sectors
Disk /dev/mapper/centos-swap: 859 MB, 859832320 bytes, 1679360 sectors
As you can see we now have /dev/sdb1, /dev/sdc1 and /dev/sdd1 which are the raid partitions
Create a RAID 5 array
Using the three raid partitions from above we can now create a RAID 5 volume using mdadm. This process is really simple so I will just show you the command to do this. Please note that this command is on one line.[user@demo ~]$ sudo mdadm --create /dev/md0 --level=5 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
The output will be
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
You can verify the status of the raid array by looking at the /proc/mdstat file.
[user@demo ~]$ sudo cat /proc/mdstat
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdd1[3] sdc1[1] sdb1[0]
4190208 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU]
unused devices: <none>
/dev/md0 is your RAID 5 volume and is now available to format using your favorite file system. I will use xfs here since that is the default in CentOS 7.
sudo mkfs.xfs /dev/md0
Create a mount folder
sudo mkdir /raid5
And mount the volume
sudo mount /dev/md0 /raid5/
You can also make the mount persistent by adding it into /etc/fstab by adding this line to it
/dev/md0 /rair5 xfs defaults 0 0
If you would look at the output of the fd -h command you will find that /raid5 is 4GB in size. Which is correct since we created a raid 5 array. You will loose one the space of one raid partition (2GB) which is reserved for the parity data.
Other RAID levels
Creating RAID 0 is a similar process but then in stead of --level=5 use --level=0[user@demo ~]$ sudo mdadm --create /dev/md0 --level=0 --raid-devices=3 /dev/sdb1 /dev/sdc1 /dev/sdd1
Creating a RAID 1 is limited to 2 drives (or raid partitions)
[user@demo ~]$ sudo mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
Have fun!