chiark / gitweb /
6b91d1cccb66d961c3ffa890a371121ef561075f
[elogind.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 BOOT_DIR_ABS="$3"
8 KERNEL_IMAGE="$4"
9
10 if [[ -f /etc/machine-id ]]; then
11     read MACHINE_ID < /etc/machine-id
12 fi
13
14 if ! [[ $MACHINE_ID ]]; then
15     exit 1
16 fi
17
18 BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION"
19 LOADER_ENTRY="/boot/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
20
21 if [[ $COMMAND == remove ]]; then
22     exec rm -f "$LOADER_ENTRY"
23 fi
24
25 if ! [[ $COMMAND == add ]]; then
26     exit 1
27 fi
28
29 if ! [[ $KERNEL_IMAGE ]]; then
30     exit 1
31 fi
32
33 if [[ -f /etc/os-release ]]; then
34     . /etc/os-release
35 fi
36
37 if ! [[ $PRETTY_NAME ]]; then
38     PRETTY_NAME="Linux $KERNEL_VERSION"
39 fi
40
41 declare -a BOOT_OPTIONS
42
43 if [[ -f /etc/kernel/cmdline ]]; then
44     readarray -t BOOT_OPTIONS < /etc/kernel/cmdline
45 fi
46
47 if ! [[ ${BOOT_OPTIONS[*]} ]]; then
48     readarray -t line < /proc/cmdline
49     for i in ${line[*]}; do
50         if [[ "${i#initrd=*}" == "$i" ]]; then
51             BOOT_OPTIONS[${#BOOT_OPTIONS[@]}]="$i"
52         fi
53     done
54 fi
55
56 if ! [[ ${BOOT_OPTIONS[*]} ]]; then
57     echo "Could not determine the kernel command line parameters." >&2
58     echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
59     exit 1
60 fi
61
62 cp --preserve "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" || {
63     echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2
64     exit 1
65 }
66
67 mkdir -p "${LOADER_ENTRY%/*}" || {
68     echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
69     exit 1
70 }
71
72 {
73     echo "title      $PRETTY_NAME"
74     echo "version    $KERNEL_VERSION"
75     echo "machine-id $MACHINE_ID"
76     echo "options    ${BOOT_OPTIONS[*]}"
77     echo "linux      $BOOT_DIR/linux"
78     [[ -f $BOOT_DIR_ABS/initrd ]] && \
79         echo "initrd     $BOOT_DIR/initrd"
80     :
81 } > "$LOADER_ENTRY" || {
82     echo "Could not create loader entry '$LOADER_ENTRY'." >&2
83     exit 1
84 }
85 exit 0