Configuring Software RAID1 on a Running Ubuntu System

Here is an example of migrating a running Ubuntu system to a software RAID1.
In the process, you will need to perform two reboots.

The first step is to switch to the root user if not yet:

sudo -i

Let’s see a list of disks and partitions:

fdisk -l
fdisk -l | grep '/dev/sd'
lsblk -o NAME,UUID

Suppose that the system uses one disk, for example /dev/sda and has one main partition, /dev/sda1.
For the test, I installed a clean Ubuntu Server 18.04, the disk was parted by default, swap was the file on the same partition.

To create a raid, we connect another disk of the same size, it will be called /dev/sdb.

Install mdadm and necessary utilities (they are usually installed by default):

apt-get install initramfs-tools mdadm

In order to make sure that all necessary modules and components are installed, execute the following command:

cat /proc/mdstat

If the necessary modules are not loaded, then load them:

modprobe linear
modprobe multipath
modprobe raid1

Let’s divide the new disk /dev/sdb in the same way as:

sfdisk -d /dev/sda | sfdisk --force /dev/sdb

Let’s check:

fdisk -l

In the next step, change the partition type of the new hard disk /dev/sdb to “Linux raid autodetect” (since partition 1, then after “t” it will not be asked to specify the partition number):

fdisk /dev/sdb
t
fd
w

Make sure that the partition type /dev/sdb is Linux raid autodetect:

fdisk -l

Create an array md0 using the missing:

mdadm --create /dev/md0 --level=1 --metadata=1.0 --raid-disks=2 missing /dev/sdb1

Let’s check:

cat /proc/mdstat

If something does not work, then you can remove the raid and try again:

mdadm --stop /dev/md0

Let’s specify the file system of the array:

mkfs.ext4 /dev/md0

Let’s make a backup copy of the configuration file mdadm and add information about the new array:

cp /etc/mdadm/mdadm.conf /etc/mdadm/mdadm_backup.conf
mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Mount /dev/md0 into the system:

mkdir /mnt/md0
mount /dev/md0 /mnt/md0
mount

At me it was displayed at the bottom of the list:

/dev/md0 on /mnt/md0 type ext4 (rw,relatime,data=ordered)

In the /etc/fstab file comment the lines about /dev/sda and add about the array:

nano /etc/fstab
/dev/md0 /               ext4    errors=remount-ro 0       1

Let’s see the file /etc/mtab whether there is a record about the raid:

cat /etc/mtab

Let’s look at the exact names of the files /vmlinuz, /initrd.img:

ls /

Create a file from the GRUB2 boot menu and open it in the editor:

cp /etc/grub.d/40_custom /etc/grub.d/09_raid1_test
nano /etc/grub.d/09_raid1_test

Add the contents (instead of /vmlinuz and /initrd.img, we’ll specify the correct names if they are different):

#!/bin/sh
exec tail -n +3 $0
# This file provides an easy way to add custom menu entries.  Simply type the
# menu entries you want to add after this comment.  Be careful not to change
# the 'exec tail' line above.
menuentry 'Debian GNU/Linux, with Linux' --class debian --class gnu-linux --class gnu --class os {
        insmod mdraid1x
        insmod part_msdos
        insmod ext2
        set root='(md/0)'
        echo    'Loading Linux'
        linux   /vmlinuz root=/dev/md0 ro  quiet
        echo    'Loading initial ramdisk ...'
        initrd  /initrd.img
}

Open the file /etc/default/grub in the text editor:

nano /etc/default/grub

Uncomment a couple of lines:

GRUB_TERMINAL=console
GRUB_DISABLE_LINUX_UUID=true

Update the loader:

update-grub

Prepare ramdisk:

update-initramfs -u

Install the bootloader on both disks:

grub-install /dev/sda
grub-install /dev/sdb

Copy all the data to the previously mounted md0 array:

cp -dpRx / /mnt/md0

Restart the system:

reboot

When the system starts, in the boot menu it will be the first menu /etc/grub.d/09_raid1_test, if there are problems with the download, you can choose to boot from /dev/sda.

Make sure that the system is started with /dev/md0:

df -h

Again, switch to the root user if not under it:

sudo -i

Change the partition type of the old hard disk:

fdisk /dev/sda
t
fd
w

Let’s check:

fdisk -l

Add to the array the old disk:

mdadm --add /dev/md0 /dev/sda1

Wait until the synchronization is completed and make sure that the raid is in order – UU:

cat /proc/mdstat

Update the array information in the mdadm configuration file:

cp /etc/mdadm/mdadm_backup.conf /etc/mdadm/mdadm.conf
mdadm --examine --scan >> /etc/mdadm/mdadm.conf

Remove our temporary GRUB menu, it’s no longer necessary:

rm -f /etc/grub.d/09_raid1_test

Update and install GRUB again:

update-grub
update-initramfs -u
grub-install /dev/sda
grub-install /dev/sdb

Restart the system to make sure it runs successfully:

reboot

At this, the migration of the running Ubuntu system to the software RAID1 is complete.
If one of the disks, /dev/sda or /dev/sdb stops working, the system will run and boot.
For stability, you can add more disks of the same size to the array.

See also my articles:
mdadm – utility for managing software RAID arrays
How to create SWAP in Linux
Managing disk partitions in Ubuntu using fdisk
Loading and Unloading Modules in Linux

Leave a comment

Leave a Reply