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