pve-kernel-meta/efiboot/zz-pve-efiboot
Thomas Lamprecht 1a72195a29 followup: code cleanup
* add some spaces for separation, increasing readability
* do not use the non-existent variable x as replacement, but an actual
  empty string ""
* don't use the "truth-y action" at end to make awk print the line ($0)
  but explicitly print $0 after the gsub, makes it easier to get for
  people with not much awk background

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
2019-10-17 07:44:52 +02:00

174 lines
4.5 KiB
Bash
Executable File

#! /bin/sh
set -e
# adapted from '/etc/kernel/postinst.d/zz-update-grub and
# /usr/lib/kernel/install.d/90-loaderentry.install, see also
# https://kernel-team.pages.debian.net/kernel-handbook/ch-update-hooks.html
MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
# - cleanup - gently delete all kernels not in kernel-keep-list
if command -V systemd-detect-virt >/dev/null 2>&1 &&
systemd-detect-virt --quiet --container; then
exit 0
fi
cleanup() {
for mount in "${MOUNTROOT}"/* ; do
if echo "${mount}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}' && \
mountpoint -q "${mount}"; then
umount "${mount}" || \
{ warn "umount of ${mount} failed - failure"; exit 0; }
fi
done
}
trap cleanup EXIT INT TERM QUIT
. /usr/share/pve-kernel-helper/scripts/functions
LOADER_TITLE="Proxmox Virtual Environment"
if [ -d /usr/share/doc/proxmox-mailgateway/ ]; then
LOADER_TITLE="Proxmox Mailgateway"
fi
potential_esps(){
lsblk --list -o PATH,UUID,FSTYPE,PARTTYPE,MOUNTPOINT |
awk '$3 == "vfat" && $4 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && $5 == "" {print $1,$2}'
}
update_esps() {
if [ ! -f "${ESP_LIST}" ]; then
warn "No ${ESP_LIST} found, skipping ESP sync."
exit 0
fi
if [ -f /etc/kernel/cmdline ]; then
CMDLINE="$(cat /etc/kernel/cmdline)"
else
warn "No /etc/kernel/cmdline found - falling back to /proc/cmdline"
# remove initrd entries
CMDLINE="$(awk '{ gsub(/\yinitrd=([0-9a-zA-Z\/\\._-])*\s*/, ""); print $0 }' /proc/cmdline)"
fi
loop_esp_list update_esp_func
}
update_esp_func() {
if ! (echo "${curr_uuid}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}'); then
warn "WARN: ${curr_uuid} read from ${ESP_LIST} does not look like a VFAT-UUID - skipping"
return
fi
path="/dev/disk/by-uuid/$curr_uuid"
if [ ! -e "${path}" ]; then
warn "WARN: ${path} does not exist - clean '${ESP_LIST}'! - skipping"
return
fi
mountpoint="${MOUNTROOT}/${curr_uuid}"
mkdir -p "${mountpoint}" || \
{ warn "creation of mountpoint ${mountpoint} failed - skipping"; return; }
mount "${path}" "${mountpoint}" || \
{ warn "mount of ${path} failed - skipping"; return; }
if [ ! -f "${mountpoint}/$PMX_LOADER_CONF" ]; then
warn "${path} contains no loader.conf - skipping"
return
fi
if [ ! -d "${mountpoint}/$PMX_ESP_DIR" ]; then
warn "${path}/$PMX_ESP_DIR does not exist- skipping"
return
fi
warn "Copying and configuring kernels on ${path}"
copy_and_config_kernels "${mountpoint}"
remove_old_kernels "${mountpoint}"
umount "${mountpoint}" || \
{ warn "umount of ${path} failed - failure"; exit 0; }
rmdir "${mountpoint}" || true
}
copy_and_config_kernels() {
esp="$1"
for kver in ${BOOT_KVERS}; do
linux_image="/boot/vmlinuz-${kver}"
initrd="/boot/initrd.img-${kver}"
if [ ! -f "${linux_image}" ]; then
warn "No linux-image ${linux_image} found - skipping"
continue
fi
if [ ! -f "${initrd}" ]; then
warn "No initrd-image ${initrd} found - skipping"
continue
fi
warn " Copying kernel and creating boot-entry for ${kver}"
KERNEL_ESP_DIR="${PMX_ESP_DIR}/${kver}"
KERNEL_LIVE_DIR="${esp}/${KERNEL_ESP_DIR}"
mkdir -p "${KERNEL_LIVE_DIR}"
cp -u --preserve=timestamps "${linux_image}" "${KERNEL_LIVE_DIR}/"
cp -u --preserve=timestamps "${initrd}" "${KERNEL_LIVE_DIR}/"
# create loader entry
cat > "${esp}/loader/entries/proxmox-${kver}.conf" <<- EOF
title ${LOADER_TITLE}
version ${kver}
options ${CMDLINE}
linux /${KERNEL_ESP_DIR}/vmlinuz-${kver}
initrd /${KERNEL_ESP_DIR}/initrd.img-${kver}
EOF
done
}
remove_old_kernels() {
esp="$1"
for kerneldir in "${esp}/${PMX_ESP_DIR}"/*; do
if [ ! -d "${kerneldir}" ]; then
warn " ${kerneldir} is not a directory - skipping"
continue
fi
kver="$(echo "${kerneldir}" | sed -r "s#^${esp}/${PMX_ESP_DIR}/(.+)\$#\\1#")"
echo "${BOOT_KVERS}" | grep -q "${kver}" && continue;
warn " Removing old version ${kver}"
rm -rf "${kerneldir}"
rm -f "${esp}/loader/entries/proxmox-${kver}.conf"
done
}
set -- $DEB_MAINT_PARAMS
mode="${1#\'}"
mode="${mode%\'}"
case $0:$mode in
# Only run on postinst configure and postrm remove, to avoid wasting
# time by calling update-grub multiple times on upgrade and removal.
# Also run if we have no DEB_MAINT_PARAMS, in order to work with old
# kernel packages.
*/postinst.d/*:|*/postinst.d/*:configure)
reexec_in_mountns "$@"
BOOT_KVERS="$(boot_kernel_list "$@")"
update_esps
;;
*/postrm.d/*:|*/postrm.d/*:remove)
reexec_in_mountns "$@"
# no newly installed kernel
BOOT_KVERS="$(boot_kernel_list)"
update_esps
;;
esac
exit 0