]> git.proxmox.com Git - pve-kernel-meta.git/blob - efiboot/zz-pve-efiboot
zz-pve-efiboot: remove unmount warning
[pve-kernel-meta.git] / efiboot / zz-pve-efiboot
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 ESP_LIST="/etc/kernel/pve-efiboot-uuids"
10 PMX_ESP_DIR="EFI/proxmox"
11
12 MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
13
14 # - cleanup - gently delete all kernels not in kernel-keep-list
15
16 if command -V systemd-detect-virt >/dev/null 2>&1 &&
17 systemd-detect-virt --quiet --container; then
18 exit 0
19 fi
20
21 cleanup() {
22
23 for mount in "${MOUNTROOT}"/* ; do
24 if echo "${mount}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}' && \
25 mountpoint -q "${mount}"; then
26 umount "${mount}"
27 fi
28 done
29
30 }
31
32 trap cleanup EXIT INT TERM QUIT
33
34 . /usr/share/pve-kernel-helper/scripts/functions
35
36 BOOT_KVERS="$(boot_kernel_list "$@")"
37
38 if [ -f /etc/kernel/cmdline ]; then
39 CMDLINE="$(cat /etc/kernel/cmdline)"
40 else
41 warn "No /etc/kernel/cmdline found - falling back to /proc/cmdline"
42 CMDLINE="$(cat /proc/cmdline)"
43 fi
44
45 potential_esps(){
46 lsblk --list -o PATH,UUID,FSTYPE,PARTTYPE,MOUNTPOINT |
47 awk '$3 == "vfat" && $4 == "c12a7328-f81f-11d2-ba4b-00a0c93ec93b" && $5 == "" {print $1,$2}'
48 }
49
50 update_esps() {
51 if [ ! -f "${ESP_LIST}" ]; then
52 warn "No ${ESP_LIST} found cannot update installed kernels - exiting"
53 esps="$(potential_esps)"
54 warn "add the UUIDs of one of the following devices to ${ESP_LIST}:"
55 warn "${esps}"
56 exit 1
57 fi
58 cat "${ESP_LIST}" | while IFS= read -r uuid; do
59 if ! (echo "${uuid}" | grep -qE '[0-9a-fA-F]{4}-[0-9a-fA-F]{4}'); then
60 warn "WARN: ${uuid} read from ${ESP_LIST} does not look like a VFAT-UUID - skipping"
61 continue
62 fi
63
64 path="/dev/disk/by-uuid/$uuid"
65 if [ ! -e "${path}" ]; then
66 warn "WARN: ${path} does not exist - clean ${ESP_LIST}! - skipping"
67 continue
68 fi
69
70 mountpoint="${MOUNTROOT}/${uuid}"
71 mkdir -p "${mountpoint}"
72 mount "${path}" "${mountpoint}" || \
73 { warn "mount of ${path} failed - skipping"; continue; }
74 if [ ! -f "${mountpoint}/loader/loader.conf" ]; then
75 warn "${path} contains no loader.conf - skipping"
76 continue
77 fi
78 if [ ! -d "${mountpoint}/EFI/proxmox" ]; then
79 warn "${path} contains no EFI/proxmox - skipping"
80 continue
81 fi
82
83 warn "Copying and configuring kernels on ${path}"
84 copy_and_config_kernels "${mountpoint}"
85 remove_old_kernels "${mountpoint}"
86
87 umount "${mountpoint}" || \
88 { warn "umount of ${path} failed - failure"; exit 2; }
89
90 rmdir "${mountpoint}"
91 done
92
93 }
94
95 copy_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
122 title Proxmox
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
132 remove_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
151 set -- $DEB_MAINT_PARAMS
152 mode="${1#\'}"
153 mode="${mode%\'}"
154 case $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.
159 */postinst.d/*:|*/postinst.d/*:configure|*/postrm.d/*:|*/postrm.d/*:remove)
160 update_esps
161 ;;
162 esac
163
164 exit 0