Skip to content

Commit 779abb5

Browse files
authored
Merge pull request #311 from desultory/dev
add kmod_init_optional, move some USB modules there
2 parents be45dab + 9ddbcf2 commit 779abb5

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ The following parameters can be used to change the kernel module pulling and ini
171171
* `kernel_version` (uname -r) Used to specify the kernel version to pull modules for, should be a directory under `/lib/modules/<kernel_version>`.
172172
* `kmod_pull_firmware` (true) Adds kernel module firmware to dependencies
173173
* `kmod_init` - Kernel modules to `modprobe` at boot.
174+
* `kmod_init_optional` - Modules to attempt to add to `kmod_init`, failing with a warning if not found.
174175
* `kmod_autodetect_lspci` (false) Finds kernel modules for PCI devices using `/sys/bus/pci/drivers`, formely used `lspci -k`.
175176
* `kmod_autodetect_lsmod` (false) Pulls kernel modules using `/proc/modules`, formerly used `lsmod`.
176177
* `kernel_modules` - Kernel modules to pull into the initramfs. These modules will not be `modprobe`'d automatically.

src/ugrd/kmod/kmod.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "desultory"
2-
__version__ = "3.3.7"
2+
__version__ = "3.4.0"
33

44
from pathlib import Path
55
from platform import uname
@@ -8,7 +8,13 @@
88
from subprocess import run
99

1010
from ugrd.exceptions import AutodetectError, ValidationError
11-
from ugrd.kmod import BuiltinModuleError, MissingModuleError, DependencyResolutionError, IgnoredModuleError, _normalize_kmod_name
11+
from ugrd.kmod import (
12+
BuiltinModuleError,
13+
DependencyResolutionError,
14+
IgnoredModuleError,
15+
MissingModuleError,
16+
_normalize_kmod_name,
17+
)
1218
from zenlib.util import colorize as c_
1319
from zenlib.util import contains, unset
1420

@@ -37,6 +43,20 @@ def _process_kmod_init_multi(self, module: str) -> None:
3743
self["kernel_modules"] = module
3844

3945

46+
def _process_kmod_init_optional_multi(self, module: str) -> None:
47+
"""Adds an optional kmod init module"""
48+
module = _normalize_kmod_name(module)
49+
if module in self["kmod_ignore"]:
50+
self.logger.warning(f"Optional kmod_init module is in the ignore list: {c_(module, 'yellow', bold=True)}")
51+
self["_kmod_removed"] = module
52+
return
53+
if module in self["kmod_init"]:
54+
self.logger.debug(f"Optional kmod_init module is already in kmod_init: {c_(module, 'yellow', bold=True)}")
55+
return
56+
self.logger.debug(f"Adding optional kmod_init module: {c_(module, 'magenta')}")
57+
self["kmod_init_optional"].append(module)
58+
59+
4060
def _process__kmod_auto_multi(self, module: str) -> None:
4161
"""Adds autodetected modules to self['kernel_modules']."""
4262
module = _normalize_kmod_name(module)
@@ -201,7 +221,9 @@ def _process_kernel_version(self, kver: str) -> None:
201221
if self["no_kmod"]:
202222
return self.logger.warning("[%s] Kernel module directory does not exist, but no_kmod is set." % kver)
203223
self.logger.error(f"Available kernel versions: {', '.join([d.name for d in Path('/lib/modules').iterdir()])}")
204-
self.logger.info("If kernel modules are not installed, and not required, set `no_kmod = true` to skip this check.")
224+
self.logger.info(
225+
"If kernel modules are not installed, and not required, set `no_kmod = true` to skip this check."
226+
)
205227
raise ValidationError(f"Kernel module directory does not exist for kernel: {kver}")
206228

207229
self.data["kernel_version"] = kver
@@ -373,7 +395,9 @@ def add_kmod_deps(self):
373395
if self.get("kernel_version"):
374396
_add_kmod_firmware(self, kmod)
375397
else:
376-
self.logger.warning(f"Kernel version is not set, skipping firmware detection for kmod: {c_(kmod, 'yellow')}")
398+
self.logger.warning(
399+
f"Kernel version is not set, skipping firmware detection for kmod: {c_(kmod, 'yellow')}"
400+
)
377401
# if no_kmod is set, continue and check for the firmware of the next module
378402
if self["no_kmod"]:
379403
continue
@@ -421,9 +445,28 @@ def process_ignored_modules(self) -> None:
421445
process_ignored_module(self, module)
422446

