From f58cca1bb2f65228d30aca60db8f609f7f7fa200 Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Tue, 18 Nov 2014 03:38:20 +0100 Subject: [PATCH 1/4] grubcfg: Don't error if /etc/default is missing. It is created later in the function. --- src/modules/grubcfg/main.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/grubcfg/main.py b/src/modules/grubcfg/main.py index 3e3ad81f0..0e6b5b5a5 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" From d5b95d79e58501aa8456c5fbb02a523d8bb3fae7 Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Tue, 18 Nov 2014 03:40:47 +0100 Subject: [PATCH 2/4] 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") From 1499963920f96c8871bb5680648c0055134cbb7c Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Tue, 18 Nov 2014 04:32:55 +0100 Subject: [PATCH 3/4] grubcfg: Create /etc/default/grub if missing. Also adds a grubcfg.conf with the following settings: * overwrite: 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. The default is "false". * defaults: 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. The default in the code is empty. The shipped grubcfg.conf currently reproduces the default settings from the Fedora installer Anaconda. Fixes #128. --- src/modules/grubcfg/grubcfg.conf | 13 ++++++++++ src/modules/grubcfg/main.py | 44 ++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/modules/grubcfg/grubcfg.conf 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) From 00604a8bcac25504ac5f571270dadca39a0b9d58 Mon Sep 17 00:00:00 2001 From: Kevin Kofler Date: Tue, 18 Nov 2014 05:34:01 +0100 Subject: [PATCH 4/4] PythonHelper: Add support for bool. Convert Python bool type from/to C++/QVariant bool (QVariant::Bool) in PythonHelper::variantToPyObject and PythonHelper::variantFromPyObject. This fixes the "override" option and any booleans in the "defaults" list in grubcfg.conf. --- src/libcalamares/PythonHelper.cpp | 6 ++++++ 1 file changed, 6 insertions(+) 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(); }