From cb6cd3bc4c7fa9f156db0aaccf096a1ae74d0811 Mon Sep 17 00:00:00 2001 From: Caleb Connolly Date: Sat, 22 Jun 2024 18:59:05 +0200 Subject: [PATCH] chroot: apk: safety net when chroot unitialized (MR 2252) We recently changed how we manage chroots, requiring the user (of the chroot) to initialize it before doing stuff like installing packages. There are however still certain edgecases in pmbootstrap where this doesn't get done (for example qemu/run.py would attempt to install the kernel package for the device, but we don't initialize the rootfs chroot in QEMU since that doesn't make sense to do). Check for and catch this situation explicitly so we don't ruin someones day, but still be loud about it so we can hopefully catch the remaining instances of this. Signed-off-by: Caleb Connolly --- pmb/chroot/apk.py | 10 ++++++++++ pmb/chroot/other.py | 2 ++ pmb/core/chroot.py | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/pmb/chroot/apk.py b/pmb/chroot/apk.py index 85d26f63..ee23bfda 100644 --- a/pmb/chroot/apk.py +++ b/pmb/chroot/apk.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import os from pathlib import Path +import traceback import pmb.chroot.apk_static from pmb.core.arch import Arch from pmb.helpers import logging @@ -204,6 +205,15 @@ def install_run_apk(to_add: List[str], to_add_local: List[Path], to_del: List[st channel = pmb.config.pmaports.read_config()["channel"] user_repo = work / "packages" / channel + # There are still some edgecases where we manage to get here while the chroot is not + # initialized. To not break the build, we initialize it here but print a big warning + # and a stack trace so hopefully folks report it. + if not chroot.is_mounted(): + logging.warning(f"({chroot}) chroot not initialized! This is a bug! Please report it.") + logging.warning(f"({chroot}) initializing the chroot for you...") + traceback.print_stack(file=logging.logfd) + pmb.chroot.init(chroot) + for (i, command) in enumerate(commands): # --no-interactive is a parameter to `add`, so it must be appended or apk # gets confused diff --git a/pmb/chroot/other.py b/pmb/chroot/other.py index cb676309..726dca74 100644 --- a/pmb/chroot/other.py +++ b/pmb/chroot/other.py @@ -23,6 +23,8 @@ def kernel_flavor_installed(chroot: Chroot, autoinstall=True): """ # Automatically install the selected kernel if autoinstall: + if not chroot.is_mounted(): + pmb.chroot.init(chroot) config = get_context().config packages = ([f"device-{config.device}"] + pmb.install.get_kernel_package(config)) diff --git a/pmb/core/chroot.py b/pmb/core/chroot.py index dcce2d1f..7ac98736 100644 --- a/pmb/core/chroot.py +++ b/pmb/core/chroot.py @@ -91,6 +91,10 @@ class Chroot: return (self / "bin/sh").is_symlink() + def is_mounted(self) -> bool: + return self.exists() and pmb.helpers.mount.ismount(self.path / "etc/apk/keys") + + @property def arch(self) -> Arch: if self.type == ChrootType.NATIVE: