]> git.proxmox.com Git - pve-kernel-meta.git/blob - proxmox-boot/functions
bump version to 7.3-8
[pve-kernel-meta.git] / proxmox-boot / functions
1 #! /bin/sh
2 set -e
3
4 ESP_LIST="/etc/kernel/proxmox-boot-uuids"
5 ESPTYPE='c12a7328-f81f-11d2-ba4b-00a0c93ec93b'
6
7 MANUAL_KERNEL_LIST="/etc/kernel/proxmox-boot-manual-kernels"
8 PINNED_KERNEL_CONF="/etc/kernel/proxmox-boot-pin"
9 NEXT_BOOT_PIN="/etc/kernel/next-boot-pin"
10
11 MOUNTROOT="${TMPDIR:-/var/tmp}/espmounts"
12 # relative to the ESP mountpoint
13 PMX_ESP_DIR="EFI/proxmox"
14 PMX_LOADER_CONF="loader/loader.conf"
15 GRUB_PIN_SNIPPET="/etc/default/grub.d/proxmox-kernel-pin.cfg"
16
17 # adapted from /etc/kernel/postinst.d/apt-auto-removal as present in
18 # debian's apt package:
19 #
20 # Mark as not-for-autoremoval those kernel packages that are:
21 # - the currently booted version, if still installed
22 # - the kernel version we've been called for
23 # - the latest kernel version (as determined by debian version number)
24 # - the second-latest kernel version
25 # - the latest kernel version of each series (e.g. 4.13, 4.15, 5.0) by
26 # marking the meta-packages
27 # - the currently pinned kernel if any
28
29 kernel_keep_versions() {
30 eval "$(apt-config shell DPKG Dir::bin::dpkg/f)"
31 test -n "$DPKG" || DPKG="/usr/bin/dpkg"
32
33 list="$("${DPKG}" -l | awk '/^[ih][^nc][ ]+pve-kernel-[0-9]+\./ && $2 !~ /-dbg(:.*)?$/ && $2 !~ /-dbgsym(:.*)?$/ { print $2; }' \
34 | sed -e 's#^pve-kernel-##' -e 's#:[^:]\+ # #')"
35
36 sorted_list="$(echo "$list" | sort --unique --reverse --version-sort)"
37
38 [ -n "$1" ] && install_version="$1"
39
40 running_version="$(uname -r | tr 'A-Z' 'a-z')"
41
42 # ignore the currently running version if attempting a reproducible build
43 if [ -n "${SOURCE_DATE_EPOCH}" ]; then
44 running_version=""
45 elif [ ! -e "/boot/vmlinuz-$running_version" ]; then
46 # ignore the current version if it got removed, the "auto-remove" logic
47 # will not be affected, because either it is installed and thus we keep
48 # it in the list, or it's already removed anyway
49 running_version=""
50 fi
51
52 latest_2_versions="$(echo "$sorted_list" | grep -E '^[^ ]+-pve' | head -n2 )"
53
54 series_metapackages="$(echo "$sorted_list" | grep -Ev '^[^ ]+-pve' | head -n2)"
55
56 oldseries="$(echo "$series_metapackages" | tail -n1)"
57 oldseries_latest_kernel="$(echo "$sorted_list" | grep -E "^${oldseries}\.[^ ]+-pve" | head -n1 )"
58
59 if [ -e "$MANUAL_KERNEL_LIST" ]; then
60 manual_kernels="$(cat "$MANUAL_KERNEL_LIST")"
61 fi
62
63 pinned_kernel="$(get_first_line "$PINNED_KERNEL_CONF")"
64 nextboot_kernel="$(get_first_line "$NEXT_BOOT_PIN")"
65
66 kernels="$(cat <<-EOF
67 $running_version
68 $install_version
69 $manual_kernels
70 $latest_2_versions
71 $series_metapackages
72 $oldseries_latest_kernel
73 $pinned_kernel
74 $nextboot_kernel
75 EOF
76 )"
77
78 echo "$kernels" | sort -u | sed -e '/^$/ d'
79 }
80
81 #bootable kernels are the same as the no_autoremove ones without the meta-package
82 boot_kernel_list() {
83 list="$(kernel_keep_versions "$@")"
84
85 echo "$list" | grep -vE '^[0-9]+\.[0-9]+$' || true
86 }
87
88 warn() {
89 echo "$@" 1>&2
90 }
91
92 reexec_in_mountns() {
93 if [ -z "$PVE_EFIBOOT_UNSHARED" ]; then
94 export PVE_EFIBOOT_UNSHARED=1
95 echo "Re-executing '$0' in new private mount namespace.."
96 unshare --mount --propagation private "$0" "$@"
97 exit 0
98 fi
99 }
100
101 loop_esp_list() {
102 if [ ! -e ${ESP_LIST} ]; then
103 return 2
104 fi
105
106 cat "${ESP_LIST}" | while IFS= read -r curr_uuid; do
107 if [ -z "$curr_uuid" ]; then
108 continue
109 fi
110 "$@"
111 done
112 }
113
114 get_first_line() {
115 file="$1"
116 if [ ! -e "$file" ]; then
117 echo ""
118 return
119 fi
120
121 while IFS= read -r line || [ -n "$line" ]; do
122 break
123 done < "${file}"
124 echo "$line"
125 }
126
127 set_grub_default() {
128 kver="$1"
129
130 if [ -z "${kver}" ]; then
131 rm -f "${GRUB_PIN_SNIPPET}"
132 else
133 # grub menu entry ids contain the internal root-device id (e.g. for zfs the GUID of
134 # the pool printed in hex) as this is independent of the ESP (or grub location)
135 # take it from /boot/grub/grub.cfg
136 root_devid=$(sed -rn "s/.*gnulinux-advanced-(.+)['] \{$/\1/p" \
137 /boot/grub/grub.cfg)
138 entry="gnulinux-advanced-${root_devid}>gnulinux-${kver}-advanced-${root_devid}"
139 echo "GRUB_DEFAULT=\"${entry}\"" > "${GRUB_PIN_SNIPPET}"
140 fi
141 }
142
143 set_systemd_boot_default() {
144 mountpoint="$1"
145 kver="$2"
146 if [ -z "${kver}" ]; then
147 entry="proxmox-*"
148 else
149 entry="proxmox-${kver}.conf"
150 fi
151
152 # replaces the current default entry, if one exists else append it at the end of the file
153 sed -ri "/^default /{h;s/ .*\$/ ${entry}/};\${x;/^$/{s//default ${entry}/;H};x}" \
154 "${mountpoint}/$PMX_LOADER_CONF"
155
156 }