|
1 | 1 | #!/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 | + |
2 | 18 | set -x |
3 | 19 |
|
4 | 20 | command=$1 |
5 | 21 | shift |
6 | 22 | declare -a kargs=( "$@" ) |
7 | 23 | ret=0 |
8 | | -args=$(chroot /host/ cat /proc/cmdline) |
9 | 24 |
|
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 |
11 | 34 |
|
12 | | -# Kernel args configuration isn't supported for Ubuntu now, so we shouldn't do anything here |
13 | 35 | 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 | + |
14 | 95 | echo $ret |
15 | 96 | exit 0 |
16 | 97 | fi |
17 | 98 |
|
18 | | -if chroot /host/ test -f /run/ostree-booted ; then |
| 99 | +if chroot "$chroot_path" test -f /run/ostree-booted ; then |
19 | 100 | for t in "${kargs[@]}";do |
20 | 101 | if [[ $command == "add" ]];then |
21 | 102 | 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 |
24 | 105 | fi |
25 | 106 | let ret++ |
26 | 107 | fi |
27 | 108 | fi |
28 | 109 | if [[ $command == "remove" ]];then |
29 | 110 | 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 |
32 | 113 | fi |
33 | 114 | let ret++ |
34 | 115 | fi |
35 | 116 | fi |
36 | 117 | done |
37 | 118 | else |
38 | | - chroot /host/ which grubby > /dev/null 2>&1 |
| 119 | + chroot "$chroot_path" which grubby > /dev/null 2>&1 |
39 | 120 | # if grubby is not there, let's tell it |
40 | 121 | if [ $? -ne 0 ]; then |
41 | 122 | exit 127 |
42 | 123 | fi |
43 | 124 | for t in "${kargs[@]}";do |
44 | 125 | if [[ $command == "add" ]];then |
45 | 126 | 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 |
48 | 129 | fi |
49 | 130 | let ret++ |
50 | 131 | fi |
51 | 132 | fi |
52 | 133 | if [[ $command == "remove" ]];then |
53 | 134 | 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 |
56 | 137 | fi |
57 | 138 | let ret++ |
58 | 139 | fi |
|
0 commit comments