2014-08-19 14:57:22 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-08-19 14:57:22 +02:00
|
|
|
# === This file is part of Calamares - <http://github.com/calamares> ===
|
|
|
|
#
|
2015-02-21 10:24:13 +01:00
|
|
|
# Copyright 2014-2015, Philip Müller <philm@manjaro.org>
|
2015-03-31 18:14:33 +02:00
|
|
|
# Copyright 2015, Teo Mrnjavac <teo@kde.org>
|
2014-08-19 14:57:22 +02:00
|
|
|
#
|
2015-02-18 15:37:03 +01:00
|
|
|
# Calamares is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2014-08-19 14:57:22 +02:00
|
|
|
#
|
2015-02-18 15:37:03 +01:00
|
|
|
# Calamares is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2014-08-19 14:57:22 +02:00
|
|
|
#
|
2015-02-18 15:37:03 +01:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-19 14:57:22 +02:00
|
|
|
|
|
|
|
import libcalamares
|
|
|
|
import os
|
2015-03-31 18:14:33 +02:00
|
|
|
import re
|
2014-08-19 14:57:22 +02:00
|
|
|
|
|
|
|
def modify_grub_default(partitions, root_mount_point, distributor):
|
2015-02-21 10:24:13 +01:00
|
|
|
""" Configures '/etc/default/grub' for hibernation and plymouth.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param partitions:
|
|
|
|
:param root_mount_point:
|
|
|
|
:param distributor:
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-19 14:57:22 +02:00
|
|
|
default_dir = os.path.join(root_mount_point, "etc/default")
|
|
|
|
default_grub = os.path.join(default_dir, "grub")
|
2015-02-17 14:38:30 +01:00
|
|
|
distributor_replace = distributor.replace("'", "'\\''")
|
2016-10-19 17:41:35 +02:00
|
|
|
dracut_bin = libcalamares.utils.target_env_call(["sh", "-c", "which dracut"])
|
2014-08-19 15:46:36 +02:00
|
|
|
use_splash = ""
|
|
|
|
swap_uuid = ""
|
2016-10-20 17:00:15 +02:00
|
|
|
swap_outer_uuid = ""
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2016-09-12 07:59:17 +02:00
|
|
|
if libcalamares.globalstorage.contains("hasPlymouth"):
|
|
|
|
if libcalamares.globalstorage.value("hasPlymouth"):
|
|
|
|
use_splash = "splash"
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2016-05-03 17:19:00 +02:00
|
|
|
cryptdevice_params = []
|
|
|
|
|
2016-10-21 00:19:22 +02:00
|
|
|
if dracut_bin == 0:
|
2016-10-19 17:41:35 +02:00
|
|
|
for partition in partitions:
|
|
|
|
if partition["fs"] == "linuxswap":
|
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
2016-10-20 17:00:15 +02:00
|
|
|
if partition["fs"] == "linuxswap" and "luksMapperName" in partition:
|
|
|
|
swap_outer_uuid = partition["luksUuid"]
|
|
|
|
|
2016-10-19 17:41:35 +02:00
|
|
|
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
|
|
|
|
cryptdevice_params = ["rd.luks.uuid={!s}".format(partition["luksUuid"])]
|
|
|
|
else:
|
|
|
|
for partition in partitions:
|
|
|
|
if partition["fs"] == "linuxswap":
|
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
|
|
|
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
|
|
|
|
cryptdevice_params = [
|
|
|
|
"cryptdevice=UUID={!s}:{!s}".format(partition["luksUuid"],
|
|
|
|
partition["luksMapperName"]),
|
|
|
|
"root=/dev/mapper/{!s}".format(partition["luksMapperName"])
|
2016-05-04 13:03:30 +02:00
|
|
|
]
|
2016-05-03 17:19:00 +02:00
|
|
|
|
2015-03-31 18:14:33 +02:00
|
|
|
kernel_params = ["quiet"]
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2016-05-03 17:19:00 +02:00
|
|
|
if cryptdevice_params:
|
|
|
|
kernel_params.extend(cryptdevice_params)
|
|
|
|
|
2015-03-31 18:14:33 +02:00
|
|
|
if use_splash:
|
|
|
|
kernel_params.append(use_splash)
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2015-03-31 18:14:33 +02:00
|
|
|
if swap_uuid:
|
|
|
|
kernel_params.append("resume=UUID={!s}".format(swap_uuid))
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2016-10-21 00:19:22 +02:00
|
|
|
if dracut_bin == 0 and swap_outer_uuid:
|
2016-10-20 17:00:15 +02:00
|
|
|
kernel_params.append("rd.luks.uuid={!s}".format(swap_outer_uuid))
|
|
|
|
|
2016-10-16 18:44:22 +02:00
|
|
|
distributor_line = "GRUB_DISTRIBUTOR='{!s}'".format(distributor_replace)
|
2014-11-18 03:40:47 +01:00
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
if not os.path.exists(default_dir):
|
|
|
|
os.mkdir(default_dir)
|
|
|
|
|
2014-11-18 03:40:47 +01:00
|
|
|
have_kernel_cmd = False
|
|
|
|
have_distributor_line = False
|
|
|
|
|
2014-11-18 04:32:55 +01:00
|
|
|
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"):
|
2015-03-31 18:14:33 +02:00
|
|
|
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(" ".join(kernel_params))
|
2014-11-18 04:32:55 +01:00
|
|
|
lines[i] = kernel_cmd
|
|
|
|
have_kernel_cmd = True
|
|
|
|
elif lines[i].startswith("GRUB_CMDLINE_LINUX_DEFAULT"):
|
2015-04-02 20:25:41 +02:00
|
|
|
regex = re.compile(r"^GRUB_CMDLINE_LINUX_DEFAULT\s*=\s*")
|
2015-03-31 18:14:33 +02:00
|
|
|
line = regex.sub("", lines[i])
|
2015-04-02 13:24:47 +02:00
|
|
|
line = line.lstrip()
|
|
|
|
line = line.lstrip("\"")
|
2015-04-02 20:25:41 +02:00
|
|
|
line = line.lstrip("'")
|
2015-04-02 13:24:47 +02:00
|
|
|
line = line.rstrip()
|
|
|
|
line = line.rstrip("\"")
|
2015-04-02 20:25:41 +02:00
|
|
|
line = line.rstrip("'")
|
2015-03-31 18:14:33 +02:00
|
|
|
existing_params = line.split()
|
|
|
|
|
|
|
|
for existing_param in existing_params:
|
|
|
|
existing_param_name = existing_param.split("=")[0]
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2015-04-01 15:50:13 +02:00
|
|
|
if existing_param_name not in ["quiet", "resume", "splash"]: # the only ones we ever add
|
2015-03-31 18:14:33 +02:00
|
|
|
kernel_params.append(existing_param)
|
|
|
|
|
|
|
|
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(" ".join(kernel_params))
|
2014-11-18 04:32:55 +01:00
|
|
|
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 = []
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-11-18 04:32:55 +01:00
|
|
|
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("'", "'\\''")
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2016-10-16 18:44:22 +02:00
|
|
|
lines.append("{!s}='{!s}'".format(key, escaped_value))
|
2014-11-18 03:40:47 +01:00
|
|
|
|
|
|
|
if not have_kernel_cmd:
|
2015-04-01 15:50:13 +02:00
|
|
|
kernel_cmd = "GRUB_CMDLINE_LINUX_DEFAULT=\"{!s}\"".format(" ".join(kernel_params))
|
2014-11-18 03:40:47 +01:00
|
|
|
lines.append(kernel_cmd)
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2014-11-18 03:40:47 +01:00
|
|
|
if not have_distributor_line:
|
|
|
|
lines.append(distributor_line)
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2016-05-05 15:53:54 +02:00
|
|
|
if cryptdevice_params:
|
|
|
|
lines.append("GRUB_ENABLE_CRYPTODISK=y")
|
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
with open(default_grub, 'w') as grub_file:
|
|
|
|
grub_file.write("\n".join(lines) + "\n")
|
|
|
|
|
2014-10-14 15:09:38 +02:00
|
|
|
return None
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2015-02-18 15:37:03 +01:00
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
def run():
|
2015-02-21 10:24:13 +01:00
|
|
|
""" Calls routine with given parameters to modify '/etc/default/grub'.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2015-07-07 19:21:13 +02:00
|
|
|
if libcalamares.globalstorage.value("bootLoader") is None:
|
|
|
|
return None
|
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2014-11-16 04:33:27 +01:00
|
|
|
branding = libcalamares.globalstorage.value("branding")
|
2014-11-19 16:50:15 +01:00
|
|
|
distributor = branding["bootloaderEntryName"]
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-10-14 15:09:38 +02:00
|
|
|
return modify_grub_default(partitions, root_mount_point, distributor)
|