kupferbootstrap/chroot/__init__.py

61 lines
2 KiB
Python
Raw Permalink Normal View History

import click
import logging
import os
from config import config
from wrapper import enforce_wrap
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
from .helpers import get_chroot_path
2021-10-19 06:40:30 +02:00
2022-02-17 21:49:24 +01:00
# export Chroot class
Chroot = Chroot
@click.command('chroot')
@click.argument('type', required=False, default='build')
@click.argument('arch', required=False, default=None)
def cmd_chroot(type: str = 'build', arch: str = None, enable_crossdirect=True):
2022-02-13 19:57:04 +01:00
"""Open a shell in a chroot"""
chroot_path = ''
if type not in ['base', 'build', 'rootfs']:
raise Exception('Unknown chroot type: ' + type)
enforce_wrap()
2022-02-18 06:32:04 +01:00
chroot: Chroot
if type == 'rootfs':
if arch:
name = 'rootfs_' + arch
else:
2021-10-19 06:40:30 +02:00
raise Exception('"rootfs" without args not yet implemented, sorry!')
# TODO: name = config.get_profile()[...]
2022-02-18 06:32:04 +01:00
chroot_path = get_chroot_path(name)
if not os.path.exists(chroot_path):
raise Exception(f"rootfs {name} doesn't exist")
else:
if not arch:
2022-02-18 06:32:04 +01:00
# TODO: arch = config.get_profile()[...]
arch = 'aarch64'
2021-10-19 06:40:30 +02:00
if type == 'base':
chroot = get_base_chroot(arch)
2022-02-18 06:32:04 +01:00
if not os.path.exists(chroot.get_path('/bin')):
chroot.initialize()
2021-10-19 06:40:30 +02:00
chroot.initialized = True
elif type == 'build':
2022-02-18 06:32:04 +01:00
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()
2021-10-19 06:40:30 +02:00
if config.file['build']['crossdirect'] and enable_crossdirect:
2022-02-18 06:32:04 +01:00
build_chroot.mount_crossdirect()
2021-10-19 06:40:30 +02:00
else:
raise Exception('Really weird bug')
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)