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