2014-08-19 14:57:22 +02:00
|
|
|
#!/usr/bin/env python3
|
2015-02-18 15:06:10 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# === This file is part of Calamares - <https://calamares.io> ===
|
2014-08-19 14:57:22 +02:00
|
|
|
#
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014-2015 Philip Müller <philm@manjaro.org>
|
|
|
|
# SPDX-FileCopyrightText: 2015-2017 Teo Mrnjavac <teo@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@siduction.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017 2019, Adriaan de Groot <groot@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017-2018 Gabriel Craciunescu <crazy@frugalware.org>
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2014-08-19 14:57:22 +02:00
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# Calamares is Free Software: see the License-Identifier above.
|
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
|
|
|
|
2019-04-20 11:22:32 +02:00
|
|
|
import gettext
|
|
|
|
_ = gettext.translation("calamares-python",
|
|
|
|
localedir=libcalamares.utils.gettext_path(),
|
|
|
|
languages=libcalamares.utils.gettext_languages(),
|
|
|
|
fallback=True).gettext
|
|
|
|
|
|
|
|
|
|
|
|
def pretty_name():
|
|
|
|
return _("Configure GRUB.")
|
|
|
|
|
2017-02-17 16:19:33 +01:00
|
|
|
|
2020-08-07 12:05:46 +02:00
|
|
|
def get_grub_config_path(root_mount_point):
|
2020-08-07 12:02:42 +02:00
|
|
|
"""
|
|
|
|
Figures out where to put the grub config files. Returns
|
2020-08-07 12:05:46 +02:00
|
|
|
a the full path of a file inside that
|
|
|
|
directory, as "the config file".
|
2020-08-07 12:02:42 +02:00
|
|
|
|
|
|
|
Returns a path into @p root_mount_point.
|
|
|
|
"""
|
|
|
|
default_dir = os.path.join(root_mount_point, "etc/default")
|
2020-08-07 12:13:51 +02:00
|
|
|
default_config_file = "grub"
|
|
|
|
|
|
|
|
if "prefer_grub_d" in libcalamares.job.configuration and libcalamares.job.configuration["prefer_grub_d"]:
|
|
|
|
possible_dir = os.path.join(root_mount_point, "etc/default/grub.d")
|
|
|
|
if os.path.exists(possible_dir) and os.path.isdir(possible_dir):
|
|
|
|
default_dir = possible_dir
|
|
|
|
default_config_file = "00calamares"
|
2020-08-07 12:02:42 +02:00
|
|
|
|
2020-08-07 12:05:46 +02:00
|
|
|
if not os.path.exists(default_dir):
|
2020-08-07 13:42:47 +02:00
|
|
|
try:
|
|
|
|
os.mkdir(default_dir)
|
|
|
|
except:
|
|
|
|
libcalamares.utils.debug("Failed to create '%r'" % default_dir)
|
|
|
|
raise
|
2020-08-07 12:05:46 +02:00
|
|
|
|
2020-08-07 12:13:51 +02:00
|
|
|
return os.path.join(default_dir, default_config_file)
|
|
|
|
|
2020-08-07 12:02:42 +02:00
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
def modify_grub_default(partitions, root_mount_point, distributor):
|
2017-03-29 17:22:44 +02:00
|
|
|
"""
|
|
|
|
Configures '/etc/default/grub' for hibernation and plymouth.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
2017-09-07 09:42:46 +02:00
|
|
|
@see bootloader/main.py, for similar handling of kernel parameters
|
|
|
|
|
2015-02-20 20:54:25 +01:00
|
|
|
:param partitions:
|
|
|
|
:param root_mount_point:
|
2019-11-04 16:02:36 +01:00
|
|
|
:param distributor: name of the distributor to fill in for
|
2019-11-04 16:06:59 +01:00
|
|
|
GRUB_DISTRIBUTOR. Must be a string. If the job setting
|
|
|
|
*keepDistributor* is set, then this is only used if no
|
|
|
|
GRUB_DISTRIBUTOR is found at all (otherwise, when *keepDistributor*
|
|
|
|
is set, the GRUB_DISTRIBUTOR lines are left unchanged).
|
|
|
|
If *keepDistributor* is unset or false, then GRUB_DISTRIBUTOR
|
|
|
|
is always updated to set this value.
|
2015-02-20 20:54:25 +01:00
|
|
|
:return:
|
|
|
|
"""
|
2020-08-07 12:05:46 +02:00
|
|
|
default_grub = get_grub_config_path(root_mount_point)
|
2015-02-17 14:38:30 +01:00
|
|
|
distributor_replace = distributor.replace("'", "'\\''")
|
2017-03-29 17:22:44 +02:00
|
|
|
dracut_bin = libcalamares.utils.target_env_call(
|
|
|
|
["sh", "-c", "which dracut"]
|
|
|
|
)
|
2018-06-11 15:25:26 +02:00
|
|
|
plymouth_bin = libcalamares.utils.target_env_call(
|
|
|
|
["sh", "-c", "which plymouth"]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Shell exit value 0 means success
|
|
|
|
have_plymouth = plymouth_bin == 0
|
|
|
|
have_dracut = dracut_bin == 0
|
2017-09-04 12:42:51 +02:00
|
|
|
|
2014-08-19 15:46:36 +02:00
|
|
|
use_splash = ""
|
|
|
|
swap_uuid = ""
|
2016-10-20 17:00:15 +02:00
|
|
|
swap_outer_uuid = ""
|
2017-09-07 09:42:46 +02:00
|
|
|
swap_outer_mappername = None
|
2020-02-24 16:49:56 +01:00
|
|
|
no_save_default = False
|
2020-11-12 22:57:02 +01:00
|
|
|
unencrypted_separate_boot = any(p["mountPoint"] == "/boot" and "luksMapperName" not in p for p in partitions):
|
2020-02-24 16:49:56 +01:00
|
|
|
|
|
|
|
for partition in partitions:
|
|
|
|
if partition["mountPoint"] in ("/", "/boot") and partition["fs"] in ("btrfs", "f2fs"):
|
|
|
|
no_save_default = True
|
|
|
|
break
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2018-06-11 15:25:26 +02:00
|
|
|
if have_plymouth:
|
|
|
|
use_splash = "splash"
|
2014-08-19 14:57:22 +02:00
|
|
|
|
2016-05-03 17:19:00 +02:00
|
|
|
cryptdevice_params = []
|
|
|
|
|
2017-09-04 12:42:51 +02:00
|
|
|
if have_dracut:
|
2016-10-19 17:41:35 +02:00
|
|
|
for partition in partitions:
|
2020-02-20 16:59:05 +01:00
|
|
|
if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
|
|
|
|
# Skip foreign swap
|
|
|
|
continue
|
2017-09-07 09:42:46 +02:00
|
|
|
has_luks = "luksMapperName" in partition
|
|
|
|
if partition["fs"] == "linuxswap" and not has_luks:
|
2016-10-19 17:41:35 +02:00
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
2017-09-07 09:42:46 +02:00
|
|
|
if (partition["fs"] == "linuxswap" and has_luks):
|
2016-10-20 17:00:15 +02:00
|
|
|
swap_outer_uuid = partition["luksUuid"]
|
2017-09-07 09:42:46 +02:00
|
|
|
swap_outer_mappername = partition["luksMapperName"]
|
2016-10-20 17:00:15 +02:00
|
|
|
|
2019-02-23 22:52:10 +01:00
|
|
|
if (partition["mountPoint"] == "/" and has_luks):
|
2017-03-29 17:22:44 +02:00
|
|
|
cryptdevice_params = [
|
|
|
|
"rd.luks.uuid={!s}".format(partition["luksUuid"])
|
|
|
|
]
|
2016-10-19 17:41:35 +02:00
|
|
|
else:
|
|
|
|
for partition in partitions:
|
2020-02-20 16:59:05 +01:00
|
|
|
if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
|
|
|
|
# Skip foreign swap
|
|
|
|
continue
|
2017-09-07 09:42:46 +02:00
|
|
|
has_luks = "luksMapperName" in partition
|
|
|
|
if partition["fs"] == "linuxswap" and not has_luks:
|
2016-10-19 17:41:35 +02:00
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
2020-03-18 17:12:33 +01:00
|
|
|
if (partition["fs"] == "linuxswap" and has_luks):
|
|
|
|
swap_outer_mappername = partition["luksMapperName"]
|
|
|
|
|
2019-02-23 22:52:10 +01:00
|
|
|
if (partition["mountPoint"] == "/" and has_luks):
|
2016-10-19 17:41:35 +02:00
|
|
|
cryptdevice_params = [
|
2017-03-29 17:22:44 +02:00
|
|
|
"cryptdevice=UUID={!s}:{!s}".format(
|
|
|
|
partition["luksUuid"], partition["luksMapperName"]
|
|
|
|
),
|
2017-09-07 09:42:46 +02:00
|
|
|
"root=/dev/mapper/{!s}".format(
|
|
|
|
partition["luksMapperName"]
|
|
|
|
)
|
2017-02-17 16:19:33 +01: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
|
|
|
|
2017-09-05 09:39:34 +02:00
|
|
|
if have_dracut and swap_outer_uuid:
|
2016-10-20 17:00:15 +02:00
|
|
|
kernel_params.append("rd.luks.uuid={!s}".format(swap_outer_uuid))
|
2020-03-18 17:12:33 +01:00
|
|
|
if swap_outer_mappername:
|
2017-09-07 09:42:46 +02:00
|
|
|
kernel_params.append("resume=/dev/mapper/{!s}".format(
|
|
|
|
swap_outer_mappername))
|
2016-10-20 17:00:15 +02:00
|
|
|
|
2016-10-16 18:44:22 +02:00
|
|
|
distributor_line = "GRUB_DISTRIBUTOR='{!s}'".format(distributor_replace)
|
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"):
|
2017-03-29 17:22:44 +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
|
|
|
|
2017-03-29 17:22:44 +02:00
|
|
|
# the only ones we ever add
|
|
|
|
if existing_param_name not in [
|
|
|
|
"quiet", "resume", "splash"]:
|
2015-03-31 18:14:33 +02:00
|
|
|
kernel_params.append(existing_param)
|
|
|
|
|
2017-03-29 17:22:44 +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
|
2017-03-29 17:22:44 +02:00
|
|
|
elif (lines[i].startswith("#GRUB_DISTRIBUTOR")
|
|
|
|
or lines[i].startswith("GRUB_DISTRIBUTOR")):
|
2019-11-18 10:48:33 +01:00
|
|
|
if libcalamares.job.configuration.get("keepDistributor", False):
|
2019-11-04 16:02:36 +01:00
|
|
|
lines[i] = distributor_line
|
2019-11-04 16:32:14 +01:00
|
|
|
have_distributor_line = True
|
|
|
|
else:
|
|
|
|
# We're not updating because of *keepDistributor*, but if
|
|
|
|
# this was a comment line, then it's still not been set.
|
2019-11-30 21:46:45 +01:00
|
|
|
have_distributor_line = have_distributor_line or not lines[i].startswith("#")
|
2020-02-24 16:49:56 +01:00
|
|
|
# If btrfs or f2fs is used, don't save default
|
|
|
|
if no_save_default and lines[i].startswith("GRUB_SAVEDEFAULT="):
|
|
|
|
lines[i] = "#GRUB_SAVEDEFAULT=\"true\""
|
2014-11-18 04:32:55 +01:00
|
|
|
else:
|
|
|
|
lines = []
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-11-18 04:32:55 +01:00
|
|
|
if "defaults" in libcalamares.job.configuration:
|
2017-03-29 17:22:44 +02:00
|
|
|
for key, value in libcalamares.job.configuration[
|
|
|
|
"defaults"].items():
|
2014-11-18 04:32:55 +01:00
|
|
|
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:
|
2017-03-29 17:22:44 +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
|
|
|
|
2020-11-03 22:15:35 +01:00
|
|
|
if cryptdevice_params and not unencrypted_separate_boot:
|
2016-05-05 15:53:54 +02:00
|
|
|
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():
|
2017-03-29 17:22:44 +02:00
|
|
|
"""
|
|
|
|
Calls routine with given parameters to modify '/etc/default/grub'.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2017-02-17 16:19:33 +01:00
|
|
|
|
|
|
|
fw_type = libcalamares.globalstorage.value("firmwareType")
|
|
|
|
|
2017-03-29 17:22:44 +02:00
|
|
|
if (libcalamares.globalstorage.value("bootLoader") is None
|
|
|
|
and fw_type != "efi"):
|
2015-07-07 19:21:13 +02:00
|
|
|
return None
|
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2017-02-17 16:19:33 +01:00
|
|
|
|
|
|
|
if fw_type == "efi":
|
|
|
|
esp_found = False
|
|
|
|
|
|
|
|
for partition in partitions:
|
2017-03-29 17:22:44 +02:00
|
|
|
if (partition["mountPoint"]
|
|
|
|
== libcalamares.globalstorage.value("efiSystemPartition")):
|
2017-02-17 16:19:33 +01:00
|
|
|
esp_found = True
|
|
|
|
|
|
|
|
if not esp_found:
|
|
|
|
return None
|
|
|
|
|
2014-08-19 14:57:22 +02:00
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2014-11-16 04:33:27 +01:00
|
|
|
branding = libcalamares.globalstorage.value("branding")
|
2019-11-04 16:06:59 +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)
|