]> git.proxmox.com Git - pve-kernel-meta.git/blame - efiboot/zz-pve-efiboot
efiboot: always exit cleanly
[pve-kernel-meta.git] / efiboot / zz-pve-efiboot
CommitLineData
b17a1cf3
SI
1#! /bin/sh
2set -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
b17a1cf3
SI
8
9MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
10
b17a1cf3
SI
11# - cleanup - gently delete all kernels not in kernel-keep-list
12
b17a1cf3
SI
13if command -V systemd-detect-virt >/dev/null 2>&1 &&
14 systemd-detect-virt --quiet --container; then
15 exit 0
16fi
17
18cleanup() {
19
b17a1cf3
SI
20 for mount in "${MOUNTROOT}"/* ; do
21 if echo "${mount}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}' && \
22 mountpoint -q "${mount}"; then
a8dad4e8
FG
23 umount "${mount}" || \
24 { warn "umount of ${mount} failed - failure"; exit 0; }
b17a1cf3
SI
25 fi
26 done
27
28}
29
30trap cleanup EXIT INT TERM QUIT
31
fbf55ac2 32. /usr/share/pve-kernel-helper/scripts/functions
b17a1cf3
SI
33
34BOOT_KVERS="$(boot_kernel_list "$@")"
35
0b3c3df6
SI
36potential_esps(){
37 lsblk --list -o PATH,UUID,FSTYPE,PARTTYPE,MOUNTPOINT |
38 awk '$3 == "vfat" && $4 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && $5 == "" {print $1,$2}'
39}
b17a1cf3
SI
40
41update_esps() {
0b3c3df6 42 if [ ! -f "${ESP_LIST}" ]; then
5b03b1fd
FG
43 warn "No ${ESP_LIST} found, skipping ESP sync."
44 exit 0
0b3c3df6 45 fi
2d87fee5
FG
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
0b3c3df6
SI
53 cat "${ESP_LIST}" | while IFS= read -r uuid; do
54 if ! (echo "${uuid}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}'); then
55 warn "WARN: ${uuid} read from ${ESP_LIST} does not look like a VFAT-UUID - skipping"
56 continue
57 fi
b17a1cf3 58
0b3c3df6
SI
59 path="/dev/disk/by-uuid/$uuid"
60 if [ ! -e "${path}" ]; then
61 warn "WARN: ${path} does not exist - clean ${ESP_LIST}! - skipping"
62 continue
63 fi
b17a1cf3 64
0b3c3df6 65 mountpoint="${MOUNTROOT}/${uuid}"
a8dad4e8
FG
66 mkdir -p "${mountpoint}" || \
67 { warn "creation of mountpoint ${mountpoint} failed - skipping"; continue; }
b17a1cf3 68 mount "${path}" "${mountpoint}" || \
0b3c3df6 69 { warn "mount of ${path} failed - skipping"; continue; }
e3928378 70 if [ ! -f "${mountpoint}/$PMX_LOADER_CONF" ]; then
b17a1cf3
SI
71 warn "${path} contains no loader.conf - skipping"
72 continue
73 fi
e3928378
FG
74 if [ ! -d "${mountpoint}/$PMX_ESP_DIR" ]; then
75 warn "${path}/$PMX_ESP_DIR does not exist- skipping"
b17a1cf3
SI
76 continue
77 fi
78
79 warn "Copying and configuring kernels on ${path}"
80 copy_and_config_kernels "${mountpoint}"
81 remove_old_kernels "${mountpoint}"
82
83 umount "${mountpoint}" || \
a8dad4e8 84 { warn "umount of ${path} failed - failure"; exit 0; }
b17a1cf3 85
a8dad4e8 86 rmdir "${mountpoint}" || true
b17a1cf3
SI
87 done
88
89}
90
91copy_and_config_kernels() {
92 esp="$1"
93
94
95 for kver in ${BOOT_KVERS}; do
96
97 linux_image="/boot/vmlinuz-${kver}"
98 initrd="/boot/initrd.img-${kver}"
99
100 if [ ! -f "${linux_image}" ]; then
101 warn "No linux-image ${linux_image} found - skipping"
102 continue
103 fi
104 if [ ! -f "${initrd}" ]; then
105 warn "No initrd-image ${initrd} found - skipping"
106 continue
107 fi
108
109 warn " Copying kernel and creating boot-entry for ${kver}"
110 KERNEL_ESP_DIR="${PMX_ESP_DIR}/${kver}"
111 KERNEL_LIVE_DIR="${esp}/${KERNEL_ESP_DIR}"
112 mkdir -p "${KERNEL_LIVE_DIR}"
113 cp -u --preserve=timestamps "${linux_image}" "${KERNEL_LIVE_DIR}/"
114 cp -u --preserve=timestamps "${initrd}" "${KERNEL_LIVE_DIR}/"
115
116 # create loader entry
117 cat > "${esp}/loader/entries/proxmox-${kver}.conf" <<- EOF
118 title Proxmox
119 version ${kver}
120 options ${CMDLINE}
121 linux /${KERNEL_ESP_DIR}/vmlinuz-${kver}
122 initrd /${KERNEL_ESP_DIR}/initrd.img-${kver}
123 EOF
124 done
125
126}
127
128remove_old_kernels() {
129 esp="$1"
130
131 for kerneldir in "${esp}/${PMX_ESP_DIR}"/*; do
132 if [ ! -d "${kerneldir}" ]; then
133 warn " ${kerneldir} is not a directory - skipping"
134 continue
135 fi
136
137 kver="$(echo "${kerneldir}" | sed -r "s#^${esp}/${PMX_ESP_DIR}/(.+)\$#\\1#")"
138
139 echo "${BOOT_KVERS}" | grep -q "${kver}" && continue;
140 warn " Removing old version ${kver}"
141 rm -rf "${kerneldir}"
142 rm -f "${esp}/loader/entries/proxmox-${kver}.conf"
143 done
144
145}
146
147set -- $DEB_MAINT_PARAMS
148mode="${1#\'}"
149mode="${mode%\'}"
150case $0:$mode in
151 # Only run on postinst configure and postrm remove, to avoid wasting
152 # time by calling update-grub multiple times on upgrade and removal.
153 # Also run if we have no DEB_MAINT_PARAMS, in order to work with old
154 # kernel packages.
155 */postinst.d/*:|*/postinst.d/*:configure|*/postrm.d/*:|*/postrm.d/*:remove)
156 update_esps
157 ;;
158esac
159
160exit 0