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

Solution 1:

after following https://blobfolio.com/2018/06/replace-grub2-with-systemd-boot-on-ubuntu-18-04/

# 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-boot Make 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