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