]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/90-loaderentry.install
core,journald: use quoted commandlines
[thirdparty/systemd.git] / src / kernel-install / 90-loaderentry.install
1 #!/usr/bin/env 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 # SPDX-License-Identifier: LGPL-2.1-or-later
5 #
6 # This file is part of systemd.
7 #
8 # systemd is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 2.1 of the License, or
11 # (at your option) any later version.
12 #
13 # systemd is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
20
21 COMMAND="$1"
22 KERNEL_VERSION="$2"
23 ENTRY_DIR_ABS="$3"
24 KERNEL_IMAGE="$4"
25 INITRD_OPTIONS_START="5"
26
27 if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then
28 exit 0
29 fi
30
31 if ! [[ -d "$ENTRY_DIR_ABS" ]]; then
32 exit 0
33 fi
34
35 MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
36
37 BOOT_ROOT=${ENTRY_DIR_ABS%/$MACHINE_ID/$KERNEL_VERSION}
38 BOOT_MNT=$(stat -c %m $BOOT_ROOT)
39 ENTRY_DIR=${ENTRY_DIR_ABS#$BOOT_MNT}
40
41 if [[ $COMMAND == remove ]]; then
42 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
43 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
44 exit 0
45 fi
46
47 if ! [[ $COMMAND == add ]]; then
48 exit 1
49 fi
50
51 if ! [[ $KERNEL_IMAGE ]]; then
52 exit 1
53 fi
54
55 if [[ -f /etc/os-release ]]; then
56 . /etc/os-release
57 elif [[ -f /usr/lib/os-release ]]; then
58 . /usr/lib/os-release
59 fi
60
61 if ! [[ $PRETTY_NAME ]]; then
62 PRETTY_NAME="Linux $KERNEL_VERSION"
63 fi
64
65 if [[ -f /etc/kernel/cmdline ]]; then
66 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
67 elif [[ -f /usr/lib/kernel/cmdline ]]; then
68 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
69 else
70 declare -a BOOT_OPTIONS
71
72 read -r -d '' -a line < /proc/cmdline
73 for i in "${line[@]}"; do
74 [[ "${i#initrd=*}" != "$i" ]] && continue
75 [[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
76 BOOT_OPTIONS+=("$i")
77 done
78 fi
79
80 if [[ -f /etc/kernel/tries ]]; then
81 read -r TRIES </etc/kernel/tries
82 if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
83 echo "/etc/kernel/tries does not contain an integer." >&2
84 exit 1
85 fi
86 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf"
87 else
88 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
89 fi
90
91 cp "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" &&
92 chown root:root "$ENTRY_DIR_ABS/linux" &&
93 chmod 0644 "$ENTRY_DIR_ABS/linux" || {
94 echo "Could not copy '$KERNEL_IMAGE to '$ENTRY_DIR_ABS/linux'." >&2
95 exit 1
96 }
97
98 INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
99
100 for initrd in "${INITRD_OPTIONS[@]}"; do
101 if [[ -f "${initrd}" ]]; then
102 initrd_basename="$(basename ${initrd})"
103 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
104 echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
105 cp "${initrd}" "$ENTRY_DIR_ABS/${initrd_basename}" &&
106 chown root:root "$ENTRY_DIR_ABS/${initrd_basename}" &&
107 chmod 0644 "$ENTRY_DIR_ABS/${initrd_basename}" || {
108 echo "Could not copy '${initrd}' to '$ENTRY_DIR_ABS/${initrd_basename}'." >&2
109 exit 1
110 }
111 fi
112 done
113
114 # If no initrd option is supplied, fall back to "initrd" which is
115 # the name used by dracut when generating it in its kernel-install hook
116 [[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd )
117
118 mkdir -p "${LOADER_ENTRY%/*}" || {
119 echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
120 exit 1
121 }
122
123 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
124 echo "Creating $LOADER_ENTRY"
125 {
126 echo "title $PRETTY_NAME"
127 echo "version $KERNEL_VERSION"
128 echo "machine-id $MACHINE_ID"
129 echo "options ${BOOT_OPTIONS[*]}"
130 echo "linux $ENTRY_DIR/linux"
131 for initrd in "${INITRD_OPTIONS[@]}"; do
132 [[ -f $ENTRY_DIR_ABS/$(basename ${initrd}) ]] && \
133 echo "initrd $ENTRY_DIR/$(basename ${initrd})"
134 done
135 :
136 } > "$LOADER_ENTRY" || {
137 echo "Could not create loader entry '$LOADER_ENTRY'." >&2
138 exit 1
139 }
140 exit 0