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