]>
Commit | Line | Data |
---|---|---|
8f51399e HH |
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" | |
0912c0b8 | 9 | INITRD_OPTIONS_START="5" |
8f51399e | 10 | |
9d8813b3 YW |
11 | if ! [[ $KERNEL_INSTALL_MACHINE_ID ]]; then |
12 | exit 0 | |
8f51399e HH |
13 | fi |
14 | ||
81818461 JMC |
15 | if ! [[ -d "$BOOT_DIR_ABS" ]]; then |
16 | exit 0 | |
17 | fi | |
18 | ||
9d8813b3 | 19 | MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID |
8f51399e HH |
20 | |
21 | BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION" | |
340defcd | 22 | BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR} |
8f51399e HH |
23 | |
24 | if [[ $COMMAND == remove ]]; then | |
bf7b6d28 LP |
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 | |
8f51399e HH |
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 | |
5ae4d543 LP |
40 | elif [[ -f /usr/lib/os-release ]]; then |
41 | . /usr/lib/os-release | |
8f51399e HH |
42 | fi |
43 | ||
44 | if ! [[ $PRETTY_NAME ]]; then | |
45 | PRETTY_NAME="Linux $KERNEL_VERSION" | |
46 | fi | |
47 | ||
6c10d399 HH |
48 | declare -a BOOT_OPTIONS |
49 | ||
8f51399e | 50 | if [[ -f /etc/kernel/cmdline ]]; then |
6fd2ccc9 | 51 | read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline |
8f51399e HH |
52 | fi |
53 | ||
54 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then | |
6fd2ccc9 | 55 | read -r -d '' -a line < /proc/cmdline |
2f3a215f HH |
56 | for i in "${line[@]}"; do |
57 | [[ "${i#initrd=*}" != "$i" ]] && continue | |
6fd2ccc9 | 58 | BOOT_OPTIONS+=("$i") |
6c10d399 | 59 | done |
8f51399e HH |
60 | fi |
61 | ||
6c10d399 | 62 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then |
8f51399e HH |
63 | echo "Could not determine the kernel command line parameters." >&2 |
64 | echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2 | |
65 | exit 1 | |
66 | fi | |
67 | ||
bf7b6d28 LP |
68 | if [[ -f /etc/kernel/tries ]]; then |
69 | read -r TRIES </etc/kernel/tries | |
70 | if ! [[ "$TRIES" =~ ^[0-9]+$ ]] ; then | |
71 | echo "/etc/kernel/tries does not contain an integer." >&2 | |
72 | exit 1 | |
73 | fi | |
74 | LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION+$TRIES.conf" | |
75 | else | |
76 | LOADER_ENTRY="$BOOT_ROOT/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf" | |
77 | fi | |
78 | ||
8b179a83 TG |
79 | cp "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" && |
80 | chown root:root "$BOOT_DIR_ABS/linux" && | |
81 | chmod 0644 "$BOOT_DIR_ABS/linux" || { | |
8f51399e HH |
82 | echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2 |
83 | exit 1 | |
84 | } | |
85 | ||
d279b185 MAP |
86 | INITRD_OPTIONS=( "${@:${INITRD_OPTIONS_START}}" ) |
87 | ||
88 | for initrd in "${INITRD_OPTIONS[@]}"; do | |
0912c0b8 MA |
89 | if [[ -f "${initrd}" ]]; then |
90 | initrd_basename="$(basename ${initrd})" | |
91 | cp "${initrd}" "$BOOT_DIR_ABS/${initrd_basename}" && | |
92 | chown root:root "$BOOT_DIR_ABS/${initrd_basename}" && | |
93 | chmod 0644 "$BOOT_DIR_ABS/${initrd_basename}" || { | |
94 | echo "Could not copy '${initrd}' to '$BOOT_DIR_ABS/${initrd_basename}'." >&2 | |
95 | exit 1 | |
96 | } | |
97 | fi | |
98 | done | |
04ca4d19 | 99 | |
d279b185 MAP |
100 | # If no initrd option is supplied, fallback to "initrd" which is |
101 | # the name used by dracut when generating it in its kernel-install hook | |
102 | [[ ${#INITRD_OPTIONS[@]} == 0 ]] && INITRD_OPTIONS=( initrd ) | |
103 | ||
8f51399e HH |
104 | mkdir -p "${LOADER_ENTRY%/*}" || { |
105 | echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2 | |
106 | exit 1 | |
107 | } | |
108 | ||
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 $BOOT_DIR/linux" | |
d279b185 | 115 | for initrd in "${INITRD_OPTIONS[@]}"; do |
0912c0b8 MA |
116 | [[ -f $BOOT_DIR_ABS/$(basename ${initrd}) ]] && \ |
117 | echo "initrd $BOOT_DIR/$(basename ${initrd})" | |
118 | done | |
852752fc | 119 | : |
8f51399e HH |
120 | } > "$LOADER_ENTRY" || { |
121 | echo "Could not create loader entry '$LOADER_ENTRY'." >&2 | |
122 | exit 1 | |
123 | } | |
124 | exit 0 |