In this tutorial we will be configuring a software RAID array in Debian Linux using MDADM.
For the purposes of this tutorial I have set up a virtual machine with a total of 5 drives. I have installed Debian Linux on the first drive, the other 4 shall be used for our RAID array. In a real-life scenario you would probably want your operating system to be on a redundant array as well, I will cover that in another tutorial.
Lets get started.
Login into your server either as root or as a user with sudo privileges.
Install MDADM by executing the following command:
sudo apt-get install mdadm
You will be prompted with a screen asking you if your array will contain the root filesystem. This is very important. If the array will contain the root filesystem it needs to be started sooner in the boot process. For this tutorial, our array will not contain the root filesystem so we don't absolutely need it to start sooner.
In the MD arrays needed for the root filesystem field type "none" and press Enter.
The installation of MDADM is complete. Now lets get our drives ready.
Execute the following command to list out our available disks:
As you can see I have 5 total disks in my system. You will notice that one disk (/dev/sda) is already partitioned. This is the disk that contains the operating system and it will not be included in the array.
The disks that will be part of the array will be as follows:
With our disks identified we need to set up one or more partitions on each disk. The partitions MUST be identical across all disks that will be part of the array. For the purposes of this tutorial I will just create 1 partition on each disk.
Execute the following command:
sudo cfdisk /dev/sdb
Select gpt and hit Enter.
You will now be presented with a screen that shows the current partition table of the selected disk. At present, there is only free space.
Using the arrow keys, select New and hit Enter.
You will now be asked to specify a size for the new partition. A default value should already be provided that will use all available disk space.
Accept the default, hit Enter.
You will now see our free space has become a Linux filesystem partition.
Using the arrow keys, select Write and hit Enter.
At the Are you sure you want to write the partition table to disk prompt, enter "yes" and hit Enter.
Using your arrow keys, select Quit and hit Enter.
Repeat Step 5 for each disk that will be part of the array.
Verify that all disks that will be part of the array now have matching partitions.
Execute the following command:
As you can see, all 4 disks have matching partitions.
We can now use MDADM to create the RAID array. In this tutorial we will be creating a RAID 5 array using all 4 disks with no spares.
Execute the following command to create the array from the partitions we just created:
sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=4 /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1 --spare-devices=0
Your new array is now being created.
Check the status of the array by executing the following command:
sudo mdadm --detail /dev/md0
As you can see above the Rebuild Status of my array is at 74%. Larger arrays will take longer to rebuild. If your array is still rebuilding it is OK to continue.
Now that we have created the array we need to save the configuration into the mdadm.conf file. You may need to double check the location of your mdadm.conf file. For Debian, our configuration file is located at /etc/mdadm/mdadm.conf.
Execute the following command to save our MDADM configuration (I had to execute as root):
sudo mdadm --detail --scan >> /etc/mdadm/mdadm.conf
Configure a filesystem on the new array by executing the following command:
sudo mkfs.ext4 /dev/md0
Set up a mount point for our new array.
Execute the following command to create a directory to house our new mount point:
sudo mkdir /mnt/raid5
Mount the array.
Execute the following command to mount the array:
sudo mount /dev/md0 /mnt/raid5
Test the mounted array by executing the following command:
In order for the array to be automatically mounted when we reboot we must modify /etc/fstab.
Open the /etc/fstab file for editing by executing the following command:
sudo nano /etc/fstab
Using the arrow keys move the cursor to a new line under the last item in the list.
Enter the following:
/dev/md0 /mnt/raid5 ext4 defaults 0 0
Press CTRL + X
When prompted with Save modified buffer press Y.
When prompted with File Name to Write hit Enter.
NOTE: For some operating systems (Ubuntu Server for example) you may have to execute the following command after completing the above steps to ensure your RAID array will be recognized upon reboot
sudo update-initramfs -u
Congratulations! You have successfully configured a RAID array using MDADM!