Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active July 27, 2022 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdamjan/ccdcda2c91119406a0f8d22f8b8f2c4a to your computer and use it in GitHub Desktop.
Save gdamjan/ccdcda2c91119406a0f8d22f8b8f2c4a to your computer and use it in GitHub Desktop.
Replace grub2 with systemd-boot on Ubuntu 22.04
  1. remove grub
apt purge --allow-remove-essential grub2-common grub-pc-bin grub-pc grub-gfxpayload-lists grub-efi-amd64-bin grub-efi-amd64-signed grub-common os-prober shim-signed libfreetype6 
apt-get autoremove --purge
rm -rf /boot/grub/
rm -rf /boot/efi/EFI/ubuntu
  1. make sure it's not installed back
    • edit /etc/apt/apt.conf.d/01autoremove
    • add "grub*"; at the end of the Never-MarkAuto-Sections section
  2. install systemd-boot (bootctl install)
  3. create /etc/kernel/cmdline with the wanted kernel command line (check /proc/cmdline for inspiration)
  4. create /etc/kernel/postinst.d/zz-update-systemd-boot (see below, and make sure it's 0755 mode) and run it
  5. reboot
#!/bin/bash
#
# This is a simple kernel hook to populate the systemd-boot entries
# whenever kernels are added or removed.
#
# 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
LATEST="${KERNELS[@]:0:1}"
echo -e "\e[2msystemd-boot\e[0m \e[1;32m${LATEST}\e[0m"
objcopy \
--add-section .osrel=/etc/os-release --change-section-vma .osrel=0x20000 \
--add-section .cmdline=/etc/kernel/cmdline --change-section-vma .cmdline=0x30000 \
--add-section .linux=/boot/vmlinuz-${LATEST} --change-section-vma .linux=0x2000000 \
--add-section .initrd=/boot/initrd.img-${LATEST} --change-section-vma .initrd=0x3000000 \
/usr/lib/systemd/boot/efi/linuxx64.efi.stub \
/boot/efi/EFI/Linux/ubuntu.efi
# Success!
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment