chiark / gitweb /
kernel-install: fixed paths in boot loader entry
[elogind.git] / src / kernel-install / kernel-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 # This file is part of systemd.
6 #
7 # Copyright 2013 Harald Hoyer
8 #
9 # systemd is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU Lesser General Public License as published by
11 # the Free Software Foundation; either version 2.1 of the License, or
12 # (at your option) any later version.
13 #
14 # systemd is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public License
20 # along with systemd; If not, see <http://www.gnu.org/licenses/>.
21
22 export LC_COLLATE=C
23
24 COMMAND="$1"
25 KERNEL_VERSION="$2"
26 KERNEL_IMAGE="$3"
27
28 [[ -f /etc/os-release ]] && . /etc/os-release
29 if ! [[ $ID ]]; then
30     echo "Can't determine the name of your distribution. Please create /etc/os-release." >&2
31     echo "See http://www.freedesktop.org/software/systemd/man/os-release.html" >&2
32     exit 1
33 fi
34
35 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
36 if ! [[ $MACHINE_ID ]]; then
37     echo "Can't determine your machine id. Please create /etc/machine-id!" >&2
38     echo "See http://www.freedesktop.org/software/systemd/man/machine-id.html" >&2
39     exit 1
40 fi
41
42 if [[ -f /etc/kernel/cmdline ]]; then
43     readarray -t BOOT_OPTIONS < /etc/kernel/cmdline
44 fi
45
46 if ! [[ "${BOOT_OPTIONS[@]}" ]]; then
47     readarray -t BOOT_OPTIONS < /proc/cmdline
48 fi
49
50 if ! [[ $BOOT_OPTIONS ]]; then
51     echo "Can't determine the kernel command line parameters." >&2
52     echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
53     exit 1
54 fi
55
56 usage()
57 {
58     {
59         echo "Usage:"
60         echo "        $0 add <kernel-version> <kernel-image>"
61         echo "        $0 remove <kernel-version> <kernel-image>"
62     } >&2
63 }
64
65 if ! ( [[ $COMMAND ]] && [[ $KERNEL_VERSION ]] && [[ $KERNEL_IMAGE ]] ); then
66     usage
67     exit 1
68 fi
69
70 BOOT_DIR="$MACHINE_ID/$KERNEL_VERSION"
71 BOOT_DIR_ABS="/boot/$BOOT_DIR"
72 LOADER_ENTRY="/boot/loader/entries/${ID}-${KERNEL_VERSION}-${MACHINE_ID}.conf"
73 ret=0
74
75 dropindirs_sort()
76 {
77     suffix=$1; shift
78     readarray -t files< <(
79         for d in "$@"; do
80             for i in "${d}/"*${suffix}; do
81                 [[ -e $i ]] && echo ${i##*/}
82             done
83         done | sort -Vu
84     )
85
86     for f in "${files[@]}"; do
87         for d in "$@"; do
88             if [[ -e "$d/$f" ]]; then
89                 echo "$d/$f"
90                 continue 2
91             fi
92         done
93     done
94 }
95
96 readarray -t PLUGINS < <(
97     dropindirs_sort ".install" \
98         "/etc/kernel/install.d" \
99         "/usr/lib/kernel/install.d"
100 )
101
102 case "$COMMAND" in
103     add)
104         mkdir -p "$BOOT_DIR_ABS" || exit 1
105
106         for f in "${PLUGINS[@]}"; do
107             [[ -x $f ]] && "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS"
108             ((ret+=$?))
109         done
110
111         if ! cp --preserve "$KERNEL_IMAGE" "$BOOT_DIR_ABS"/linux; then
112             echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'!" >&2
113         fi
114
115         [[ -d /boot/loader/entries ]] || mkdir -p /boot/loader/entries
116
117         {
118             echo "title      $PRETTY_NAME"
119             echo "version    $KERNEL_VERSION"
120             echo "machine-id $MACHINE_ID"
121             echo "options    ${BOOT_OPTIONS[@]}"
122             echo "linux      $BOOT_DIR/linux"
123             [[ -f "${BOOT_DIR_ABS}"/initrd ]] && \
124                 echo "initrd     $BOOT_DIR/initrd"
125             :
126         } > $LOADER_ENTRY
127
128         ((ret+=$?))
129
130         if ! [[ -f $LOADER_ENTRY ]]; then
131             echo "Could not create '$LOADER_ENTRY'!" >&2
132         fi
133         ;;
134
135     remove)
136         for f in "${PLUGINS[@]}"; do
137             [[ -x $f ]] && "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
138             ((ret+=$?))
139         done
140
141         rm -fr "$BOOT_DIR_ABS"
142         rm -f "$LOADER_ENTRY"
143         ;;
144
145     *)
146         usage
147         ret=1;;
148 esac
149
150 ((ret+=$?))
151
152 exit $ret