Skip to content

Instantly share code, notes, and snippets.

@txhammer68
Created December 15, 2022 05:15
Show Gist options
  • Save txhammer68/84650da9037e9d4ca94613f266eab2c1 to your computer and use it in GitHub Desktop.
Save txhammer68/84650da9037e9d4ca94613f266eab2c1 to your computer and use it in GitHub Desktop.
systemd-boot
#!/bin/bash
#
# This is a simple kernel hook to populate the systemd-boot entries
# whenever kernels are added or removed.
#/etc/kernel/postinst.d/zz-update-systemd-boot
# The UUID of your disk.
# Note: if using LVM, this should be the LVM partition.
UUID="18401d69-3d12-47f2-98dc-e236ebb454f1"
# 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. For example, mine are currently
# "subvol=@ quiet splash intel_pstate=enable".
ROOTFLAGS="quiet apparmor=1 security=apparmor loglevel=3 mitigations=off udev.log_priority=3 resume=UUID=a37d3f86-6da8-416b-b831-6e2e50378228"
ROOTFLAGS1="quiet apparmor=1 security=apparmor loglevel=3 mitigations=off udev.log_priority=3 resume=UUID=a37d3f86-6da8-416b-b831-6e2e50378228 3"
# Our kernels.
KERNELS=()
FIND="find /boot -maxdepth 1 -name 'vmlinuz-*' -type f -not -name '*.dpkg-tmp' -print0 | sort -Vrz"
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/plasma.conf
title Plasma OS
linux /ubuntu/vmlinuz
initrd /ubuntu/initrd.img
options root=UUID=${UUID} ro ${ROOTFLAGS}
EOF
done
for FILE in config initrd.img System.map vmlinuz; do
cp "/boot/${FILE}-${LATEST}" "/boot/efi/ubuntu/${FILE}"
cat << EOF > /boot/efi/loader/entries/recover.conf
title Recovery Console
linux /ubuntu/vmlinuz
initrd /ubuntu/initrd.img
options root=UUID=${UUID} ro ${ROOTFLAGS1}
EOF
done
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/kubuntu-${VERSION}.conf
title Kubuntu ${VERSION}
linux /ubuntu/vmlinuz-${VERSION}
initrd /ubuntu/initrd.img-${VERSION}
options root=UUID=${UUID} ro ${ROOTFLAGS}
EOF
done
done
fi
# Success!
echo -e "\e[2m---\e[0m"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment