]> git.proxmox.com Git - pve-kernel-meta.git/blame - efiboot/zz-pve-efiboot
zz-efiboot: output ESP_LIST fn quoted, to avoid copying the !
[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 33
f879b040
TL
34LOADER_TITLE="Proxmox Virtual Environment"
35if [ -d /usr/share/doc/proxmox-mailgateway/ ]; then
36 LOADER_TITLE="Proxmox Mailgateway"
37fi
38
0b3c3df6
SI
39potential_esps(){
40 lsblk --list -o PATH,UUID,FSTYPE,PARTTYPE,MOUNTPOINT |
41 awk '$3 == "vfat" && $4 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && $5 == "" {print $1,$2}'
42}
b17a1cf3
SI
43
44update_esps() {
0b3c3df6 45 if [ ! -f "${ESP_LIST}" ]; then
5b03b1fd
FG
46 warn "No ${ESP_LIST} found, skipping ESP sync."
47 exit 0
0b3c3df6 48 fi
2d87fee5
FG
49 if [ -f /etc/kernel/cmdline ]; then
50 CMDLINE="$(cat /etc/kernel/cmdline)"
51 else
52 warn "No /etc/kernel/cmdline found - falling back to /proc/cmdline"
53 CMDLINE="$(cat /proc/cmdline)"
54 fi
55
401a69c9
FG
56 loop_esp_list update_esp_func
57}
b17a1cf3 58
401a69c9
FG
59update_esp_func() {
60 if ! (echo "${curr_uuid}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}'); then
61 warn "WARN: ${curr_uuid} read from ${ESP_LIST} does not look like a VFAT-UUID - skipping"
62 return
63 fi
b17a1cf3 64
401a69c9
FG
65 path="/dev/disk/by-uuid/$curr_uuid"
66 if [ ! -e "${path}" ]; then
f43873b2 67 warn "WARN: ${path} does not exist - clean '${ESP_LIST}'! - skipping"
401a69c9
FG
68 return
69 fi
b17a1cf3 70
401a69c9
FG
71 mountpoint="${MOUNTROOT}/${curr_uuid}"
72 mkdir -p "${mountpoint}" || \
73 { warn "creation of mountpoint ${mountpoint} failed - skipping"; return; }
74 mount "${path}" "${mountpoint}" || \
75 { warn "mount of ${path} failed - skipping"; return; }
76 if [ ! -f "${mountpoint}/$PMX_LOADER_CONF" ]; then
77 warn "${path} contains no loader.conf - skipping"
78 return
79 fi
80 if [ ! -d "${mountpoint}/$PMX_ESP_DIR" ]; then
81 warn "${path}/$PMX_ESP_DIR does not exist- skipping"
82 return
83 fi
b17a1cf3 84
401a69c9
FG
85 warn "Copying and configuring kernels on ${path}"
86 copy_and_config_kernels "${mountpoint}"
87 remove_old_kernels "${mountpoint}"
b17a1cf3 88
401a69c9
FG
89 umount "${mountpoint}" || \
90 { warn "umount of ${path} failed - failure"; exit 0; }
b17a1cf3 91
401a69c9 92 rmdir "${mountpoint}" || true
b17a1cf3
SI
93}
94
95copy_and_config_kernels() {
96 esp="$1"
97
98
99 for kver in ${BOOT_KVERS}; do
100
101 linux_image="/boot/vmlinuz-${kver}"
102 initrd="/boot/initrd.img-${kver}"
103
104 if [ ! -f "${linux_image}" ]; then
105 warn "No linux-image ${linux_image} found - skipping"
106 continue
107 fi
108 if [ ! -f "${initrd}" ]; then
109 warn "No initrd-image ${initrd} found - skipping"
110 continue
111 fi
112
113 warn " Copying kernel and creating boot-entry for ${kver}"
114 KERNEL_ESP_DIR="${PMX_ESP_DIR}/${kver}"
115 KERNEL_LIVE_DIR="${esp}/${KERNEL_ESP_DIR}"
116 mkdir -p "${KERNEL_LIVE_DIR}"
117 cp -u --preserve=timestamps "${linux_image}" "${KERNEL_LIVE_DIR}/"
118 cp -u --preserve=timestamps "${initrd}" "${KERNEL_LIVE_DIR}/"
119
120 # create loader entry
121 cat > "${esp}/loader/entries/proxmox-${kver}.conf" <<- EOF
f879b040 122 title ${LOADER_TITLE}
b17a1cf3
SI
123 version ${kver}
124 options ${CMDLINE}
125 linux /${KERNEL_ESP_DIR}/vmlinuz-${kver}
126 initrd /${KERNEL_ESP_DIR}/initrd.img-${kver}
127 EOF
128 done
129
130}
131
132remove_old_kernels() {
133 esp="$1"
134
135 for kerneldir in "${esp}/${PMX_ESP_DIR}"/*; do
136 if [ ! -d "${kerneldir}" ]; then
137 warn " ${kerneldir} is not a directory - skipping"
138 continue
139 fi
140
141 kver="$(echo "${kerneldir}" | sed -r "s#^${esp}/${PMX_ESP_DIR}/(.+)\$#\\1#")"
142
143 echo "${BOOT_KVERS}" | grep -q "${kver}" && continue;
144 warn " Removing old version ${kver}"
145 rm -rf "${kerneldir}"
146 rm -f "${esp}/loader/entries/proxmox-${kver}.conf"
147 done
148
149}
150
151set -- $DEB_MAINT_PARAMS
152mode="${1#\'}"
153mode="${mode%\'}"
154case $0:$mode in
155 # Only run on postinst configure and postrm remove, to avoid wasting
156 # time by calling update-grub multiple times on upgrade and removal.
157 # Also run if we have no DEB_MAINT_PARAMS, in order to work with old
158 # kernel packages.
c2f02a98 159 */postinst.d/*:|*/postinst.d/*:configure)
cfb0e459 160 reexec_in_mountns "$@"
c2f02a98
FG
161 BOOT_KVERS="$(boot_kernel_list "$@")"
162 update_esps
163 ;;
164 */postrm.d/*:|*/postrm.d/*:remove)
cfb0e459 165 reexec_in_mountns "$@"
c2f02a98
FG
166 # no newly installed kernel
167 BOOT_KVERS="$(boot_kernel_list)"
168 update_esps
b17a1cf3
SI
169 ;;
170esac
171
172exit 0