]> git.proxmox.com Git - systemd.git/blob - src/kernel-install/90-loaderentry.install
New upstream version 249~rc1
[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 if [[ $BOOT_MNT == '/' ]]; then
40 ENTRY_DIR=$ENTRY_DIR_ABS
41 else
42 ENTRY_DIR=${ENTRY_DIR_ABS#$BOOT_MNT}
43 fi
44
45 if [[ $COMMAND == remove ]]; then
46 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
47 rm -f "$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+"*".conf"
48 exit 0
49 fi
50
51 if ! [[ $COMMAND == add ]]; then
52 exit 1
53 fi
54
55 if ! [[ $KERNEL_IMAGE ]]; then
56 exit 1
57 fi
58
59 if [[ -f /etc/os-release ]]; then
60 . /etc/os-release
61 elif [[ -f /usr/lib/os-release ]]; then
62 . /usr/lib/os-release
63 fi
64
65 if ! [[ $PRETTY_NAME ]]; then
66 PRETTY_NAME="Linux $KERNEL_VERSION"
67 fi
68
69 if [[ -f /etc/kernel/cmdline ]]; then
70 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
71 elif [[ -f /usr/lib/kernel/cmdline ]]; then
72 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
73 else
74 declare -a BOOT_OPTIONS
75
76 read -r -d '' -a line < /proc/cmdline
77 for i in "${line[@]}"; do
78 [[ "${i#initrd=*}" != "$i" ]] && continue
79 [[ "${i#BOOT_IMAGE=*}" != "$i" ]] && continue
80 BOOT_OPTIONS+=("$i")
81 done
82 fi
83
84 if [[ -f /etc/kernel/tries ]]; then
85 read -r TRIES </etc/kernel/tries
86 if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then
87 echo "/etc/kernel/tries does not contain an integer." >&2
88 exit 1
89 fi
90 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf"
91 else
92 LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
93 fi
94
95 install -g root -o root -m 0644 "$KERNEL_IMAGE" "$ENTRY_DIR_ABS/linux" || {
96 echo "Could not copy '$KERNEL_IMAGE' to '$ENTRY_DIR_ABS/linux'." >&2
97 exit 1
98 }
99
100 INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" )
101
102 for initrd in "${INITRD_OPTIONS[@]}"; do
103 if [[ -f "${initrd}" ]]; then
104 initrd_basename="$(basename ${initrd})"
105 [ "$KERNEL_INSTALL_VERBOSE" -gt 0 ] && \
106 echo "Installing $ENTRY_DIR_ABS/${initrd_basename}"
107 install -g root -o root -m 0644 "${initrd}" "$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