mirror of
				https://git.proxmox.com/git/proxmox-kernel-helper
				synced 2025-11-04 02:55:43 +00:00 
			
		
		
		
	by setting the desired version in a dedicated file, which is used by the systemd service as condition for removing it and refreshing upon reboot. 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>
		
			
				
	
	
		
			216 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			216 lines
		
	
	
		
			5.7 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
 | 
						|
 | 
						|
 | 
						|
 | 
						|
# - 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 /etc/pve/ ]; then
 | 
						|
	LOADER_TITLE="Proxmox Virtual Environment"
 | 
						|
elif [ -d /usr/share/doc/proxmox-mailgateway/ ]; then
 | 
						|
	LOADER_TITLE="Proxmox Mailgateway"
 | 
						|
elif [ -d /usr/share/doc/proxmox-backup/ ]; then
 | 
						|
	LOADER_TITLE="Proxmox Backup Server"
 | 
						|
fi
 | 
						|
 | 
						|
update_esps() {
 | 
						|
	if [ ! -f "${ESP_LIST}" ]; then
 | 
						|
	    warn "No ${ESP_LIST} found, skipping ESP sync."
 | 
						|
	    exit 0
 | 
						|
	fi
 | 
						|
	if [ -f /etc/kernel/cmdline ]; then
 | 
						|
		# we can have cmdline files with multiple or no new line at all, handle both!
 | 
						|
		CMDLINE=$(get_first_line /etc/kernel/cmdline)
 | 
						|
		echo ${CMDLINE} | grep -q 'root=' || \
 | 
						|
			{ warn "No root= parameter in /etc/kernel/cmdline found!"; exit 1; }
 | 
						|
	else
 | 
						|
		warn "No /etc/kernel/cmdline found - falling back to /proc/cmdline"
 | 
						|
		# remove initrd entries
 | 
						|
		CMDLINE="$(perl -pe 's/\binitrd=([0-9a-zA-Z\\\/.-])*\s*//g;' /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 [ -d /sys/firmware/efi ]; then
 | 
						|
		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
 | 
						|
	elif [ ! -d "${mountpoint}/grub" ]; then
 | 
						|
		warn "${path} contains no grub directory - skipping"
 | 
						|
		return
 | 
						|
	fi
 | 
						|
	warn "Copying and configuring kernels on ${path}"
 | 
						|
	copy_and_config_kernels "${mountpoint}"
 | 
						|
 | 
						|
	pinned_kernel=$(get_first_line "${PINNED_KERNEL_CONF}")
 | 
						|
 | 
						|
	if [ -e "${NEXT_BOOT_PIN}" ]; then
 | 
						|
	    pinned_kernel=$(get_first_line "${NEXT_BOOT_PIN}")
 | 
						|
	fi
 | 
						|
	if [ -d /sys/firmware/efi ]; then
 | 
						|
		set_systemd_boot_default "${mountpoint}" "${pinned_kernel}"
 | 
						|
		remove_old_kernels_efi "${mountpoint}"
 | 
						|
	else
 | 
						|
		set_grub_default "${pinned_kernel}"
 | 
						|
		remove_old_kernels_legacy "${mountpoint}"
 | 
						|
		mount --bind "${mountpoint}" "/boot"
 | 
						|
		update-grub
 | 
						|
		umount /boot
 | 
						|
 | 
						|
	fi
 | 
						|
 | 
						|
	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
 | 
						|
 | 
						|
		if [ -d /sys/firmware/efi ]; then
 | 
						|
 | 
						|
			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 --preserve=timestamps "${linux_image}" "${KERNEL_LIVE_DIR}/"
 | 
						|
			cp --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
 | 
						|
		else
 | 
						|
			warn "	Copying kernel ${kver}"
 | 
						|
			cp --preserve=timestamps "${linux_image}" "${esp}/"
 | 
						|
			cp --preserve=timestamps "${initrd}" "${esp}/"
 | 
						|
		fi
 | 
						|
	done
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
remove_old_kernels_efi() {
 | 
						|
	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
 | 
						|
 | 
						|
}
 | 
						|
 | 
						|
remove_old_kernels_legacy() {
 | 
						|
	esp="$1"
 | 
						|
 | 
						|
	for kernel in "${esp}/"vmlinuz-*; do
 | 
						|
		kver="$(echo "${kernel}" | sed -r "s#^${esp}/vmlinuz-(.+)\$#\\1#")"
 | 
						|
 | 
						|
		echo "${BOOT_KVERS}" | grep -q "${kver}" && continue;
 | 
						|
		warn "	Removing old version ${kver}"
 | 
						|
		rm -rf "${esp}/vmlinuz-${kver}"
 | 
						|
		rm -rf "${esp}/initrd.img-${kver}"
 | 
						|
	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
 |