Merge branch 'issue-1788' into calamares

This is in response to the issue, and cleans up a bunch of code,
but does not actually resolve the issue (because I can't quite
tell what the issue should be).

SEE #1788
This commit is contained in:
Adriaan de Groot 2021-09-21 15:21:52 +02:00
commit 87e7233292
2 changed files with 111 additions and 74 deletions

View File

@ -28,7 +28,39 @@ def pretty_name():
return _("Configuring mkinitcpio.") return _("Configuring mkinitcpio.")
def cpuinfo(): def detect_plymouth():
"""
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
return target_env_call(["sh", "-c", "which plymouth"]) == 0
class cpuinfo(object):
"""
Object describing the current CPU's characteristics. It may be
be considered a named tuple, there's no behavior here.
Fields in the object:
- is_intel (if it's definitely an Intel CPU)
- is_amd (if it's definitely an AMD CPU)
- number_of_cores
It is possible for both is_* fields to be False.
"""
def __init__(self):
self.is_intel = False
self.is_amd = False
self.number_of_cores = 0
cpu = self._cpuinfo()
self.is_intel = cpu['proc0']['vendor_id'].lower() == "genuineintel"
self.is_amd = cpu['proc0']['vendor_id'].lower() == "authenticamd"
self.number_of_cores = len(cpu)
@staticmethod
def _cpuinfo():
""" """
Return the information in /proc/cpuinfo as a dictionary in the following Return the information in /proc/cpuinfo as a dictionary in the following
format: format:
@ -59,14 +91,11 @@ def cpuinfo():
return cpu_info return cpu_info
def write_mkinitcpio_lines(hooks, modules, files, root_mount_point): def get_host_initcpio():
""" """
Set up mkinitcpio.conf. Reads the host system mkinitcpio.conf and returns all
the lines from that file, or an empty list if it does
:param hooks: not exist.
:param modules:
:param files:
:param root_mount_point:
""" """
hostfile = "/etc/mkinitcpio.conf" hostfile = "/etc/mkinitcpio.conf"
try: try:
@ -76,48 +105,52 @@ def write_mkinitcpio_lines(hooks, modules, files, root_mount_point):
libcalamares.utils.debug("Could not open host file '%s'" % hostfile) libcalamares.utils.debug("Could not open host file '%s'" % hostfile)
mklins = [] mklins = []
for i in range(len(mklins)): return mklins
if mklins[i].startswith("HOOKS"):
joined_hooks = ' '.join(hooks)
mklins[i] = "HOOKS=\"{!s}\"".format(joined_hooks)
elif mklins[i].startswith("MODULES"):
joined_modules = ' '.join(modules)
mklins[i] = "MODULES=\"{!s}\"".format(joined_modules)
elif mklins[i].startswith("FILES"):
joined_files = ' '.join(files)
mklins[i] = "FILES=\"{!s}\"".format(joined_files)
path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
with open(path, "w") as mkinitcpio_file:
mkinitcpio_file.write("\n".join(mklins) + "\n")
def detect_plymouth(): def write_mkinitcpio_lines(hooks, modules, files, root_mount_point):
""" """
Checks existence (runnability) of plymouth in the target system. Set up mkinitcpio.conf.
@return True if plymouth exists in the target, False otherwise :param hooks:
""" :param modules:
# Used to only check existence of path /usr/bin/plymouth in target :param files:
return target_env_call(["sh", "-c", "which plymouth"]) == 0
def modify_mkinitcpio_conf(partitions, root_mount_point):
"""
Modifies mkinitcpio.conf
:param partitions:
:param root_mount_point: :param root_mount_point:
""" """
cpu = cpuinfo() mklins = get_host_initcpio()
swap_uuid = ""
btrfs = "" target_path = os.path.join(root_mount_point, "etc/mkinitcpio.conf")
lvm2 = "" with open(target_path, "w") as mkinitcpio_file:
hooks = ["base", "udev", "autodetect", "modconf", "block", "keyboard", for line in mklins:
"keymap"] # Replace HOOKS, MODULES and FILES lines with what we
# have found via find_initcpio_features()
if line.startswith("HOOKS"):
line = "HOOKS=\"{!s}\"".format(' '.join(hooks))
elif line.startswith("MODULES"):
line = "MODULES=\"{!s}\"".format(' '.join(modules))
elif lines.startswith("FILES"):
line = "FILES=\"{!s}\"".format(' '.join(files))
mkinitcpio_file.write(line + "\n")
def find_initcpio_features(partitions, root_mount_point):
"""
Returns a tuple (hooks, modules, files) needed to support
the given @p partitions (filesystems types, encryption, etc)
in the target.
:param partitions: (from GS)
:param root_mount_point: (from GS)
:return 3-tuple of lists
"""
hooks = ["base", "udev", "autodetect", "modconf", "block", "keyboard", "keymap"]
modules = [] modules = []
files = [] files = []
swap_uuid = ""
uses_btrfs = False
uses_lvm2 = False
encrypt_hook = False encrypt_hook = False
openswap_hook = False openswap_hook = False
unencrypted_separate_boot = False unencrypted_separate_boot = False
@ -137,10 +170,10 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
openswap_hook = True openswap_hook = True
if partition["fs"] == "btrfs": if partition["fs"] == "btrfs":
btrfs = "yes" uses_btrfs = True
if "lvm2" in partition["fs"]: if "lvm2" in partition["fs"]:
lvm2 = "yes" uses_lvm2 = True
if partition["mountPoint"] == "/" and "luksMapperName" in partition: if partition["mountPoint"] == "/" and "luksMapperName" in partition:
encrypt_hook = True encrypt_hook = True
@ -162,7 +195,7 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
): ):
files.append("/crypto_keyfile.bin") files.append("/crypto_keyfile.bin")
if lvm2: if uses_lvm2:
hooks.append("lvm2") hooks.append("lvm2")
if swap_uuid != "": if swap_uuid != "":
@ -172,15 +205,12 @@ def modify_mkinitcpio_conf(partitions, root_mount_point):
else: else:
hooks.extend(["filesystems"]) hooks.extend(["filesystems"])
if btrfs == "yes" and cpu['proc0']['vendor_id'].lower() != "genuineintel": if uses_btrfs:
modules.append("crc32c") modules.append("crc32c-intel" if cpuinfo().is_intel else "crc32c")
elif (btrfs == "yes"
and cpu['proc0']['vendor_id'].lower() == "genuineintel"):
modules.append("crc32c-intel")
else: else:
hooks.append("fsck") hooks.append("fsck")
write_mkinitcpio_lines(hooks, modules, files, root_mount_point) return (hooks, modules, files)
def run(): def run():
@ -201,6 +231,7 @@ def run():
return (_("Configuration Error"), return (_("Configuration Error"),
_("No root mount point is given for <pre>{!s}</pre> to use." ).format("initcpiocfg")) _("No root mount point is given for <pre>{!s}</pre> to use." ).format("initcpiocfg"))
modify_mkinitcpio_conf(partitions, root_mount_point) hooks, modules, files = find_initcpio_features(partitions, root_mount_point)
write_mkinitcpio_lines(hooks, modules, files, root_mount_point)
return None return None

View File

@ -1,7 +1,13 @@
# SPDX-FileCopyrightText: no # SPDX-FileCopyrightText: no
# SPDX-License-Identifier: CC0-1.0 # SPDX-License-Identifier: CC0-1.0
#
# Writes a mkinitcpio.conf into the target system. It copies
# the host system's /etc/mkinitcpio.conf, and replaces any
# HOOKS, MODULES, and FILES lines with calculated values
# based on what the installation (seems to) need.
--- ---
type: "job" type: "job"
name: "initcpiocfg" name: "initcpiocfg"
interface: "python" interface: "python"
script: "main.py" script: "main.py"
noconfig: true