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