2014-08-19 15:54: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-08-19 15:54:02 +02:00
|
|
|
#
|
2020-08-24 16:36:47 +02:00
|
|
|
# SPDX-FileCopyrightText: 2014 Rohan Garg <rohan@kde.org>
|
|
|
|
# SPDX-FileCopyrightText: 2015 2019-2020, Philip Müller <philm@manjaro.org>
|
|
|
|
# SPDX-FileCopyrightText: 2017 Alf Gaida <agaida@sidution.org>
|
|
|
|
# SPDX-FileCopyrightText: 2019 Adriaan de Groot <groot@kde.org>
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2014-08-19 15:54:02 +02:00
|
|
|
#
|
2020-08-25 16:05:56 +02:00
|
|
|
# Calamares is Free Software: see the License-Identifier above.
|
2014-08-19 15:54:02 +02:00
|
|
|
#
|
|
|
|
|
2019-12-16 09:34:21 +01:00
|
|
|
from libcalamares.utils import debug, target_env_call
|
2014-08-19 15:54:02 +02:00
|
|
|
import os
|
2014-10-16 19:34:43 +02:00
|
|
|
from collections import OrderedDict
|
2014-08-19 15:54:02 +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 _("Configuring mkinitcpio.")
|
|
|
|
|
2015-02-18 16:03:57 +01:00
|
|
|
|
2014-10-16 19:34:43 +02:00
|
|
|
def cpuinfo():
|
2017-03-29 21:59:22 +02:00
|
|
|
"""
|
|
|
|
Return the information in /proc/cpuinfo as a dictionary in the following
|
|
|
|
format:
|
2015-02-21 10:30:13 +01:00
|
|
|
|
2014-10-16 19:34:43 +02:00
|
|
|
cpu_info['proc0']={...}
|
|
|
|
cpu_info['proc1']={...}
|
2015-02-18 16:03:57 +01:00
|
|
|
"""
|
2015-06-14 14:33:05 +02:00
|
|
|
cpu_info = OrderedDict()
|
2015-02-18 16:03:57 +01:00
|
|
|
procinfo = OrderedDict()
|
2014-10-16 19:34:43 +02:00
|
|
|
|
|
|
|
nprocs = 0
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2020-12-06 18:40:18 +01:00
|
|
|
with open("/proc/cpuinfo") as cpuinfo_file:
|
2016-05-06 17:50:07 +02:00
|
|
|
for line in cpuinfo_file:
|
2014-10-16 19:34:43 +02:00
|
|
|
if not line.strip():
|
|
|
|
# end of one processor
|
2015-06-14 14:33:05 +02:00
|
|
|
cpu_info["proc{!s}".format(nprocs)] = procinfo
|
2015-02-18 15:13:39 +01:00
|
|
|
nprocs += 1
|
2014-10-16 19:34:43 +02:00
|
|
|
# Reset
|
2015-02-18 16:03:57 +01:00
|
|
|
procinfo = OrderedDict()
|
2014-10-16 19:34:43 +02:00
|
|
|
else:
|
2020-12-06 18:40:18 +01:00
|
|
|
if len(line.split(":")) == 2:
|
|
|
|
splitted_line = line.split(":")[1].strip()
|
|
|
|
procinfo[line.split(":")[0].strip()] = splitted_line
|
2014-10-16 19:34:43 +02:00
|
|
|
else:
|
2020-12-06 18:40:18 +01:00
|
|
|
procinfo[line.split(":")[0].strip()] = ""
|
2014-10-16 19:34:43 +02:00
|
|
|
|
2015-06-14 14:33:05 +02:00
|
|
|
return cpu_info
|
2014-08-19 15:54:02 +02:00
|
|
|
|
2015-02-18 16:03:57 +01:00
|
|
|
|
2016-05-06 17:50:07 +02:00
|
|
|
def write_mkinitcpio_lines(hooks, modules, files, root_mount_point):
|
2017-03-29 21:59:22 +02:00
|
|
|
"""
|
|
|
|
Set up mkinitcpio.conf.
|
2014-08-19 15:54:02 +02:00
|
|
|
|
2015-02-20 20:54:25 +01:00
|
|
|
:param hooks:
|
|
|
|
:param modules:
|
2016-05-06 17:50:07 +02:00
|
|
|
:param files:
|
2015-02-20 20:54:25 +01:00
|
|
|
:param root_mount_point:
|
|
|
|
"""
|
2017-08-31 10:51:34 +02:00
|
|
|
hostfile = "/etc/mkinitcpio.conf"
|
2017-08-23 16:45:04 +02:00
|
|
|
try:
|
2017-08-31 10:51:34 +02:00
|
|
|
with open(hostfile, "r") as mkinitcpio_file:
|
2017-08-23 16:45:04 +02:00
|
|
|
mklins = [x.strip() for x in mkinitcpio_file.readlines()]
|
|
|
|
except FileNotFoundError:
|
2017-08-31 10:51:34 +02:00
|
|
|
libcalamares.utils.debug("Could not open host file '%s'" % hostfile)
|
2017-08-23 16:45:04 +02:00
|
|
|
mklins = []
|
2014-08-19 15:54:02 +02:00
|
|
|
|
|
|
|
for i in range(len(mklins)):
|
|
|
|
if mklins[i].startswith("HOOKS"):
|
2020-12-06 18:40:18 +01:00
|
|
|
joined_hooks = " ".join(hooks)
|
|
|
|
mklins[i] = "HOOKS='{!s}'".format(joined_hooks)
|
2014-08-19 15:54:02 +02:00
|
|
|
elif mklins[i].startswith("MODULES"):
|
2015-02-17 14:38:30 +01:00
|
|
|
joined_modules = ' '.join(modules)
|
2020-12-06 18:40:18 +01:00
|
|
|
mklins[i] = "MODULES='{!s}'".format(joined_modules)
|
2016-05-06 17:50:07 +02:00
|
|
|
elif mklins[i].startswith("FILES"):
|
|
|
|
joined_files = ' '.join(files)
|
2020-12-06 18:40:18 +01:00
|
|
|
mklins[i] = "FILES='{!s}'".format(joined_files)
|
2014-08-19 15:54:02 +02:00
|
|
|
|
|
|
|
path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
with open(path, "w") as mkinitcpio_file:
|
|
|
|
mkinitcpio_file.write("\n".join(mklins) + "\n")
|
|
|
|
|
2021-04-16 10:20:47 +02:00
|
|
|
|
2019-12-16 09:34:21 +01:00
|
|
|
def detect_plymouth():
|
2020-01-06 14:34:47 +01:00
|
|
|
"""
|
|
|
|
Checks existence (runnability) of plymouth in the target system.
|
|
|
|
|
|
|
|
@return True if plymouth exists in the target, False otherwise
|
|
|
|
"""
|
|
|
|
# Used to only check existence of path /usr/bin/plymouth in target
|
2021-04-16 10:20:47 +02:00
|
|
|
return target_env_call(["sh", "-c", "which plymouth"]) == 0
|
2019-12-16 09:34:21 +01:00
|
|
|
|
2015-02-18 16:03:57 +01:00
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
def modify_mkinitcpio_conf(partitions, root_mount_point):
|
2017-03-29 21:59:22 +02:00
|
|
|
"""
|
|
|
|
Modifies mkinitcpio.conf
|
2014-08-19 15:54:02 +02:00
|
|
|
|
2015-02-20 20:54:25 +01:00
|
|
|
:param partitions:
|
|
|
|
:param root_mount_point:
|
|
|
|
"""
|
2014-10-17 11:52:29 +02:00
|
|
|
cpu = cpuinfo()
|
2014-08-19 15:54:02 +02:00
|
|
|
swap_uuid = ""
|
2020-12-06 18:40:18 +01:00
|
|
|
btrfs = False
|
|
|
|
lvm2 = False
|
|
|
|
hooks = [
|
|
|
|
"base",
|
|
|
|
"udev",
|
|
|
|
"autodetect",
|
|
|
|
"modconf",
|
|
|
|
"block",
|
|
|
|
"keyboard",
|
|
|
|
"keymap"
|
|
|
|
]
|
2014-08-19 15:54:02 +02:00
|
|
|
modules = []
|
2016-05-06 17:50:07 +02:00
|
|
|
files = []
|
2016-05-03 13:43:15 +02:00
|
|
|
encrypt_hook = False
|
2016-05-13 16:09:35 +02:00
|
|
|
openswap_hook = False
|
2016-11-17 18:14:37 +01:00
|
|
|
unencrypted_separate_boot = False
|
2020-12-08 16:54:05 +01:00
|
|
|
is_cpu_intel = cpu["proc0"]["vendor_id"].lower() == "genuineintel"
|
2014-08-19 15:54:02 +02:00
|
|
|
|
2020-11-03 12:57:19 +01:00
|
|
|
# It is important that the plymouth hook comes before any encrypt hook
|
|
|
|
if detect_plymouth():
|
|
|
|
hooks.append("plymouth")
|
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
for partition in partitions:
|
2020-02-24 18:22:32 +01:00
|
|
|
if partition["fs"] == "linuxswap" and not partition.get("claimed", None):
|
|
|
|
# Skip foreign swap
|
|
|
|
continue
|
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
if partition["fs"] == "linuxswap":
|
|
|
|
swap_uuid = partition["uuid"]
|
2016-05-13 16:09:35 +02:00
|
|
|
if "luksMapperName" in partition:
|
|
|
|
openswap_hook = True
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
if partition["fs"] == "btrfs":
|
2020-12-06 18:40:18 +01:00
|
|
|
btrfs = True
|
2014-08-19 15:54:02 +02:00
|
|
|
|
2017-12-30 08:57:52 +01:00
|
|
|
if "lvm2" in partition["fs"]:
|
2020-12-06 18:40:18 +01:00
|
|
|
lvm2 = True
|
2017-12-30 08:48:00 +01:00
|
|
|
|
2016-05-11 16:35:53 +02:00
|
|
|
if partition["mountPoint"] == "/" and "luksMapperName" in partition:
|
2016-05-03 13:43:15 +02:00
|
|
|
encrypt_hook = True
|
|
|
|
|
2020-11-06 20:45:01 +01:00
|
|
|
if (partition["mountPoint"] == "/boot" and "luksMapperName" not in partition):
|
2016-11-17 18:14:37 +01:00
|
|
|
unencrypted_separate_boot = True
|
|
|
|
|
2019-12-16 09:40:54 +01:00
|
|
|
if partition["mountPoint"] == "/usr":
|
|
|
|
hooks.append("usr")
|
|
|
|
|
2016-05-03 13:43:15 +02:00
|
|
|
if encrypt_hook:
|
2020-12-05 21:57:51 +01:00
|
|
|
if detect_plymouth() and unencrypted_separate_boot:
|
2020-11-03 12:55:24 +01:00
|
|
|
hooks.append("plymouth-encrypt")
|
|
|
|
else:
|
|
|
|
hooks.append("encrypt")
|
2020-12-06 18:40:18 +01:00
|
|
|
crypto_file = "crypto_keyfile.bin"
|
2016-11-17 18:14:37 +01:00
|
|
|
if not unencrypted_separate_boot and \
|
2017-03-29 21:59:22 +02:00
|
|
|
os.path.isfile(
|
2020-12-06 18:40:18 +01:00
|
|
|
os.path.join(root_mount_point, crypto_file)
|
2017-03-29 21:59:22 +02:00
|
|
|
):
|
2020-12-06 18:40:18 +01:00
|
|
|
files.append("/{!s}".format(crypto_file))
|
2016-05-03 13:43:15 +02:00
|
|
|
|
2017-12-30 08:48:00 +01:00
|
|
|
if lvm2:
|
|
|
|
hooks.append("lvm2")
|
|
|
|
|
2017-06-04 11:37:12 +02:00
|
|
|
if swap_uuid != "":
|
2016-05-13 16:09:35 +02:00
|
|
|
if encrypt_hook and openswap_hook:
|
|
|
|
hooks.extend(["openswap"])
|
2014-08-19 15:54:02 +02:00
|
|
|
hooks.extend(["resume", "filesystems"])
|
|
|
|
else:
|
|
|
|
hooks.extend(["filesystems"])
|
|
|
|
|
2020-12-08 16:54:05 +01:00
|
|
|
if btrfs and not is_cpu_intel:
|
2014-08-19 15:54:02 +02:00
|
|
|
modules.append("crc32c")
|
2020-12-08 16:54:05 +01:00
|
|
|
|
|
|
|
elif btrfs and is_cpu_intel:
|
2014-08-19 15:54:02 +02:00
|
|
|
modules.append("crc32c-intel")
|
2020-12-08 16:54:05 +01:00
|
|
|
|
|
|
|
elif not btrfs:
|
2014-08-19 15:54:02 +02:00
|
|
|
hooks.append("fsck")
|
|
|
|
|
2016-05-06 17:50:07 +02:00
|
|
|
write_mkinitcpio_lines(hooks, modules, files, root_mount_point)
|
2014-08-19 15:54:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def run():
|
2017-03-29 21:59:22 +02:00
|
|
|
"""
|
2020-12-06 18:40:18 +01:00
|
|
|
Calls routine with given parameters to modify "/etc/mkinitcpio.conf".
|
2015-02-20 20:54:25 +01:00
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2014-08-19 15:54:02 +02:00
|
|
|
partitions = libcalamares.globalstorage.value("partitions")
|
|
|
|
root_mount_point = libcalamares.globalstorage.value("rootMountPoint")
|
2019-04-28 15:33:20 +02:00
|
|
|
|
|
|
|
if not partitions:
|
|
|
|
libcalamares.utils.warning("partitions is empty, {!s}".format(partitions))
|
|
|
|
return (_("Configuration Error"),
|
2020-12-06 18:40:18 +01:00
|
|
|
_("No partitions are defined for <pre>{!s}</pre> to use.").format("initcpiocfg"))
|
2019-04-28 15:33:20 +02:00
|
|
|
if not root_mount_point:
|
|
|
|
libcalamares.utils.warning("rootMountPoint is empty, {!s}".format(root_mount_point))
|
|
|
|
return (_("Configuration Error"),
|
2020-12-06 18:40:18 +01:00
|
|
|
_("No root mount point is given for <pre>{!s}</pre> to use.").format("initcpiocfg"))
|
2019-04-28 15:33:20 +02:00
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
modify_mkinitcpio_conf(partitions, root_mount_point)
|
2015-06-14 12:56:56 +02:00
|
|
|
|
2014-08-19 15:54:02 +02:00
|
|
|
return None
|