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
Category: Linux | Comments Off on Linux: Configuring Software RAID1 on a Running Ubuntu System
January 26

Linux: Ubuntu – netplan – After applying a sub interface removal it still shows up as active.

As of 2021, netplan seems to be very touchy and rarely seems to do what I expect with complex configurations.

After removing a sub interface on a bond I could still see it as active with ip addr. To get rid of this I first disabled the inter face the deleted it manually:

Category: Linux | Comments Off on Linux: Ubuntu – netplan – After applying a sub interface removal it still shows up as active.
January 24

Linux: MAAS – cli error when uploading an image

When trying to do MAAS commands you may receive an error similar to:

argument COMMAND: invalid choice: ‘boot-resource’ (choose from ‘login’, ‘logout’ … )

This error is due to you not being logged into the MAAS server from the command line.

To login use the following syntax:

  1. maas login yourmaasadminuser http://localhost:5240/MAAS
  2. paste your API Key. (This can be found from your MAAS gui. Click on the user name at the top right hand corner of the page and then click API keys under details.
  3. If the user and API key are correct you will receive a message that ‘You are now logged in to the MAAS server at http://localhost:5240?MAAS/api/2.0/ with the profile name ‘yourmaasadminuser’.
  4. You can now enter you commands and they should work as long as you command syntax is correct.

By: tconrad

Category: Linux | Comments Off on Linux: MAAS – cli error when uploading an image
December 8

Linux: Create Network Bridge For KVM

The bridged network is a dedicated network interface to a virtual machine that helps virtual machines to connect outside the host machine.

Let us list the available network connections.COPY

nmcli connection show

Output:

NAME                UUID                                  TYPE      DEVICE
Wired connection 1  fbbdd6f9-0970-354e-8693-ff8050a85c77  ethernet  enp0s3

Now, we will create a virtual bridge network br0 with the help of physical interface enp0s3.

sudo nmcli con add ifname br0 type bridge con-name br0

sudo nmcli con add type bridge-slave ifname enp0s3 master br0

Next, we will assign the IP address of the physical interface to the bridge interface as the Bridge network interface will act as the primary network interface of your host system.COPY

sudo nmcli con mod br0 ipv4.addresses 192.168.0.10/24

sudo nmcli con mod br0 ipv4.gateway 192.168.0.1

sudo nmcli con mod br0 ipv4.dns "8.8.8.8","192.168.0.1"

sudo nmcli con mod br0 ipv4.method manual

KVM requires a few additional network settings. So, set them.COPY

sudo nmcli con modify br0 bridge.stp no

sudo nmcli con modify br0 bridge.forward-delay 0

Disable the physical interface and enable the network bridge.COPY

sudo nmcli con down "Wired connection 1" && sudo nmcli con up br0

Run the above command in the system terminal as you may lose SSH sessions when running them remotely.

Finally, check the network connections.COPY

sudo nmcli con show
Output:

NAME                 UUID                                  TYPE      DEVICE
br0                  ee117099-4935-4dde-a1f5-4981b0d9585e  bridge    br0
bridge-slave-enp0s3  492b5c81-e59d-4150-9b24-0348cd0dd87c  ethernet  enp0s3
Wired connection 1   fbbdd6f9-0970-354e-8693-ff8050a85c77  ethernet  --

By: Raj

Category: Linux | Comments Off on Linux: Create Network Bridge For KVM