From 0e7faae2a6d04fc775c50ede4dd99a18354ea20f Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Fri, 1 Sep 2023 14:32:29 +0200 Subject: [PATCH] Don't run depmod unless needed For distro kernels, depmod is already executed by the package manager, so let's make sure we don't rerun it unless needed. To make this work, we have to run depmod and modinfo from inside the image, as running modinfo from the host on files generated by depmod from the image can lead to all sorts of compatibility issues so we opt to run both depmod and modinfo from inside the image. --- mkosi/__init__.py | 14 +++++++++++++- mkosi/kmod.py | 6 +++--- mkosi/run.py | 3 ++- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 5686d8b27..910aedefe 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -1462,7 +1462,19 @@ def run_depmod(state: MkosiState) -> None: if state.config.bootable == ConfigFeature.disabled: return + outputs = ( + "modules.dep", + "modules.dep.bin", + "modules.symbols", + "modules.symbols.bin", + ) + + filters = state.config.kernel_modules_include or state.config.kernel_modules_exclude + for kver, _ in gen_kernel_images(state): + if not filters and all((state.root / "usr/lib/modules" / kver / o).exists() for o in outputs): + continue + process_kernel_modules( state.root, kver, state.config.kernel_modules_include, @@ -1470,7 +1482,7 @@ def run_depmod(state: MkosiState) -> None: ) with complete_step(f"Running depmod for {kver}"): - run(["depmod", "--all", "--basedir", state.root, kver]) + bwrap(chroot_cmd(state.root) + ["depmod", "--all", kver]) def run_sysusers(state: MkosiState) -> None: diff --git a/mkosi/kmod.py b/mkosi/kmod.py index cdf138be7..1522358e1 100644 --- a/mkosi/kmod.py +++ b/mkosi/kmod.py @@ -8,7 +8,7 @@ from collections.abc import Iterator, Sequence from pathlib import Path from mkosi.log import complete_step, log_step -from mkosi.run import run +from mkosi.run import bwrap, chroot_cmd def filter_kernel_modules(root: Path, kver: str, include: Sequence[str], exclude: Sequence[str]) -> list[Path]: @@ -58,8 +58,8 @@ def resolve_module_dependencies(root: Path, kver: str, modules: Sequence[str]) - # We could run modinfo once for each module but that's slow. Luckily we can pass multiple modules to # modinfo and it'll process them all in a single go. We get the modinfo for all modules to build two maps # that map the path of the module to its module dependencies and its firmware dependencies respectively. - info = run(["modinfo", "--basedir", root, "--set-version", kver, "--null", *nametofile.keys(), *builtin], - stdout=subprocess.PIPE).stdout + info = bwrap(chroot_cmd(root) + ["modinfo", "--set-version", kver, "--null", *nametofile.keys(), *builtin], + stdout=subprocess.PIPE).stdout log_step("Calculating required kernel modules and firmware") diff --git a/mkosi/run.py b/mkosi/run.py index 7345253d8..a94c944d8 100644 --- a/mkosi/run.py +++ b/mkosi/run.py @@ -278,6 +278,7 @@ def bwrap( scripts: Mapping[str, Sequence[PathString]] = {}, env: Mapping[str, str] = {}, stdin: _FILE = None, + stdout: _FILE = None, input: Optional[str] = None, ) -> CompletedProcess: cmdline: list[PathString] = [ @@ -335,7 +336,7 @@ def bwrap( ] try: - result = run([*cmdline, *cmd], env=env, log=False, stdin=stdin, input=input) + result = run([*cmdline, *cmd], env=env, log=False, stdin=stdin, stdout=stdout, input=input) except subprocess.CalledProcessError as e: if log: logging.error(f"\"{shlex.join(os.fspath(s) for s in cmd)}\" returned non-zero exit code {e.returncode}.") -- 2.47.2