Skip to content

Instantly share code, notes, and snippets.

@brad-jones
Created June 13, 2018 05:56
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 brad-jones/db91242277654d873c062d19bcafdbd3 to your computer and use it in GitHub Desktop.
Save brad-jones/db91242277654d873c062d19bcafdbd3 to your computer and use it in GitHub Desktop.
Useful for when you want to dual boot with MacOs or not use Grub.
#!/usr/bin/env bash
set -eo pipefail;
echo "Fedora Systemd-Boot Kernel Update Script";
echo "================================================================================";
latestVmlinuz="$(ls -t /boot/vmlinuz* | head -1)";
latestVersion="$(echo $latestVmlinuz | sed -e 's~/boot/vmlinuz-~~' -e 's~.x86_64~~')";
echo "Installing latest kernel ($latestVersion) into systemd-boot";
latestVmlinuzHmac="/boot/.vmlinuz-$latestVersion.x86_64.hmac";
latestInitramfs="/boot/initramfs-$latestVersion.x86_64.img";
latestConfig="/boot/config-$latestVersion.x86_64";
latestSystemMap="/boot/System.map-$latestVersion.x86_64";
if [[ ! -f "$latestVmlinuz" || ! -f "$latestVmlinuzHmac" || ! -f "$latestInitramfs" || ! -f "$latestConfig" || ! -f "$latestSystemMap" ]];
then
echo "Kernel version mis-match, nothing done.";
exit 1;
fi
echo "Mounting /dev/sda1 /mnt/boot";
if [[ ! -d /mnt/boot ]]; then mkdir -p /mnt/boot; fi
mount /dev/sda1 /mnt/boot;
if [[ -f "/mnt/boot/loader/entries/fedora-$latestVersion.conf" ]];
then
echo "Kernel is up to date, nothing to do.";
umount /mnt/boot;
exit 0;
fi
echo "Copying $latestConfig -> /mnt/boot/fedora/config-$latestVersion.x86_64";
cp $latestConfig /mnt/boot/fedora/;
echo "Copying $latestVmlinuz -> /mnt/boot/fedora/vmlinuz-$latestVersion.x86_64";
cp $latestVmlinuz /mnt/boot/fedora/;
echo "Copying $latestVmlinuzHmac -> /mnt/boot/fedora/.vmlinuz-$latestVersion.x86_64.hmac";
cp $latestVmlinuzHmac /mnt/boot/fedora/;
echo "Copying $latestInitramfs -> /mnt/boot/fedora/initramfs-$latestVersion.x86_64.img";
cp $latestInitramfs /mnt/boot/fedora/;
echo "Copying $latestSystemMap -> /mnt/boot/fedora/System.map-$latestVersion.x86_64";
cp $latestSystemMap /mnt/boot/fedora/;
bootEntry="/mnt/boot/loader/entries/fedora-$latestVersion.conf";
echo "Writing new boot entry ($bootEntry)";
echo -e "title Fedora $latestVersion\n" > $bootEntry;
echo -e "linux /fedora/vmlinuz-$latestVersion.x86_64\n" >> $bootEntry;
echo -e "initrd /fedora/initramfs-$latestVersion.x86_64.img\n" >> $bootEntry;
echo -e "options root=/dev/mapper/fedora-root ro rd.lvm.lv=fedora/root rd.lvm.lv=fedora/swap rhgb quiet LANG=en_AU.UTF-8" >> $bootEntry;
echo "Updating the default entry in (/mnt/boot/loader/loader.conf)";
sed -i "s/^default .*\$/default fedora-$latestVersion/" /mnt/boot/loader/loader.conf
for file in /mnt/boot/fedora/*;
do
filename="/boot/$(basename $file)";
if [[ ! -f "$filename" ]];
then
echo "Deleting old kernel files $file";
rm -f "$file";
if [[ "$filename" == *"/boot/vmlinuz-"* ]];
then
oldVersion="$(echo $filename | sed -e 's~/boot/vmlinuz-~~' -e 's~.x86_64~~')";
echo "Deleting old boot entry /mnt/boot/loader/entries/fedora-$oldVersion.conf";
rm -f "/mnt/boot/loader/entries/fedora-$oldVersion.conf";
fi
fi
done
umount /mnt/boot;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment