]> git.proxmox.com Git - pve-kernel-meta.git/blob - efiboot/zz-pve-efiboot
ad78caf6011965deb497aaaf380826b08bc86796
[pve-kernel-meta.git] / efiboot / zz-pve-efiboot
1 #! /bin/sh
2 set -e
3
4 # adapted from '/etc/kernel/postinst.d/zz-update-grub and
5 # /usr/lib/kernel/install.d/90-loaderentry.install, see also
6 # https://kernel-team.pages.debian.net/kernel-handbook/ch-update-hooks.html
7
8 # relative to the ESP mountpoint
9 PMX_ESP_DIR="EFI/proxmox"
10
11 MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
12
13 # - cleanup - gently delete all kernels not in kernel-keep-list
14
15 if command -V systemd-detect-virt >/dev/null 2>&1 &&
16 systemd-detect-virt --quiet --container; then
17 exit 0
18 fi
19
20 cleanup() {
21
22 warn "unmounting ESPs"
23 for mount in "${MOUNTROOT}"/* ; do
24 if echo "${mount}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}' && \
25 mountpoint -q "${mount}"; then
26 umount "${mount}"
27 fi
28 done
29
30 }
31
32 trap cleanup EXIT INT TERM QUIT
33
34 . /usr/share/pve-kernel-helper/scripts/functions
35
36 BOOT_KVERS="$(boot_kernel_list "$@")"
37
38 if [ -f /etc/kernel/cmdline ]; then
39 CMDLINE="$(cat /etc/kernel/cmdline)"
40 else
41 warn "No /etc/kernel/cmdline found - falling back to /proc/cmdline"
42 CMDLINE="$(cat /proc/cmdline)"
43 fi
44
45
46 update_esps() {
47 esps="$(lsblk --list -o PATH,UUID,FSTYPE,PARTTYPE,MOUNTPOINT |
48 awk -v OFS=';' '$3 == "vfat" && $4 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && $5 == "" {print $1,$2}')"
49
50 for esp in ${esps}; do
51 path="$(echo "${esp}" | cut -d ';' -f1)"
52 uuid="$(echo "${esp}" | cut -d ';' -f2)"
53 mountpoint="${MOUNTROOT}/${uuid}"
54
55 mkdir -p "${mountpoint}"
56 mount "${path}" "${mountpoint}" || \
57 { warn "mount of ${esp} failed - skipping"; continue; }
58 if [ ! -f "${mountpoint}/loader/loader.conf" ]; then
59 warn "${path} contains no loader.conf - skipping"
60 continue
61 fi
62 if [ ! -d "${mountpoint}/EFI/proxmox" ]; then
63 warn "${path} contains no EFI/proxmox - skipping"
64 continue
65 fi
66
67 warn "Copying and configuring kernels on ${path}"
68 copy_and_config_kernels "${mountpoint}"
69 remove_old_kernels "${mountpoint}"
70
71 umount "${mountpoint}" || \
72 { warn "umount of ${esp} failed - failure"; exit 2; }
73
74 rmdir "${mountpoint}"
75 done
76
77 }
78
79 copy_and_config_kernels() {
80 esp="$1"
81
82
83 for kver in ${BOOT_KVERS}; do
84
85 linux_image="/boot/vmlinuz-${kver}"
86 initrd="/boot/initrd.img-${kver}"
87
88 if [ ! -f "${linux_image}" ]; then
89 warn "No linux-image ${linux_image} found - skipping"
90 continue
91 fi
92 if [ ! -f "${initrd}" ]; then
93 warn "No initrd-image ${initrd} found - skipping"
94 continue
95 fi
96
97 warn " Copying kernel and creating boot-entry for ${kver}"
98 KERNEL_ESP_DIR="${PMX_ESP_DIR}/${kver}"
99 KERNEL_LIVE_DIR="${esp}/${KERNEL_ESP_DIR}"
100 mkdir -p "${KERNEL_LIVE_DIR}"
101 cp -u --preserve=timestamps "${linux_image}" "${KERNEL_LIVE_DIR}/"
102 cp -u --preserve=timestamps "${initrd}" "${KERNEL_LIVE_DIR}/"
103
104 # create loader entry
105 cat > "${esp}/loader/entries/proxmox-${kver}.conf" <<- EOF
106 title Proxmox
107 version ${kver}
108 options ${CMDLINE}
109 linux /${KERNEL_ESP_DIR}/vmlinuz-${kver}
110 initrd /${KERNEL_ESP_DIR}/initrd.img-${kver}
111 EOF
112 done
113
114 }
115
116 remove_old_kernels() {
117 esp="$1"
118
119 for kerneldir in "${esp}/${PMX_ESP_DIR}"/*; do
120 if [ ! -d "${kerneldir}" ]; then
121 warn " ${kerneldir} is not a directory - skipping"
122 continue
123 fi
124
125 kver="$(echo "${kerneldir}" | sed -r "s#^${esp}/${PMX_ESP_DIR}/(.+)\$#\\1#")"
126
127 echo "${BOOT_KVERS}" | grep -q "${kver}" && continue;
128 warn " Removing old version ${kver}"
129 rm -rf "${kerneldir}"
130 rm -f "${esp}/loader/entries/proxmox-${kver}.conf"
131 done
132
133 }
134
135 set -- $DEB_MAINT_PARAMS
136 mode="${1#\'}"
137 mode="${mode%\'}"
138 case $0:$mode in
139 # Only run on postinst configure and postrm remove, to avoid wasting
140 # time by calling update-grub multiple times on upgrade and removal.
141 # Also run if we have no DEB_MAINT_PARAMS, in order to work with old
142 # kernel packages.
143 */postinst.d/*:|*/postinst.d/*:configure|*/postrm.d/*:|*/postrm.d/*:remove)
144 update_esps
145 ;;
146 esac
147
148 exit 0