pve-kernel-meta/proxmox-boot/functions
Stoiko Ivanov 83a41a1a03 fix #3761: proxmox-boot: add pin/unpin for kernel-version
The 2 commands follow the mechanics of p-b-t kernel add/remove in
writing the desired abi-version to a config-file in /etc/kernel and
actually modifying the boot-loader configuration upon p-b-t refresh.

A dedicated new file is used instead of writing the version (with some
kind of annotation) to the manual kernel list to keep parsing the file
simple (and hopefully also cause fewer problems with manually edited
files)

For systemd-boot we write the entry into the loader.conf on the ESP(s)
instead of relying on the `bootctl set-default` mechanics (bootctl(1))
which write the entry in an EFI-var. This was preferred, because of a
few reports of unwriteable EFI-vars on some systems (e.g. DELL servers
have a setting preventing writing EFI-vars from the OS). The rationale
in `Why not simply rely on the EFI boot menu logic?` from [0] also
makes a few points in that direction.

For grub the following choices were made:
* write the pinned version (or actually the menu-path leading to it)
  to a snippet in /etc/default/grub.d instead of editing the grub.cfg
  files on the partition. Mostly to divert as little as possible from
  the grub-workflow I assume people are used to.
* the 'root-device-id' part of the menu-entries is parsed from
  /boot/grub/grug.cfg since it was stable (the same on all ESPs and in
  /boot/grub), saves us from copying the part of "find device behind
  /, mangle it if zfs/btrfs, call grub_probe a few times" part of
  grub-mkconfig - and seems a bit more robust

Tested with a BIOS and an UEFI VM with / on ZFS.

[0] https://systemd.io/BOOT_LOADER_SPECIFICATION/

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
Reviewed-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Tested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2022-02-22 13:31:31 +01:00

154 lines
4.1 KiB
Bash
Executable File

#! /bin/sh
set -e
ESP_LIST="/etc/kernel/proxmox-boot-uuids"
ESPTYPE='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
MANUAL_KERNEL_LIST="/etc/kernel/pve-efiboot-manual-kernels"
PINNED_KERNEL_CONF="/etc/kernel/proxmox-boot-pin"
MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
# relative to the ESP mountpoint
PMX_ESP_DIR="EFI/proxmox"
PMX_LOADER_CONF="loader/loader.conf"
GRUB_PIN_SNIPPET="/etc/default/grub.d/proxmox-kernel-pin.cfg"
# adapted from /etc/kernel/postinst.d/apt-auto-removal as present in
# debian's apt package:
#
# Mark as not-for-autoremoval those kernel packages that are:
# - the currently booted version, if still installed
# - the kernel version we've been called for
# - the latest kernel version (as determined by debian version number)
# - the second-latest kernel version
# - the latest kernel version of each series (e.g. 4.13, 4.15, 5.0) by
# marking the meta-packages
# - the currently pinned kernel if any
kernel_keep_versions() {
eval "$(apt-config shell DPKG Dir::bin::dpkg/f)"
test -n "$DPKG" || DPKG="/usr/bin/dpkg"
list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+pve-kernel-[0-9]+\./ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2; }' \
| sed -e 's#^pve-kernel-##' -e 's#:[^:]\+ # #')"
sorted_list="$(echo "$list" | sort --unique --reverse --version-sort)"
[ -n "$1" ] && install_version="$1"
running_version="$(uname -r | tr 'A-Z' 'a-z')"
# ignore the currently running version if attempting a reproducible build
if [ -n "${SOURCE_DATE_EPOCH}" ]; then
running_version=""
elif [ ! -e "/boot/vmlinuz-$running_version" ]; then
# ignore the current version if it got removed, the "auto-remove" logic
# will not be affected, because either it is installed and thus we keep
# it in the list, or it's already removed anyway
running_version=""
fi
latest_2_versions="$(echo "$sorted_list" | grep -E '^[^ ]+-pve' | head -n2 )"
series_metapackages="$(echo "$sorted_list" | grep -Ev '^[^ ]+-pve' | head -n2)"
oldseries="$(echo "$series_metapackages" | tail -n1)"
oldseries_latest_kernel="$(echo "$sorted_list" | grep -E "^${oldseries}\.[^ ]+-pve" | head -n1 )"
if [ -e "$MANUAL_KERNEL_LIST" ]; then
manual_kernels="$(cat "$MANUAL_KERNEL_LIST")"
fi
pinned_kernel="$(get_first_line "$PINNED_KERNEL_CONF")"
kernels="$(cat <<-EOF
$running_version
$install_version
$manual_kernels
$latest_2_versions
$series_metapackages
$oldseries_latest_kernel
$pinned_kernel
EOF
)"
echo "$kernels" | sort -u | sed -e '/^$/ d'
}
#bootable kernels are the same as the no_autoremove ones without the meta-package
boot_kernel_list() {
list="$(kernel_keep_versions "$@")"
echo "$list" | grep -vE '^[0-9]+\.[0-9]+$' || true
}
warn() {
echo "$@" 1>&2
}
reexec_in_mountns() {
if [ -z "$PVE_EFIBOOT_UNSHARED" ]; then
export PVE_EFIBOOT_UNSHARED=1
echo "Re-executing '$0' in new private mount namespace.."
unshare --mount --propagation private "$0" "$@"
exit 0
fi
}
loop_esp_list() {
if [ ! -e ${ESP_LIST} ]; then
return 2
fi
cat "${ESP_LIST}" | while IFS= read -r curr_uuid; do
if [ -z "$curr_uuid" ]; then
continue
fi
"$@"
done
}
get_first_line() {
file="$1"
if [ ! -e "$file" ]; then
echo ""
return
fi
while IFS= read -r line || [ -n "$line" ]; do
break
done < "${file}"
echo "$line"
}
set_grub_default() {
kver="$1"
if [ -z "${kver}" ]; then
rm -f "${GRUB_PIN_SNIPPET}"
else
# grub menu entry ids contain the internal root-device id (e.g. for zfs the GUID of
# the pool printed in hex) as this is independent of the ESP (or grub location)
# take it from /boot/grub/grub.cfg
root_devid=$(sed -rn "s/.*gnulinux-advanced-(.+)['] \{$/\1/p" \
/boot/grub/grub.cfg)
entry="gnulinux-advanced-${root_devid}>gnulinux-${kver}-advanced-${root_devid}"
echo "GRUB_DEFAULT=\"${entry}\"" > "${GRUB_PIN_SNIPPET}"
fi
}
set_systemd_boot_default() {
mountpoint="$1"
kver="$2"
if [ -z "${kver}" ]; then
entry="proxmox-*"
else
entry="proxmox-${kver}.conf"
fi
# replaces the current default entry, if one exists else append it at the end of the file
sed -ri "/^default /{h;s/ .*\$/ ${entry}/};\${x;/^$/{s//default ${entry}/;H};x}" \
"${mountpoint}/$PMX_LOADER_CONF"
}