"""
assert state.config.clean_package_metadata in (False, True, None)
- if state.config.clean_package_metadata is False or state.for_cache:
+ if state.config.clean_package_metadata is False:
return
# we try then all: metadata will only be touched if any of them are in the
def remove_files(state: MkosiState) -> None:
"""Remove files based on user-specified patterns"""
- if not state.config.remove_files or state.for_cache:
+ if not state.config.remove_files:
return
with complete_step("Removing files…"):
unlink_try_hard(p)
-def install_distribution(state: MkosiState, cached: bool) -> None:
- if cached:
- return
-
+def install_distribution(state: MkosiState) -> None:
if state.config.base_trees:
if not state.config.packages:
return
state.installer.install_packages(state, state.config.packages)
-def install_build_packages(state: MkosiState, cached: bool) -> None:
- if state.config.build_script is None or cached:
+def install_build_packages(state: MkosiState) -> None:
+ if state.config.build_script is None:
return
with complete_step(f"Installing build packages for {str(state.config.distribution).capitalize()}"), mount_build_overlay(state):
def remove_packages(state: MkosiState) -> None:
"""Remove packages listed in config.remove_packages"""
- if not state.config.remove_packages or state.for_cache:
+ if not state.config.remove_packages:
return
with complete_step(f"Removing {len(state.config.packages)} packages…"):
writable) or the image runs with a transient machine ID, that changes on
each boot (if the image is read-only).
"""
-
- if state.for_cache:
- return
-
with complete_step("Resetting machine ID"):
machine_id = state.root / "etc/machine-id"
machine_id.unlink(missing_ok=True)
def configure_root_password(state: MkosiState) -> None:
"Set the root account password, or just delete it so it's easy to log in"
- if state.for_cache:
- return
-
if state.config.password == "":
with complete_step("Deleting root password"):
def configure_autologin(state: MkosiState) -> None:
- if not state.config.autologin or state.for_cache:
+ if not state.config.autologin:
return
with complete_step("Setting up autologin…"):
return mount_overlay([state.root], state.build_overlay, state.workdir, state.root, read_only)
-def run_prepare_script(state: MkosiState, cached: bool, build: bool) -> None:
+def run_prepare_script(state: MkosiState, build: bool) -> None:
if state.config.prepare_script is None:
return
- if cached:
- return
if build and state.config.build_script is None:
return
def run_postinst_script(state: MkosiState) -> None:
if state.config.postinst_script is None:
return
- if state.for_cache:
- return
with complete_step("Running postinstall script…"):
bwrap: list[PathString] = [
def run_finalize_script(state: MkosiState) -> None:
if state.config.finalize_script is None:
return
- if state.for_cache:
- return
with complete_step("Running finalize script…"):
run([state.config.finalize_script],
def install_boot_loader(state: MkosiState) -> None:
- if state.for_cache or state.config.bootable is False:
+ if state.config.bootable is False:
return
if state.config.output_format == OutputFormat.cpio and state.config.bootable is None:
state.workspace / "mkosi.esl"])
-def install_base_trees(state: MkosiState, cached: bool) -> None:
- if not state.config.base_trees or cached or state.config.overlay:
+def install_base_trees(state: MkosiState) -> None:
+ if not state.config.base_trees or state.config.overlay:
return
with complete_step("Copying in base trees…"):
shutil.unpack_archive(path, state.root)
-def install_skeleton_trees(state: MkosiState, cached: bool) -> None:
- if not state.config.skeleton_trees or cached:
+def install_skeleton_trees(state: MkosiState) -> None:
+ if not state.config.skeleton_trees:
return
with complete_step("Copying in skeleton file trees…"):
if not state.config.extra_trees:
return
- if state.for_cache:
- return
-
with complete_step("Copying in extra file trees…"):
for source, target in state.config.extra_trees:
t = state.root
def install_build_dest(state: MkosiState) -> None:
- if state.for_cache:
- return
-
if state.config.build_script is None:
return
def make_tar(state: MkosiState) -> None:
if state.config.output_format != OutputFormat.tar:
return
- if state.for_cache:
- return
cmd: list[PathString] = [tar_binary(), "-C", state.root, "-c", "--xattrs", "--xattrs-include=*"]
if state.config.tar_strip_selinux_context:
def make_initrd(state: MkosiState) -> None:
if state.config.output_format != OutputFormat.cpio:
return
- if state.for_cache:
- return
make_cpio(state.root, find_files(state.root, state.root), state.staging / state.config.output.name)
def make_directory(state: MkosiState) -> None:
- if state.config.output_format != OutputFormat.directory or state.for_cache:
+ if state.config.output_format != OutputFormat.directory:
return
os.rename(state.root, state.staging / state.config.output.name)
# benefit that they can be signed like normal EFI binaries, and can encode everything necessary to boot a
# specific root device, including the root hash.
- if state.for_cache or state.config.bootable is False:
+ if state.config.bootable is False:
return
for kver, kimg in gen_kernel_images(state):
def configure_ssh(state: MkosiState) -> None:
- if state.for_cache or not state.config.ssh:
+ if not state.config.ssh:
return
state.root.joinpath("etc/systemd/system/ssh.socket").write_text(
def configure_initrd(state: MkosiState) -> None:
- if state.for_cache or not state.config.make_initrd:
+ if not state.config.make_initrd:
return
if not state.root.joinpath("init").exists():
def run_kernel_install(state: MkosiState) -> None:
- if state.for_cache:
- return
-
if state.config.initrds:
return
def run_sysusers(state: MkosiState) -> None:
- if state.for_cache:
- return
-
with complete_step("Generating system users"):
run(["systemd-sysusers", "--root", state.root])
def run_preset_all(state: MkosiState) -> None:
- if state.for_cache:
- return
-
with complete_step("Applying presets…"):
run(["systemctl", "--root", state.root, "preset-all"])
def run_selinux_relabel(state: MkosiState) -> None:
- if state.for_cache:
- return
-
selinux = state.root / "etc/selinux/config"
if not selinux.exists():
return
final, build = cache_tree_paths(state.config)
if not final.exists() or (state.config.build_script and not build.exists()):
return False
- if state.for_cache and final.exists() and (not state.config.build_script or build.exists()):
- return True
with complete_step("Copying cached trees"):
copy_path(final, state.root)
def invoke_repart(state: MkosiState, skip: Sequence[str] = [], split: bool = False) -> Optional[str]:
- if not state.config.output_format == OutputFormat.disk or state.for_cache:
+ if not state.config.output_format == OutputFormat.disk:
return None
cmdline: list[PathString] = [
return f"roothash={roothash}" if roothash else f"usrhash={usrhash}" if usrhash else None
-def build_image(state: MkosiState, *, manifest: Optional[Manifest] = None) -> None:
+def build_image(state: MkosiState, *, for_cache: bool, manifest: Optional[Manifest] = None) -> None:
with mount_image(state):
- cached = reuse_cache_tree(state)
- install_base_trees(state, cached)
- install_skeleton_trees(state, cached)
- install_distribution(state, cached)
- run_prepare_script(state, cached, build=False)
- install_build_packages(state, cached)
- run_prepare_script(state, cached, build=True)
+ cached = reuse_cache_tree(state) if not for_cache else False
+
+ if not cached:
+ install_base_trees(state)
+ install_skeleton_trees(state)
+ install_distribution(state)
+ run_prepare_script(state, build=False)
+ install_build_packages(state)
+ run_prepare_script(state, build=True)
+
+ if for_cache:
+ return
+
configure_root_password(state)
configure_autologin(state)
configure_initrd(state)
def run_build_script(state: MkosiState) -> None:
- if state.config.build_script is None or state.for_cache:
+ if state.config.build_script is None:
return
# Make sure that if mkosi.installdir/ is used, any leftover files from a previous run are removed.
config=config,
workspace=workspace_dir,
cache=cache,
- for_cache=False,
)
manifest = Manifest(config)
# If caching is requested, then make sure we have cache trees around we can make use of
if need_cache_tree(state):
with complete_step("Building cache image"):
- state = dataclasses.replace(state, for_cache=True)
- build_image(state)
+ build_image(state, for_cache=True)
save_cache(state)
with complete_step("Building image"):
- state = dataclasses.replace(state, for_cache=False)
- build_image(state, manifest=manifest)
+ state = dataclasses.replace(state, )
+ build_image(state, manifest=manifest, for_cache=False)
copy_nspawn_settings(state)
calculate_sha256sum(state)