]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/90-loaderentry.install
Merge pull request #13439 from yuwata/core-support-systemctl-clean-more
[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 ENTRY_DIR_ABS="$3"
8 KERNEL_IMAGE="$4"
9 INITRD_OPTIONS_START="5"
10
11 if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
12 exit 0
13 fi
14
15 if ! [[ -d "$ENTRY_DIR_ABS" ]]; then
16 exit 0
17 fi
18
19 MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
20
21 ENTRY_DIR="/$MACHINE_ID/$KERNEL_VERSION"
22 BOOT_ROOT=${ENTRY_DIR_ABS%$ENTRY_DIR}
23
24 if [[ $COMMAND == remove ]]; then
25 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
26 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
27 exit 0
28 fi
29
30 if ! [[ $COMMAND == add ]]; then
31 exit 1
32 fi
33
34 if ! [[ $KERNEL_IMAGE ]]; then
35 exit 1
36 fi
37
38 if [[ -f /etc/os-release ]]; then
39 . /etc/os-release
40 elif [[ -f /usr/lib/os-release ]]; then
41 . /usr/lib/os-release
42 fi
43
44 if ! [[ $PRETTY_NAME ]]; then
45 PRETTY_NAME="Linux $KERNEL_VERSION"
46 fi
47
48 if [[ -f /etc/kernel/cmdline ]]; then
49 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
50 elif [[ -f /usr/lib/kernel/cmdline ]]; then
51 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
52 else
53 declare -a BOOT_OPTIONS
54
55 read -r -d '' -a line < /proc/cmdline
56 for i in "${line[@]}"; do
57 [[ "${i#initrd=*}" != "$i" ]] && continue
58 BOOT_OPTIONS+=("$i")
59 done
60 fi
61
62 if [[ -f /etc/kernel/tries ]]; then
63 read -r TRIES </etc/kernel/tries
64 if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
65 echo "/etc/kernel/tries does not contain an integer." >&2
66 exit 1
67 fi
68 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf"
69 else
70 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
71 fi
72
73 cp "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" &&
74 chown root:root "$ENTRY_DIR_ABS/linux" &&
75 chmod 0644 "$ENTRY_DIR_ABS/linux" || {
76 echo "Could not copy '$KERNEL_IMAGE to '$ENTRY_DIR_ABS/linux'." >&2
77 exit 1
78 }
79
80 INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
81
82 for initrd in "${INITRD_OPTIONS[@]}"; do
83 if [[ -f "${initrd}" ]]; then
84 initrd_basename="$(basename ${initrd})"
85 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
86 echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
87 cp "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" &&
88 chown root:root "$ENTRY_DIR_ABS/${initrd_basename}" &&
89 chmod 0644 "$ENTRY_DIR_ABS/${initrd_basename}" || {
90 echo "Could not copy '${initrd}' to '$ENTRY_DIR_ABS/${initrd_basename}'." >&2
91 exit 1
92 }
93 fi
94 done
95
96 # If no initrd option is supplied, fallback to "initrd" which is
97 # the name used by dracut when generating it in its kernel-install hook
98 [[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd )
99
100 mkdir -p "${LOADER_ENTRY%/*}" || {
101 echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
102 exit 1
103 }
104
105 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
106 echo "Creating $LOADER_ENTRY"
107 {
108 echo "title $PRETTY_NAME"
109 echo "version $KERNEL_VERSION"
110 echo "machine-id $MACHINE_ID"
111 echo "options ${BOOT_OPTIONS[*]}"
112 echo "linux $ENTRY_DIR/linux"
113 for initrd in "${INITRD_OPTIONS[@]}"; do
114 [[ -f $ENTRY_DIR_ABS/$(basename ${initrd}) ]] && \
115 echo "initrd $ENTRY_DIR/$(basename ${initrd})"
116 done
117 :
118 } > "$LOADER_ENTRY" || {
119 echo "Could not create loader entry '$LOADER_ENTRY'." >&2
120 exit 1
121 }
122 exit 0