diff --git a/src/libcalamares/PythonHelper.cpp b/src/libcalamares/PythonHelper.cpp index 4dee34d35..a4bccf18a 100644 --- a/src/libcalamares/PythonHelper.cpp +++ b/src/libcalamares/PythonHelper.cpp @@ -53,6 +53,9 @@ variantToPyObject( const QVariant& variant ) case QVariant::String: return bp::object( variant.toString().toStdString() ); + case QVariant::Bool: + return bp::object( variant.toBool() ); + default: return bp::object(); } @@ -78,6 +81,9 @@ variantFromPyObject( const boost::python::object& pyObject ) else if ( pyType == "str" ) return QVariant( QString::fromStdString( bp::extract< std::string >( pyObject ) ) ); + else if ( pyType == "bool" ) + return QVariant( bp::extract< bool >( pyObject ) ); + else return QVariant(); } 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 ffa165d5a..ecec4531a 100644 --- a/src/modules/grubcfg/main.py +++ b/src/modules/grubcfg/main.py @@ -29,9 +29,6 @@ def modify_grub_default(partitions, root_mount_point, distributor): use_splash = "" swap_uuid = "" - if not os.path.exists(default_dir): - return ("Directory does not exist", "The directory {} does not exist on " - "the target".format(default_dir)) if os.path.exists(plymouth_bin): use_splash = "splash" @@ -44,19 +41,51 @@ 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 - elif lines[i].startswith("GRUB_CMDLINE_LINUX_DEFAULT"): - lines[i] = kernel_cmd - elif lines[i].startswith("#GRUB_DISTRIBUTOR") or lines[i].startswith("GRUB_DISTRIBUTOR"): - lines[i] = "GRUB_DISTRIBUTOR='%s'" % distributor.replace("'", "'\\''") + 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) + + if not have_distributor_line: + lines.append(distributor_line) with open(default_grub, 'w') as grub_file: grub_file.write("\n".join(lines) + "\n")