chroot: add --usb flag to make usb devices available (MR 2554)

Add a flag to bind-mount in the necessary sysfs directories for USB
devices to be accessed inside the chroot. This is the same as how we do
it for "pmbootstrap flasher" but allows running arbitrary commands in
the chroot.

This is useful to allow using pmbootstrap chroot's as a sandbox for
custom flashing procedures.

Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
Caleb Connolly 2025-02-16 20:49:04 +00:00 committed by Oliver Smith
parent 192a29fb1b
commit 67773c4293
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
3 changed files with 13 additions and 0 deletions

View file

@ -27,6 +27,7 @@ import pmb.helpers.devices
import pmb.helpers.git import pmb.helpers.git
import pmb.helpers.lint import pmb.helpers.lint
import pmb.helpers.logging import pmb.helpers.logging
import pmb.helpers.mount
import pmb.helpers.pmaports import pmb.helpers.pmaports
import pmb.helpers.repo import pmb.helpers.repo
import pmb.helpers.repo_missing import pmb.helpers.repo_missing
@ -211,6 +212,11 @@ def chroot(args: PmbArgs) -> None:
size_reserve = 2048 # 2 GiB size_reserve = 2048 # 2 GiB
pmb.install.blockdevice.create_and_mount_image(args, size_boot, size_root, size_reserve) pmb.install.blockdevice.create_and_mount_image(args, size_boot, size_root, size_reserve)
# Bind mount in sysfs dirs to accessing USB devices (e.g. for running fastboot)
if args.chroot_usb:
for folder in pmb.config.flash_mount_bind:
pmb.helpers.mount.bind(folder, Chroot.native() / folder)
pmb.helpers.apk.update_repository_list(chroot.path, user_repository=True) pmb.helpers.apk.update_repository_list(chroot.path, user_repository=True)
# Run the command as user/root # Run the command as user/root

View file

@ -1102,6 +1102,12 @@ def get_parser() -> argparse.ArgumentParser:
help="Mount the rootfs image and treat it like a normal chroot.", help="Mount the rootfs image and treat it like a normal chroot.",
action="store_true", action="store_true",
) )
chroot.add_argument(
"--usb",
help="Make USB devices accessible inside the chroot.",
action="store_true",
dest="chroot_usb",
)
chroot.add_argument( chroot.add_argument(
"command", "command",
default=["sh", "-i"], default=["sh", "-i"],

View file

@ -78,6 +78,7 @@ class PmbArgs(Namespace):
built: bool built: bool
ccache: bool ccache: bool
ccache_size: str ccache_size: str
chroot_usb: bool
cipher: str cipher: str
clear_log: bool clear_log: bool
cmdline: str cmdline: str