Advertisement
Guest User

zz-update-systemd-boot

a guest
Dec 23rd, 2020
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # This is a simple kernel hook to populate the systemd-boot entries
  4. # whenever kernels are added or removed.
  5.  
  6. # The UUID of your root "/" disk.
  7. UUID="852b5870-0055-4d5b-91f7-101e5ead3b6f"
  8.  
  9. # Any rootflags you wish to set. For example, mine are currently
  10. # "subvol=@ quiet splash intel_pstate=enable".
  11. #ROOTFLAGS="subvol=@"
  12.  
  13. # Our kernels.
  14. KERNELS=()
  15. FIND="find /boot -maxdepth 1 -name 'vmlinuz-*' -type f -not -name '*.dpkg-tmp' -print0 | sort -Vrz"
  16. while IFS= read -r -u3 -d $'\0' LINE; do
  17. KERNEL=$(basename "${LINE}")
  18. KERNELS+=("${KERNEL:8}")
  19. done 3< <(eval "${FIND}")
  20.  
  21. # There has to be at least one kernel.
  22. if [ ${#KERNELS[@]} -lt 1 ]; then
  23. echo -e "\e[2msystemd-boot\e[0m \e[1;31mNo kernels found.\e[0m"
  24. exit 1
  25. fi
  26.  
  27. # Perform a nuclear clean to ensure everything is always in
  28. # perfect sync.
  29. rm /boot/efi/loader/entries/*.conf
  30. rm -rf /boot/efi/ubuntu
  31. mkdir /boot/efi/ubuntu
  32.  
  33. # Copy the latest kernel files to a consistent place so we can
  34. # keep using the same loader configuration.
  35. LATEST="${KERNELS[@]:0:1}"
  36. echo -e "\e[2msystemd-boot\e[0m \e[1;32m${LATEST}\e[0m"
  37. for FILE in config initrd.img System.map vmlinuz; do
  38. cp "/boot/${FILE}-${LATEST}" "/boot/efi/ubuntu/${FILE}"
  39. cat << EOF > /boot/efi/loader/entries/ubuntu.conf
  40. title Ubuntu
  41. linux /ubuntu/vmlinuz
  42. initrd /ubuntu/initrd.img
  43. options root=UUID=${UUID} rw
  44. EOF
  45. done
  46.  
  47. # Copy any legacy kernels over too, but maintain their version-
  48. # based names to avoid collisions.
  49. if [ ${#KERNELS[@]} -gt 1 ]; then
  50. LEGACY=("${KERNELS[@]:1}")
  51. for VERSION in "${LEGACY[@]}"; do
  52. echo -e "\e[2msystemd-boot\e[0m \e[1;32m${VERSION}\e[0m"
  53. for FILE in config initrd.img System.map vmlinuz; do
  54. cp "/boot/${FILE}-${VERSION}" "/boot/efi/ubuntu/${FILE}-${VERSION}"
  55. cat << EOF > /boot/efi/loader/entries/ubuntu-${VERSION}.conf
  56. title Ubuntu ${VERSION}
  57. linux /ubuntu/vmlinuz-${VERSION}
  58. initrd /ubuntu/initrd.img-${VERSION}
  59. options root=UUID=${UUID} rw
  60. EOF
  61. done
  62. done
  63. fi
  64.  
  65. # reset the boot order so "ubuntu" comes first. It's difficult to predict
  66. # other entry situations, so we'll just focus on this one goal. This assumes
  67. # that the ubuntu boot option is "active" (has an asterisk after it).
  68. curorder=`efibootmgr | grep ^BootOrder | cut -d" " -f2`
  69. ubuntu=`efibootmgr | grep " ubuntu$" | cut -c5-8`
  70. # remove the current ubuntu entry (3 ways) if present, and prepend ubuntu and trim more than 5th entry
  71. tmporder=`echo $curorder | sed "s/,$ubuntu,/,/" | sed "s/$ubuntu,//" | sed "s/,$ubuntu//" | sed "s/^/$ubuntu,/" | cut -d"," -f1-5`
  72. efibootmgr --bootorder $tmporder
  73.  
  74. # Success!
  75. echo -e "\e[2m---\e[0m"
  76. exit 0
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement