return flatten(["--bind", src, target] for src, target in sorted(set(sources), key=lambda s: s[1]))
+def script_maybe_chroot(script: Path, mountpoint: str) -> list[str]:
+ return ["mkosi-chroot", mountpoint] if script.suffix == ".chroot" else [os.fspath(script)]
+
+
def run_prepare_scripts(state: MkosiState, build: bool) -> None:
if not state.config.prepare_scripts:
return
with complete_step(step_msg.format(script)):
bwrap(
- [script, arg],
+ script_maybe_chroot(script, "/work/prepare") + [arg],
network=True,
readonly=True,
options=finalize_mounts(state.config),
],
)
+ cmdline = state.args.cmdline if state.args.verb == Verb.build else []
+
with complete_step(f"Running build script {script}…"):
bwrap(
- [script, *(state.args.cmdline if state.args.verb == Verb.build else [])],
+ script_maybe_chroot(script, "/work/build-script") + cmdline,
network=state.config.with_network,
readonly=True,
options=finalize_mounts(state.config),
with complete_step(f"Running postinstall script {script}…"):
bwrap(
- [script, "final"],
+ script_maybe_chroot(script, "/work/postinst") + ["final"],
network=state.config.with_network,
readonly=True,
options=finalize_mounts(state.config),
with complete_step(f"Running finalize script {script}…"):
bwrap(
- [script],
+ script_maybe_chroot(script, "/work/finalize"),
network=state.config.with_network,
readonly=True,
options=finalize_mounts(state.config),
metavar="PATH",
section="Content",
parse=config_make_list_parser(delimiter=",", parse=make_path_parser(executable=True)),
- paths=("mkosi.prepare",),
+ paths=("mkosi.prepare", "mkosi.prepare.chroot"),
path_default=False,
help="Prepare script to run inside the image before it is cached",
compat_names=("PrepareScript",),
metavar="PATH",
section="Content",
parse=config_make_list_parser(delimiter=",", parse=make_path_parser(executable=True)),
- paths=("mkosi.build",),
+ paths=("mkosi.build", "mkosi.build.chroot"),
path_default=False,
help="Build script to run inside image",
compat_names=("BuildScript",),
name="PostInstallationScripts",
section="Content",
parse=config_make_list_parser(delimiter=",", parse=make_path_parser(executable=True)),
- paths=("mkosi.postinst",),
+ paths=("mkosi.postinst", "mkosi.postinst.chroot"),
path_default=False,
help="Postinstall script to run inside image",
compat_names=("PostInstallationScript",),
metavar="PATH",
section="Content",
parse=config_make_list_parser(delimiter=",", parse=make_path_parser(executable=True)),
- paths=("mkosi.finalize",),
+ paths=("mkosi.finalize", "mkosi.finalize.chroot"),
path_default=False,
help="Postinstall script to run outside image",
compat_names=("FinalizeScript",),
* If **`mkosi.finalize`** (`FinalizeScripts=`) exists, it is executed as
the last step of preparing an image.
+If a script uses the `.chroot` extension, mkosi will chroot into the
+image using `mkosi-chroot` (see below) before executing the script. For
+example, if `mkosi.postinst.chroot` exists, mkosi will chroot into the
+image and execute it as the post-installation script.
+
Scripts executed by mkosi receive the following environment variables:
* `$CHROOT_SCRIPT` contains the path to the running script relative to