How to replace grub with bootloader "systemd-boot" in ubuntu 20.04?

ubuntu@ubuntu:~$ lsblk -po NAME,SIZE,TYPE,FSTYPE,PARTLABEL
NAME SIZE TYPE FSTYPE PARTLABEL
/dev/loop0 1.9G loop squashfs
/dev/loop1 27.1M loop squashfs
/dev/loop2 55M loop squashfs
/dev/loop3 240.8M loop squashfs
/dev/loop4 62.1M loop squashfs
/dev/loop5 49.8M loop squashfs
/dev/sda 465.8G disk
├─/dev/sda1 292M part vfat CLR_BOOT
├─/dev/sda2 512M part swap CLR_SWAP
├─/dev/sda3 108.6G part ext4 CLR_ROOT
├─/dev/sda4 16M part Microsoft reserved partition
├─/dev/sda5 79G part ntfs Basic data partition
└─/dev/sda6 277.4G part ntfs Basic data partition
/dev/sdb 30.2G disk iso9660
├─/dev/sdb1 2.5G part iso9660
├─/dev/sdb2 3.9M part vfat
└─/dev/sdb3 27.7G part ext4
/dev/sr0 1024M rom
ubuntu@ubuntu:~$ 
2

1 Answer

after following

# Everything in this tutorial should be done as root:
sudo -i
# Now hop on into the EFI partition root.
cd /boot/efi
# Configuration files will go here:
mkdir -p loader/entries
# And kernels will go here:
mkdir ubuntu

Put the following into /boot/efi/loader/loader.conf (change the timeout value to your pleasing)

default ubuntu
timeout 1
editor 0

Put the following to /etc/kernel/postinst.d/zz-update-systemd-bootMake sure to change the CHANGEMEs.

#!/bin/bash
#
# This is a simple kernel hook to populate the systemd-boot entries
# whenever kernels are added or removed.
#
# The UUID of your disk.
UUID="CHANGEME"
# The LUKS volume slug you want to use, which will result in the
# partition being mounted to /dev/mapper/CHANGEME.
VOLUME="CHANGEME"
# Any rootflags you wish to set.
ROOTFLAGS="CHANGEME"
# Our kernels.
KERNELS=()
FIND="find /boot -maxdepth 1 -name 'vmlinuz-*' -type f -print0 | sort -rz"
while IFS= read -r -u3 -d $'\0' LINE; do KERNEL=$(basename "${LINE}") KERNELS+=("${KERNEL:8}")
done 3< <(eval "${FIND}")
# There has to be at least one kernel.
if [ ${#KERNELS[@]} -lt 1 ]; then echo -e "\e[2msystemd-boot\e[0m \e[1;31mNo kernels found.\e[0m" exit 1
fi
# Perform a nuclear clean to ensure everything is always in perfect
# sync.
rm /boot/efi/loader/entries/*.conf
rm -rf /boot/efi/ubuntu
mkdir /boot/efi/ubuntu
# Copy the latest kernel files to a consistent place so we can keep
# using the same loader configuration.
LATEST="${KERNELS[@]:0:1}"
echo -e "\e[2msystemd-boot\e[0m \e[1;32m${LATEST}\e[0m"
for FILE in config initrd.img System.map vmlinuz; do cp "/boot/${FILE}-${LATEST}" "/boot/efi/ubuntu/${FILE}" cat << EOF > /boot/efi/loader/entries/ubuntu.conf
title Ubuntu GNOME
linux /ubuntu/vmlinuz
initrd /ubuntu/initrd.img
options cryptdevice=UUID=${UUID}:${VOLUME} root=/dev/mapper/${VOLUME} ro rootflags=${ROOTFLAGS}
EOF
done
# Copy any legacy kernels over too, but maintain their version-based
# names to avoid collisions.
if [ ${#KERNELS[@]} -gt 1 ]; then LEGACY=("${KERNELS[@]:1}") for VERSION in "${LEGACY[@]}"; do echo -e "\e[2msystemd-boot\e[0m \e[1;32m${VERSION}\e[0m" for FILE in config initrd.img System.map vmlinuz; do cp "/boot/${FILE}-${VERSION}" "/boot/efi/ubuntu/${FILE}-${VERSION}" cat << EOF > /boot/efi/loader/entries/ubuntu-${VERSION}.conf
title Ubuntu GNOME ${VERSION}
linux /ubuntu/vmlinuz-${VERSION}
initrd /ubuntu/initrd.img-${VERSION}
options cryptdevice=UUID=${UUID}:${VOLUME} root=/dev/mapper/${VOLUME} ro rootflags=${ROOTFLAGS}
EOF done done
fi
# Success!
exit 0

If your setup is simple, you might do without any ROOTFLAGS and VOLUME and the appropriate line in the script might be as follows: options root=UUID=${UUID} ro

Take care of permissions:

chown root: /etc/kernel/postinst.d/zz-update-systemd-boot
chmod 0755 /etc/kernel/postinst.d/zz-update-systemd-bootcd
/etc/kernel/postrm.d/ && ln -s ../postinst.d/zz-update-systemd-boot zz-update-systemd-boot
[ -d "/etc/initramfs/post-update.d" ] || mkdir -p /etc/initramfs/post-update.d
cd /etc/initramfs/post-update.d/ && ln -s ../../kernel/postinst.d/zz-update-systemd-boot zz-update-systemd-boot

Your /boot/efi/loader/entries/ubuntu.conf should then look something like this (obviously, you need to cahnge the UUID):

title Ubuntu GNOME
linux /ubuntu/vmlinuz
initrd /ubuntu/initrd.img
options root=UUID=81c4bc1c-1a7e-4822-acae-220bbe572240 ro

to see UUID

ubuntu@ubuntu:~$ lsblk -f
NAME FSTYPE LABEL UUID FSAVAIL FSUSE% MOUNTPOINT
loop0 squashfs 0 100% /rofs
loop1 squashfs 0 100% /snap/snapd/7264
loop2 squashfs 0 100% /snap/core18/1705
loop3 squashfs 0 100% /snap/gnome-3-34-1804/24
loop4 squashfs 0 100% /snap/gtk-common-themes/1
loop5 squashfs 0 100% /snap/snap-store/433
sda
├─sda1 vfat 1A74-A270 113.2M 61% /media/ubuntu/1A74-A270
├─sda2 swap 10842320-1286-413f-bf08-3e0ca76bcf2f [SWAP]
├─sda3 ext4 81c4bc1c-1a7e-4822-acae-220bbe572240 87.6G 13% /media/ubuntu/81c4bc1c-1a
├─sda4
├─sda5 ntfs 80D47B63D47B59FC
└─sda6 ntfs router_data 4416017316016770
sdb iso9660 Ubuntu 20.04 LTS amd64 2020-04-23-07-51-42-00
├─sdb1 iso9660 Ubuntu 20.04 LTS amd64 2020-04-23-07-51-42-00 0 100% /cdrom
├─sdb2 vfat 1AC3-20ED
└─sdb3 ext4 writable b8474e17-164a-4fb3-94ff-d4e68f2e1548 25.7G 0% /var/crash
sr0

Look up your current kernel and reinstall it to trigger the hooks you just created: sudo apt install --reinstall linux-image-5.13.0-22-generic.

Actually Install systemd-boot For most people, installation consists of a single command:

Again, this should go to the EFI partition:

bootctl install --path=/boot/efi

To verify the bootloaders installed on the system — and their order — run:

efibootmgr
reboot

once everything ok you can remove the existence of grub in your system

# Purge the packages.
apt-get purge grub*
# Purge any obsolete dependencies.
apt-get autoremove --purge

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like