A hard drive disk is a device with limits (performance, lifetime, …) and a broken disk often leads to a data loss, and sometimes with a loss of data more or less important.
In order to avoid that kind of inconvenience, there are several hardware solutions allowing disk replication, but most of time with a too high cost for a standard use (non professional). Fortunately, there is also a software RAID, which can be set up easily and quickly without any additional hardware (assuming you already own at least 2 hard drive disks) given that it’s working on a software layer between the hardware abstraction layer and the file system.
There are different levels of RAID ; the most common levels are:
- RAID 0 : the “striping” allows to improve performance by splitting the IO requests on several devices in parallel (2 to n disks)
- RAID 1 : the “mirroring” allows to write same data on several disks at the same time (2 to n disks)
- RAID 5 : the “striping and mirroring” allows the combination of RAID 0 and RAID 1 (3 to n disks)
I will present here how to set up a RAID 1 solution (mirroring) but it will be easy to adapt the following steps if you need another RAID level.
RAID 1 setup
For the setting up, ensure you have the mdadm package installed and ready on your system. For Debian/Ubuntu, you can use the following command:
sudo apt-get install mdadm
Prepare both disks in Software Raid (type 0xfd) thanks to the fdisk tool:
sudo fdisk /dev/sdb sudo fdisk /dev/sdc
You can now shrink and erase both disks with zeros thanks to the command below:
mdadm --zero-superblock /dev/sdb /dev/sdc
Create your RAID 1 (or any level, by changing the level attribute):
mdadm --create /dev/md0 --level=1 --raid-devices=2 /dev/sdb1 /dev/sdc1
Format this new partition in ext4:
Mount your partition you just created:
mkdir /data mount /dev/md0 /data
To get an automatic mounting point after any restart of the system, edit the file /etc/fstab and add this following line:
/dev/md0 /data ext4 noatime,rw 0 0
You can now check and follow your RAID status, by using following commands:
cat /proc/mdstat mdadm --query /dev/md0
To ensure the RAID is always working correctly and be informed when a disk is out of order, it is possible to set a monitoring solution thanks to the common tools of mdadm with a mail alert system.
Check that the monitoring settings are correctly defined in the file /etc/mdadm/mdadm.conf with the sender address (MAILFROM) and the receiver address (MAILADDR):
# instruct the monitoring daemon where to send mail alerts MAILADDR [email protected] MAILFROM [email protected]
You will need a mail server enabled on your server (postfix, ssmtp, …) and listening on port 25.
To check and test this configuration, ou can easily perform a test by sending a mail report thanks to this command:
sudo mdadm --monitor --scan --test --oneshot
To ensure that monitoring is enabled for your RAID devices, just check that the –monitor option is correctly set by performing the following command:
ps aux | grep mdadm >>/sbin/mdadm --monitor --pid-file /var/run/mdadm/monitor.pid --daemonise --scan --syslog
If it’s not, you will have to add the option in the /etc/default/mdadm file (DAEMON_OPTIONS).