]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/90-loaderentry.install
kernel-install: support the case /etc/machine-id is missing or empty (#5975)
[thirdparty/systemd.git] / src / kernel-install / 90-loaderentry.install
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4
5 COMMAND="$1"
6 KERNEL_VERSION="$2"
7 BOOT_DIR_ABS="$3"
8 KERNEL_IMAGE="$4"
9
10 if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
11 exit 0
12 fi
13
14 MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
15
16 BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION"
17 BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR}
18 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
19
20 if [[ $COMMAND == remove ]]; then
21 exec rm -f "$LOADER_ENTRY"
22 fi
23
24 if ! [[ $COMMAND == add ]]; then
25 exit 1
26 fi
27
28 if ! [[ $KERNEL_IMAGE ]]; then
29 exit 1
30 fi
31
32 if [[ -f /etc/os-release ]]; then
33 . /etc/os-release
34 elif [[ -f /usr/lib/os-release ]]; then
35 . /usr/lib/os-release
36 fi
37
38 if ! [[ $PRETTY_NAME ]]; then
39 PRETTY_NAME="Linux $KERNEL_VERSION"
40 fi
41
42 declare -a BOOT_OPTIONS
43
44 if [[ -f /etc/kernel/cmdline ]]; then
45 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
46 fi
47
48 if ! [[ ${BOOT_OPTIONS[*]} ]]; then
49 read -r -d '' -a line < /proc/cmdline
50 for i in "${line[@]}"; do
51 [[ "${i#initrd=*}" != "$i" ]] && continue
52 BOOT_OPTIONS+=("$i")
53 done
54 fi
55
56 if ! [[ ${BOOT_OPTIONS[*]} ]]; then
57 echo "Could not determine the kernel command line parameters." >&2
58 echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
59 exit 1
60 fi
61
62 cp "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" &&
63 chown root:root "$BOOT_DIR_ABS/linux" &&
64 chmod 0644 "$BOOT_DIR_ABS/linux" || {
65 echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2
66 exit 1
67 }
68
69 mkdir -p "${LOADER_ENTRY%/*}" || {
70 echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
71 exit 1
72 }
73
74 {
75 echo "title $PRETTY_NAME"
76 echo "version $KERNEL_VERSION"
77 echo "machine-id $MACHINE_ID"
78 echo "options ${BOOT_OPTIONS[*]}"
79 echo "linux $BOOT_DIR/linux"
80 [[ -f $BOOT_DIR_ABS/initrd ]] && \
81 echo "initrd $BOOT_DIR/initrd"
82 :
83 } > "$LOADER_ENTRY" || {
84 echo "Could not create loader entry '$LOADER_ENTRY'." >&2
85 exit 1
86 }
87 exit 0