This guide will walk you through the steps to create different types of RAID arrays (RAID 0, RAID 1, RAID 5, and RAID 10) on a Linux system using the "mdadm" tool.
RAID (Redundant array of independent disks) arrays come in different configurations designed to improve performance, redundancy, or both. Here are some key points to consider:
Required number of drives and disk failure tolerance for RAID levels:
- RAID 0
- Minimum number of drives: 2
- Disk failure tolerance: No tolerance. If any drive fails, all data is lost.
- RAID 1
- Minimum number of drives: 2
- Disk failure tolerance: One drive failure can be tolerated without data loss, as the data is mirrored on the other drive.
- RAID 5
- Minimum number of drives: 3
- Disk failure tolerance: One drive failure can be tolerated. Data can be reconstructed from the information stored on the remaining drives.
- RAID 10
- Minimum number of drives: 4
- Disk Failure Tolerance: Each mirrored pair can tolerate up to one drive failure. Multiple drive failures can be tolerated if they are in different mirrored pairs.
Root Access:
- Root (sudo) access is essential to perform these operations.
Introduction
In this tutorial, we'll show you how to create a new RAID array while leaving your existing RAID array untouched. By following these steps, you will have two separate RAID arrays, improving your system's redundancy and performance for different data sets. Although this guide focuses on RAID 1, the principles can be applied to configure any RAID setup that suits your needs.
Step-by-Step Instructions
Step 1: Prepare Your Drives
The first step is identifying the drives connected to your system and determining which ones are available for creating a RAID array. This involves ensuring the drives are unmounted and not part of any existing RAID configuration.
- List all block devices:
Use the “lsblk” command to list all block devices (see Fig.1):
$ lsblk
This command lists all block devices, their mount points, and their current usage. Look for devices that are not mounted or part of another RAID array.
Example output:
Fig. 1. The “lsblk” command output shows existing drives and their partitions. - Unmount the drives:
Make sure the drives you want to use are not mounted. If they are, unmount them using the “umount” command:
$ sudo umount /dev/sdX
Replace “/dev/sdX” with the appropriate drive name.
Warning: Unmounting itself does not cause data loss. However, creating a RAID array using these drives will result in data loss on those drives. Ensure you have backups if necessary. - Check for existing RAID configurations:
Use the following command to see if any drives are already included in a RAID array (see Fig 2):
$ cat /proc/mdstat
This will display the status of any existing RAID arrays. If the drives you want to use are part of an existing array, you must remove them before proceeding.
Example output:
Fig. 2. The output of the cat ”/proc/mdstat” command showing the status of the active RAID-1 array ”md0”. - (Optional) Clear previous RAID configurations:
Warning: This action will erase RAID metadata and any existing file systems on the drives, potentially resulting in data loss. Skip this step if the drives are new or have never been part of a RAID array. However, if a drive was previously part of a RAID array, you'll need to remove any remaining RAID metadata:
$ sudo mdadm --zero-superblock /dev/sdX
Replace "/dev/sdX" with the appropriate drive name. Repeat this for all drives you want to use in the new RAID array.
Step 2: Create the RAID Array
Once the drives have been prepared, the next step is to create the RAID array. The following commands are provided for the creation of different RAID array types (see Fig 3):
For RAID 0
- Use the following command to create a RAID 0 array:
$ sudo mdadm --create --verbose /dev/mdX --level=0 --raid-devices=2 /dev/sdc /dev/sdd
Replace "mdX" with your desired RAID device name and replace "/dev/sdc" and "/dev/sd" with the appropriate drive names.
For RAID 1
- Use the following command to create a RAID 1 array:
$ sudo mdadm --create --verbose /dev/mdX --level=1 --raid-devices=2 /dev/sdc /dev/sdd
Replace "mdX" with your desired RAID device name and replace "/dev/sdc" and "/dev/sdd" with the appropriate drive names.
For RAID 5
- Use the following command to create a RAID 5 array:
$ sudo mdadm --create --verbose /dev/mdX --level=5 --raid-devices=3 /dev/sdc /dev/sdd /dev/sde
Replace "mdX" with your desired RAID device name and replace "/dev/sdc," "/dev/sdd", and "/dev/sde" with the appropriate drive names.
For RAID 10
- Use the following command to create a RAID 10 array:
$ sudo mdadm --create --verbose /dev/mdX --level=10 --raid-devices=4 /dev/sdc /dev/sdd /dev/sde /dev/sdf
Replace "mdX" with your desired RAID device name and replace "/dev/sdc", "/dev/sdd", "/dev/sde" and "/dev/sdf" with the appropriate drive names.
Fig. 3. The process of creating a RAID 1 array from drives ”nvme2n1” and ”nvme3n1”.
Step 3: Check the RAID Array Status
- Verify the status of the RAID array (see Fig. 4):
$ sudo mdadm --detail /dev/mdX
Replace “/dev/mdX” with your RAID array device name.
Fig. 4. Verifying the status of the newly created RAID array.
Step 4: Save the RAID Configuration
- Create a variable to store the UUID of the new RAID array:
This command extracts the UUID of the new RAID array and stores it in a variable named “NEW_UUID”:
$ NEW_UUID=$(sudo mdadm --detail /dev/mdX | grep 'UUID' | awk '{print $3}')
Replace “/dev/mdX” with your RAID array device name. - Append only the new RAID array details to “mdadm.conf” (see Fig. 5):
This command appends the new RAID array's details to the “mdadm.conf” file using the UUID stored in “$NEW_UUID” variable:
$ sudo mdadm --detail --scan | grep $NEW_UUID | sudo tee -a /etc/mdadm/mdadm.conf
Fig. 5. Adding the new RAID array details to the mdadm.conf file. - Update the “initramfs” (see Fig. 6):
This command updates the initial RAM filesystem to include the new RAID configuration:
$ sudo update-initramfs -u
Fig. 6. Updating “initramfs”. - Assemble the RAID array:
This command ensures that the RAID array is correctly assembled on system startup:
$ sudo mdadm --assemble --scan
Step 5: Create a Filesystem on the RAID Array
- Create a filesystem on the RAID array (e.g., ext4) (see Fig. 7):
$ sudo mkfs.ext4 /dev/mdX
Replace “/dev/mdX” with your RAID array device name.
Fig. 7. Creating an ”ext4” filesystem inside the new RAID array.
Step 6: Mount the RAID Array
- Create a mount point:
$ sudo mkdir -p /mnt/mdX
Replace “/mnt/mdX” with your desired mount point. - Mount the RAID array:
$ sudo mount /dev/mdX /mnt/mdX
Replace “/dev/mdX” with your RAID array device name and “/mnt/mdX” with your mount point. - Edit the “/etc/fstab” file:
Open the “/etc/fstab” file in a text editor to ensure the RAID array persists through reboots:
$ sudo nano /etc/fstab
- Add an entry to “/etc/fstab” (see Fig. 8):
At the bottom of the “/etc/fstab” file, add the following line:
/dev/mdX /mnt/mdX <filesystem> defaults 0 2
Replace “/dev/mdX” with your RAID array device name, “/mnt/mdX” with your mount point, and “<filesystem>” with your created filesystem type.
Fig. 8. Adding an entry for the new RAID array to the ”/etc/fstab” configuration file. - Save and exit the editor:
- Press Ctrl + X to exit.
- Press Y to confirm you want to save the changes.
- Press Enter to finalize the save.
Step 7: Verify the RAID Array
- Check the status of the RAID array (see Fig 9):
$ sudo mdadm --detail /dev/mdX
Replace “/dev/mdX” with your RAID array device name.
Fig. 9. Checking the status of the newly created RAID array and ensuring that resync has started.
Summary
Following these steps, you can effectively create and manage various RAID arrays on your Linux system, ensuring improved performance and data redundancy. In this case, you've made a separate RAID array using two drives while leaving your existing RAID array untouched.