chiark / gitweb /
kernel-install: fix help output
[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:"
25     echo "        $0 add KERNEL-VERSION KERNEL-IMAGE"
26     echo "        $0 remove KERNEL-VERSION"
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 for i in "$@"; do
58     if [ "$i" == "--help" -o "$i" == "-h" ]; then
59         usage
60         exit 0
61     fi
62 done
63
64 if [[ "${0##*/}" == 'installkernel' ]]; then
65     COMMAND='add'
66 else
67     COMMAND="$1"
68     shift
69 fi
70
71 KERNEL_VERSION="$1"
72 KERNEL_IMAGE="$2"
73
74 if [[ -f /etc/machine-id ]]; then
75     read MACHINE_ID < /etc/machine-id
76 fi
77
78 if ! [[ $MACHINE_ID ]]; then
79     echo "Could not determine your machine ID from /etc/machine-id." >&2
80     echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2
81     exit 1
82 fi
83
84 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
85     echo "Not enough arguments" >&2
86     exit 1
87 fi
88
89 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
90 ret=0
91
92 readarray -t PLUGINS < <(
93     dropindirs_sort ".install" \
94         "/etc/kernel/install.d" \
95         "/usr/lib/kernel/install.d"
96 )
97
98 case $COMMAND in
99     add)
100         if [[ ! "$KERNEL_IMAGE" ]]; then
101             echo "Command 'add' requires an argument" >&2
102             exit 1
103         fi
104
105         mkdir -p "$BOOT_DIR_ABS" || {
106             echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
107             exit 1
108         }
109
110         for f in "${PLUGINS[@]}"; do
111             if [[ -x $f ]]; then
112                 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE"
113                 ((ret+=$?))
114             fi
115         done
116         ;;
117
118     remove)
119         for f in "${PLUGINS[@]}"; do
120             if [[ -x $f ]]; then
121                 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
122                 ((ret+=$?))
123             fi
124         done
125
126         rm -rf "$BOOT_DIR_ABS"
127         ((ret+=$?))
128         ;;
129
130     *)
131         echo "Unknown command '$COMMAND'" >&2
132         exit 1
133         ;;
134 esac
135
136 exit $ret