2014-10-12 19:45:02 +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-10-12 19:45:02 +02:00
|
|
|
#
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014 Aurélien Gâteau <agateau@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2014 Anke Boersma <demm@kaosx.us>
|
|
|
|
# SPDX-FileCopyrightText: 2014 Daniel Hillenbrand <codeworkx@bbqlinux.org>
|
|
|
|
# SPDX-FileCopyrightText: 2014 Benjamin Vaudour <benjamin.vaudour@yahoo.fr>
|
|
|
|
# SPDX-FileCopyrightText: 2014-2019 Kevin Kofler <kevin.kofler@chello.at>
|
|
|
|
# SPDX-FileCopyrightText: 2015-2018 Philip Mueller <philm@manjaro.org>
|
|
|
|
# SPDX-FileCopyrightText: 2016-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 Gabriel Craciunescu <crazy@frugalware.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017 Ben Green <Bezzy1999@hotmail.com>
|
2021-01-30 11:37:41 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Neal Gompa <ngompa13@gmail.com>
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2014-10-12 19:45:02 +02:00
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# Calamares is Free Software: see the License-Identifier above.
|
2014-10-12 19:45:02 +02:00
|
|
|
#
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
import fileinput
|
2014-10-12 19:45:02 +02:00
|
|
|
import os
|
2022-12-23 00:13:43 +01:00
|
|
|
import re
|
2017-03-16 16:59:28 +01:00
|
|
|
import shutil
|
2014-10-12 19:45:02 +02:00
|
|
|
import subprocess
|
|
|
|
|
2016-08-23 10:20:52 +02:00
|
|
|
import libcalamares
|
|
|
|
|
2015-08-06 12:13:21 +02:00
|
|
|
from libcalamares.utils import check_target_env_call
|
2014-10-12 19:45:02 +02:00
|
|
|
|
2019-04-19 16:39:41 +02:00
|
|
|
import gettext
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2019-04-19 16:39:41 +02:00
|
|
|
_ = gettext.translation("calamares-python",
|
|
|
|
localedir=libcalamares.utils.gettext_path(),
|
|
|
|
languages=libcalamares.utils.gettext_languages(),
|
|
|
|
fallback=True).gettext
|
|
|
|
|
2019-05-10 21:28:37 +02:00
|
|
|
# This is the sanitizer used all over to tidy up filenames
|
|
|
|
# to make identifiers (or to clean up names to make filenames).
|
|
|
|
file_name_sanitizer = str.maketrans(" /()", "_-__")
|
2019-04-19 16:39:41 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2019-04-19 16:39:41 +02:00
|
|
|
def pretty_name():
|
|
|
|
return _("Install bootloader.")
|
|
|
|
|
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
def get_uuid():
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Checks and passes 'uuid' to other routine.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-10-12 19:45:02 +02:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
for partition in partitions:
|
|
|
|
if partition["mountPoint"] == "/":
|
2018-01-30 11:26:29 +01:00
|
|
|
libcalamares.utils.debug("Root partition uuid: \"{!s}\"".format(partition["uuid"]))
|
2014-10-12 19:45:02 +02:00
|
|
|
return partition["uuid"]
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
return ""
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-11-13 05:10:20 +01:00
|
|
|
|
2015-02-19 17:39:11 +01:00
|
|
|
def get_kernel_line(kernel_type):
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Passes 'kernel_line' to other routine based on configuration file.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param kernel_type:
|
|
|
|
:return:
|
|
|
|
"""
|
2015-02-19 18:10:31 +01:00
|
|
|
if kernel_type == "fallback":
|
|
|
|
if "fallbackKernelLine" in libcalamares.job.configuration:
|
2015-02-19 17:39:11 +01:00
|
|
|
return libcalamares.job.configuration["fallbackKernelLine"]
|
|
|
|
else:
|
|
|
|
return " (fallback)"
|
2015-02-19 18:10:31 +01:00
|
|
|
else:
|
|
|
|
if "kernelLine" in libcalamares.job.configuration:
|
|
|
|
return libcalamares.job.configuration["kernelLine"]
|
2015-02-19 17:39:11 +01:00
|
|
|
else:
|
|
|
|
return ""
|
2014-11-19 17:01:02 +01:00
|
|
|
|
2015-02-18 16:03:57 +01:00
|
|
|
|
2021-11-07 16:32:52 +01:00
|
|
|
def get_zfs_root():
|
|
|
|
"""
|
|
|
|
Looks in global storage to find the zfs root
|
|
|
|
|
|
|
|
:return: A string containing the path to the zfs root or None if it is not found
|
|
|
|
"""
|
|
|
|
|
2021-11-17 00:48:49 +01:00
|
|
|
zfs = libcalamares.globalstorage.value("zfsDatasets")
|
2021-11-07 16:32:52 +01:00
|
|
|
|
|
|
|
if not zfs:
|
|
|
|
libcalamares.utils.warning("Failed to locate zfs dataset list")
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Find the root dataset
|
|
|
|
for dataset in zfs:
|
|
|
|
try:
|
2021-11-16 00:59:33 +01:00
|
|
|
if dataset["mountpoint"] == "/":
|
2021-11-17 00:48:49 +01:00
|
|
|
return dataset["zpool"] + "/" + dataset["dsName"]
|
2021-11-07 16:32:52 +01:00
|
|
|
except KeyError:
|
|
|
|
# This should be impossible
|
2021-11-16 00:59:33 +01:00
|
|
|
libcalamares.utils.warning("Internal error handling zfs dataset")
|
2021-11-07 16:32:52 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-11-16 00:59:33 +01:00
|
|
|
def is_btrfs_root(partition):
|
|
|
|
""" Returns True if the partition object refers to a btrfs root filesystem
|
|
|
|
|
|
|
|
:param partition: A partition map from global storage
|
|
|
|
:return: True if btrfs and root, False otherwise
|
|
|
|
"""
|
|
|
|
return partition["mountPoint"] == "/" and partition["fs"] == "btrfs"
|
|
|
|
|
|
|
|
|
|
|
|
def is_zfs_root(partition):
|
|
|
|
""" Returns True if the partition object refers to a zfs root filesystem
|
|
|
|
|
|
|
|
:param partition: A partition map from global storage
|
|
|
|
:return: True if zfs and root, False otherwise
|
|
|
|
"""
|
|
|
|
return partition["mountPoint"] == "/" and partition["fs"] == "zfs"
|
|
|
|
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
def get_kernel_params(uuid):
|
|
|
|
kernel_params = libcalamares.job.configuration.get("kernelParams", ["quiet"])
|
|
|
|
kernel_params.append("rw")
|
2016-05-04 13:05:04 +02:00
|
|
|
|
2014-11-11 06:13:15 +01:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
2016-05-04 13:05:04 +02:00
|
|
|
swap_uuid = ""
|
2020-03-18 17:21:20 +01:00
|
|
|
swap_outer_mappername = None
|
2022-12-23 00:13:43 +01:00
|
|
|
swap_outer_uuid = None
|
2016-05-04 13:05:04 +02:00
|
|
|
|
|
|
|
cryptdevice_params = []
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
have_dracut = libcalamares.utils.target_env_call(["sh", "-c", "which dracut"]) == 0
|
2023-08-19 10:26:36 +02:00
|
|
|
uses_sd-encrypt = libcalamares.utils.target_env_call(["sh", "-c", "grep -q sd-encrypt /etc/mkinitcpio.conf"])
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2017-09-07 09:42:46 +02:00
|
|
|
# Take over swap settings:
|
|
|
|
# - unencrypted swap partition sets swap_uuid
|
|
|
|
# - encrypted root sets cryptdevice_params
|
2014-11-11 06:13:15 +01:00
|
|
|
for partition in partitions:
|
2020-03-18 17:21:20 +01:00
|
|
|
if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
|
2022-12-23 00:13:43 +01:00
|
|
|
# Skip foreign swap
|
2020-03-18 17:21:20 +01:00
|
|
|
continue
|
2017-09-07 09:42:46 +02:00
|
|
|
has_luks = "luksMapperName" in partition
|
|
|
|
if partition["fs"] == "linuxswap" and not has_luks:
|
2016-05-04 13:05:04 +02:00
|
|
|
swap_uuid = partition["uuid"]
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
if partition["fs"] == "linuxswap" and has_luks:
|
2020-03-18 17:21:20 +01:00
|
|
|
swap_outer_mappername = partition["luksMapperName"]
|
2022-12-23 00:13:43 +01:00
|
|
|
swap_outer_uuid = partition["luksUuid"]
|
2020-03-18 17:21:20 +01:00
|
|
|
|
2017-09-07 09:42:46 +02:00
|
|
|
if partition["mountPoint"] == "/" and has_luks:
|
2023-08-17 17:14:47 +02:00
|
|
|
if have_dracut or uses_sd-encrypt:
|
2022-12-23 00:13:43 +01:00
|
|
|
cryptdevice_params = [f"rd.luks.uuid={partition['luksUuid']}"]
|
|
|
|
else:
|
|
|
|
cryptdevice_params = [f"cryptdevice=UUID={partition['luksUuid']}:{partition['luksMapperName']}"]
|
|
|
|
cryptdevice_params.append(f"root=/dev/mapper/{partition['luksMapperName']}")
|
2016-05-04 13:05:04 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
# btrfs and zfs handling
|
2021-09-05 13:34:38 +02:00
|
|
|
for partition in partitions:
|
2021-11-18 17:04:49 +01:00
|
|
|
# If a btrfs root subvolume wasn't set, it means the root is directly on the partition
|
|
|
|
# and this option isn't needed
|
2021-11-16 00:59:33 +01:00
|
|
|
if is_btrfs_root(partition):
|
2021-11-18 17:04:49 +01:00
|
|
|
btrfs_root_subvolume = libcalamares.globalstorage.value("btrfsRootSubvolume")
|
|
|
|
if btrfs_root_subvolume:
|
|
|
|
kernel_params.append("rootflags=subvol=" + btrfs_root_subvolume)
|
2021-09-05 13:34:38 +02:00
|
|
|
|
2021-11-07 16:32:52 +01:00
|
|
|
# zfs needs to be told the location of the root dataset
|
2021-11-16 00:59:33 +01:00
|
|
|
if is_zfs_root(partition):
|
|
|
|
zfs_root_path = get_zfs_root()
|
|
|
|
if zfs_root_path is not None:
|
2022-12-23 00:13:43 +01:00
|
|
|
kernel_params.append("root=ZFS=" + zfs_root_path)
|
2021-11-07 16:32:52 +01:00
|
|
|
else:
|
|
|
|
# Something is really broken if we get to this point
|
2021-11-16 00:59:33 +01:00
|
|
|
libcalamares.utils.warning("Internal error handling zfs dataset")
|
|
|
|
raise Exception("Internal zfs data missing, please contact your distribution")
|
2021-09-05 13:34:38 +02:00
|
|
|
|
2016-05-04 13:05:04 +02:00
|
|
|
if cryptdevice_params:
|
|
|
|
kernel_params.extend(cryptdevice_params)
|
|
|
|
else:
|
|
|
|
kernel_params.append("root=UUID={!s}".format(uuid))
|
|
|
|
|
|
|
|
if swap_uuid:
|
|
|
|
kernel_params.append("resume=UUID={!s}".format(swap_uuid))
|
2015-02-15 00:03:50 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
if have_dracut and swap_outer_uuid:
|
|
|
|
kernel_params.append(f"rd.luks.uuid={swap_outer_uuid}")
|
|
|
|
|
2020-03-18 17:21:20 +01:00
|
|
|
if swap_outer_mappername:
|
2022-12-23 00:13:43 +01:00
|
|
|
kernel_params.append(f"resume=/dev/mapper/{swap_outer_mappername}")
|
2020-03-18 17:21:20 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
return kernel_params
|
2019-02-08 18:00:58 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
|
|
|
|
def create_systemd_boot_conf(installation_root_path, efi_dir, uuid, kernel, kernel_version):
|
|
|
|
"""
|
|
|
|
Creates systemd-boot configuration files based on given parameters.
|
|
|
|
|
|
|
|
:param installation_root_path: A string containing the absolute path to the root of the installation
|
|
|
|
:param efi_dir: A string containing the path to the efi dir relative to the root of the installation
|
|
|
|
:param uuid: A string containing the UUID of the root volume
|
|
|
|
:param kernel: A string containing the path to the kernel relative to the root of the installation
|
|
|
|
:param kernel_version: The kernel version string
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Get the kernel params and write them to /etc/kernel/cmdline
|
|
|
|
# This file is used by kernel-install
|
|
|
|
kernel_params = " ".join(get_kernel_params(uuid))
|
|
|
|
kernel_cmdline_path = os.path.join(installation_root_path, "etc", "kernel")
|
|
|
|
os.makedirs(kernel_cmdline_path, exist_ok=True)
|
|
|
|
with open(os.path.join(kernel_cmdline_path, "cmdline"), "w") as cmdline_file:
|
|
|
|
cmdline_file.write(kernel_params)
|
|
|
|
|
|
|
|
libcalamares.utils.debug(f"Configuring kernel version {kernel_version}")
|
2019-02-08 18:00:58 +01:00
|
|
|
|
2021-02-22 00:27:46 +01:00
|
|
|
# get the machine-id
|
2021-09-20 02:23:56 +02:00
|
|
|
with open(os.path.join(installation_root_path, "etc", "machine-id"), 'r') as machineid_file:
|
2021-02-22 00:27:46 +01:00
|
|
|
machine_id = machineid_file.read().rstrip('\n')
|
2019-02-08 18:00:58 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
# Ensure the directory exists
|
2021-09-20 02:23:56 +02:00
|
|
|
machine_dir = os.path.join(installation_root_path + efi_dir, machine_id)
|
|
|
|
os.makedirs(machine_dir, exist_ok=True)
|
2019-02-08 18:00:58 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
# Call kernel-install for each kernel
|
|
|
|
libcalamares.utils.target_env_process_output(["kernel-install",
|
|
|
|
"add",
|
|
|
|
kernel_version,
|
|
|
|
os.path.join("/", kernel)])
|
2019-02-08 18:00:58 +01:00
|
|
|
|
2021-02-22 00:27:46 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
def create_loader(loader_path, installation_root_path):
|
|
|
|
"""
|
|
|
|
Writes configuration for loader.
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
:param loader_path: The absolute path to the loader.conf file
|
|
|
|
:param installation_root_path: The path to the root of the target installation
|
|
|
|
"""
|
2021-11-30 11:39:30 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
# get the machine-id
|
|
|
|
with open(os.path.join(installation_root_path, "etc", "machine-id"), 'r') as machineid_file:
|
|
|
|
machine_id = machineid_file.read().rstrip('\n')
|
2021-02-22 00:27:46 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
try:
|
|
|
|
loader_entries = libcalamares.job.configuration["loaderEntries"]
|
|
|
|
except KeyError:
|
|
|
|
libcalamares.utils.debug("No aditional loader entries found in config")
|
|
|
|
loader_entries = []
|
|
|
|
pass
|
2021-11-30 11:39:30 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
lines = [f"default {machine_id}*"]
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
lines.extend(loader_entries)
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2016-05-04 13:30:54 +02:00
|
|
|
with open(loader_path, 'w') as loader_file:
|
|
|
|
for line in lines:
|
2022-12-23 00:13:43 +01:00
|
|
|
loader_file.write(line + "\n")
|
2014-10-16 21:08:18 +02:00
|
|
|
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
class SuffixIterator(object):
|
2022-01-18 15:09:44 +01:00
|
|
|
"""
|
|
|
|
Wrapper for one of the "generator" classes below to behave like
|
|
|
|
a proper Python iterator. The iterator is initialized with a
|
|
|
|
maximum number of attempts to generate a new suffix.
|
|
|
|
"""
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2022-01-18 15:09:44 +01:00
|
|
|
def __init__(self, attempts, generator):
|
|
|
|
self.generator = generator
|
|
|
|
self.attempts = attempts
|
|
|
|
self.counter = 0
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
self.counter += 1
|
|
|
|
if self.counter <= self.attempts:
|
|
|
|
return self.generator.next()
|
|
|
|
raise StopIteration
|
|
|
|
|
|
|
|
|
2021-11-19 12:34:35 +01:00
|
|
|
class serialEfi(object):
|
|
|
|
"""
|
|
|
|
EFI Id generator that appends a serial number to the given name.
|
|
|
|
"""
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2021-11-19 12:34:35 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
# So the first call to next() will bump it to 0
|
|
|
|
self.counter = -1
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
self.counter += 1
|
|
|
|
if self.counter > 0:
|
|
|
|
return "{!s}{!s}".format(self.name, self.counter)
|
|
|
|
else:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
def render_in_base(value, base_values, length=-1):
|
|
|
|
"""
|
|
|
|
Renders @p value in base-N, where N is the number of
|
|
|
|
items in @p base_values. When rendering, use the items
|
|
|
|
of @p base_values (e.g. use "0123456789" to get regular decimal
|
|
|
|
rendering, or "ABCDEFGHIJ" for letters-as-numbers 'encoding').
|
|
|
|
|
|
|
|
If length is positive, pads out to at least that long with
|
|
|
|
leading "zeroes", whatever base_values[0] is.
|
|
|
|
"""
|
|
|
|
if value < 0:
|
|
|
|
raise ValueError("Cannot render negative values")
|
|
|
|
if len(base_values) < 2:
|
|
|
|
raise ValueError("Insufficient items for base-N rendering")
|
|
|
|
if length < 1:
|
|
|
|
length = 1
|
|
|
|
digits = []
|
|
|
|
base = len(base_values)
|
|
|
|
while value > 0:
|
|
|
|
place = value % base
|
|
|
|
value = value // base
|
|
|
|
digits.append(base_values[place])
|
|
|
|
while len(digits) < length:
|
|
|
|
digits.append(base_values[0])
|
|
|
|
return "".join(reversed(digits))
|
|
|
|
|
|
|
|
|
|
|
|
class randomEfi(object):
|
|
|
|
"""
|
|
|
|
EFI Id generator that appends a random 4-digit hex number to the given name.
|
|
|
|
"""
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2021-11-19 12:34:35 +01:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
# So the first call to next() will bump it to 0
|
|
|
|
self.counter = -1
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
self.counter += 1
|
|
|
|
if self.counter > 0:
|
|
|
|
import random
|
|
|
|
v = random.randint(0, 65535) # 16 bits
|
|
|
|
return "{!s}{!s}".format(self.name, render_in_base(v, "0123456789ABCDEF", 4))
|
|
|
|
else:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class phraseEfi(object):
|
|
|
|
"""
|
|
|
|
EFI Id generator that appends a random phrase to the given name.
|
|
|
|
"""
|
|
|
|
words = ("Sun", "Moon", "Mars", "Soyuz", "Falcon", "Kuaizhou", "Gaganyaan")
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
# So the first call to next() will bump it to 0
|
|
|
|
self.counter = -1
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
self.counter += 1
|
|
|
|
if self.counter > 0:
|
|
|
|
import random
|
|
|
|
desired_length = 1 + self.counter // 5
|
|
|
|
v = random.randint(0, len(self.words) ** desired_length)
|
2021-11-19 12:42:34 +01:00
|
|
|
return "{!s}{!s}".format(self.name, render_in_base(v, self.words))
|
2021-11-19 12:34:35 +01:00
|
|
|
else:
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
def get_efi_suffix_generator(name):
|
2022-01-18 15:09:44 +01:00
|
|
|
"""
|
2022-07-02 16:48:02 +02:00
|
|
|
Handle EFI bootloader Ids with ${<something>} for suffix-processing.
|
2022-01-18 15:09:44 +01:00
|
|
|
"""
|
2022-07-02 16:48:02 +02:00
|
|
|
if "${" not in name:
|
|
|
|
raise ValueError("Misplaced call to get_efi_suffix_generator, no ${}")
|
|
|
|
if not name.endswith("}"):
|
|
|
|
raise ValueError("Misplaced call to get_efi_suffix_generator, no trailing ${}")
|
|
|
|
if name.count("${") > 1:
|
|
|
|
raise ValueError("EFI ID {!r} contains multiple generators".format(name))
|
|
|
|
import re
|
|
|
|
prefix, generator_name = re.match("(.*)\${([^}]*)}$", name).groups()
|
|
|
|
if generator_name not in ("SERIAL", "RANDOM", "PHRASE"):
|
|
|
|
raise ValueError("EFI suffix {!r} is unknown".format(generator_name))
|
2021-11-19 12:34:35 +01:00
|
|
|
|
|
|
|
generator = None
|
2022-07-02 16:48:02 +02:00
|
|
|
if generator_name == "SERIAL":
|
|
|
|
generator = serialEfi(prefix)
|
|
|
|
elif generator_name == "RANDOM":
|
|
|
|
generator = randomEfi(prefix)
|
|
|
|
elif generator_name == "PHRASE":
|
|
|
|
generator = phraseEfi(prefix)
|
2021-11-19 12:34:35 +01:00
|
|
|
if generator is None:
|
2022-07-02 16:48:02 +02:00
|
|
|
raise ValueError("EFI suffix {!r} is unsupported".format(generator_name))
|
2021-11-19 12:34:35 +01:00
|
|
|
|
|
|
|
return generator
|
|
|
|
|
|
|
|
|
2022-01-18 15:09:44 +01:00
|
|
|
def change_efi_suffix(efi_directory, bootloader_id):
|
|
|
|
"""
|
|
|
|
Returns a label based on @p bootloader_id that is usable within
|
2022-07-02 16:48:02 +02:00
|
|
|
@p efi_directory. If there is a ${<something>} suffix marker
|
2022-01-18 15:09:44 +01:00
|
|
|
in the given id, tries to generate a unique label.
|
|
|
|
"""
|
2022-07-02 16:48:02 +02:00
|
|
|
if bootloader_id.endswith("}") and "${" in bootloader_id:
|
2022-01-18 15:09:44 +01:00
|
|
|
# Do 10 attempts with any suffix generator
|
2022-12-23 00:13:43 +01:00
|
|
|
g = SuffixIterator(10, get_efi_suffix_generator(bootloader_id))
|
2022-01-18 15:09:44 +01:00
|
|
|
else:
|
|
|
|
# Just one attempt
|
|
|
|
g = [bootloader_id]
|
|
|
|
|
|
|
|
for candidate_name in g:
|
|
|
|
if not os.path.exists(os.path.join(efi_directory, candidate_name)):
|
|
|
|
return candidate_name
|
|
|
|
return bootloader_id
|
|
|
|
|
|
|
|
|
|
|
|
def efi_label(efi_directory):
|
|
|
|
"""
|
|
|
|
Returns a sanitized label, possibly unique, that can be
|
|
|
|
used within @p efi_directory.
|
|
|
|
"""
|
2018-02-20 16:47:14 +01:00
|
|
|
if "efiBootloaderId" in libcalamares.job.configuration:
|
2022-12-23 00:13:43 +01:00
|
|
|
efi_bootloader_id = change_efi_suffix(efi_directory, libcalamares.job.configuration["efiBootloaderId"])
|
2018-02-20 16:47:14 +01:00
|
|
|
else:
|
|
|
|
branding = libcalamares.globalstorage.value("branding")
|
|
|
|
efi_bootloader_id = branding["bootloaderEntryName"]
|
|
|
|
|
|
|
|
return efi_bootloader_id.translate(file_name_sanitizer)
|
|
|
|
|
|
|
|
|
2018-05-28 15:24:43 +02:00
|
|
|
def efi_word_size():
|
|
|
|
# get bitness of the underlying UEFI
|
|
|
|
try:
|
|
|
|
sysfile = open("/sys/firmware/efi/fw_platform_size", "r")
|
|
|
|
efi_bitness = sysfile.read(2)
|
|
|
|
except Exception:
|
|
|
|
# if the kernel is older than 4.0, the UEFI bitness likely isn't
|
|
|
|
# exposed to the userspace so we assume a 64 bit UEFI here
|
|
|
|
efi_bitness = "64"
|
|
|
|
return efi_bitness
|
|
|
|
|
|
|
|
|
2020-05-06 13:31:02 +02:00
|
|
|
def efi_boot_next():
|
|
|
|
"""
|
|
|
|
Tell EFI to definitely boot into the just-installed
|
|
|
|
system next time.
|
|
|
|
"""
|
|
|
|
boot_mgr = libcalamares.job.configuration["efiBootMgr"]
|
|
|
|
boot_entry = None
|
2022-08-07 14:43:06 +02:00
|
|
|
efi_bootvars = subprocess.check_output([boot_mgr], universal_newlines=True)
|
2020-05-06 13:31:02 +02:00
|
|
|
for line in efi_bootvars.split('\n'):
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
words = line.split()
|
|
|
|
if len(words) >= 2 and words[0] == "BootOrder:":
|
|
|
|
boot_entry = words[1].split(',')[0]
|
|
|
|
break
|
|
|
|
if boot_entry:
|
|
|
|
subprocess.call([boot_mgr, "-n", boot_entry])
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2021-09-20 02:23:56 +02:00
|
|
|
def get_kernels(installation_root_path):
|
|
|
|
"""
|
|
|
|
Gets a list of kernels and associated values for each kernel. This will work as is for many distros.
|
|
|
|
If not, it should be safe to modify it to better support your distro
|
|
|
|
|
|
|
|
:param installation_root_path: A string with the absolute path to the root of the installation
|
|
|
|
|
|
|
|
Returns a list of 3-tuples
|
|
|
|
|
|
|
|
Each 3-tuple contains the kernel, kernel_type and kernel_version
|
|
|
|
"""
|
2022-12-23 00:13:43 +01:00
|
|
|
try:
|
|
|
|
kernel_search_path = libcalamares.job.configuration["kernelSearchPath"]
|
|
|
|
except KeyError:
|
|
|
|
libcalamares.utils.warning("No kernel pattern found in configuration, using '/usr/lib/modules'")
|
|
|
|
kernel_search_path = "/usr/lib/modules"
|
|
|
|
pass
|
|
|
|
|
2021-09-20 02:23:56 +02:00
|
|
|
kernel_list = []
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
try:
|
|
|
|
kernel_pattern = libcalamares.job.configuration["kernelPattern"]
|
|
|
|
except KeyError:
|
|
|
|
libcalamares.utils.warning("No kernel pattern found in configuration, using 'vmlinuz'")
|
|
|
|
kernel_pattern = "vmlinuz"
|
|
|
|
pass
|
|
|
|
|
|
|
|
# find all the installed kernels
|
2021-11-30 11:39:30 +01:00
|
|
|
for root, dirs, files in os.walk(os.path.join(installation_root_path, kernel_search_path.lstrip('/'))):
|
|
|
|
for file in files:
|
2022-12-23 00:13:43 +01:00
|
|
|
if re.search(kernel_pattern, file):
|
2021-09-20 02:23:56 +02:00
|
|
|
rel_root = os.path.relpath(root, installation_root_path)
|
2022-12-23 00:13:43 +01:00
|
|
|
kernel_list.append((os.path.join(rel_root, file), "default", os.path.basename(root)))
|
2021-09-20 02:23:56 +02:00
|
|
|
|
|
|
|
return kernel_list
|
2020-05-06 13:31:02 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
|
2015-06-11 00:32:34 +02:00
|
|
|
def install_systemd_boot(efi_directory):
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Installs systemd-boot as bootloader for EFI setups.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param efi_directory:
|
|
|
|
"""
|
2018-01-30 11:26:29 +01:00
|
|
|
libcalamares.utils.debug("Bootloader: systemd-boot")
|
2021-09-20 02:23:56 +02:00
|
|
|
installation_root_path = libcalamares.globalstorage.value("rootMountPoint")
|
|
|
|
install_efi_directory = installation_root_path + efi_directory
|
2015-02-20 00:28:44 +01:00
|
|
|
uuid = get_uuid()
|
2016-05-04 13:30:54 +02:00
|
|
|
loader_path = os.path.join(install_efi_directory,
|
|
|
|
"loader",
|
|
|
|
"loader.conf")
|
|
|
|
subprocess.call(["bootctl",
|
|
|
|
"--path={!s}".format(install_efi_directory),
|
|
|
|
"install"])
|
2021-02-22 00:27:46 +01:00
|
|
|
|
2021-09-20 02:23:56 +02:00
|
|
|
for (kernel, kernel_type, kernel_version) in get_kernels(installation_root_path):
|
|
|
|
create_systemd_boot_conf(installation_root_path,
|
2022-12-23 00:13:43 +01:00
|
|
|
efi_directory,
|
|
|
|
uuid,
|
|
|
|
kernel,
|
|
|
|
kernel_version)
|
|
|
|
|
|
|
|
create_loader(loader_path, installation_root_path)
|
2021-02-22 00:27:46 +01:00
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
|
2021-06-28 17:05:58 +02:00
|
|
|
def get_grub_efi_parameters():
|
|
|
|
"""
|
|
|
|
Returns a 3-tuple of suitable parameters for GRUB EFI installation,
|
|
|
|
depending on the host machine architecture. The return is
|
|
|
|
- target name
|
|
|
|
- grub.efi name
|
|
|
|
- boot.efi name
|
|
|
|
all three are strings. May return None if there is no suitable
|
|
|
|
set for the current machine. May return unsuitable values if the
|
|
|
|
host architecture is unknown (e.g. defaults to x86_64).
|
|
|
|
"""
|
|
|
|
import platform
|
|
|
|
efi_bitness = efi_word_size()
|
|
|
|
cpu_type = platform.machine()
|
|
|
|
|
|
|
|
if efi_bitness == "32":
|
|
|
|
# Assume all 32-bitters are legacy x86
|
2022-12-23 00:13:43 +01:00
|
|
|
return "i386-efi", "grubia32.efi", "bootia32.efi"
|
2021-06-28 17:05:58 +02:00
|
|
|
elif efi_bitness == "64" and cpu_type == "aarch64":
|
2022-12-23 00:13:43 +01:00
|
|
|
return "arm64-efi", "grubaa64.efi", "bootaa64.efi"
|
2022-03-03 07:11:32 +01:00
|
|
|
elif efi_bitness == "64" and cpu_type == "loongarch64":
|
2022-12-23 00:13:43 +01:00
|
|
|
return "loongarch64-efi", "grubloongarch64.efi", "bootloongarch64.efi"
|
2021-06-28 17:05:58 +02:00
|
|
|
elif efi_bitness == "64":
|
|
|
|
# If it's not ARM, must by AMD64
|
2022-12-23 00:13:43 +01:00
|
|
|
return "x86_64-efi", "grubx64.efi", "bootx64.efi"
|
|
|
|
libcalamares.utils.warning(
|
|
|
|
"Could not find GRUB parameters for bits {b} and cpu {c}".format(b=repr(efi_bitness), c=repr(cpu_type)))
|
2021-06-28 17:05:58 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-11-16 20:48:34 +01:00
|
|
|
def run_grub_mkconfig(partitions, output_file):
|
2021-11-07 16:32:52 +01:00
|
|
|
"""
|
|
|
|
Runs grub-mkconfig in the target environment
|
|
|
|
|
2021-11-16 20:48:34 +01:00
|
|
|
:param partitions: The partitions list from global storage
|
2021-11-07 16:32:52 +01:00
|
|
|
:param output_file: A string containing the path to the generating grub config file
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
# zfs needs an environment variable set for grub-mkconfig
|
2021-11-16 00:59:33 +01:00
|
|
|
if any([is_zfs_root(partition) for partition in partitions]):
|
2021-11-07 16:32:52 +01:00
|
|
|
check_target_env_call(["sh", "-c", "ZPOOL_VDEV_NAME_PATH=1 " +
|
|
|
|
libcalamares.job.configuration["grubMkconfig"] + " -o " + output_file])
|
|
|
|
else:
|
|
|
|
# The input file /etc/default/grub should already be filled out by the
|
|
|
|
# grubcfg job module.
|
|
|
|
check_target_env_call([libcalamares.job.configuration["grubMkconfig"], "-o", output_file])
|
|
|
|
|
|
|
|
|
2022-01-18 15:09:44 +01:00
|
|
|
def run_grub_install(fw_type, partitions, efi_directory):
|
2021-11-16 20:48:34 +01:00
|
|
|
"""
|
|
|
|
Runs grub-install in the target environment
|
|
|
|
|
|
|
|
:param fw_type: A string which is "efi" for UEFI installs. Any other value results in a BIOS install
|
|
|
|
:param partitions: The partitions list from global storage
|
|
|
|
:param efi_directory: The path of the efi directory relative to the root of the install
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
|
|
|
is_zfs = any([is_zfs_root(partition) for partition in partitions])
|
|
|
|
|
|
|
|
# zfs needs an environment variable set for grub
|
|
|
|
if is_zfs:
|
|
|
|
check_target_env_call(["sh", "-c", "echo ZPOOL_VDEV_NAME_PATH=1 >> /etc/environment"])
|
|
|
|
|
|
|
|
if fw_type == "efi":
|
2022-02-01 16:49:18 +01:00
|
|
|
assert efi_directory is not None
|
2022-01-18 15:09:44 +01:00
|
|
|
efi_bootloader_id = efi_label(efi_directory)
|
2021-11-16 20:48:34 +01:00
|
|
|
efi_target, efi_grub_file, efi_boot_file = get_grub_efi_parameters()
|
|
|
|
|
|
|
|
if is_zfs:
|
|
|
|
check_target_env_call(["sh", "-c", "ZPOOL_VDEV_NAME_PATH=1 " + libcalamares.job.configuration["grubInstall"]
|
|
|
|
+ " --target=" + efi_target + " --efi-directory=" + efi_directory
|
|
|
|
+ " --bootloader-id=" + efi_bootloader_id + " --force"])
|
|
|
|
else:
|
|
|
|
check_target_env_call([libcalamares.job.configuration["grubInstall"],
|
|
|
|
"--target=" + efi_target,
|
|
|
|
"--efi-directory=" + efi_directory,
|
|
|
|
"--bootloader-id=" + efi_bootloader_id,
|
|
|
|
"--force"])
|
|
|
|
else:
|
2022-02-01 16:49:18 +01:00
|
|
|
assert efi_directory is None
|
2021-11-16 20:48:34 +01:00
|
|
|
if libcalamares.globalstorage.value("bootLoader") is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
boot_loader = libcalamares.globalstorage.value("bootLoader")
|
|
|
|
if boot_loader["installPath"] is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
if is_zfs:
|
|
|
|
check_target_env_call(["sh", "-c", "ZPOOL_VDEV_NAME_PATH=1 "
|
|
|
|
+ libcalamares.job.configuration["grubInstall"]
|
|
|
|
+ " --target=i386-pc --recheck --force "
|
|
|
|
+ boot_loader["installPath"]])
|
|
|
|
else:
|
|
|
|
check_target_env_call([libcalamares.job.configuration["grubInstall"],
|
|
|
|
"--target=i386-pc",
|
|
|
|
"--recheck",
|
|
|
|
"--force",
|
|
|
|
boot_loader["installPath"]])
|
|
|
|
|
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
def install_grub(efi_directory, fw_type):
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Installs grub as bootloader, either in pc or efi mode.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param efi_directory:
|
|
|
|
:param fw_type:
|
|
|
|
"""
|
2021-11-16 20:48:34 +01:00
|
|
|
# get the partition from global storage
|
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
|
|
|
if not partitions:
|
|
|
|
libcalamares.utils.warning(_("Failed to install grub, no partitions defined in global storage"))
|
|
|
|
return
|
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
if fw_type == "efi":
|
2018-01-30 11:26:29 +01:00
|
|
|
libcalamares.utils.debug("Bootloader: grub (efi)")
|
2021-09-20 02:23:56 +02:00
|
|
|
installation_root_path = libcalamares.globalstorage.value("rootMountPoint")
|
|
|
|
install_efi_directory = installation_root_path + efi_directory
|
2016-08-23 10:20:52 +02:00
|
|
|
|
2017-03-16 16:59:28 +01:00
|
|
|
if not os.path.isdir(install_efi_directory):
|
2017-03-24 16:39:25 +01:00
|
|
|
os.makedirs(install_efi_directory)
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2022-01-18 15:09:44 +01:00
|
|
|
efi_bootloader_id = efi_label(efi_directory)
|
2021-06-28 17:05:58 +02:00
|
|
|
|
|
|
|
efi_target, efi_grub_file, efi_boot_file = get_grub_efi_parameters()
|
2017-10-24 21:32:15 +02:00
|
|
|
|
2021-11-16 20:48:34 +01:00
|
|
|
run_grub_install(fw_type, partitions, efi_directory)
|
2016-08-23 10:20:52 +02:00
|
|
|
|
|
|
|
# VFAT is weird, see issue CAL-385
|
2017-03-24 16:39:25 +01:00
|
|
|
install_efi_directory_firmware = (vfat_correct_case(
|
2022-12-23 00:13:43 +01:00
|
|
|
install_efi_directory,
|
|
|
|
"EFI"))
|
2017-03-16 16:59:28 +01:00
|
|
|
if not os.path.exists(install_efi_directory_firmware):
|
2017-03-24 16:39:25 +01:00
|
|
|
os.makedirs(install_efi_directory_firmware)
|
2016-09-16 16:30:17 +02:00
|
|
|
|
2017-03-16 16:59:28 +01:00
|
|
|
# there might be several values for the boot directory
|
|
|
|
# most usual they are boot, Boot, BOOT
|
|
|
|
|
2017-03-24 16:39:25 +01:00
|
|
|
install_efi_boot_directory = (vfat_correct_case(
|
2022-12-23 00:13:43 +01:00
|
|
|
install_efi_directory_firmware,
|
|
|
|
"boot"))
|
2017-03-16 16:59:28 +01:00
|
|
|
if not os.path.exists(install_efi_boot_directory):
|
2017-03-24 16:39:25 +01:00
|
|
|
os.makedirs(install_efi_boot_directory)
|
2016-08-23 10:20:52 +02:00
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
# Workaround for some UEFI firmwares
|
2021-11-16 20:48:34 +01:00
|
|
|
fallback = "installEFIFallback"
|
|
|
|
libcalamares.utils.debug("UEFI Fallback: " + str(libcalamares.job.configuration.get(fallback, "<unset>")))
|
|
|
|
if libcalamares.job.configuration.get(fallback, True):
|
2018-01-30 11:22:36 +01:00
|
|
|
libcalamares.utils.debug(" .. installing '{!s}' fallback firmware".format(efi_boot_file))
|
2018-01-29 22:55:07 +01:00
|
|
|
efi_file_source = os.path.join(install_efi_directory_firmware,
|
2021-11-16 20:48:34 +01:00
|
|
|
efi_bootloader_id,
|
|
|
|
efi_grub_file)
|
|
|
|
efi_file_target = os.path.join(install_efi_boot_directory, efi_boot_file)
|
2018-01-29 22:55:07 +01:00
|
|
|
|
|
|
|
shutil.copy2(efi_file_source, efi_file_target)
|
2015-02-20 00:28:44 +01:00
|
|
|
else:
|
2018-01-30 11:26:29 +01:00
|
|
|
libcalamares.utils.debug("Bootloader: grub (bios)")
|
2022-02-01 16:49:18 +01:00
|
|
|
run_grub_install(fw_type, partitions, None)
|
2016-05-04 13:30:54 +02:00
|
|
|
|
2021-11-16 20:48:34 +01:00
|
|
|
run_grub_mkconfig(partitions, libcalamares.job.configuration["grubCfg"])
|
2015-02-20 00:28:44 +01:00
|
|
|
|
|
|
|
|
2018-02-20 10:22:35 +01:00
|
|
|
def install_secureboot(efi_directory):
|
|
|
|
"""
|
|
|
|
Installs the secureboot shim in the system by calling efibootmgr.
|
|
|
|
"""
|
2022-01-18 15:09:44 +01:00
|
|
|
efi_bootloader_id = efi_label(efi_directory)
|
2018-05-28 15:26:20 +02:00
|
|
|
|
2021-09-20 02:23:56 +02:00
|
|
|
installation_root_path = libcalamares.globalstorage.value("rootMountPoint")
|
|
|
|
install_efi_directory = installation_root_path + efi_directory
|
2018-05-28 15:26:20 +02:00
|
|
|
|
|
|
|
if efi_word_size() == "64":
|
2021-01-30 11:37:41 +01:00
|
|
|
install_efi_bin = "shimx64.efi"
|
|
|
|
elif efi_word_size() == "32":
|
|
|
|
install_efi_bin = "shimia32.efi"
|
2022-12-23 00:13:43 +01:00
|
|
|
else:
|
|
|
|
libcalamares.utils.warning(f"Unknown efi word size of {efi_word_size()} found")
|
|
|
|
return None
|
2018-05-28 15:26:20 +02:00
|
|
|
|
2018-05-28 17:47:47 +02:00
|
|
|
# Copied, roughly, from openSUSE's install script,
|
|
|
|
# and pythonified. *disk* is something like /dev/sda,
|
|
|
|
# while *drive* may return "(disk/dev/sda,gpt1)" ..
|
|
|
|
# we're interested in the numbers in the second part
|
|
|
|
# of that tuple.
|
|
|
|
efi_drive = subprocess.check_output([
|
|
|
|
libcalamares.job.configuration["grubProbe"],
|
2019-05-10 21:35:00 +02:00
|
|
|
"-t", "drive", "--device-map=", install_efi_directory]).decode("ascii")
|
2018-05-28 17:47:47 +02:00
|
|
|
efi_disk = subprocess.check_output([
|
|
|
|
libcalamares.job.configuration["grubProbe"],
|
2019-05-10 21:35:00 +02:00
|
|
|
"-t", "disk", "--device-map=", install_efi_directory]).decode("ascii")
|
2018-05-28 17:47:47 +02:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
efi_drive_partition = efi_drive.replace("(", "").replace(")", "").split(",")[1]
|
2018-05-28 17:47:47 +02:00
|
|
|
# Get the first run of digits from the partition
|
2019-05-11 00:26:59 +02:00
|
|
|
efi_partition_number = None
|
2018-05-28 17:47:47 +02:00
|
|
|
c = 0
|
|
|
|
start = None
|
|
|
|
while c < len(efi_drive_partition):
|
|
|
|
if efi_drive_partition[c].isdigit() and start is None:
|
|
|
|
start = c
|
|
|
|
if not efi_drive_partition[c].isdigit() and start is not None:
|
2019-05-11 00:26:59 +02:00
|
|
|
efi_partition_number = efi_drive_partition[start:c]
|
2018-05-28 17:47:47 +02:00
|
|
|
break
|
|
|
|
c += 1
|
2019-05-11 00:26:59 +02:00
|
|
|
if efi_partition_number is None:
|
2018-05-28 17:47:47 +02:00
|
|
|
raise ValueError("No partition number found for %s" % install_efi_directory)
|
|
|
|
|
2018-02-20 16:47:14 +01:00
|
|
|
subprocess.call([
|
2018-06-17 07:47:58 +02:00
|
|
|
libcalamares.job.configuration["efiBootMgr"],
|
2018-02-20 16:47:14 +01:00
|
|
|
"-c",
|
|
|
|
"-w",
|
|
|
|
"-L", efi_bootloader_id,
|
2018-05-28 17:47:47 +02:00
|
|
|
"-d", efi_disk,
|
2019-05-11 00:26:59 +02:00
|
|
|
"-p", efi_partition_number,
|
2018-05-28 15:26:20 +02:00
|
|
|
"-l", install_efi_directory + "/" + install_efi_bin])
|
2018-02-20 16:47:14 +01:00
|
|
|
|
2020-05-06 13:31:02 +02:00
|
|
|
efi_boot_next()
|
2020-05-01 21:25:19 +02:00
|
|
|
|
2019-05-12 16:11:35 +02:00
|
|
|
# The input file /etc/default/grub should already be filled out by the
|
|
|
|
# grubcfg job module.
|
|
|
|
check_target_env_call([libcalamares.job.configuration["grubMkconfig"],
|
|
|
|
"-o", os.path.join(efi_directory, "EFI",
|
|
|
|
efi_bootloader_id, "grub.cfg")])
|
|
|
|
|
2018-02-20 10:22:35 +01:00
|
|
|
|
2016-09-16 16:30:17 +02:00
|
|
|
def vfat_correct_case(parent, name):
|
|
|
|
for candidate in os.listdir(parent):
|
|
|
|
if name.lower() == candidate.lower():
|
2016-11-05 17:41:38 +01:00
|
|
|
return os.path.join(parent, candidate)
|
2016-09-16 16:30:17 +02:00
|
|
|
return os.path.join(parent, name)
|
2016-08-23 10:20:52 +02:00
|
|
|
|
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
def efi_partitions(efi_boot_path):
|
|
|
|
"""
|
|
|
|
The (one) partition mounted on @p efi_boot_path, or an empty list.
|
|
|
|
"""
|
|
|
|
return [p for p in libcalamares.globalstorage.value("partitions") if p["mountPoint"] == efi_boot_path]
|
|
|
|
|
|
|
|
|
|
|
|
def update_refind_config(efi_directory, installation_root_path):
|
|
|
|
"""
|
|
|
|
:param efi_directory: The path to the efi directory relative to the root
|
|
|
|
:param installation_root_path: The path to the root of the installation
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
kernel_list = libcalamares.job.configuration["refindKernelList"]
|
|
|
|
except KeyError:
|
|
|
|
libcalamares.utils.warning('refindKernelList not set. Skipping updating refind.conf')
|
|
|
|
return
|
|
|
|
|
|
|
|
# Update the config in the file
|
|
|
|
for line in fileinput.input(installation_root_path + efi_directory + "/EFI/refind/refind.conf", inplace=True):
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("#extra_kernel_version_strings") or line.startswith("extra_kernel_version_strings"):
|
|
|
|
line = line.lstrip("#")
|
|
|
|
for kernel in kernel_list:
|
|
|
|
if kernel not in line:
|
|
|
|
line += "," + kernel
|
|
|
|
print(line)
|
|
|
|
|
|
|
|
|
|
|
|
def install_refind(efi_directory):
|
|
|
|
try:
|
|
|
|
installation_root_path = libcalamares.globalstorage.value("rootMountPoint")
|
|
|
|
except KeyError:
|
|
|
|
libcalamares.utils.warning('Global storage value "rootMountPoint" missing')
|
|
|
|
|
|
|
|
install_efi_directory = installation_root_path + efi_directory
|
|
|
|
uuid = get_uuid()
|
|
|
|
kernel_params = " ".join(get_kernel_params(uuid))
|
|
|
|
conf_path = os.path.join(installation_root_path, "boot/refind_linux.conf")
|
|
|
|
|
|
|
|
check_target_env_call(["refind-install"])
|
|
|
|
|
|
|
|
with open(conf_path, "r") as refind_file:
|
|
|
|
filedata = [x.strip() for x in refind_file.readlines()]
|
|
|
|
|
|
|
|
with open(conf_path, 'w') as refind_file:
|
|
|
|
for line in filedata:
|
|
|
|
if line.startswith('"Boot with standard options"'):
|
|
|
|
line = f'"Boot with standard options" "{kernel_params}"'
|
|
|
|
elif line.startswith('"Boot to single-user mode"'):
|
|
|
|
line = f'"Boot to single-user mode" "{kernel_params}" single'
|
|
|
|
refind_file.write(line + "\n")
|
|
|
|
|
|
|
|
update_refind_config(efi_directory, installation_root_path)
|
|
|
|
|
|
|
|
|
2015-02-20 00:28:44 +01:00
|
|
|
def prepare_bootloader(fw_type):
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Prepares bootloader.
|
2017-03-16 16:59:28 +01:00
|
|
|
Based on value 'efi_boot_loader', it either calls systemd-boot
|
|
|
|
or grub to be installed.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:param fw_type:
|
|
|
|
:return:
|
|
|
|
"""
|
2022-12-23 00:13:43 +01:00
|
|
|
|
|
|
|
# Get the boot loader selection from global storage if it is set in the config file
|
|
|
|
try:
|
|
|
|
gs_name = libcalamares.job.configuration["efiBootLoaderVar"]
|
|
|
|
if libcalamares.globalstorage.contains(gs_name):
|
|
|
|
efi_boot_loader = libcalamares.globalstorage.value(gs_name)
|
|
|
|
else:
|
|
|
|
libcalamares.utils.warning(
|
|
|
|
f"Specified global storage value not found in global storage")
|
|
|
|
return None
|
|
|
|
except KeyError:
|
|
|
|
# If the conf value for using global storage is not set, use the setting from the config file.
|
|
|
|
try:
|
|
|
|
efi_boot_loader = libcalamares.job.configuration["efiBootLoader"]
|
|
|
|
except KeyError:
|
|
|
|
if fw_type == "efi":
|
|
|
|
libcalamares.utils.warning("Configuration missing both efiBootLoader and efiBootLoaderVar on an EFI "
|
|
|
|
"system, bootloader not installed")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# If the user has selected not to install bootloader, bail out here
|
|
|
|
if efi_boot_loader.casefold() == "none":
|
|
|
|
libcalamares.utils.debug("Skipping bootloader installation since no bootloader was selected")
|
|
|
|
return None
|
|
|
|
|
2015-02-20 00:33:16 +01:00
|
|
|
efi_directory = libcalamares.globalstorage.value("efiSystemPartition")
|
2015-06-14 05:08:52 +02:00
|
|
|
|
|
|
|
if efi_boot_loader == "systemd-boot" and fw_type == "efi":
|
2015-06-11 00:32:34 +02:00
|
|
|
install_systemd_boot(efi_directory)
|
2018-02-20 10:22:35 +01:00
|
|
|
elif efi_boot_loader == "sb-shim" and fw_type == "efi":
|
|
|
|
install_secureboot(efi_directory)
|
2022-12-23 00:13:43 +01:00
|
|
|
elif efi_boot_loader == "refind" and fw_type == "efi":
|
|
|
|
install_refind(efi_directory)
|
2018-02-20 10:22:35 +01:00
|
|
|
elif efi_boot_loader == "grub" or fw_type != "efi":
|
2015-02-20 01:00:31 +01:00
|
|
|
install_grub(efi_directory, fw_type)
|
2018-02-20 10:22:35 +01:00
|
|
|
else:
|
2022-12-23 00:13:43 +01:00
|
|
|
libcalamares.utils.debug("WARNING: the combination of "
|
|
|
|
"boot-loader '{!s}' and firmware '{!s}' "
|
|
|
|
"is not supported.".format(efi_boot_loader, fw_type))
|
2014-10-12 19:45:02 +02:00
|
|
|
|
2014-10-16 21:08:18 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
def run():
|
2017-03-24 16:39:25 +01:00
|
|
|
"""
|
|
|
|
Starts procedure and passes 'fw_type' to other routine.
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2015-07-07 19:15:48 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
fw_type = libcalamares.globalstorage.value("firmwareType")
|
2017-01-17 18:13:51 +01:00
|
|
|
|
2022-12-23 00:13:43 +01:00
|
|
|
if libcalamares.globalstorage.value("bootLoader") is None and fw_type != "efi":
|
|
|
|
libcalamares.utils.warning("Non-EFI system, and no bootloader is set.")
|
2017-01-17 18:13:51 +01:00
|
|
|
return None
|
|
|
|
|
2017-02-17 16:20:43 +01:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
|
|
|
if fw_type == "efi":
|
2019-04-19 16:43:07 +02:00
|
|
|
efi_system_partition = libcalamares.globalstorage.value("efiSystemPartition")
|
2022-12-23 00:13:43 +01:00
|
|
|
esp_found = [p for p in partitions if p["mountPoint"] == efi_system_partition]
|
2017-02-17 16:20:43 +01:00
|
|
|
if not esp_found:
|
2022-12-23 00:13:43 +01:00
|
|
|
libcalamares.utils.warning("EFI system, but nothing mounted on {!s}".format(efi_system_partition))
|
2017-02-17 16:20:43 +01:00
|
|
|
return None
|
|
|
|
|
2021-05-24 23:05:46 +02:00
|
|
|
try:
|
|
|
|
prepare_bootloader(fw_type)
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
|
|
libcalamares.utils.warning(str(e))
|
|
|
|
libcalamares.utils.debug("stdout:" + str(e.stdout))
|
|
|
|
libcalamares.utils.debug("stderr:" + str(e.stderr))
|
|
|
|
return (_("Bootloader installation error"),
|
2022-12-23 00:13:43 +01:00
|
|
|
_("The bootloader could not be installed. The installation command <pre>{!s}</pre> returned error "
|
|
|
|
"code {!s}.")
|
2021-05-24 23:05:46 +02:00
|
|
|
.format(e.cmd, e.returncode))
|
2015-06-14 05:08:52 +02:00
|
|
|
|
2014-10-12 19:45:02 +02:00
|
|
|
return None
|