Ubuntu

How to create RAID arrays using MDADM on ubuntu

RAID is a virtualization platform for data storage that integrates several physical disc drives into one or more logical units. Based on the required level of reliability and efficiency, data is scattered across the drives in one of many ways, referred to as RAID levels. Different systems are known as ‘RAID’ followed by an integer, such as RAID 0 or RAID 1. Each system, or level of RAID, provides a different balance between the key goals, i.e. stability, usability, performance, and strength.

RAID uses disc mirroring or disc striping methods, mirroring on more than one drive would copy similar data. Partition striping allows distributing data across many disc drives. The storage capacity of each drive is split into units that range from a sector (512 bytes) up to multiple megabytes. RAID levels higher than RAID 0 offer protection against unrepairable read errors in the field, as well as against entire physical drive failures.

The RAID devices are deployed via the application driver md. The Linux software RAID array currently supports RAID 0 (strip), RAID 1 (mirror), RAID 4, RAID 5, RAID 6, and RAID 10. Mdadm is a Linux utility used to control and manage RAID devices for applications. Several core operating modes of mdadm are assembled, build, create, follow, monitor, grow, incremental and auto-detect. The name derives from the nodes of the multiple devices (md) that it controls or manages. Let’s look at creating different kinds of Raid arrays using mdadm.

Creating a RAID 0 array:

RAID 0 is the mechanism by which data is separated into blocks, and those blocks are scattered through various storage devices like hard drives. Means that each disc holds a portion of the data and while accessing that data, several discs would be referenced. In raid 0, as blocks are striped, its performance is excellent, but due to no mirroring strategy, a single failure of the device would destroy all the data.

In order to get started, you have to first identify the component devices by using the following command:

ubuntu@ubuntu:~$ lsblk -o NAME, SIZE, TYPE

We have two discs without a filesystem, each 50G in size, as we can see from the screenshot. In this case, the identifiers /dev/ch1 and /dev/ch2 were given to these devices for this session. These are raw components that we are going to use to create the array.

To use these components to create a RAID 0 array, specify them in –create command. You’ll need to define the system name that you want to build (in our case, /dev/mch0), the RAID level, i.e. 0, and the number of devices:

ubuntu@ubuntu:~$ sudo mdadm --create --verbose /dev/mch0 --level=0
--raid-devices=2 /dev/ch1 /dev/ch2

By testing the /proc/mdstat log, we can guarantee that the RAID was created successfully:

ubuntu@ubuntu:~$ cat /proc/mdstat

The /dev/mch0 system has been created with the /dev/ch2 and /dev/ch1 devices in the RAID 0 setup. Now mount the file system on that array using the following command:

ubuntu@ubuntu:~$ sudo mkfs.ext4 -F /dev/mch0

Now, Create a mount point and mount the filesystem by the following commands:

ubuntu@ubuntu:~$ sudo mkdir -p /mnt/mch0
ubuntu@ubuntu:~$ sudo mount /dev/mch0 /mnt/mch0

Check if there is any new space available or not:

ubuntu@ubuntu:~$ df -h -x devtmpfs -x tmpfs

Now we have to change the /etc/mdadm/mdadm.conf file to make sure the list is automatically reassembled at boot. You will search the current array automatically, connect the file and update the initial RAM filesystem by the following sequence of commands :

ubuntu@ubuntu:~$ sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
ubuntu@ubuntu:~$ sudo update-initramfs -u

In order to mount automatically at boot, add new file system mount options in etc/fstab file available:

Each boot can now automatically add your RAID 0 array and mount it.

Creating a RAID 5 array:

Raid 5 arrays are created by stripping the data along with various devices. A measured parity block is one part of each stripe. The parity block and the remaining blocks will be used to determine the missing data in case the device fails. The system obtaining the parity block is rotated such that there is a balanced sum of parity information for each device. While the info about parity is shared, the storage value of one disc can be used for parity. When in a damaged state, RAID 5 will suffer from very poor results.

For creating the RAID 5 array, we have to first identify the component devices as we identified in RAID 0. But in RAID 5 we should have at least 3 storage devices. Find the identifiers for these devices by using the following command:

ubuntu@ubuntu:~$ lsblk -o NAME, SIZE, TYPE

Use the –create command to create a RAID 5 array but use the value 5 for “level” in this case.

ubuntu@ubuntu:~$ sudo mdadm --create --verbose /dev/md0 --level=5
--raid-devices=3 /dev/sda /dev/sdb /dev/sdc

This can take a certain time to complete, even during this time, the array may be used. By testing the /proc/mdstat log, you can track the progress of creation:

ubuntu@ubuntu:~$ cat /proc/mdstat

Now, create and mount the filesystem on the array by executing the following sequence of commands:

ubuntu@ubuntu:~$ sudo mkfs.ext4 -F /dev/md0
ubuntu@ubuntu:~$ sudo mkdir -p /mnt/md0
ubuntu@ubuntu:~$ sudo mount /dev/md0 /mnt/md0

After mounting this, you can confirm whether it is accessible or not::

ubuntu@ubuntu:~$ df -h -x devtmpfs -x tmpfs

For automatic assembling and mounting of RAID 5 arrays at each boot, you have to adjust the initramfs and add the recently created filesystem to fstab file by executing these commands:

Conclusion:

RAID provides efficiency and stability by combining multiple hard drives together. In that way, it gives the system one large capacity hard drive with a much better speed than normal partitioned drives. On the other hand, it doesn’t facilitate redundancy and fault tolerance, and in case, one drive fails all of the data is lost.

About the author

Usama Azad

A security enthusiast who loves Terminal and Open Source. My area of expertise is Python, Linux (Debian), Bash, Penetration testing, and Firewalls. I’m born and raised in Wazirabad, Pakistan and currently doing Undergraduation from National University of Science and Technology (NUST). On Twitter i go by @UsamaAzad14