2021-10-01 18:25:42 +02:00
|
|
|
import click
|
2021-08-06 02:21:50 +02:00
|
|
|
import logging
|
2021-08-06 05:24:06 +02:00
|
|
|
import os
|
2021-10-24 04:32:47 +02:00
|
|
|
|
2022-11-09 15:22:06 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-10-08 04:04:27 +02:00
|
|
|
from config.state import config
|
2021-10-01 18:25:42 +02:00
|
|
|
from wrapper import enforce_wrap
|
2022-10-08 04:13:20 +02:00
|
|
|
from devices.device import get_profile_device
|
2021-10-01 18:25:42 +02:00
|
|
|
|
2022-02-18 06:46:05 +01:00
|
|
|
from .abstract import Chroot
|
|
|
|
from .base import get_base_chroot
|
2022-02-18 06:32:04 +01:00
|
|
|
from .build import get_build_chroot, BuildChroot
|
2021-10-19 06:40:30 +02:00
|
|
|
|
2022-09-04 06:45:35 +02:00
|
|
|
CHROOT_TYPES = ['base', 'build', 'rootfs']
|
|
|
|
|
2021-10-04 14:36:39 +02:00
|
|
|
|
2021-10-01 18:25:42 +02:00
|
|
|
@click.command('chroot')
|
2022-09-04 06:45:35 +02:00
|
|
|
@click.argument('type', required=False, type=click.Choice(CHROOT_TYPES), default='build')
|
|
|
|
@click.argument(
|
|
|
|
'name',
|
|
|
|
required=False,
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
@click.pass_context
|
2022-11-09 15:22:06 +01:00
|
|
|
def cmd_chroot(ctx: click.Context, type: str = 'build', name: Optional[str] = None, enable_crossdirect=True):
|
2022-09-04 06:45:35 +02:00
|
|
|
"""Open a shell in a chroot. For rootfs NAME is a profile name, for others the architecture (e.g. aarch64)."""
|
|
|
|
|
|
|
|
if type not in CHROOT_TYPES:
|
|
|
|
raise Exception(f'Unknown chroot type: "{type}"')
|
|
|
|
|
|
|
|
if type == 'rootfs':
|
2022-10-16 23:25:47 +02:00
|
|
|
from image.image import cmd_inspect
|
2022-09-04 06:45:35 +02:00
|
|
|
assert isinstance(cmd_inspect, click.Command)
|
|
|
|
ctx.invoke(cmd_inspect, profile=name, shell=True)
|
|
|
|
return
|
2021-10-01 18:25:42 +02:00
|
|
|
|
|
|
|
enforce_wrap()
|
2022-09-04 06:45:35 +02:00
|
|
|
|
2022-02-18 06:32:04 +01:00
|
|
|
chroot: Chroot
|
2022-09-04 06:45:35 +02:00
|
|
|
arch = name
|
|
|
|
if not arch:
|
2022-10-08 04:13:20 +02:00
|
|
|
arch = get_profile_device().arch
|
2022-09-04 06:45:35 +02:00
|
|
|
assert arch
|
|
|
|
if type == 'base':
|
|
|
|
chroot = get_base_chroot(arch)
|
|
|
|
if not os.path.exists(chroot.get_path('/bin')):
|
|
|
|
chroot.initialize()
|
|
|
|
chroot.initialized = True
|
|
|
|
elif type == 'build':
|
|
|
|
build_chroot: BuildChroot = get_build_chroot(arch, activate=True)
|
|
|
|
chroot = build_chroot # type safety
|
|
|
|
if not os.path.exists(build_chroot.get_path('/bin')):
|
|
|
|
build_chroot.initialize()
|
|
|
|
build_chroot.initialized = True
|
|
|
|
build_chroot.mount_pkgbuilds()
|
|
|
|
build_chroot.mount_chroots()
|
|
|
|
assert arch and config.runtime.arch
|
|
|
|
if config.file.build.crossdirect and enable_crossdirect and arch != config.runtime.arch:
|
|
|
|
build_chroot.mount_crossdirect()
|
2021-10-01 18:25:42 +02:00
|
|
|
else:
|
2022-09-04 06:45:35 +02:00
|
|
|
raise Exception('Really weird bug')
|
2021-10-19 06:40:30 +02:00
|
|
|
|
2022-08-29 00:53:09 +02:00
|
|
|
chroot.mount_packages()
|
2022-01-28 19:04:23 +01:00
|
|
|
chroot.activate()
|
2021-10-19 06:40:30 +02:00
|
|
|
logging.debug(f'Starting shell in {chroot.name}:')
|
|
|
|
chroot.run_cmd('bash', attach_tty=True)
|