Merge pull request #155 from calamares/fix-issue-128

grubcfg: Create /etc/default/grub if missing.
This commit is contained in:
Teo Mrnjavac 2014-11-19 17:20:06 +01:00
commit 01c3244396
3 changed files with 60 additions and 12 deletions

View file

@ -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")