Skip to main content
  1. Posts/

Setup Raid-1 On Linux

·3 mins

Today I decided to setup my HP Proliant server that has been collecting dust for some time now. The server has five drives of which one SSD for boot and four 2TB harddrives. I will be using two of the four harddrives to setup raid-1.

The server is running on Ubuntu 20.04 but this commands should work on any other distro.

First we need to install ‘mdadm’

Ubuntu/Debian:

sudo apt install mdadm 

Fedora/CentOS:

sudo dnf install mdadm 

Arch:

pacman -Sy mdadm

Now lets check when the harddrives are located with lsblk.

sudo lsblk -l

Location of my harddrives:

    /dev/sdb
    /dev/sdc
    /dev/sdd
    /dev/sde

I will be using /dev/sdv and /dev/sdc for my raid-1 setup.

We need to partition the harddrives execute the following commands on BOTH /dev/sdb and /dev/sdc

create partition-table:

sudo parted -s /dev/sdb mklabel msdos
sudo parted -s /dev/sdc mklabel msdos

create partition:

sudo parted -s /dev/sdb mkpart primary 1MiB 100%
sudo parted -s /dev/sdc mkpart primary 1MiB 100%

Now create the partitions and flag the partitions with the raid flag:

sudo parted -s /dev/sdb set 1 raid on
sudo parted -s /dev/sdc set 1 raid on

Now let’s create the raid setup:

sudo mdadm \
--verbose \
--create /dev/md0 \
--level=1 \
--raid-devices=2 \
/dev/sdb1 /dev/sdc1

We used the –verbose option in order to make the command output more information about the operations that are being performed.

We used mdadm in “create mode”, that’s why passed the –create option, providing the name of the device that should be created (/dev/md0 in this case). We than specified what level to use for the RAID with –level, and the number of devices that should be part of it with –raid-devices and as last we provided the path of the devices which should be used.

The raid setup has been created. Now we need to create a filesystem to use it. I will be creating a ext4 filesystem:

sudo mkfs.ext4 /dev/md0

Now we are ready to use it but first we want the disk to be mounted at boot. To mount the disk we need to know the UUID of the filesystem and a mount point.

Get the UUID:

lsblk -o UUID /dev/md0

Create the mount point:

sudo mkdir /mnt/data

Let’s mount the disk at boot with fstab. Add the following line to your fstab file (/etc/fstab):

UUID=YOUR_UUID   /mnt/data    ext4          defaults       0       2 

Replace ‘YOUR_UUID’ with the UUID of your disk /dev/md0

No we are done and can restart the server. After the server has been rebooted we can log back in and double check if the disk is mounted correctly on /mnt/data.

findmnt | grep /mnt/data