summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Small <tim@seoss.co.uk>2022-11-24 13:49:22 +0000
committerLars Wirzenius <liw@liw.fi>2023-04-07 08:06:23 +0000
commit8a9977bc1703fd61670249750227d803649c95a5 (patch)
tree32a4cba1698c5113eaee4f3e0e6882877b1b4e04
parent5b91eb58673d43e27523877816169b14e1ab6dfc (diff)
downloadvmdb2-8a9977bc1703fd61670249750227d803649c95a5.tar.gz
Don't duplicate GRUB_TIMEOUT= in /etc/default/grub
Previously the `GRUB_TIMEOUT` parameter in `/etc/default/grub` was set at the end of the file. This superseded the previous definition of `GRUB_TIMEOUT=5` at the top of the file, but the resulting duplication was potentially confusing for the user. This uses a newly added helper method to update the timeout instead. When possible, the new method updates the original definitions within the defaults file instead (at the same location within the file as the original definition, or after commented-out example lines).
-rw-r--r--vmdb/plugins/grub_plugin.py27
1 files changed, 24 insertions, 3 deletions
diff --git a/vmdb/plugins/grub_plugin.py b/vmdb/plugins/grub_plugin.py
index 59b8d8a..cf40e2d 100644
--- a/vmdb/plugins/grub_plugin.py
+++ b/vmdb/plugins/grub_plugin.py
@@ -378,6 +378,29 @@ class GrubStepRunner(vmdb.StepRunnerInterface):
with open(filename, "w") as f:
f.write("\n".join(lines) + "\n")
+ def set_grub_default(self, chroot, param, value):
+ filename = self.chroot_path(chroot, "/etc/default/grub")
+ newdefault = param + "=" + str(value) + "\n"
+
+ found_param = False
+ newcontents = ""
+ with open(filename, "r+") as f:
+ for line in f:
+ if line.startswith(param + "=") and not found_param:
+ newcontents += newdefault
+ found_param = True
+ elif line.startswith("#" + param + "=") and not found_param:
+ newcontents += line
+ newcontents += newdefault
+ found_param = True
+ else:
+ newcontents += line
+ if found_param:
+ f.seek(0)
+ f.write(newcontents)
+ else:
+ f.write(newdefault)
+
def add_grub_serial_console(self, chroot):
filename = self.chroot_path(chroot, "/etc/default/grub")
@@ -394,6 +417,4 @@ class GrubStepRunner(vmdb.StepRunnerInterface):
f.write("GRUB_ENABLE_CRYPTODISK=y\n")
def set_grub_timeout(self, chroot, timeout):
- filename = self.chroot_path(chroot, "/etc/default/grub")
- with open(filename, "a") as f:
- f.write("GRUB_TIMEOUT={}\n".format(timeout))
+ self.set_grub_default(chroot, "GRUB_TIMEOUT", timeout)