SRCDIR=str(Path.cwd()),
CHROOT_SRCDIR="/work/src",
BUILDROOT=str(state.root),
+ MKOSI_UID=str(state.uid),
+ MKOSI_GID=str(state.gid),
)
chroot: list[PathString] = chroot_cmd(
OUTPUTDIR=str(state.staging),
CHROOT_OUTPUTDIR="/work/out",
BUILDROOT=str(state.root),
+ MKOSI_UID=str(state.uid),
+ MKOSI_GID=str(state.gid),
)
if state.config.build_dir is not None:
OUTPUTDIR=str(state.staging),
CHROOT_OUTPUTDIR="/work/out",
BUILDROOT=str(state.root),
+ MKOSI_UID=str(state.uid),
+ MKOSI_GID=str(state.gid),
)
chroot = chroot_cmd(
OUTPUTDIR=str(state.staging),
CHROOT_OUTPUTDIR="/work/out",
BUILDROOT=str(state.root),
+ MKOSI_UID=str(state.uid),
+ MKOSI_GID=str(state.gid),
)
chroot = chroot_cmd(
with complete_step("Building initrd"):
args, [config] = parse_config(cmdline)
unlink_output(args, config)
- build_image(args, config)
+ build_image(args, config, state.uid, state.gid)
symlink.symlink_to(config.output_dir / config.output)
os.utime(p, (mtime, mtime), follow_symlinks=False)
-def build_image(args: MkosiArgs, config: MkosiConfig) -> None:
+def build_image(args: MkosiArgs, config: MkosiConfig, uid: int, gid: int) -> None:
manifest = Manifest(config) if config.manifest_format else None
workspace = tempfile.TemporaryDirectory(dir=config.workspace_dir, prefix=".mkosi-tmp")
with workspace, scopedenv({"TMPDIR" : workspace.name}):
- state = MkosiState(args, config, Path(workspace.name))
+ state = MkosiState(args, config, Path(workspace.name), uid, gid)
install_package_manager_trees(state)
with mount_base_trees(state):
run(["mkdir", "--parents", p], user=uid, group=gid)
with acl_toggle_build(config, uid):
- build_image(args, config)
+ build_image(args, config, uid, gid)
# Make sure all build outputs that are not directories are owned by the user running mkosi.
for p in config.output_dir.iterdir():
To allow for image customization that cannot be implemented using
mkosi's builtin features, mkosi supports running scripts at various
points during the image build process that can customize the image as
-needed. Scripts are executed on the host system with a customized
-environment to simplify modifying the image. For each script, the
-configured build sources (`BuildSources=`) are mounted into the current
-working directory before running the script and `$SRCDIR` is set to
-point to the current working directory. The following scripts are
-supported:
+needed. Scripts are executed on the host system as root (either real
+root or root within the user namespace that mkosi created when running
+unprivileged) with a customized environment to simplify modifying the
+image. For each script, the configured build sources (`BuildSources=`)
+are mounted into the current working directory before running the script
+and `$SRCDIR` is set to point to the current working directory. The
+following scripts are supported:
* If **`mkosi.prepare`** (`PrepareScript=`) exists, it is first called
with the `final` argument, right after the software packages are
[SOURCE_DATE_EPOCH](https://reproducible-builds.org/specs/source-date-epoch/)
for more information.
+* `$MKOSI_UID` and `$MKOSI_GID` are the respectively the uid, gid of the
+ user that invoked mkosi, potentially translated to a uid in the user
+ namespace that mkosi is running in. These can be used in combination
+ with `setpriv` to run commands as the user that invoked mkosi (e.g.
+ `setpriv --reuid=$MKOSI_UID --regid=$MKOSI_GID --clear-groups <command>`)
+
Additionally, when a script is executed, a few scripts are made
available via `$PATH` to simplify common usecases.