chiark / gitweb /
1a694578a1c563f5f62c93a019aa12a91da117c0
[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="/boot/$MACHINE_ID/$KERNEL_VERSION"
71 ret=0
72
73 dropindirs_sort()
74 {
75     suffix=$1; shift
76     readarray -t files< <(
77         for d in "$@"; do
78             for i in "${d}/"*${suffix}; do
79                 [[ -e $i ]] && echo ${i##*/}
80             done
81         done | sort -Vu
82     )
83
84     for f in "${files[@]}"; do
85         for d in "$@"; do
86             if [[ -e "$d/$f" ]]; then
87                 echo "$d/$f"
88                 continue 2
89             fi
90         done
91     done
92 }
93
94 readarray -t PLUGINS < <(
95     dropindirs_sort ".install" \
96         "/etc/kernel/install.d" \
97         "/usr/lib/kernel/install.d"
98 )
99
100 case "$COMMAND" in
101     add)
102         mkdir -p "$BOOT_DIR" || exit 1
103
104         for f in "${PLUGINS[@]}"; do
105             [[ -x $f ]] && "$f" add "$KERNEL_VERSION" "$BOOT_DIR"
106             ((ret+=$?))
107         done
108
109         if ! cp --preserve "$KERNEL_IMAGE" "$BOOT_DIR"/linux; then
110             echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR/linux'!" >&2
111         fi
112
113         [[ -d /boot/loader/entries ]] || mkdir -p /boot/loader/entries
114
115         {
116             echo "title      $PRETTY_NAME"
117             echo "version    $KERNEL_VERSION"
118             echo "machine-id $MACHINE_ID"
119             echo "options    $BOOT_OPTIONS"
120             echo "linux      $BOOT_DIR/linux"
121             [[ -f "${BOOT_DIR}"/initrd ]] && \
122                 echo "initrd     $BOOT_DIR/initrd"
123             :
124         } > "/boot/loader/entries/${ID}-${KERNEL_VERSION}-${MACHINE_ID}.conf"
125
126         ((ret+=$?))
127
128         if ! [[ -f "/boot/loader/entries/${ID}-${KERNEL_VERSION}-${MACHINE_ID}.conf" ]]; then
129             echo "Could not create '/boot/loader/entries/${ID}-${KERNEL_VERSION}-${MACHINE_ID}.conf'!" >&2
130         fi
131         ;;
132
133     remove)
134         for f in "${PLUGINS[@]}"; do
135             [[ -x $f ]] && "$f" remove "$KERNEL_VERSION" "$BOOT_DIR"
136             ((ret+=$?))
137         done
138
139         rm -fr "$BOOT_DIR"
140         rm -f "/boot/loader/entries/${ID}-${KERNEL_VERSION}-${MACHINE_ID}.conf"
141         ;;
142
143     *)
144         usage
145         ret=1;;
146 esac
147
148 ((ret+=$?))
149
150 exit $ret