423447

448+
def _process_optional_modules(self) -> None:
449+
"""Processes optional kernel modules."""
450+
for kmod in self["kmod_init_optional"]:
451+
if kmod in self["kmod_init"]:
452+
self.logger.debug(f"Optional kmod_init module is already in kmod_init: {c_(kmod, 'yellow', bold=True)}")
453+
continue
454+
try:
455+
_process_kmod_dependencies(self, kmod)
456+
self["kmod_init"] = kmod # add to kmod_init so it will be loaded
457+
except IgnoredModuleError as e:
458+
self.logger.info(e)
459+
except BuiltinModuleError:
460+
self.logger.debug(f"Optional kmod_init module is built-in, skipping: {c_(kmod, 'yellow')}")
461+
continue
462+
except DependencyResolutionError as e:
463+
self.logger.warning(f"[{c_(kmod, 'yellow', bold=True)}] Failed to process optional kernel module dependencies: {e}")
464+
465+
424466
@unset("no_kmod", "no_kmod is enabled, skipping.", log_level=30)
425467
def process_modules(self) -> None:
426468
"""Processes all kernel modules, adding dependencies to the initramfs."""
469+
_process_optional_modules(self)
427470
self.logger.debug("Processing kernel modules: %s" % self["kernel_modules"])
428471
for kmod in self["kernel_modules"].copy():
429472
self.logger.debug("Processing kernel module: %s" % kmod)
@@ -441,6 +484,7 @@ def process_modules(self) -> None:
441484
self.logger.info(e)
442485
except DependencyResolutionError as e:
443486
if kmod in self["kmod_init"]:
487+
# Once optional modules are fully implemented, this should raise an exception instead
444488
self.logger.error("[%s] Failed to get modinfo for init kernel module: %s" % (kmod, e))
445489
self.logger.debug("[%s] Failed to get modinfo for kernel module: %s" % (kmod, e))
446490
self["kmod_ignore"] = kmod
@@ -457,8 +501,10 @@ def process_modules(self) -> None:
457501
except BuiltinModuleError:
458502
continue # Don't add built-in modules to the ignore list
459503
except IgnoredModuleError as e:
460-
self.logger.debug(e)
461-
except DependencyResolutionError as e:
504+
self.logger.debug(e) # when autodetected modules are ignored, only debug log
505+
except (
506+
DependencyResolutionError
507+
) as e: # log a warning, not error or exception when autodetected modules have missing deps
462508
self.logger.warning("[%s] Failed to process autodetected kernel module dependencies: %s" % (kmod, e))
463509
self["kmod_ignore"] = kmod
464510

@@ -471,7 +517,9 @@ def load_modules(self) -> str:
471517
removed_kmods = ", ".join(self["_kmod_removed"])
472518
if self["no_kmod"]:
473519
if included_kmods or init_kmods:
474-
self.logger.warning("no_kmod is enabled, but kernel modules are set, ensure the following kernel modules are built into the kernel:")
520+
self.logger.warning(
521+
"no_kmod is enabled, but kernel modules are set, ensure the following kernel modules are built into the kernel:"
522+
)
475523
self.logger.warning(f"Init kernel modules: {c_(init_kmods, 'red', bold=True)}")
476524
self.logger.warning(f"Included kernel modules: {c_(included_kmods, 'red', bold=True)}")
477525
return

src/ugrd/kmod/kmod.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ kmod_autodetect_lsmod = "bool" # Whether or not to automatically pull currently
2020
kmod_autodetect_lspci = "bool" # Whether or not to automatically pull kernel modules from lspci -k
2121
kernel_modules = "NoDupFlatList" # Kernel modules to pull into the initramfs
2222
kmod_init = "NoDupFlatList" # Kernel modules to load at initramfs startup
23+
kmod_init_optional = "NoDupFlatList" # Kernel modules to try to add to kmod_init
2324
no_kmod = "bool" # Disables kernel modules entirely
2425

2526
[imports.config_processing]

src/ugrd/kmod/usb.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
kmod_init = ['uas', 'usbcore', 'scsi_mod', 'ehci_hcd', 'ohci_hcd', 'uhci_hcd', 'xhci_hcd']
1+
kmod_init = ['uas', 'usbcore']
2+
kmod_init_optional = ['ehci_hcd', 'ohci_hcd', 'uhci_hcd', 'xhci_hcd']

0 commit comments

Comments
 (0)