What is RAID?
RAID stands for Redundant Array of Inexpensive Disks. RAID allows you to turn multiple physical hard drives into a single logical hard drive. There are many RAID levels such as RAID 0, RAID 1, RAID 5, RAID 10 etc.
Here we will discuss about RAID 1 which is also known as disk mirroring. RAID 1 creates identical copies of data. If you have two hard drives in RAID 1, then data will be written to both drives. The two hard drives have the same data.
The nice part about RAID 1 is that if one of your hard drive fails, your computer or server would still be up and running because you have a complete, intact copy of the data on the other hard drive. You can pull the failed hard drive out while the computer is running, insert a new hard drive and it will automatically rebuilds the mirror.
The downside of RAID 1 is that you don’t get any extra disk space. If your two hard drives are both 1TB, then the total usable volume is 1TB instead of 2TB.
Hardware RAID vs Software RAID
To set up RAID, you can either use a hard drive controller, or use a piece of software to create it. A hard drive controller is a PCIe card that you put into a computer. Then you connect your hard drives to this card. When you boot up the computer, you are going to see an option that allows you to configure the RAID. You can install an operating system on top of hardware RAID which can increase uptime.
Software RAID requires you already installed an operating system. It’s good for storing data.
Basic Steps to Create Software RAID 1 on Linux
You can see mine is /dev/sdb and /dev/sdc.
Then run the following 2 commands to make new MBR partition table on the two hard drives. (Note: this is going to wipe out all existing partitions and data from these two hard drives. Make sure your data is backed up.)
sudo parted /dev/sdb mklabel msdos
sudo parted /dev/sdc mklabel msdos
You can create GPT partition table by replacing msdos with gpt, but for the sake of compatibility, this tutorial will create MBR partition table.
Next, use the fdisk command to create a new partition on each drive and format them as a Linux raid autodetect file system. First do this on /dev/sdb.
sudo fdisk /dev/sdb
Follow these instructions.
Follow the same instruction to create a Linux raid autodetect
partition on /dev/sdc
.
Now
we have two raid devices /dev/sdb1
and /dev/sdc1
.
mdadm
is used for managing MD (multiple devices) devices, also
known as Linux software RAID.
Debian/Ubuntu: sudo apt install mdadm
CentOS/Redhat: sudo yum install mdadm
SUSE: sudo zypper install mdadm
Arch Linux sudo pacman -S mdadm
Let’s examine the two devices.
sudo mdadm --examine /dev/sdb /dev/sdc
You can see that both are the type fd (Linux raid autodetect). At this stage, there’s no RAID setup on /dev/sdb1
and /dev/sdc1
which can be inferred with this command.
sudo mdadm --examine /dev/sdb1 /dev/sdc1
Execute
the following command to create RAID 1. The logical drive will be named /dev/md0
.
sudo mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/sdb1 /dev/sdc1
Note: If you see this message: “Device or resource busy”, then you may need to reboot the OS.
Now we can check it with:
cat /proc/mdstat
You can see that md0 is active and is a RAID 1 setup. To get more detailed information about /dev/md0
, we can use the below commands:
sudo mdadm --detail /dev/md0
To obtain detailed information about each raid device, run this command:
sudo mdadm --examine /dev/sdb1 /dev/sdc1
Let’s format it to ext4 file system.
sudo mkfs.ext4 /dev/md0
Then
create a mount point /mnt/raid1
and mount the RAID 1 drive.
sudo mkdir /mnt/raid1
sudo mount /dev/md0 /mnt/raid1
You can use this command to check how much disk space you have.
df -h /mnt/raid1
Now
let’s go to /mnt/raid1
and create a text file.
cd /mnt/raid1
sudo nano raid1.txt
Write something like
This is raid 1 device.
Save and close the file. Next, remove one of your drive out from your computer and check the status RAID 1 device again.
sudo mdadm --examine /dev/sdb1 /dev/sdc1
You can see that /dev/sdc1
is not available. If we check /dev/md0
, we can see that one RAID device is removed.
sudo mdadm --detail /dev/md0