]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/kernel-install/kernel-install
Merge pull request #4879 from poettering/systemd
[thirdparty/systemd.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 SKIP_REMAINING=77
23
24 usage()
25 {
26 echo "Usage:"
27 echo " $0 add KERNEL-VERSION KERNEL-IMAGE"
28 echo " $0 remove KERNEL-VERSION"
29 }
30
31 dropindirs_sort()
32 {
33 local suffix=$1; shift
34 local -a files
35 local f d i
36
37 readarray -t files <<<"$(
38 for d in "$@"; do
39 for i in "$d/"*"$suffix"; do
40 if [[ -e "$i" ]]; then
41 echo "${i##*/}"
42 fi
43 done
44 done | sort -Vu
45 )"
46
47 for f in "${files[@]}"; do
48 for d in "$@"; do
49 if [[ -e "$d/$f" ]]; then
50 echo "$d/$f"
51 continue 2
52 fi
53 done
54 done
55 }
56
57 export LC_COLLATE=C
58
59 for i in "$@"; do
60 if [ "$i" == "--help" -o "$i" == "-h" ]; then
61 usage
62 exit 0
63 fi
64 done
65
66 if [[ "${0##*/}" == 'installkernel' ]]; then
67 COMMAND='add'
68 else
69 COMMAND="$1"
70 shift
71 fi
72
73 KERNEL_VERSION="$1"
74 KERNEL_IMAGE="$2"
75
76 if [[ -f /etc/machine-id ]]; then
77 read MACHINE_ID < /etc/machine-id
78 fi
79
80 if ! [[ $MACHINE_ID ]]; then
81 echo "Could not determine your machine ID from /etc/machine-id." >&2
82 echo "Please run 'systemd-machine-id-setup' as root. See man:machine-id(5)" >&2
83 exit 1
84 fi
85
86 if [[ ! $COMMAND ]] || [[ ! $KERNEL_VERSION ]]; then
87 echo "Not enough arguments" >&2
88 exit 1
89 fi
90
91 if [[ -d /efi/loader/entries ]] || [[ -d /efi/$MACHINE_ID ]]; then
92 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
93 elif [[ -d /boot/loader/entries ]] || [[ -d /boot/$MACHINE_ID ]]; then
94 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
95 elif [[ -d /boot/efi/loader/entries ]] || [[ -d /boot/efi/$MACHINE_ID ]]; then
96 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
97 elif mountpoint -q /efi; then
98 BOOT_DIR_ABS="/efi/$MACHINE_ID/$KERNEL_VERSION"
99 elif mountpoint -q /boot/efi; then
100 BOOT_DIR_ABS="/boot/efi/$MACHINE_ID/$KERNEL_VERSION"
101 else
102 BOOT_DIR_ABS="/boot/$MACHINE_ID/$KERNEL_VERSION"
103 fi
104
105 ret=0
106
107 readarray -t PLUGINS <<<"$(
108 dropindirs_sort ".install" \
109 "/etc/kernel/install.d" \
110 "/usr/lib/kernel/install.d"
111 )"
112
113 case $COMMAND in
114 add)
115 if [[ ! "$KERNEL_IMAGE" ]]; then
116 echo "Command 'add' requires an argument" >&2
117 exit 1
118 fi
119
120 mkdir -p "$BOOT_DIR_ABS" || {
121 echo "Could not create boot directory '$BOOT_DIR_ABS'." >&2
122 exit 1
123 }
124
125 for f in "${PLUGINS[@]}"; do
126 if [[ -x $f ]]; then
127 "$f" add "$KERNEL_VERSION" "$BOOT_DIR_ABS" "$KERNEL_IMAGE"
128 x=$?
129 if [[ $x == $SKIP_REMAINING ]]; then
130 exit 0
131 fi
132 ((ret+=$x))
133 fi
134 done
135 ;;
136
137 remove)
138 for f in "${PLUGINS[@]}"; do
139 if [[ -x $f ]]; then
140 "$f" remove "$KERNEL_VERSION" "$BOOT_DIR_ABS"
141 x=$?
142 if [[ $x == $SKIP_REMAINING ]]; then
143 exit 0
144 fi
145 ((ret+=$x))
146 fi
147 done
148
149 rm -rf "$BOOT_DIR_ABS"
150 ((ret+=$?))
151 ;;
152
153 *)
154 echo "Unknown command '$COMMAND'" >&2
155 exit 1
156 ;;
157 esac
158
159 exit $ret