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