January 28

Linux: Configuring Software RAID1 on a Running Ubuntu System

This 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

List the disk partitions - look to see if the drives appear as sd, vd, nvme, etc..
After seeing the drive type modify these instructions accordingly.
fdisk -l

Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: gpt
Disk identifier: 318758B2-4D70-4BD5-A9FC-C4DE66E692F7

Device     Start      End  Sectors Size Type
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 52428766 52424671  25G Linux RAID


fdisk -l | grep '/dev/sd'
Disk /dev/sda: 25 GiB, 26843545600 bytes, 52428800 sectors
/dev/sda1   2048     4095     2048   1M BIOS boot
/dev/sda2   4096 52428766 52424671  25G Linux RAID


lsblk -o NAME,UUID
NAME    UUID
sda     
├─sda1  
└─sda2  3cc51393-2a21-9158-6f63-87a3a85bb7ee
  └─md0 003fde66-cb4a-453e-9cc5-87f4ce46a722


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 20.04, the disk was parted by default, flat ext4 with no LVM, 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

Divide the new disk /dev/sdb in the same way as:
sfdisk -d /dev/sda | sfdisk --force /dev/sdb

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
29
w

Make sure that the partition type /dev/sdb is Linux RAID:
fdisk -l

Create an array md0 using the missing:
mdadm --create /dev/md0 --level=1 --metadata=1.0 --raid-disks=2 missing /dev/sdb1

Check:
cat /proc/mdstat

If something does not work, then you can remove the raid and try again:
mdadm --stop /dev/md0

Specify the file system of the array:
mkfs.ext4 /dev/md0

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

For 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.  In this case /boot/vmlinuz-5.4.0-96-generic, /boot/initrd.img-5.4.0-96-generic:
ls /boot

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   /boot/vmlinuz-5.4.0-96-generic root=/dev/md0 ro  quiet
        echo    'Loading initial ramdisk ...'
        initrd  /boot/initrd.img-5.4.0-96-generic
}

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

After the copy has completed, restart the system:
reboot

Note:
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.  You can also try to remount the system in rw mode if needed:
mount -o remount,rw /partition/identifier /mount/point
I initially had an /etc/fstab issue so I did the followind: mount -o remount,rw /dev/md0 /


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

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

Copy the working partition of /dev/sdb to /dev/sda
sfdisk -d /dev/sdb | sfdisk --force /dev/sda

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.

By: vyacheslav
Update for 20.04 by tconrad


Copyright 2021. All rights reserved.

Posted January 28, 2022 by Timothy Conrad in category "Linux

About the Author

If I were to describe myself with one word it would be, creative. I am interested in almost everything which keeps me rather busy. Here you will find some of my technical musings. Securely email me using - PGP: 4CB8 91EB 0C0A A530 3BE9 6D76 B076 96F1 6135 0A1B