From d5b95d79e58501aa8456c5fbb02a523d8bb3fae7 Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Tue, 18 Nov 2014 03:40:47 +0100 Subject: [PATCH] grubcfg: Handle the case where expected lines are missing. Check whether we actually had a GRUB_CMDLINE_LINUX_DEFAULT and a GRUB_DISTRIBUTOR line, add any of them if it was missing. --- src/modules/grubcfg/main.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/modules/grubcfg/main.py b/src/modules/grubcfg/main.py index 0e6b5b5a5..51c0ba537 100644 --- a/src/modules/grubcfg/main.py +++ b/src/modules/grubcfg/main.py @@ -41,19 +41,33 @@ def modify_grub_default(partitions, root_mount_point, distributor): else: kernel_cmd = 'GRUB_CMDLINE_LINUX_DEFAULT="quiet %s"' % use_splash + distributor_line = "GRUB_DISTRIBUTOR='%s'" % distributor.replace("'", "'\\''") + if not os.path.exists(default_dir): os.mkdir(default_dir) with open(default_grub, 'r') as grub_file: lines = [x.strip() for x in grub_file.readlines()] + have_kernel_cmd = False + have_distributor_line = False + for i in range(len(lines)): if lines[i].startswith("#GRUB_CMDLINE_LINUX_DEFAULT"): lines[i] = kernel_cmd + have_kernel_cmd = True elif lines[i].startswith("GRUB_CMDLINE_LINUX_DEFAULT"): lines[i] = kernel_cmd + have_kernel_cmd = True elif lines[i].startswith("#GRUB_DISTRIBUTOR") or lines[i].startswith("GRUB_DISTRIBUTOR"): - lines[i] = "GRUB_DISTRIBUTOR='%s'" % distributor.replace("'", "'\\''") + lines[i] = distributor_line + have_distributor_line = True + + if not have_kernel_cmd: + lines.append(kernel_cmd) + + if not have_distributor_line: + lines.append(distributor_line) with open(default_grub, 'w') as grub_file: grub_file.write("\n".join(lines) + "\n")