diff --git a/src/modules/grubcfg/grubcfg.conf b/src/modules/grubcfg/grubcfg.conf new file mode 100644 index 000000000..608c9b2b4 --- /dev/null +++ b/src/modules/grubcfg/grubcfg.conf @@ -0,0 +1,13 @@ +--- +# If set to true, always creates /etc/default/grub from scratch even if the file +# already existed. If set to false, edits the existing file instead. +overwrite: false +# Default entries to write to /etc/default/grub if it does not exist yet or if +# we are overwriting it. Note that in addition, GRUB_CMDLINE_LINUX_DEFAULT and +# GRUB_DISTRIBUTOR will always be written, with automatically detected values. +defaults: + GRUB_TIMEOUT: 5 + GRUB_DEFAULT: "saved" + GRUB_DISABLE_SUBMENU: true + GRUB_TERMINAL_OUTPUT: "console" + GRUB_DISABLE_RECOVERY: true diff --git a/src/modules/grubcfg/main.py b/src/modules/grubcfg/main.py index 51c0ba537..ca53024cb 100644 --- a/src/modules/grubcfg/main.py +++ b/src/modules/grubcfg/main.py @@ -46,22 +46,40 @@ def modify_grub_default(partitions, root_mount_point, distributor): 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] = distributor_line - have_distributor_line = True + if "overwrite" in libcalamares.job.configuration: + overwrite = libcalamares.job.configuration["overwrite"] + else: + overwrite = False + + if os.path.exists(default_grub) and not overwrite: + with open(default_grub, 'r') as grub_file: + lines = [x.strip() for x in grub_file.readlines()] + + 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] = distributor_line + have_distributor_line = True + else: + lines = [] + if "defaults" in libcalamares.job.configuration: + for key, value in libcalamares.job.configuration["defaults"].items(): + if value.__class__.__name__ == "bool": + if value: + escaped_value = "true" + else: + escaped_value = "false" + else: + escaped_value = str(value).replace("'", "'\\''") + lines.append("%s='%s'" % (key, escaped_value)) if not have_kernel_cmd: lines.append(kernel_cmd)