From: Jörg Behrmann Date: Wed, 17 Apr 2024 13:42:19 +0000 (+0200) Subject: Add artifacts directories to pass around build artifacts X-Git-Tag: v23.1~106^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F2639%2Fhead;p=thirdparty%2Fmkosi.git Add artifacts directories to pass around build artifacts Also pick up microcode and initrds from the artifact directory --- diff --git a/mkosi/__init__.py b/mkosi/__init__.py index ee76a7586..74fe26d4f 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -545,6 +545,7 @@ def run_prepare_scripts(context: Context, build: bool) -> None: SRCDIR="/work/src", CHROOT_SRCDIR="/work/src", PACKAGEDIR="/work/packages", + ARTIFACTDIR="/work/artifacts", SCRIPT="/work/prepare", CHROOT_SCRIPT="/work/prepare", MKOSI_UID=str(INVOKING_USER.uid), @@ -596,6 +597,7 @@ def run_prepare_scripts(context: Context, build: bool) -> None: Mount(script, "/work/prepare", ro=True), Mount(json, "/work/config.json", ro=True), Mount(context.root, "/buildroot"), + Mount(context.artifacts, "/work/artifacts"), *context.config.distribution.package_manager(context.config).mounts(context), ], options=["--dir", "/work/src", "--chdir", "/work/src"], @@ -621,6 +623,7 @@ def run_build_scripts(context: Context) -> None: SRCDIR="/work/src", CHROOT_SRCDIR="/work/src", PACKAGEDIR="/work/packages", + ARTIFACTDIR="/work/artifacts", SCRIPT="/work/build-script", CHROOT_SCRIPT="/work/build-script", MKOSI_UID=str(INVOKING_USER.uid), @@ -675,6 +678,7 @@ def run_build_scripts(context: Context) -> None: Mount(context.root, "/buildroot"), Mount(context.install_dir, "/work/dest"), Mount(context.staging, "/work/out"), + Mount(context.artifacts, "/work/artifacts"), *( [Mount(context.config.build_dir, "/work/build")] if context.config.build_dir @@ -709,6 +713,7 @@ def run_postinst_scripts(context: Context) -> None: SRCDIR="/work/src", CHROOT_SRCDIR="/work/src", PACKAGEDIR="/work/packages", + ARTIFACTDIR="/work/artifacts", MKOSI_UID=str(INVOKING_USER.uid), MKOSI_GID=str(INVOKING_USER.gid), MKOSI_CONFIG="/work/config.json", @@ -748,6 +753,7 @@ def run_postinst_scripts(context: Context) -> None: Mount(json, "/work/config.json", ro=True), Mount(context.root, "/buildroot"), Mount(context.staging, "/work/out"), + Mount(context.artifacts, "/work/artifacts"), *context.config.distribution.package_manager(context.config).mounts(context), ], options=["--dir", "/work/src", "--chdir", "/work/src"], @@ -771,6 +777,7 @@ def run_finalize_scripts(context: Context) -> None: SRCDIR="/work/src", CHROOT_SRCDIR="/work/src", PACKAGEDIR="/work/packages", + ARTIFACTDIR="/work/artifacts", SCRIPT="/work/finalize", CHROOT_SCRIPT="/work/finalize", MKOSI_UID=str(INVOKING_USER.uid), @@ -810,6 +817,7 @@ def run_finalize_scripts(context: Context) -> None: Mount(json, "/work/config.json", ro=True), Mount(context.root, "/buildroot"), Mount(context.staging, "/work/out"), + Mount(context.artifacts, "/work/artifacts"), *context.config.distribution.package_manager(context.config).mounts(context), ], options=["--dir", "/work/src", "--chdir", "/work/src"], @@ -1674,7 +1682,7 @@ def want_initrd(context: Context) -> bool: if context.config.output_format not in (OutputFormat.disk, OutputFormat.directory): return False - if not any(gen_kernel_images(context)): + if not any((context.artifacts / "io.mkosi.initrd").glob("*")) and not any(gen_kernel_images(context)): return False return True @@ -2166,6 +2174,24 @@ def finalize_cmdline(context: Context, roothash: Optional[str]) -> list[str]: return cmdline + context.config.kernel_command_line +def finalize_initrds(context: Context) -> list[Path]: + if any((context.artifacts / "io.mkosi.microcode").glob("*")): + initrds = sorted((context.artifacts / "io.mkosi.microcode").iterdir()) + elif microcode := build_microcode_initrd(context): + initrds = [microcode] + else: + initrds = [] + + if context.config.initrds: + initrds += context.config.initrds + elif any((context.artifacts / "io.mkosi.initrd").glob("*")): + initrds += sorted((context.artifacts / "io.mkosi.initrd").iterdir()) + else: + initrds += [build_default_initrd(context)] + + return initrds + + def install_type1( context: Context, kver: str, @@ -2179,7 +2205,6 @@ def install_type1( dst.mkdir(parents=True, exist_ok=True) entry.parent.mkdir(parents=True, exist_ok=True) - microcode = build_microcode_initrd(context) kmods = build_kernel_modules_initrd(context, kver) cmdline = finalize_cmdline(context, finalize_roothash(partitions)) @@ -2194,11 +2219,7 @@ def install_type1( else: kimg = Path(shutil.copy2(context.root / kimg, dst / "vmlinuz")) - initrds = [Path(shutil.copy2(microcode, dst.parent / "microcode.initrd"))] if microcode else [] - initrds += [ - Path(shutil.copy2(initrd, dst.parent / initrd.name)) - for initrd in (context.config.initrds or [build_default_initrd(context)]) - ] + initrds = [Path(shutil.copy2(initrd, dst.parent / initrd.name)) for initrd in finalize_initrds(context)] initrds += [Path(shutil.copy2(kmods, dst / "kernel-modules.initrd"))] with entry.open("w") as f: @@ -2283,11 +2304,7 @@ def install_uki(context: Context, kver: str, kimg: Path, token: str, partitions: return else: - microcode = build_microcode_initrd(context) - - initrds = [microcode] if microcode else [] - initrds += context.config.initrds or [build_default_initrd(context)] - + initrds = finalize_initrds(context) if context.config.kernel_modules_initrd: initrds += [build_kernel_modules_initrd(context, kver)] @@ -2473,9 +2490,8 @@ def copy_initrd(context: Context) -> None: return for kver, _ in gen_kernel_images(context): - microcode = build_microcode_initrd(context) - initrds = [microcode] if microcode else [] - initrds += context.config.initrds or [build_default_initrd(context)] + initrds = finalize_initrds(context) + if context.config.kernel_modules_initrd: kver = next(gen_kernel_images(context))[0] initrds += [build_kernel_modules_initrd(context, kver)] diff --git a/mkosi/context.py b/mkosi/context.py index b9cdb62de..2b23250ec 100644 --- a/mkosi/context.py +++ b/mkosi/context.py @@ -45,6 +45,7 @@ class Context: self.staging.mkdir() self.pkgmngr.mkdir() self.packages.mkdir() + self.artifacts.mkdir() self.install_dir.mkdir(exist_ok=True) @property @@ -63,6 +64,10 @@ class Context: def packages(self) -> Path: return self.workspace / "packages" + @property + def artifacts(self) -> Path: + return self.workspace / "artifacts" + @property def install_dir(self) -> Path: return self.workspace / "dest" diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 159dabf74..dba72b5aa 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -1242,10 +1242,13 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, `Initrds=`, `--initrd` -: Use user-provided initrd(s). Takes a comma separated list of paths to - initrd files. This option may be used multiple times in which case the - initrd lists are combined. If no initrds are specified and a bootable - image is requested, mkosi will automatically build a default initrd. +: Use user-provided initrd(s). Takes a comma separated list of paths to initrd + files. This option may be used multiple times in which case the initrd lists + are combined. If no initrds are specified and a bootable image is requested, + mkosi will look for initrds in a subdirectory `io.mkosi.initrd` of the + artifact directory (see `$ARTIFACTDIR` in the section **ENVIRONMENT + VARIABLES**), if none are found there mkosi will automatically build a + default initrd. `InitrdPackages=`, `--initrd-package=` @@ -2175,6 +2178,26 @@ Scripts executed by mkosi receive the following environment variables: repository. Build scripts can add more packages to the local repository by writing the packages to `$PACKAGEDIR`. +* `$ARTIFACTDIR` points to the directory that is used to pass around build + artifacts generated during the build and make them available for use by + mkosi. This is similar to `PACKAGEDIR`, but is meant for artifacts that may + not be packages understood by the package manager, e.g. initrds created by + other initrd generators than mkosi. Build scripts can add more artifacts to + the directory by placing them in `$ARTIFACTDIR`. Files in this directory are + only available for the current build and are not copied out like the contents + of `$OUTPUTDIR`. + + `mkosi` will also use certain subdirectories of an artifacts directory to + automatically use their contents at certain steps. Currently the following + two subdirectories in the artifact directory are used by mkosi: + - `io.mkosi.microcode`: All files in this directory are used as microcode + files, i.e. they are prepended to the initrds in lexicographical order. + - `io.mkosi.initrd`: All files in this directory are used as initrds and + joined in lexicographical order. + + It is recommend users of `$ARTIFACTDIR` put things for their own use in a + similar namespaced directory, e.h. `local.my.namespace`. + * `$BUILDROOT` is the root directory of the image being built, optionally with the build overlay mounted on top depending on the script that's being executed. @@ -2231,6 +2254,7 @@ Consult this table for which script receives which environment variables: | `CHROOT_OUTPUTDIR` | | | | X | X | X | | | `BUILDROOT` | | | X | X | X | X | | | `PACKAGEDIR` | | | x | x | x | x | | +| `ARTIFACTDIR` | | | x | x | x | x | | | `WITH_DOCS` | | | X | X | | | | | `WITH_TESTS` | | | X | X | | | | | `WITH_NETWORK` | | | X | X | | | |