@@ -35,13 +35,17 @@ IS_OS_UBUNTU=true; [[ "$(chroot "$chroot_path" grep -i ubuntu /etc/os-release -c
3535if ${IS_OS_UBUNTU} ; then
3636 grub_config=" /etc/default/grub"
3737 # Operate on the copy of the file
38- cp ${chroot_path} /${grub_config} /tmp/grub
38+ # Use host's /tmp because container root might be read-only
39+ tmp_grub_path=$( chroot " $chroot_path " mktemp)
40+ tmp_grub_in_container=" ${chroot_path}${tmp_grub_path} "
41+ cp ${chroot_path} /${grub_config} " ${tmp_grub_in_container} "
3942
4043 for t in " ${kargs[@]} " ; do
4144 if [[ $command == " add" ]]; then
4245 # Modify only GRUB_CMDLINE_LINUX_DEFAULT line if it's not already present
43- line=$( grep -P " ^\s*GRUB_CMDLINE_LINUX_DEFAULT" /tmp/grub )
46+ line=$( grep -P " ^\s*GRUB_CMDLINE_LINUX_DEFAULT" " ${tmp_grub_in_container} " )
4447 if [ $? -ne 0 ]; then
48+ rm -f " ${tmp_grub_in_container} "
4549 exit 1
4650 fi
4751
@@ -59,14 +63,14 @@ if ${IS_OS_UBUNTU} ; then
5963 if [ $found == false ]; then
6064 # Append to the end of the line
6165 new_param=" ${arr[@]} ${t} "
62- sed -i " s/\(^\s*$g \" \)\(.*\)\" /\1${new_param} \" /" /tmp/grub
66+ sed -i " s/\(^\s*$g \" \)\(.*\)\" /\1${new_param} \" /" " ${tmp_grub_in_container} "
6367 let ret++
6468 fi
6569 fi
6670
6771 if [[ $command == " remove" ]]; then
6872 # Remove from everywhere, except commented lines
69- ret=$(( ret + $(grep - E '^[[: space: ]]* GRUB_CMDLINE_LINUX(_DEFAULT)? [[: space: ]]*= .* (^| [[: space: ]]| ")'"$t "'([[: space: ]]| "| $)' / tmp / grub | wc - l)) )
73+ ret=$(( ret + $(grep - E '^[[: space: ]]* GRUB_CMDLINE_LINUX(_DEFAULT)? [[: space: ]]*= .* (^| [[: space: ]]| ")'"$t "'([[: space: ]]| "| $)' " ${tmp_grub_in_container} " | wc - l)) )
7074 if [ $ret -gt 0 ]; then
7175 while read line; do
7276 if [[ " $line " =~ GRUB_CMDLINE_LINUX ]]; then
@@ -79,19 +83,20 @@ if ${IS_OS_UBUNTU} ; then
7983 new_param=" ${new_param} ${item} "
8084 fi
8185 done
82- sed -i " s/\(^\s*$g \" \)\(.*\)\" /\1${new_param} \" /" /tmp/grub
86+ sed -i " s/\(^\s*$g \" \)\(.*\)\" /\1${new_param} \" /" " ${tmp_grub_in_container} "
8387 fi
84- done < /tmp/grub
88+ done < " ${tmp_grub_in_container} "
8589 fi
8690 fi
8791 done
8892
8993 if [ $ret -ne 0 ]; then
9094 # Update grub only if there were changes
91- cp /tmp/grub ${chroot_path} /${grub_config}
95+ cp " ${tmp_grub_in_container} " ${chroot_path} /${grub_config}
9296 chroot " $chroot_path " update-grub
9397 fi
9498
99+ rm -f " ${tmp_grub_in_container} "
95100 echo $ret
96101 exit 0
97102fi
0 commit comments