chiark / gitweb /
be4a8e274d0e89f513b2c363a4b22e8b1a64dd4c
[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 usage()
23 {
24     echo "Usage:" >&2
25     echo "        $0 add <kernel-version> <kernel-image>" >&2
26     echo "        $0 remove <kernel-version> <kernel-image>" >&2
27 }
28
29 dropindirs_sort()
30 {
31     local suffix=$1; shift
32     local -a files
33     local f d i
34
35     readarray -t files < <(
36         for d in "$@"; do
37             for i in "$d/"*"$suffix"; do
38                 if [[ -e "$i" ]]; then
39                     echo "${i##*/}"
40                 fi
41             done
42         done | sort -Vu
43     )
44
45     for f in "${files[@]}"; do
46         for d in "$@"; do
47             if [[ -e "$d/$f" ]]; then
48                 echo "$d/$f"
49                 continue 2
50             fi
51         done
52     done
53 }
54
55 export LC_COLLATE=C
56
57 COMMAND="$1"
58 KERNEL_VERSION="$2"
59 KERNEL_IMAGE="$3"
60
61 if [[ -f /etc/os-release ]]; then
62     . /etc/os-release
63 fi
64
65 if ! [[ $ID ]]; then
66     echo "Could not determine the distribution name from /etc/os-release." >&2
67     echo "Please specify ID=... in /etc/os-release. See man:os-release(5)" >&2
68     exit 1
69 fi
70
71 if [[ -f /etc/machine-id ]]; then
72     read MACHINE_ID < /etc/machine-id
73 fi
74
75 if ! [[ $MACHINE_ID ]]; then
76     echo "Could not determine your machine ID from /etc/machine-id." >&2
77     echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2
78     exit 1
79 fi
80
81 if [[ -f /etc/kernel/cmdline ]]; then
82     readarray -t BOOT_OPTIONS < /etc/kernel/cmdline
83 fi
84
85 if ! [[ ${BOOT_OPTIONS[*]} ]]; then
86     readarray -t BOOT_OPTIONS < /proc/cmdline
87 fi
88
89 if ! [[ $BOOT_OPTIONS ]]; then
90     echo "Could not determine the kernel command line parameters." >&2
91     echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2
92     exit 1
93 fi
94
95 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
96     usage
97     exit 1
98 fi
99
100 BOOT_DIR="/$MACHINE_ID/$KERNEL_VERSION"
101 BOOT_DIR_ABS="/boot$BOOT_DIR"
102 LOADER_ENTRY="/boot/loader/entries/$MACHINE_ID-$KERNEL_VERSION.conf"
103 ret=0
104
105 readarray -t PLUGINS < <(
106     dropindirs_sort ".install" \
107         "/etc/kernel/install.d" \
108         "/usr/lib/kernel/install.d"
109 )
110
111 case $COMMAND in
112     add)
113         if [[ ! $KERNEL_IMAGE ]]; then
114             usage
115             exit 1
116         fi
117
118         mkdir -p "$BOOT_DIR_ABS" || {
119             echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
120             exit 1
121         }
122
123         for f in "${PLUGINS[@]}"; do
124             [[ -x $f ]] && "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS"
125             ((ret+=$?))
126         done
127
128         cp --preserve "$KERNEL_IMAGE" "$BOOT_DIR_ABS/linux" || {
129             echo "Could not copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'." >&2
130             exit 1
131         }
132
133         mkdir -p "${LOADER_ENTRY%/*}" || {
134             echo "Could not create loader entry directory '${LOADER_ENTRY%/*}'." >&2
135             exit 1
136         }
137
138         {
139             echo "title      $PRETTY_NAME"
140             echo "version    $KERNEL_VERSION"
141             echo "machine-id $MACHINE_ID"
142             echo "options    ${BOOT_OPTIONS[*]}"
143             echo "linux      $BOOT_DIR/linux"
144             [[ -f $BOOT_DIR_ABS/initrd ]] && \
145                 echo "initrd     $BOOT_DIR/initrd"
146         } > "$LOADER_ENTRY" || {
147             echo "Could not create loader entry '$LOADER_ENTRY'." >&2
148             exit 1
149         }
150         ;;
151
152     remove)
153         for f in "${PLUGINS[@]}"; do
154             [[ -x $f ]] && "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
155             ((ret+=$?))
156         done
157
158         rm -rf "$LOADER_ENTRY" "$BOOT_DIR_ABS"
159         ;;
160
161     *)
162         usage
163         exit 1
164         ;;
165 esac
166
167 ((ret+=$?))
168
169 exit $ret