| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Requires sed to be present |
| 4 |
|
| 5 |
set -euxo pipefail |
| 6 |
|
| 7 |
# Mount /boot. Note that we mount /boot but we don't unmount it because we |
| 8 |
# are run in a systemd unit with MountFlags=slave so it is unmounted for us. |
| 9 |
bootmnt=/mnt/boot_partition |
| 10 |
mkdir -p ${bootmnt} |
| 11 |
bootdev=/dev/disk/by-label/boot |
| 12 |
mount -o rw ${bootdev} ${bootmnt} |
| 13 |
grubcfg="${bootmnt}/grub/grub.cfg" |
| 14 |
|
| 15 |
orig_kernelopts="$(grep kernelopts= $grubcfg | sed s,^kernelopts=,,)" |
| 16 |
# add leading and trailing whitespace to allow for easy sed replacements |
| 17 |
kernelopts=" $orig_kernelopts " |
| 18 |
|
| 19 |
while [[ $# -gt 0 ]] |
| 20 |
do |
| 21 |
key="$1" |
| 22 |
|
| 23 |
case $key in |
| 24 |
--should-exist) |
| 25 |
arg="$2" |
| 26 |
# don't repeat the arg |
| 27 |
if [[ ! "${kernelopts[*]}" =~ " ${arg} " ]]; then |
| 28 |
kernelopts="$kernelopts$arg " |
| 29 |
fi |
| 30 |
shift 2 |
| 31 |
;; |
| 32 |
--should-not-exist) |
| 33 |
kernelopts="$(echo "$kernelopts" | sed "s| $2 | |g")" |
| 34 |
shift 2 |
| 35 |
;; |
| 36 |
*) |
| 37 |
echo "Unknown option" |
| 38 |
exit 1 |
| 39 |
;; |
| 40 |
esac |
| 41 |
done |
| 42 |
|
| 43 |
# trim the leading and trailing whitespace |
| 44 |
kernelopts="$(echo "$kernelopts" | sed -e 's,^[[:space:]]*,,' -e 's,[[:space:]]*$,,')" |
| 45 |
|
| 46 |
# only apply the changes & reboot if changes have been made |
| 47 |
if [[ "$kernelopts" != "$orig_kernelopts" ]]; then |
| 48 |
sed -i "s|^\(kernelopts=\).*|\1$kernelopts|" $grubcfg |
| 49 |
|
| 50 |
systemctl reboot --force |
| 51 |
fi |