A hardware-based hard drive RAID is costly. For this reason, users regularly create a software RAID on Linux to satisfy their large data needs.
Setting up a storage pool has gotten easier on Linux over the years, thanks to tools like Mdadm. With this program, users can create a software RAID array in a matter of minutes!
The best way to create a RAID array on Linux is to use the Mdadm tool. Mdadm is a command-line utility that allows for quick and easy manipulation of RAID devices. Unfortunately, this software doesn’t come with most distributions by default. If you want to use it, you’ll need to install it by hand.
Open up a terminal and follow the instructions that correspond with your Linux operating system.
sudo apt install mdadm
sudo apt install mdadm
sudo pacman -S mdadm
sudo dnf install mdadm -y
sudo zypper install mdadm
Most Linux distributions have access to Mdadm and package it for their users. That said, if you can’t find the program in your Linux distribution’s package repository, there’s another way to go: building from source. Follow the instructions below to learn how to get Mdadm working.
Step 1: Head over to the Mdadm GitHub page and learn what dependencies the program needs to compile correctly.
Step 2: Clone the source code with the git command.
git clone https://github.com/neilbrown/mdadm cd mdadm
Step 3: Compile the code with the make command.
Step 4: Install Mdadm on Linux with the make install command.
sudo make install
Prepare Hard Drives
RAID arrays on Linux require at least two hard drives. These hard drives must be completely blank, with nothing on them. Launch a terminal and use the DD command to zero each of them out.
Note: use the lsblk command to determine each hard drive’s label.
sudo dd if=/dev/zero of=/dev/sdX
sudo mdadm --zero-superblock /dev/sdX
Making A RAID Array
Using Mdadm to create a new RAID array is very straightforward. To do it, write out the following command in a terminal. Be sure to modify and remove the X‘s in the terminal operation, as your drive labels will differ from the example given.
Note: in this example, we’re using the Mdadm RAID management tool to create a device with two hard drives. If your RAID needs more than two hard drives, change raid-devices=2 to raid-devices=3 or a higher number.
sudo mdadm --create --verbose /dev/md0 --level=0 --raid-devices=2 /dev/sdX /dev/sdXX
Let Mdadm create the RAID device. Be patient, and let the tool work. When the process is complete, it’s time to check it to see if the drive array is working.
You can check that your RAID was made successfully by executing the cat command in a terminal window.
Formate RAID Array
When you combine drives to make a RAID array, your hard drives pool together to create a single storage device. This storage device is entirely blank and inaccessible. You will not be able to place any data on it, without formatting it.
Formatting a RAID drive on Linux requires the use of the MKFS command. In a terminal, execute mkfs.ext4. Running this command will create a new Extended4 file system on the array
sudo mkfs.ext4 -F /dev/md0
Mount RAID Array
Before you can access your new RAID device on Linux, it needs to be mounted to the system. To mount it, launch a terminal and follow the steps below.
Step 1: Create a new folder for the RAID to mount to with the mkdir command.
sudo mkdir -p /mount/md0
Step 2: Use the mount command and bind the RAID array to the new /mnt/md0 folder.
sudo mount /dev/md0 /mnt/md0
Once mounted, access your RAID array with the CD command.
For a permanent RAID mount on Linux, do:
sudo echo "#RAID 0 Mount in /mnt/md0" >> /etc/fstab sudo echo '/dev/md0 /mnt/md0 ext4 defaults,nofail,discard 0 0' >> /etc/fstab
If you’d like to un-mount your RAID, you’ll need to make use of the umount command.
Note: DO NOT try to un-mount your RAID device while copying data!
sudo umount /dev/md0
The traditional umount command should work. If you run into trouble and the RAID refuses to un-mount from the system, try to use the “l” switch.
sudo umount /dev/md0 -l