chroots: use path from config

Signed-off-by: InsanePrawn <insane.prawny@gmail.com>
This commit is contained in:
InsanePrawn 2021-09-13 04:19:13 +02:00
parent 63a9cf1218
commit 04cce26ca0
3 changed files with 43 additions and 37 deletions

View file

@ -2,15 +2,16 @@ import logging
import subprocess import subprocess
import os import os
import shutil import shutil
from config import config
def create_chroot( def get_chroot_path(chroot_name, override_basepath: str = None) -> str:
chroot_path, base_path = config.file['paths']['chroots'] if not override_basepath else override_basepath
packages=['base'], return os.path.join(base_path, chroot_name)
pacman_conf='/app/local/etc/pacman.conf',
chroot_base_path='/chroot',
extra_repos={}, def create_chroot(chroot_name, packages=['base'], pacman_conf='/app/local/etc/pacman.conf', extra_repos={}, chroot_base_path: str = None):
): chroot_path = get_chroot_path(chroot_name, override_basepath=chroot_base_path)
pacman_conf_target = chroot_path + '/etc/pacman.conf' pacman_conf_target = chroot_path + '/etc/pacman.conf'
os.makedirs(chroot_path + '/etc', exist_ok=True) os.makedirs(chroot_path + '/etc', exist_ok=True)
@ -29,14 +30,19 @@ def create_chroot(
'-yyuu', '-yyuu',
]) ])
if result.returncode != 0: if result.returncode != 0:
logging.fatal('Failed to install system') raise Exception('Failed to install chroot')
exit(1) return chroot_path
def create_chroot_user(chroot_path): def create_chroot_user(
user = 'kupfer' chroot_name,
password = '123456' chroot_base_path: str = None,
groups = ['network', 'video', 'audio', 'optical', 'storage', 'input', 'scanner', 'games', 'lp', 'rfkill', 'wheel'] user='kupfer',
password='123456',
groups=['network', 'video', 'audio', 'optical', 'storage', 'input', 'scanner', 'games', 'lp', 'rfkill', 'wheel'],
):
chroot_path = get_chroot_path(chroot_name, override_basepath=chroot_base_path)
install_script = '\n'.join([ install_script = '\n'.join([
f'if ! id -u "{user}" >/dev/null 2>&1; then', f'if ! id -u "{user}" >/dev/null 2>&1; then',
f' useradd -m {user}', f' useradd -m {user}',

View file

@ -3,7 +3,7 @@ import os
import subprocess import subprocess
import click import click
from logger import logging from logger import logging
from chroot import create_chroot, create_chroot_user from chroot import create_chroot, create_chroot_user, get_chroot_path
from constants import DEVICES, FLAVOURS from constants import DEVICES, FLAVOURS
@ -23,21 +23,20 @@ def get_device_and_flavour() -> tuple[str, str]:
return (device, flavour) return (device, flavour)
def get_image_name(device, flavour): def get_image_name(device, flavour) -> str:
return f'{device}-{flavour}-rootfs.img' return f'{device}-{flavour}-rootfs.img'
def mount_rootfs_image(path): def mount_rootfs_image(image_path, mount_path):
rootfs_mount = '/mnt/kupfer/rootfs' if not os.path.exists(mount_path):
if not os.path.exists(rootfs_mount): os.makedirs(mount_path)
os.makedirs(rootfs_mount)
def umount(): def umount():
subprocess.run( subprocess.run(
[ [
'umount', 'umount',
'-lc', '-lc',
rootfs_mount, mount_path,
], ],
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
) )
@ -48,15 +47,13 @@ def mount_rootfs_image(path):
'mount', 'mount',
'-o', '-o',
'loop', 'loop',
path, image_path,
rootfs_mount, mount_path,
]) ])
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed to loop mount {path} to {rootfs_mount}') logging.fatal(f'Failed to loop mount {image_path} to {mount_path}')
exit(1) exit(1)
return rootfs_mount
def dump_bootimg(image_name: str) -> str: def dump_bootimg(image_name: str) -> str:
path = '/tmp/boot.img' path = '/tmp/boot.img'
@ -164,8 +161,9 @@ def cmd_build():
if result.returncode != 0: if result.returncode != 0:
logging.fatal(f'Failed to create ext4 filesystem on {image_name}') logging.fatal(f'Failed to create ext4 filesystem on {image_name}')
exit(1) exit(1)
chroot_name = f'rootfs_{device}-{flavour}'
rootfs_mount = mount_rootfs_image(image_name) rootfs_mount = get_chroot_path(chroot_name)
mount_rootfs_image(image_name, rootfs_mount)
extra_repos = { extra_repos = {
'main': { 'main': {
@ -187,12 +185,12 @@ def cmd_build():
} }
create_chroot( create_chroot(
rootfs_mount, chroot_name,
packages=['base', 'base-kupfer'] + DEVICES[device] + FLAVOURS[flavour], packages=['base', 'base-kupfer'] + DEVICES[device] + FLAVOURS[flavour],
pacman_conf='/app/local/etc/pacman.conf', pacman_conf='/app/local/etc/pacman.conf',
extra_repos=extra_repos, extra_repos=extra_repos,
) )
create_chroot_user(rootfs_mount) create_chroot_user(chroot_name)
""" """
@ -204,7 +202,8 @@ def cmd_inspect():
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
image_name = get_image_name(device, flavour) image_name = get_image_name(device, flavour)
rootfs_mount = mount_rootfs_image(image_name) rootfs_mount = get_chroot_path(f'rootfs_{device}-flavour')
mount_rootfs_image(image_name, rootfs_mount)
logging.info(f'Inspect the rootfs image at {rootfs_mount}') logging.info(f'Inspect the rootfs image at {rootfs_mount}')

View file

@ -116,15 +116,16 @@ def check_prebuilts():
exit(1) exit(1)
def setup_chroot(chroot_path='/chroot/root'): def setup_build_chroot(arch='aarch64'):
logging.info('Initializing root chroot') chroot_name = f'build_{arch}'
logging.info('Initializing {arch} build chroot')
extra_repos = {} extra_repos = {}
for repo in REPOSITORIES: for repo in REPOSITORIES:
extra_repos[repo] = { extra_repos[repo] = {
'Server': f'file:///src/prebuilts/{repo}', 'Server': f'file:///src/prebuilts/{repo}',
} }
create_chroot( chroot_path = create_chroot(
chroot_path, chroot_name,
packages=['base-devel'], packages=['base-devel'],
pacman_conf='/app/local/etc/pacman.conf', pacman_conf='/app/local/etc/pacman.conf',
extra_repos=extra_repos, extra_repos=extra_repos,
@ -135,7 +136,7 @@ def setup_chroot(chroot_path='/chroot/root'):
'--root', '--root',
chroot_path, chroot_path,
'--arch', '--arch',
'aarch64', arch,
'--config', '--config',
chroot_path + '/etc/pacman.conf', chroot_path + '/etc/pacman.conf',
]) ])
@ -466,7 +467,7 @@ def cmd_packages():
@click.command(name='build') @click.command(name='build')
@click.argument('paths', nargs=-1) @click.argument('paths', nargs=-1)
def cmd_build(paths): def cmd_build(paths, arch='aarch64'):
check_prebuilts() check_prebuilts()
paths = list(paths) paths = list(paths)
@ -484,7 +485,7 @@ def cmd_build(paths):
logging.info('Building %s', ', '.join(map(lambda x: x.path, need_build))) logging.info('Building %s', ', '.join(map(lambda x: x.path, need_build)))
crosscompile = config.file['build']['crosscompile'] crosscompile = config.file['build']['crosscompile']
for package in need_build: for package in need_build:
setup_chroot() setup_build_chroot(arch=arch)
build_package(package, enable_crosscompile=crosscompile) build_package(package, enable_crosscompile=crosscompile)
add_package_to_repo(package) add_package_to_repo(package)