from collections.abc import Sequence
from pathlib import Path
from textwrap import dedent
-from typing import Optional
from mkosi.architecture import Architecture
from mkosi.distributions import DistributionInstaller
def invoke_emerge(
state: MkosiState,
- sysroot: Optional[Path] = None,
- bwrap_params: list[PathString] = [],
+ sysroot: Path,
pkgs: Sequence[str] = (),
actions: Sequence[str] = (),
opts: Sequence[str] = (),
env: dict[str, str] = {},
) -> None:
thread_counts = (os.cpu_count() or 1) * 2 # * 2 for hyperthreading
- bwrap: list[PathString] = []
- root: Optional[Path] = None
- if sysroot is not None:
- # This is the mount-point inside our sysroot where we mount root
- target_root_mntp = "/tmp/mkosi-root"
- bwrap += ["--bind", state.root, target_root_mntp]
- root = Path(target_root_mntp)
- else:
- sysroot = state.root
-
- emerge_default_opts = [
+ # This is the mount-point inside our sysroot where we mount root
+ target_root_mntp = "/tmp/mkosi-root"
+ bwrap: list[PathString] = ["--bind", state.root, target_root_mntp]
+ root = Path(target_root_mntp)
+
+ cmd = [
+ "emerge",
+ *pkgs,
"--buildpkg=y",
"--usepkg=y",
"--keep-going=y",
*([f"--root={root}"] if root else []),
]
if ARG_DEBUG.get():
- emerge_default_opts += ["--verbose", "--quiet=n", "--quiet-fail=n"]
+ cmd += ["--verbose", "--quiet=n", "--quiet-fail=n"]
else:
- emerge_default_opts += ["--quiet-build", "--quiet"]
- cmd = ["emerge", *pkgs, *emerge_default_opts, *opts, *actions]
+ cmd += ["--quiet-build", "--quiet"]
+ cmd += [*opts, *actions]
bwrap += [
"--bind", state.cache_dir / "binpkgs", "/var/cache/binpkgs",
"--bind", state.cache_dir / "distfiles", "/var/cache/distfiles",
"--bind", state.cache_dir / "repos", "/var/db/repos",
- *bwrap_params
]
run_workspace_command(sysroot, cmd, bwrap_params=bwrap, network=True,
env=env)
"""usrmerge tracker bug: https://bugs.gentoo.org/690294"""
- gentoo_mirrors = "http://distfiles.gentoo.org"
- if state.config.mirror:
- gentoo_mirrors = state.config.mirror
+ assert state.config.mirror
# http://distfiles.gentoo.org/releases/amd64/autobuilds/latest-stage3.txt
stage3tsf_path_url = urllib.parse.urljoin(
- gentoo_mirrors.partition(" ")[0],
+ state.config.mirror.partition(" ")[0],
f"releases/{arch}/autobuilds/latest-stage3.txt",
)
user_config_path = cls.stage3_cache / USER_CONFIG_PATH
- emerge_vars = {
- "FEATURES": " ".join([
- # -user* are required for access to USER_CONFIG_PATH
- "-userfetch",
- "-userpriv",
- "-usersync",
- "-usersandbox",
- "-sandbox",
- "-pid-sandbox", # for cross-compile scenarios
- "-network-sandbox",
- "parallel-install",
- "getbinpkg",
- "-candy",
- "noman",
- "nodoc",
- "noinfo",
- ]),
- "USE": "initramfs symlink"
- }
-
for d in ("binpkgs", "distfiles", "repos"):
state.cache_dir.joinpath(d).mkdir(exist_ok=True)
stage3_url_path = urllib.parse.urljoin(
- gentoo_mirrors, f"releases/{arch}/autobuilds/{stage3_tar}",
+ state.config.mirror, f"releases/{arch}/autobuilds/{stage3_tar}",
)
if not stage3_tar_path.is_file():
log_step(f"Fetching {stage3_url_path}")
cls.stage3_cache.mkdir(parents=True, exist_ok=True)
- log_step(f"Extracting {stage3_tar.name} to {cls.stage3_cache}")
- run([
- "tar",
- "--numeric-owner",
- "-C", cls.stage3_cache,
- "--extract",
- "--file", stage3_tar_path,
- "--exclude", "./dev",
- ])
- unlink_try_hard(cls.stage3_cache.joinpath("dev"))
- unlink_try_hard(cls.stage3_cache.joinpath("proc"))
- unlink_try_hard(cls.stage3_cache.joinpath("sys"))
+ with complete_step(f"Extracting {stage3_tar.name} to {cls.stage3_cache}"):
+ run([
+ "tar",
+ "--numeric-owner",
+ "-C", cls.stage3_cache,
+ "--extract",
+ "--file", stage3_tar_path,
+ "--exclude", "./dev",
+ ])
+
+ unlink_try_hard(cls.stage3_cache.joinpath("dev"))
+ unlink_try_hard(cls.stage3_cache.joinpath("proc"))
+ unlink_try_hard(cls.stage3_cache.joinpath("sys"))
package_use = user_config_path / "package.use"
package_use.mkdir(exist_ok=True)
"""
)
)
- if state.config.make_initrd:
- package_use.joinpath("minimal").write_text(
- dedent(
- """\
- # MKOSI
- */* minimal
- """
- )
+ package_use.joinpath("minimal").write_text(
+ dedent(
+ """\
+ # MKOSI
+ */* minimal
+ """
)
+ )
package_env = user_config_path / "package.env"
package_env.mkdir(exist_ok=True)
# we use this so we don't need to touch upstream files.
# we also use this for documenting build environment.
+ emerge_vars = {
+ "FEATURES": " ".join([
+ # -user* are required for access to USER_CONFIG_PATH
+ "-userfetch",
+ "-userpriv",
+ "-usersync",
+ "-usersandbox",
+ "-sandbox",
+ "-pid-sandbox", # for cross-compile scenarios
+ "-network-sandbox",
+ "parallel-install",
+ "getbinpkg",
+ "-candy",
+ *(["noman", "nodoc", "noinfo"] if state.config.with_docs else []),
+ ]),
+ "USE": "initramfs symlink"
+ }
emerge_vars_str = ""
emerge_vars_str += "\n".join(f'{k}="${{{k}}} {v}"' for k, v in emerge_vars.items())
)
)
- repos_cfg = cls.stage3_cache / BINREPOS_CONF_FILE
- with repos_cfg.open(mode='a') as f:
+ with (cls.stage3_cache / BINREPOS_CONF_FILE).open(mode='a') as f:
for repo in state.config.repositories:
f.write(
dedent(
"--verbose-conflicts",
"--changed-use",
"--newuse",
+ "--root-deps=rdeps",
]
- env = {}
- env.update(emerge_vars)
- env.update({"USE": f"{emerge_vars['USE']} build"})
- with complete_step("merging stage2"):
- invoke_emerge(state, sysroot=cls.stage3_cache, opts=opts+["--emptytree", "--nodeps"],
- pkgs=["sys-apps/baselayout", "sys-apps/util-linux"], env=env)
- opts += ["--noreplace", "--root-deps=rdeps"]
+ with complete_step("Merging stage2"):
+ invoke_emerge(state, sysroot=cls.stage3_cache,
+ opts=opts+["--emptytree", "--nodeps"],
+ pkgs=["sys-apps/baselayout"],
+ env={**emerge_vars, 'USE': 'build'})
+
+ opts += ["--noreplace"]
+
with complete_step("Merging bare minimal atoms"):
- invoke_emerge(state, sysroot=cls.stage3_cache, opts=opts+["--exclude", "sys-devel/*"],
- pkgs=["app-shells/bash", "sys-apps/systemd"], env=emerge_vars)
- with complete_step("Merging atoms required for boot"):
- invoke_emerge(state, sysroot=cls.stage3_cache, opts=opts,
- pkgs=["sys-kernel/gentoo-kernel-bin"])
+ invoke_emerge(state, sysroot=cls.stage3_cache,
+ opts=opts+["--exclude", "sys-devel/*"],
+ pkgs=["app-shells/bash", "sys-apps/systemd"],
+ env=emerge_vars)
+
if state.config.make_initrd:
return
+ invoke_emerge(state, sysroot=cls.stage3_cache, opts=opts,
+ pkgs=["sys-kernel/gentoo-kernel-bin"])
invoke_emerge(state, sysroot=cls.stage3_cache, opts=opts,
pkgs=["@system"])