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