kupferbootstrap/chroot/device.py
InsanePrawn 9ff6f24489 chroot/: fix class vars vs instance vars
looking at you, Chroot.active_mounts = []
2022-03-03 16:54:43 +01:00

60 lines
2.3 KiB
Python

import atexit
import os
from constants import Arch, BASE_PACKAGES
from distro.distro import get_kupfer_local, get_kupfer_https
from utils import check_findmnt
from typing import Optional
from .base import BaseChroot
from .build import BuildChroot
from .abstract import get_chroot
class DeviceChroot(BuildChroot):
copy_base: bool = False
def create_rootfs(self, reset, pacman_conf_target, active_previously):
clss = BuildChroot if self.copy_base else BaseChroot
clss.create_rootfs(self, reset, pacman_conf_target, active_previously)
def mount_rootfs(self, source_path: str, fs_type: str = None, options: list[str] = [], allow_overlay: bool = False):
if self.active:
raise Exception(f'{self.name}: Chroot is marked as active, not mounting a rootfs over it.')
if not os.path.exists(source_path):
raise Exception('Source does not exist')
if not allow_overlay:
really_active = []
for mnt in self.active_mounts:
if check_findmnt(self.get_path(mnt)):
really_active.append(mnt)
if really_active:
raise Exception(f'{self.name}: Chroot has submounts active: {really_active}')
if os.path.ismount(self.path):
raise Exception(f'{self.name}: There is already something mounted at {self.path}, not mounting over it.')
if os.path.exists(os.path.join(self.path, 'usr/bin')):
raise Exception(f'{self.name}: {self.path}/usr/bin exists, not mounting over existing rootfs.')
os.makedirs(self.path, exist_ok=True)
atexit.register(self.deactivate)
self.mount(source_path, '/', fs_type=fs_type, options=options)
def get_device_chroot(
device: str,
flavour: str,
arch: Arch,
packages: list[str] = BASE_PACKAGES,
use_local_repos: bool = True,
extra_repos: Optional[dict] = None,
**kwargs,
) -> DeviceChroot:
name = f'rootfs_{device}-{flavour}'
repos = dict(get_kupfer_local(arch).repos if use_local_repos else get_kupfer_https(arch).repos)
repos.update(extra_repos or {})
default = DeviceChroot(name, arch, initialize=False, copy_base=False, base_packages=packages, extra_repos=repos)
chroot = get_chroot(name, **kwargs, extra_repos=repos, default=default)
assert isinstance(chroot, DeviceChroot)
return chroot