From: Daan De Meyer Date: Mon, 6 Nov 2023 20:35:09 +0000 (+0100) Subject: Add BuildSourcesEphemeral= X-Git-Tag: v19~21^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F2046%2Fhead;p=thirdparty%2Fmkosi.git Add BuildSourcesEphemeral= Unfortunately there are use cases where it's useful to persist changes to the source directory. A prime example is projects with Makefile that do not provide a properly functioning `make install`. The only way to use those with mkosi build scripts is to do an in-tree build in the build script and then mounting the source directory into the VM or container when booting it. For this to work we need changes to the source directory to be persisted. To support this use case, let's gate the ephemeral source directories behind an option that's disabled by default (for backwards compatibitity reasons). --- diff --git a/NEWS.md b/NEWS.md index 49a0e0f0b..b30e43e9b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -38,9 +38,10 @@ `-kernel` or `QemuKernel=` - We don't create subdirectories beneath the configured cache directory anymore. -- Source directories are now made ephemeral when running scripts. This - means any changes made to source directories while running scripts - will be undone after the scripts have finished executing. +- Added `BuildSourcesEphemeral=` to make source directories ephemeral + when running scripts. This means any changes made to source + directories while running scripts will be undone after the scripts + have finished executing. - Workspace directories are now created outside of any source directories. mkosi will either use `XDG_CACHE_HOME`, `$HOME/.cache` or `/var/tmp` depending on the situation. diff --git a/mkosi/__init__.py b/mkosi/__init__.py index adcf854d5..b6329b9b8 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -325,7 +325,7 @@ def mount_build_overlay(state: MkosiState, volatile: bool = False) -> Iterator[P def finalize_mounts(config: MkosiConfig) -> Iterator[list[PathString]]: with contextlib.ExitStack() as stack: sources = [ - (stack.enter_context(mount_overlay([source])), target) + (stack.enter_context(mount_overlay([source])) if config.build_sources_ephemeral else source, target) for source, target in [(Path.cwd(), Path.cwd())] + [t.with_prefix(Path.cwd()) for t in config.build_sources] ] diff --git a/mkosi/config.py b/mkosi/config.py index d2f620d09..fbbae311b 100644 --- a/mkosi/config.py +++ b/mkosi/config.py @@ -776,6 +776,7 @@ class MkosiConfig: postinst_scripts: list[Path] finalize_scripts: list[Path] build_sources: list[ConfigTree] + build_sources_ephemeral: bool environment: dict[str, str] with_tests: bool with_network: bool @@ -1467,6 +1468,13 @@ SETTINGS = ( match=config_match_build_sources, help="Path for sources to build", ), + MkosiConfigSetting( + dest="build_sources_ephemeral", + metavar="BOOL", + section="Content", + parse=config_parse_boolean, + help="Make build sources ephemeral when running scripts", + ), MkosiConfigSetting( dest="environment", short="-E", @@ -2811,6 +2819,7 @@ Clean Package Manager Metadata: {yes_no_auto(config.clean_package_metadata)} Postinstall Scripts: {line_join_list(config.postinst_scripts)} Finalize Scripts: {line_join_list(config.finalize_scripts)} Build Sources: {line_join_tree_list(config.build_sources)} + Build Sources Ephemeral: {yes_no(config.build_sources_ephemeral)} Script Environment: {line_join_list(env)} Run Tests in Build Scripts: {yes_no(config.with_tests)} Scripts With Network: {yes_no(config.with_network)} diff --git a/mkosi/resources/mkosi.md b/mkosi/resources/mkosi.md index 9c93c76fc..170e78236 100644 --- a/mkosi/resources/mkosi.md +++ b/mkosi/resources/mkosi.md @@ -906,6 +906,13 @@ boolean argument: either `1`, `yes`, or `true` to enable, or `0`, `no`, with all build sources mounted in it is mounted to `/work/src` inside the image's root directory. +`BuildSourcesEphemeral=`, `--build-sources-ephemeral=` + +: Takes a boolean. Disabled by default. Configures whether changes to + source directories (The working directory and configured using + `BuildSources=`) are persisted. If enabled, all source directories + will be reset to their original state after scripts finish executing. + `Environment=`, `--environment=` : Adds variables to the environment that package managers and the diff --git a/tests/test_json.py b/tests/test_json.py index 7a80b539c..373aedee8 100644 --- a/tests/test_json.py +++ b/tests/test_json.py @@ -99,6 +99,7 @@ def test_config() -> None: "target": "/frob" } ], + "BuildSourcesEphemeral": true, "CacheDirectory": "/is/this/the/cachedir", "CacheOnly": true, "Checksum": false, @@ -268,6 +269,7 @@ def test_config() -> None: build_packages = ["pkg1", "pkg2"], build_scripts = [Path("/path/to/buildscript")], build_sources = [ConfigTree(Path("/qux"), Path("/frob"))], + build_sources_ephemeral = True, cache_dir = Path("/is/this/the/cachedir"), cache_only = True, checksum = False,