Debian Codegraph @

pkg/lib/kernelopt.sh

raw
1 #!/bin/sh
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # Helper to add, modify, and remove a kernel command line option. This supports
4 # grub and zipl, i. e. x86, arm64, and s390x. Either grubby (Fedora, RHEL) or
5 # update-grub (Debian, Ubuntu) needs to be available.
6 #
7 # Copyright (C) 2019 Red Hat, Inc
8 set -eu
9
10 error() {
11 echo "$1" >&2
12 exit 1
13 }
14
15 grub() {
16 key="${2%=*}" # split off optional =value
17
18 # For the non-BLS case, or if someone overrides those with grub2-mkconfig,
19 # update-grub or update-bootloader, change it in /etc/default/grub
20 if [ -e /etc/default/grub ]; then
21 if [ "$1" = set ]; then
22 # replace existing argument, otherwise append it
23 sed -i.bak -r "/^[[:space:]]*GRUB_CMDLINE_LINUX\b/ { s/$key(=[^[:space:]\"]*)?/$2/g; t; s/\"$/ $2\"/ }" /etc/default/grub
24 else
25 sed -i.bak -r "/^[[:space:]]*GRUB_CMDLINE_LINUX\b/ s/$key(=[^[:space:]\"]*)?//g" /etc/default/grub
26 fi
27 fi
28
29 # on Fedora and RHEL, use grubby; this covers grub and BLS; s390x's zipl also supports BLS there
30 if type grubby >/dev/null 2>&1; then
31 if [ "$1" = set ]; then
32 grubby --args="$2" --update-kernel=ALL
33 else
34 grubby --remove-args="$2" --update-kernel=ALL
35 fi
36
37 # on Debian/Ubuntu, use update-grub, which reads from /etc/default/grub
38 elif [ -e /etc/default/grub ] && type update-grub >/dev/null 2>&1; then
39 update-grub
40
41 # on Suse platforms, use update-bootloader
42 # Since earlier we updated /etc/default/grub we can just run update-bootloader
43 elif type update-bootloader >/dev/null 2>&1; then
44 update-bootloader
45
46 # on OSTree, the kernel config is inside the image
47 elif cur=$(rpm-ostree kargs 2>&1); then
48 if [ "$1" = set ]; then
49 # replace if already present; can happen in the middle (must be separated by space) or at the beginning of line
50 if [ "${cur% $key *}" != "$cur" ] || [ "${cur% $key=*}" != "$cur" ] || [ "${cur#${key}[ =]}" != "$cur" ]; then
51 rpm-ostree kargs --replace="$2"
52 else
53 rpm-ostree kargs --append="$2"
54 fi
55 else
56 rpm-ostree kargs --delete="$key"
57 fi
58 else
59 error "No supported grub update mechanism found (grubby, update-grub, update-bootloader or rpm-ostree)"
60 fi
61 }
62
63 update_zipl() {
64 if type zipl >/dev/null 2>&1; then
65 zipl
66 fi
67 }
68
69 #
70 # main
71 #
72
73 if [ -z "${2:-}" ] || [ -n "${3:-}" ] || [ "$1" != "set" -a "$1" != "remove" ]; then
74 error "Usage: '$0 set <option>[=<value>]' or '$0 remove <option>'"
75 fi
76
77 grub "$1" "$2"
78 update_zipl