Skip to content

Commit fbab698

Browse files
alekseysene0ne
authored andcommitted
Add Ubuntu support
Many customers rely on the Ubuntu operating system, and the ability to automatically update GRUB parameters is essential. Signed-off-by: Aleksey Senin <[email protected]>
1 parent f9ef68a commit fbab698

File tree

1 file changed

+94
-13
lines changed

1 file changed

+94
-13
lines changed

bindata/scripts/kargs.sh

Lines changed: 94 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,139 @@
11
#!/bin/bash
2+
# Copyright 2025 sriov-network-device-plugin authors
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
# SPDX-License-Identifier: Apache-2.0
17+
218
set -x
319

420
command=$1
521
shift
622
declare -a kargs=( "$@" )
723
ret=0
8-
args=$(chroot /host/ cat /proc/cmdline)
924

10-
IS_OS_UBUNTU=true; [[ "$(chroot /host/ grep -i ubuntu /etc/os-release -c)" == "0" ]] && IS_OS_UBUNTU=false
25+
if [[ -d "/bindata/scripts" ]];then
26+
chroot_path="/host/"
27+
else
28+
chroot_path="/"
29+
fi
30+
31+
args=$(chroot "$chroot_path" cat /proc/cmdline)
32+
33+
IS_OS_UBUNTU=true; [[ "$(chroot "$chroot_path" grep -i ubuntu /etc/os-release -c)" == "0" ]] && IS_OS_UBUNTU=false
1134

12-
# Kernel args configuration isn't supported for Ubuntu now, so we shouldn't do anything here
1335
if ${IS_OS_UBUNTU} ; then
36+
grub_config="/etc/default/grub"
37+
# Operate on the copy of the file
38+
cp ${chroot_path}/${grub_config} /tmp/grub
39+
40+
for t in "${kargs[@]}";do
41+
if [[ $command == "add" ]];then
42+
# Modify only GRUB_CMDLINE_LINUX_DEFAULT line if it's not already present
43+
line=$(grep -P "^\s*GRUB_CMDLINE_LINUX_DEFAULT" /tmp/grub)
44+
if [ $? -ne 0 ];then
45+
exit 1
46+
fi
47+
48+
IFS='"' read g param q <<< "$line"
49+
arr=($param)
50+
found=false
51+
52+
for item in "${arr[@]}"; do
53+
if [[ "$item" == "${t}" ]]; then
54+
found=true
55+
break
56+
fi
57+
done
58+
59+
if [ $found == false ];then
60+
# Append to the end of the line
61+
new_param="${arr[@]} ${t}"
62+
sed -i "s/\(^\s*$g\"\)\(.*\)\"/\1${new_param}\"/" /tmp/grub
63+
let ret++
64+
fi
65+
fi
66+
67+
if [[ $command == "remove" ]];then
68+
# Remove from everywhere, except commented lines
69+
ret=$((ret + $(grep -E '^[[:space:]]*GRUB_CMDLINE_LINUX(_DEFAULT)?[[:space:]]*=.*(^|[[:space:]]|")'"$t"'([[:space:]]|"|$)' /tmp/grub | wc -l)))
70+
if [ $ret -gt 0 ];then
71+
while read line;do
72+
if [[ "$line" =~ GRUB_CMDLINE_LINUX ]];then
73+
IFS='"' read g param q <<< "$line"
74+
arr=($param)
75+
new_param=""
76+
77+
for item in "${arr[@]}"; do
78+
if [[ "$item" != "${t}" ]]; then
79+
new_param="${new_param} ${item}"
80+
fi
81+
done
82+
sed -i "s/\(^\s*$g\"\)\(.*\)\"/\1${new_param}\"/" /tmp/grub
83+
fi
84+
done < /tmp/grub
85+
fi
86+
fi
87+
done
88+
89+
if [ $ret -ne 0 ];then
90+
# Update grub only if there were changes
91+
cp /tmp/grub ${chroot_path}/${grub_config}
92+
chroot "$chroot_path" update-grub
93+
fi
94+
1495
echo $ret
1596
exit 0
1697
fi
1798

18-
if chroot /host/ test -f /run/ostree-booted ; then
99+
if chroot "$chroot_path" test -f /run/ostree-booted ; then
19100
for t in "${kargs[@]}";do
20101
if [[ $command == "add" ]];then
21102
if [[ $args != *${t}* ]];then
22-
if chroot /host/ rpm-ostree kargs | grep -vq ${t}; then
23-
chroot /host/ rpm-ostree kargs --append ${t} > /dev/null 2>&1
103+
if chroot "$chroot_path" rpm-ostree kargs | grep -vq ${t}; then
104+
chroot "$chroot_path" rpm-ostree kargs --append ${t} > /dev/null 2>&1
24105
fi
25106
let ret++
26107
fi
27108
fi
28109
if [[ $command == "remove" ]];then
29110
if [[ $args == *${t}* ]];then
30-
if chroot /host/ rpm-ostree kargs | grep -q ${t}; then
31-
chroot /host/ rpm-ostree kargs --delete ${t} > /dev/null 2>&1
111+
if chroot "$chroot_path" rpm-ostree kargs | grep -q ${t}; then
112+
chroot "$chroot_path" rpm-ostree kargs --delete ${t} > /dev/null 2>&1
32113
fi
33114
let ret++
34115
fi
35116
fi
36117
done
37118
else
38-
chroot /host/ which grubby > /dev/null 2>&1
119+
chroot "$chroot_path" which grubby > /dev/null 2>&1
39120
# if grubby is not there, let's tell it
40121
if [ $? -ne 0 ]; then
41122
exit 127
42123
fi
43124
for t in "${kargs[@]}";do
44125
if [[ $command == "add" ]];then
45126
if [[ $args != *${t}* ]];then
46-
if chroot /host/ grubby --info=DEFAULT | grep args | grep -vq ${t}; then
47-
chroot /host/ grubby --update-kernel=DEFAULT --args=${t} > /dev/null 2>&1
127+
if chroot "$chroot_path" grubby --info=DEFAULT | grep args | grep -vq ${t}; then
128+
chroot "$chroot_path" grubby --update-kernel=DEFAULT --args=${t} > /dev/null 2>&1
48129
fi
49130
let ret++
50131
fi
51132
fi
52133
if [[ $command == "remove" ]];then
53134
if [[ $args == *${t}* ]];then
54-
if chroot /host/ grubby --info=DEFAULT | grep args | grep -q ${t}; then
55-
chroot /host/ grubby --update-kernel=DEFAULT --remove-args=${t} > /dev/null 2>&1
135+
if chroot "$chroot_path" grubby --info=DEFAULT | grep args | grep -q ${t}; then
136+
chroot "$chroot_path" grubby --update-kernel=DEFAULT --remove-args=${t} > /dev/null 2>&1
56137
fi
57138
let ret++
58139
fi

0 commit comments

Comments
 (0)