Skip to content

Commit c1900de

Browse files
committed
split module autodetect into lsmod and lspci options, add nosound and novideo modules
Signed-off-by: Zen <[email protected]>
1 parent 9e6f4ff commit c1900de

File tree

7 files changed

+57
-11
lines changed

7 files changed

+57
-11
lines changed

config_example.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# This config will decrypt the LUKS volume with uuid "fdf442da-0574-4531-98c7-55227a041f1d", mapping it to "/dev/mapper/root"
22
# It will attempt to mount the btrfs volume with label "rootfs" to /mnt/root
3-
# It will pull all current kernel modules from the active kernel version
3+
# It will pull all current kernel modules from lspci -k results
44
# The "ugrd.base.kmod_nvme" module will be used to load the nvme module if it's not detected in lsmod
55
# The "ugrd.base.cpio" module will pack the initramfs into a cpio archive
66

77
modules = [
88
"ugrd.base.base",
99
"ugrd.base.kmod",
1010
"ugrd.base.kmod_nvme",
11+
"ugrd.base.kmod_nosound",
12+
"ugrd.base.kmod_novideo",
1113
"ugrd.crypto.cryptsetup",
1214
"ugrd.base.btrfs",
1315
"ugrd.base.cpio",
@@ -18,7 +20,7 @@ out_dir = "/usr/src/initramfs" # Output here instead of /tmp/initramfs_out
1820

1921
# Optionally supply a kernel version, uses the current kernel version if not specified
2022
#kernel_version = "6.1.53-gentoo-dist"
21-
kmod_autodetect = true
23+
kmod_autodetect_lspci = true
2224

2325
# Create device nodes only inside the CPIO
2426
mknod_cpio = true

readme.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ Modules can use `_kmod_depend` to add required modules. Simply using the `ugrd.c
122122
The following parameters can be used to change the kernel module pulling and initializing behavior:
123123

124124
* `kernel_version` (uname -r) is used to specify the kernel version to pull modules for, should be a directory under `/lib/modules/<kernel_version>`.
125-
* `kmod_autodetect` (false) if set to `true`, will populate `kernel_modules` with modules listed in `lsmod`.
126125
* `kmod_init` is used to specify kernel modules to load at boot. If set, ONLY these modules will be loaded with modprobe.
126+
* `kmod_autodetect_lspci` (false) if set to `true`, will populate `kernel_modules` with modules listed in `lspci -k`.
127+
* `kmod_autodetect_lsmod` (false) if set to `true`, will populate `kernel_modules` with modules listed in `lsmod`.
127128
* `kernel_modules` is used to define a list of kernel module names to pull into the initramfs. These modules will not be `modprobe`'d automatically if `kmod_init` is also set.
128129
* `kmod_ignore` is used to specify kernel modules to ignore. If a module depends on one of these, it will throw an error and drop it from being included.
129130
* `kmod_ignore_softdeps` (false) ignore softdeps when checking kernel module dependencies.
@@ -136,6 +137,8 @@ Some helper modules have been created to make importing required kernel modules
136137

137138
`base.ugrd.kmod_nvme`, `kmod_usb`, and `kmod_fat` can be used to load modules for NVME's, USB storage, and the FAT file system respectively.
138139

140+
Similarly `base.ugrd.kmod_novideo` and `kmod_nosound` exist to ignore video and sound devices that may appear when autodetecting modules.
141+
139142
#### base.console
140143

141144
This module creates an agetty session. This is used by the `ugrd.crypto.gpg` module so the tty can be used for input and output.

ugrd/base/kmod.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
__author__ = 'desultory'
22

3-
__version__ = '0.4.5'
3+
__version__ = '0.4.7'
44

55
from pathlib import Path
66

@@ -153,7 +153,34 @@ def resolve_kmod(self, module_name):
153153
self.logger.warning("[%s] Kernel module has no dependencies." % module_name)
154154

155155

156-
def get_all_modules(self):
156+
def get_lspci_modules(self):
157+
"""
158+
Gets the name of all kernel modules being used by hardware visible in lspci -k
159+
"""
160+
try:
161+
cmd = self._run(['lspci', '-k'])
162+
except RuntimeError as e:
163+
raise DependencyResolutionError("Failed to get list of kernel modules") from e
164+
165+
raw_modules = set()
166+
# Iterate over all output lines
167+
for line in cmd.stdout.decode('utf-8').split('\n'):
168+
# If the line contains the string 'Kernel modules:' or 'Kernel driver in use:', it contains the name of a kernel module
169+
if 'Kernel modules:' in line or 'Kernel driver in use:' in line:
170+
module = line.split(':')[1]
171+
if ',' in module:
172+
# If there are multiple modules, split them and add them to the module set
173+
for module in module.split(','):
174+
raw_modules.add(module.strip())
175+
else:
176+
# Add the single module to the module set
177+
raw_modules.add(module.strip())
178+
179+
self.logger.debug("Kernel modules in use by hardware: %s" % raw_modules)
180+
return list(raw_modules)
181+
182+
183+
def get_lsmod_modules(self):
157184
"""
158185
Gets the name of all currently installed kernel modules
159186
"""
@@ -215,10 +242,14 @@ def calculate_modules(self):
215242
Adds the contents of _kmod_depend if specified.
216243
If kernel_modules is empty, pulls all currently loaded kernel modules.
217244
"""
218-
if self.config_dict['kmod_autodetect']:
219-
self.logger.info("Autodetecting kernel modules")
220-
autodetected_modules = get_all_modules(self)
221-
self.logger.info("Autodetected kernel modules: %s" % autodetected_modules)
245+
if self.config_dict['kmod_autodetect_lsmod']:
246+
autodetected_modules = get_lsmod_modules(self)
247+
self.logger.info("Autodetected kernel modules from lsmod: %s" % autodetected_modules)
248+
self.config_dict['kernel_modules'] = autodetected_modules
249+
250+
if self.config_dict['kmod_autodetect_lspci']:
251+
autodetected_modules = get_lspci_modules(self)
252+
self.logger.info("Autodetected kernel modules from lscpi -k: %s" % autodetected_modules)
222253
self.config_dict['kernel_modules'] = autodetected_modules
223254

224255
if self.config_dict['_kmod_depend']:

ugrd/base/kmod.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
binaries = [ "modprobe", "lsmod" ]
22

3+
# Ignore this because it's only builtin
4+
kmod_ignore = ['pcieport', 'piix4_smbus']
5+
36
[custom_parameters]
47
kernel_modules = "NoDupFlatList" # Kernel modules to pull into the initramfs
58
kernel_version = "str" # Kernel version to use for the initramfs
69
kmod_ignore_softdeps = "bool" # Whether or not softdeps are ignored
710
kmod_init = "NoDupFlatList" # Kernel modules to load at initramfs startup
8-
kmod_autodetect = "bool" # Whether or not to automatically pull currently loaded kernel modules
11+
kmod_autodetect_lsmod = "bool" # Whether or not to automatically pull currently loaded kernel modules
12+
kmod_autodetect_lspci = "bool" # Whether or not to automatically pull kernel modules from lspci -k
913
kmod_ignore = "NoDupFlatList" # Kernel modules to ignore when loading
1014
_kmod_depend = "NoDupFlatList" # Meant to be used internally, defines kernel modules to automatically load, typically within other module config
1115

ugrd/base/kmod_nosound.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod_depends = ['ugrd.base.kmod']
2+
3+
kmod_ignore = ['snd_hda_intel', 'snd-hda-core', 'snd-hda-codec', 'snd', 'snd-intel-dspcfg', 'snd-pcm', 'snd-hwdep', 'soundcore', 'snd-intel-sdw-acpi', 'snd-timer']

ugrd/base/kmod_novideo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod_depends = ['ugrd.base.kmod']
2+
3+
kmod_ignore = ['amdgpu', 'drm_kms_helper', 'ttm', 'drm_display_helper', 'gpu-sched', 'drm_buddy', 'video', 'drm_ttm_helper']

ugrd/initramfs_generator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
__author__ = "desultory"
3-
__version__ = "0.6.2"
3+
__version__ = "0.6.4"
44

55
from tomllib import load
66
from pathlib import Path

0 commit comments

Comments
 (0)