chiark / gitweb /
journalctl: use _COMM= match for scripts
[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/machine-id ]]; then
62     read MACHINE_ID < /etc/machine-id
63 fi
64
65 if ! [[ $MACHINE_ID ]]; then
66     echo "Could not determine your machine ID from /etc/machine-id." >&2
67     echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2
68     exit 1
69 fi
70
71 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
72     usage
73     exit 1
74 fi
75
76 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
77 ret=0
78
79 readarray -t PLUGINS < <(
80     dropindirs_sort ".install" \
81         "/etc/kernel/install.d" \
82         "/usr/lib/kernel/install.d"
83 )
84
85 case $COMMAND in
86     add)
87         if [[ ! $KERNEL_IMAGE ]]; then
88             usage
89             exit 1
90         fi
91
92         mkdir -p "$BOOT_DIR_ABS" || {
93             echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
94             exit 1
95         }
96
97         for f in "${PLUGINS[@]}"; do
98             if [[ -x $f ]]; then
99                 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE"
100                 ((ret+=$?))
101             fi
102         done
103         ;;
104
105     remove)
106         for f in "${PLUGINS[@]}"; do
107             if [[ -x $f ]]; then
108                 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
109                 ((ret+=$?))
110             fi
111         done
112
113         rm -rf "$BOOT_DIR_ABS"
114         ((ret+=$?))
115         ;;
116
117     *)
118         usage
119         exit 1
120         ;;
121 esac
122
123 exit $ret