Skip to content
Snippets Groups Projects
Unverified Commit f228c84f authored by Casey's avatar Casey :recycle:
Browse files

chroot: apk: use apk.static in all cases


In 0b4fb911 (chroot: always run apk static v2 (MR 2423)) we adjusted
install_run_apk() to run apk static on the host and pass in the local
binary repo with "--repository". This function can call apk in two ways,
either with the progress bar handling or without, the second case was
never updated and still ran apk inside the chroot incorrectly.

Signed-off-by: default avatarCaleb Connolly <caleb@postmarketos.org>
parent 0264bfe5
No related branches found
No related tags found
No related merge requests found
Pipeline #209092 passed
......@@ -111,9 +111,7 @@ def packages_get_locally_built_apks(package_list: list[str], arch: Arch) -> list
for channel in channels:
apk_path = get_context().config.work / "packages" / channel / arch / apk_file
if apk_path.exists():
# FIXME: use /mnt/pmb… until MR 2351 is reverted (pmb#2388)
# local.append(apk_path)
local.append(Path("/mnt/pmbootstrap/packages/") / channel / arch / apk_file)
local.append(apk_path)
break
# Record all the packages we have visited so far
......@@ -190,13 +188,12 @@ def install_run_apk(to_add: list[str], to_add_local: list[Path], to_del: list[st
if context.offline:
command = ["--no-network"] + command
if i == 0:
pmb.helpers.apk.apk_with_progress(command, chroot)
else:
# Virtual package related commands don't actually install or remove
# packages, but only mark the right ones as explicitly installed.
# They finish up almost instantly, so don't display a progress bar.
pmb.chroot.root(["apk", "--no-progress"] + command, chroot)
# Virtual package related commands don't actually install or remove
# packages, but only mark the right ones as explicitly installed.
# So only display a progress bar for the "apk add" command which is
# always the first one we process (i == 0).
pmb.helpers.apk.run(command, with_progress=(i == 0), chroot=chroot)
def install(packages, chroot: Chroot, build=True, quiet: bool = False):
......
......@@ -179,11 +179,12 @@ def init() -> None:
extract(version, apk_static)
def run(parameters):
def run(parameters, with_progress=True):
# --no-interactive is a parameter to `add`, so it must be appended or apk
# gets confused
parameters += ["--no-interactive"]
if "add" in parameters:
parameters += ["--no-interactive"]
if get_context().offline:
parameters = ["--no-network"] + parameters
pmb.helpers.apk.apk_with_progress(parameters)
pmb.helpers.apk.run(parameters, with_progress=with_progress)
......@@ -17,6 +17,7 @@ import pmb.helpers.run
import pmb.helpers.other
from pmb.core import Chroot, ChrootType
from pmb.core.context import get_context
from pmb.types import PathString
class UsrMerge(enum.Enum):
......@@ -159,10 +160,18 @@ def init(chroot: Chroot, usr_merge=UsrMerge.AUTO):
# Install alpine-base
pmb.helpers.repo.update(arch)
pkgs = ["alpine-base"]
cmd = ["--root", chroot.path, "--cache-dir", apk_cache, "--initdb", "--arch", arch]
cmd: list[PathString] = [
"--root",
chroot.path,
"--cache-dir",
apk_cache,
"--initdb",
"--arch",
arch,
]
for channel in pmb.config.pmaports.all_channels():
cmd += ["--repository", config.work / "packages" / channel]
pmb.chroot.apk_static.run(cmd + ["add", *pkgs])
pmb.helpers.apk.run(cmd + ["add", *pkgs])
# Merge /usr
if usr_merge is UsrMerge.AUTO and pmb.config.is_systemd_selected(config):
......
......@@ -51,7 +51,7 @@ def delete(hook, suffix: Chroot):
if hook not in list_chroot(suffix):
raise RuntimeError("There is no such hook installed!")
prefix = pmb.config.initfs_hook_prefix
pmb.chroot.root(["apk", "del", f"{prefix}{hook}"], suffix)
pmb.helpers.apk.run(["del", f"{prefix}{hook}"], suffix)
def update(suffix: Chroot):
......
......@@ -184,18 +184,6 @@ def zap_pkgs_online_mismatch(confirm=True, dry=False):
# Iterate over existing apk caches
for path in paths:
arch = Arch.from_str(path.name.split("_", 2)[2])
if arch.is_native():
chroot = Chroot.native()
else:
chroot = Chroot.buildroot(arch)
# Skip if chroot does not exist
# FIXME: should we init the buildroot to do it anyway?
# what if we run apk.static with --arch instead?
if not chroot.exists():
continue
# Clean the cache with apk
logging.info(f"({chroot}) apk -v cache clean")
if not dry:
pmb.chroot.root(["apk", "-v", "cache", "clean"], chroot)
pmb.helpers.apk.cache_clean(arch)
......@@ -99,7 +99,7 @@ def _create_command_with_progress(command, fifo):
:param fifo: path of the fifo
:returns: full command in list form
"""
flags = ["--no-progress", "--progress-fd", "3"]
flags = ["--progress-fd", "3"]
command_full = [command[0]] + flags + command[1:]
command_flat = pmb.helpers.run_core.flat_cmd([command_full])
command_flat = f"exec 3>{fifo}; {command_flat}"
......@@ -122,23 +122,15 @@ def _compute_progress(line):
return cur / tot if tot > 0 else 0
def apk_with_progress(command: Sequence[PathString], chroot: Chroot | None = None):
def _apk_with_progress(command: Sequence[str]):
"""Run an apk subcommand while printing a progress bar to STDOUT.
:param command: apk subcommand in list form
:raises RuntimeError: when the apk command fails
"""
fifo = _prepare_fifo()
_command: list[str] = [str(get_context().config.work / "apk.static")]
if chroot:
_command.extend(["--root", str(chroot.path), "--arch", str(chroot.arch)])
for c in command:
if isinstance(c, Arch):
_command.append(str(c))
else:
_command.append(os.fspath(c))
command_with_progress = _create_command_with_progress(_command, fifo)
log_msg = " ".join(_command)
command_with_progress = _create_command_with_progress(command, fifo)
log_msg = " ".join(command)
with pmb.helpers.run.root(["cat", fifo], output="pipe") as p_cat:
with pmb.helpers.run.root(command_with_progress, output="background") as p_apk:
while p_apk.poll() is None:
......@@ -149,6 +141,103 @@ def apk_with_progress(command: Sequence[PathString], chroot: Chroot | None = Non
pmb.helpers.run_core.check_return_code(p_apk.returncode, log_msg)
def _prepare_cmd(command: Sequence[PathString], chroot: Chroot | None) -> list[str]:
"""Prepare the apk command.
Returns a tuple of the first part of the command with generic apk flags, and the second part
with the subcommand and its arguments.
"""
# Our _apk_with_progress() wrapper also need --no-progress, since all that does is
# prevent apk itself from rendering progress bars. We instead want it to tell us
# the progress so we can render it. So we always set --no-progress.
_command: list[str] = [str(get_context().config.work / "apk.static"), "--no-progress"]
if chroot:
_command.extend(["--root", str(chroot.path), "--arch", str(chroot.arch)])
if get_context().offline:
_command.append("--no-network")
for c in command:
if isinstance(c, Arch):
_command.append(str(c))
else:
_command.append(os.fspath(c))
# Always be non-interactive
if c == "add":
_command.append("--no-interactive")
return _command
def run(command: Sequence[PathString], with_progress=True, chroot: Chroot | None = None):
"""Run an apk subcommand.
:param command: apk subcommand in list form
:param with_progress: whether to print a progress bar
:param chroot: chroot to run the command in
:raises RuntimeError: when the apk command fails
"""
_command = _prepare_cmd(command, chroot)
if with_progress:
_apk_with_progress(_command)
else:
pmb.helpers.run.root(_command)
def cache_clean(arch: Arch):
"""Clean the APK cache for a specific architecture."""
work = get_context().config.work
cache_dir = work / f"cache_apk_{arch}"
if not cache_dir.exists():
return
# The problem here is that apk's "cache clean" command really
# expects to be run against an apk-managed rootfs and will return
# errors if it can't access certain paths (even though it will
# actually clean the cache like we want).
# We could just ignore the return value, but then we wouldn't know
# if something actually went wrong with apk...
# So we do this dance of creating a rootfs with only the files that
# APK needs to be happy
tmproot = work / "tmp" / "apk_root"
if not (tmproot / "etc/apk/repositories").exists():
tmproot.mkdir(exist_ok=True)
(tmproot / "var/cache").mkdir(exist_ok=True, parents=True)
(tmproot / "etc/apk").mkdir(exist_ok=True, parents=True)
(tmproot / "lib/apk/db").mkdir(exist_ok=True, parents=True)
# (tmproot / "var/cache/apk/installed").touch(exist_ok=True)
(tmproot / "etc/apk/world").touch(exist_ok=True)
(tmproot / "lib/apk/db/installed").touch(exist_ok=True)
(tmproot / "lib/apk/db/triggers").touch(exist_ok=True)
(tmproot / "etc/apk/keys").symlink_to(work / "config_apk_keys")
# Our fake rootfs needs a valid repositories file for apk
# to have something to compare the cache against
update_repository_list(tmproot)
# Point our tmproot cache dir to the real cache dir
# this is much simpler than passing --cache-dir to apk
# since even with that flag apk will also check it's
# "static cache".
(tmproot / "var/cache/apk").unlink(missing_ok=True)
(tmproot / "var/cache/apk").symlink_to(cache_dir)
command: list[PathString] = [
"-v",
"--root",
tmproot,
"--arch",
str(arch),
]
command += ["cache", "clean"]
_command = _prepare_cmd(command, None)
pmb.helpers.run.root(_command)
def check_outdated(version_installed, action_msg):
"""Check if the provided alpine version is outdated.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment