image.py: use Device instead of the device name from config

This commit is contained in:
InsanePrawn 2022-08-29 23:30:00 +02:00
parent 606a7a9af3
commit 5a565662eb
3 changed files with 30 additions and 22 deletions

View file

@ -6,7 +6,8 @@ from config import config
from constants import BOOT_STRATEGIES, FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION from constants import BOOT_STRATEGIES, FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION
from exec.file import makedir from exec.file import makedir
from fastboot import fastboot_boot, fastboot_erase_dtbo from fastboot import fastboot_boot, fastboot_erase_dtbo
from image import get_device_and_flavour, losetup_rootfs_image, get_image_path, dump_aboot, dump_lk2nd from image import get_flavour, get_device_name, losetup_rootfs_image, get_image_path, dump_aboot, dump_lk2nd
from packages.device import get_profile_device
from wrapper import enforce_wrap from wrapper import enforce_wrap
LK2ND = FLASH_PARTS['LK2ND'] LK2ND = FLASH_PARTS['LK2ND']
@ -20,7 +21,8 @@ TYPES = [LK2ND, JUMPDRIVE, ABOOT]
def cmd_boot(type): def cmd_boot(type):
"""Boot JumpDrive or the Kupfer aboot image. Erases Android DTBO in the process.""" """Boot JumpDrive or the Kupfer aboot image. Erases Android DTBO in the process."""
enforce_wrap() enforce_wrap()
device, flavour = get_device_and_flavour() device = get_profile_device()
flavour = get_flavour()
# TODO: parse arch and sector size # TODO: parse arch and sector size
sector_size = 4096 sector_size = 4096
image_path = get_image_path(device, flavour) image_path = get_image_path(device, flavour)
@ -28,7 +30,7 @@ def cmd_boot(type):
if strategy == FASTBOOT: if strategy == FASTBOOT:
if type == JUMPDRIVE: if type == JUMPDRIVE:
file = f'boot-{device}.img' file = f'boot-{get_device_name(device)}.img'
path = os.path.join(config.get_path('jumpdrive'), file) path = os.path.join(config.get_path('jumpdrive'), file)
makedir(os.path.dirname(path)) makedir(os.path.dirname(path))
if not os.path.exists(path): if not os.path.exists(path):

View file

@ -6,7 +6,8 @@ from constants import FLASH_PARTS, LOCATIONS
from exec.cmd import run_root_cmd from exec.cmd import run_root_cmd
from exec.file import get_temp_dir from exec.file import get_temp_dir
from fastboot import fastboot_flash from fastboot import fastboot_flash
from image import dd_image, partprobe, shrink_fs, losetup_rootfs_image, losetup_destroy, dump_aboot, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name, get_image_path from image import dd_image, partprobe, shrink_fs, losetup_rootfs_image, losetup_destroy, dump_aboot, dump_lk2nd, dump_qhypstub, get_flavour, get_image_name, get_image_path
from packages.device import get_profile_device
from wrapper import enforce_wrap from wrapper import enforce_wrap
ABOOT = FLASH_PARTS['ABOOT'] ABOOT = FLASH_PARTS['ABOOT']
@ -21,7 +22,8 @@ ROOTFS = FLASH_PARTS['ROOTFS']
def cmd_flash(what: str, location: str): def cmd_flash(what: str, location: str):
"""Flash a partition onto a device. `location` takes either a path to a block device or one of emmc, sdcard""" """Flash a partition onto a device. `location` takes either a path to a block device or one of emmc, sdcard"""
enforce_wrap() enforce_wrap()
device, flavour = get_device_and_flavour() device = get_profile_device()
flavour = get_flavour()
device_image_name = get_image_name(device, flavour) device_image_name = get_image_name(device, flavour)
device_image_path = get_image_path(device, flavour) device_image_path = get_image_path(device, flavour)

View file

@ -7,16 +7,16 @@ import click
import logging import logging
from signal import pause from signal import pause
from subprocess import CompletedProcess from subprocess import CompletedProcess
from typing import Optional from typing import Optional, Union
from chroot.device import DeviceChroot, get_device_chroot from chroot.device import DeviceChroot, get_device_chroot
from constants import Arch, BASE_PACKAGES, DEVICES, FLAVOURS from constants import Arch, BASE_PACKAGES, FLAVOURS
from config import config, Profile from config import config, Profile
from distro.distro import get_base_distro, get_kupfer_https from distro.distro import get_base_distro, get_kupfer_https
from exec.cmd import run_root_cmd, generate_cmd_su from exec.cmd import run_root_cmd, generate_cmd_su
from exec.file import root_write_file, root_makedir, makedir from exec.file import root_write_file, root_makedir, makedir
from packages import build_enable_qemu_binfmt, build_packages_by_paths from packages import build_enable_qemu_binfmt, build_packages_by_paths
from packages.device import get_profile_device from packages.device import Device, get_profile_device
from ssh import copy_ssh_keys from ssh import copy_ssh_keys
from wrapper import check_programs_wrap, wrap_if_foreign_arch from wrapper import check_programs_wrap, wrap_if_foreign_arch
@ -131,23 +131,25 @@ def losetup_destroy(loop_device):
) )
def get_device_and_flavour(profile_name: Optional[str] = None) -> tuple[str, str]: def get_flavour(profile_name: Optional[str] = None) -> str:
config.enforce_config_loaded() config.enforce_config_loaded()
profile = config.get_profile(profile_name) profile = config.get_profile(profile_name)
if not profile['device']:
raise Exception("Please set the device using 'kupferbootstrap config init ...'")
if not profile['flavour']: if not profile['flavour']:
raise Exception("Please set the flavour using 'kupferbootstrap config init ...'") raise Exception("Please set the flavour using 'kupferbootstrap config init ...'")
return (profile['device'], profile['flavour']) return profile['flavour']
def get_image_name(device, flavour, img_type='full') -> str: def get_device_name(device: Union[str, Device]) -> str:
return f'{device}-{flavour}-{img_type}.img' return device.name if isinstance(device, Device) else device
def get_image_path(device, flavour, img_type='full') -> str: def get_image_name(device: Union[str, Device], flavour, img_type='full') -> str:
return f'{get_device_name(device)}-{flavour}-{img_type}.img'
def get_image_path(device: Union[str, Device], flavour, img_type='full') -> str:
return os.path.join(config.get_path('images'), get_image_name(device, flavour, img_type)) return os.path.join(config.get_path('images'), get_image_name(device, flavour, img_type))
@ -299,7 +301,7 @@ def create_boot_fs(device: str, blocksize: int):
def install_rootfs( def install_rootfs(
rootfs_device: str, rootfs_device: str,
bootfs_device: str, bootfs_device: str,
device: str, device: Union[str, Device],
flavour: str, flavour: str,
arch: Arch, arch: Arch,
packages: list[str], packages: list[str],
@ -308,7 +310,7 @@ def install_rootfs(
): ):
user = profile['username'] or 'kupfer' user = profile['username'] or 'kupfer'
post_cmds = FLAVOURS[flavour].get('post_cmds', []) post_cmds = FLAVOURS[flavour].get('post_cmds', [])
chroot = get_device_chroot(device=device, flavour=flavour, arch=arch, packages=packages, use_local_repos=use_local_repos) chroot = get_device_chroot(device=get_device_name(device), flavour=flavour, arch=arch, packages=packages, use_local_repos=use_local_repos)
mount_chroot(rootfs_device, bootfs_device, chroot) mount_chroot(rootfs_device, bootfs_device, chroot)
@ -388,16 +390,17 @@ def cmd_build(profile_name: str = None,
Unless overriden, required packages will be built or preferably downloaded from HTTPS repos. Unless overriden, required packages will be built or preferably downloaded from HTTPS repos.
""" """
arch = get_profile_device(profile_name).arch device = get_profile_device(profile_name)
arch = device.arch
check_programs_wrap(['makepkg', 'pacman', 'pacstrap']) check_programs_wrap(['makepkg', 'pacman', 'pacstrap'])
profile: Profile = config.get_profile(profile_name) profile: Profile = config.get_profile(profile_name)
device, flavour = get_device_and_flavour(profile_name) flavour = get_flavour(profile_name)
size_extra_mb: int = int(profile["size_extra_mb"]) size_extra_mb: int = int(profile["size_extra_mb"])
sector_size = 4096 sector_size = 4096
rootfs_size_mb = FLAVOURS[flavour].get('size', 2) * 1000 rootfs_size_mb = FLAVOURS[flavour].get('size', 2) * 1000
packages = BASE_PACKAGES + DEVICES[device] + FLAVOURS[flavour]['packages'] + profile['pkgs_include'] packages = BASE_PACKAGES + [device.package.name] + FLAVOURS[flavour]['packages'] + profile['pkgs_include']
if arch != config.runtime.arch: if arch != config.runtime.arch:
build_enable_qemu_binfmt(arch) build_enable_qemu_binfmt(arch)
@ -459,9 +462,10 @@ def cmd_build(profile_name: str = None,
@click.argument('profile', required=False) @click.argument('profile', required=False)
def cmd_inspect(profile: str = None, shell: bool = False): def cmd_inspect(profile: str = None, shell: bool = False):
"""Open a shell in a device image""" """Open a shell in a device image"""
arch = get_profile_device(profile).arch device = get_profile_device(profile)
arch = device.arch
wrap_if_foreign_arch(arch) wrap_if_foreign_arch(arch)
device, flavour = get_device_and_flavour(profile) flavour = get_flavour(profile)
# TODO: PARSE DEVICE SECTOR SIZE # TODO: PARSE DEVICE SECTOR SIZE
sector_size = 4096 sector_size = 4096
chroot = get_device_chroot(device, flavour, arch) chroot = get_device_chroot(device, flavour, arch)