@@ -9,8 +9,67 @@ args=$(chroot /host/ cat /proc/cmdline)
99
1010IS_OS_UBUNTU=true; [[ " $( chroot /host/ grep -i ubuntu /etc/os-release -c) " == " 0" ]] && IS_OS_UBUNTU=false
1111
12- # Kernel args configuration isn't supported for Ubuntu now, so we shouldn't do anything here
1312if ${IS_OS_UBUNTU} ; then
13+ grub_config=" /etc/default/grub"
14+ # Operate on the copy of the file
15+ cp /host/${grub_config} /tmp/grub
16+
17+ for t in " ${kargs[@]} " ; do
18+ if [[ $command == " add" ]]; then
19+ # Modify only GRUB_CMDLINE_LINUX_DEFAULT line if it's not already present
20+ line=$( grep -P " ^\s*GRUB_CMDLINE_LINUX_DEFAULT" /tmp/grub)
21+ if [ $? -ne 0 ]; then
22+ exit 1
23+ fi
24+
25+ IFS=' "' read g param q <<< " $line"
26+ arr=($param )
27+ found=false
28+
29+ for item in " ${arr[@]} " ; do
30+ if [[ " $item " == " ${t} " ]]; then
31+ found=true
32+ break
33+ fi
34+ done
35+
36+ if [ $found == false ]; then
37+ # Append to the end of the line
38+ t=" ${arr[@]} ${t} "
39+ sed -i " s/\(^\s*GRUB_CMDLINE_LINUX_DEFAULT=\" \)\(.*\)\" /\1${t} \" /" /tmp/grub
40+ let ret++
41+ fi
42+ fi
43+
44+ if [[ $command == " remove" ]]; then
45+ # Remove from everywhere, except commented lines
46+ ret=$(( ret + $(grep - E '^[[: space: ]]* GRUB_CMDLINE_LINUX(_DEFAULT)? [[: space: ]]*= .* (^| [[: space: ]]| ")'"$t "'([[: space: ]]| "| $)' / tmp/ grub | wc - l)) )
47+ if [ $ret -gt 0 ]; then
48+ while read line; do
49+ if [[ " $line " =~ GRUB_CMDLINE_LINUX ]]; then
50+ IFS=' "' read g param q <<< " $line"
51+
52+ arr=($param )
53+ new_param=" "
54+
55+ for item in " ${arr[@]} " ; do
56+ if [[ " $item " != " ${t} " ]]; then
57+ new_param=" ${new_param} ${item} "
58+ fi
59+ done
60+ sed -i " s/\(^\s*$g \" \)\(.*\)\" /\1${new_param} \" /" /tmp/grub
61+ fi
62+ done < /tmp/grub
63+ fi
64+ fi
65+ done
66+
67+ if [ $ret -ne 0 ]; then
68+ # Update grub only if there were changes
69+ cp /tmp/grub /host/${grub_config}
70+ chroot " /host" update-grub
71+ fi
72+
1473 echo $ret
1574 exit 0
1675fi
0 commit comments