2021-04-06 00:12:02 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""System A"""
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2023-01-13 13:55:39 +00:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Dor Askayo <dor.askayo@gmail.com>
|
2021-04-06 00:12:02 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
|
2021-05-18 12:37:21 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Melg Eight <public.melg8@gmail.com>
|
2023-01-28 00:11:32 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021-23 fosslinux <fosslinux@aussies.space>
|
2021-04-06 00:12:02 +01:00
|
|
|
|
|
|
|
import os
|
2023-03-15 06:20:49 +00:00
|
|
|
# pylint: disable=deprecated-module
|
2021-04-06 00:12:02 +01:00
|
|
|
from distutils.dir_util import copy_tree
|
|
|
|
import shutil
|
2022-12-21 03:17:37 +00:00
|
|
|
import tarfile
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-10-20 21:11:48 +01:00
|
|
|
from lib.sysgeneral import SysGeneral, stage0_arch_map
|
2023-03-31 19:08:50 +01:00
|
|
|
from lib.utils import run
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-08-05 13:16:19 +01:00
|
|
|
# pylint: disable=consider-using-with
|
2023-01-13 11:15:27 +00:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
2021-07-06 01:52:10 +01:00
|
|
|
class SysA(SysGeneral):
|
2021-04-06 00:12:02 +01:00
|
|
|
"""
|
|
|
|
Class responsible for preparing sources for System A.
|
|
|
|
"""
|
2023-01-13 11:15:27 +00:00
|
|
|
|
|
|
|
git_dir = os.path.dirname(os.path.join(__file__))
|
|
|
|
sys_dir = os.path.join(git_dir, 'sysa')
|
2023-01-28 00:11:32 +00:00
|
|
|
sysb_dir = os.path.join(git_dir, 'sysb')
|
|
|
|
sysc_dir = os.path.join(git_dir, 'sysc')
|
2023-01-13 11:15:27 +00:00
|
|
|
cache_dir = os.path.join(sys_dir, 'distfiles')
|
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
2023-01-28 00:11:32 +00:00
|
|
|
def __init__(self, tmpdir, arch, external_sources,
|
|
|
|
early_preseed, repo_path):
|
2021-04-06 00:12:02 +01:00
|
|
|
self.arch = arch
|
2022-12-21 03:17:37 +00:00
|
|
|
self.early_preseed = early_preseed
|
2022-05-23 06:56:18 +01:00
|
|
|
self.external_sources = external_sources
|
2023-01-28 00:11:32 +00:00
|
|
|
self.repo_path = repo_path
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2023-01-28 00:11:32 +00:00
|
|
|
self.tmp_dir = tmpdir.add_sys("sysa")
|
|
|
|
|
2023-03-31 19:08:50 +01:00
|
|
|
def prepare(self, create_initramfs, kernel_bootstrap=False):
|
2021-04-06 00:12:02 +01:00
|
|
|
"""
|
2021-05-06 02:14:11 +01:00
|
|
|
Prepare directory structure for System A.
|
2022-05-23 15:02:33 +01:00
|
|
|
We create an empty tmp directory, unpack stage0-posix.
|
2022-04-07 00:09:45 +01:00
|
|
|
Rest of the files are unpacked into more structured directory /sysa
|
2021-04-06 00:12:02 +01:00
|
|
|
"""
|
2022-12-21 03:17:37 +00:00
|
|
|
if self.early_preseed:
|
|
|
|
# Extract tar containing preseed
|
|
|
|
with tarfile.open(self.early_preseed, "r") as seed:
|
|
|
|
seed.extractall(self.tmp_dir)
|
|
|
|
shutil.copy2(os.path.join(self.sys_dir, 'base-preseeded.kaem'),
|
|
|
|
os.path.join(self.tmp_dir, 'kaem.x86'))
|
|
|
|
else:
|
|
|
|
self.stage0_posix()
|
|
|
|
|
2022-04-07 00:09:45 +01:00
|
|
|
self.sysa()
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
# sysb must be added to sysa as it is another initramfs stage
|
|
|
|
self.sysb()
|
|
|
|
|
2022-05-23 06:56:18 +01:00
|
|
|
self.sysc(create_initramfs)
|
2022-05-23 14:09:45 +01:00
|
|
|
|
2023-03-31 19:08:50 +01:00
|
|
|
if kernel_bootstrap:
|
|
|
|
self.create_fiwix_file_list()
|
|
|
|
self.create_builder_hex0_disk_image(os.path.join(self.tmp_dir, 'sysa.img'))
|
|
|
|
return
|
|
|
|
|
2023-01-28 00:11:32 +00:00
|
|
|
if self.repo_path:
|
2022-05-28 22:59:42 +01:00
|
|
|
repo_dir = os.path.join(self.tmp_dir, 'usr', 'src', 'repo-preseeded')
|
2023-01-28 00:11:32 +00:00
|
|
|
shutil.copytree(self.repo_path, repo_dir)
|
2022-05-28 22:59:42 +01:00
|
|
|
|
2022-05-23 14:09:45 +01:00
|
|
|
if create_initramfs:
|
2022-05-23 14:04:13 +01:00
|
|
|
self.make_initramfs()
|
2021-10-10 16:01:50 +01:00
|
|
|
|
2022-04-13 00:08:29 +01:00
|
|
|
def sysa(self):
|
2022-04-24 04:59:17 +01:00
|
|
|
"""Copy in sysa files for sysa."""
|
2023-01-13 13:55:39 +00:00
|
|
|
source_manifest = self.get_source_manifest()
|
|
|
|
self.get_packages(source_manifest)
|
2022-04-24 04:59:17 +01:00
|
|
|
|
2022-04-13 00:08:29 +01:00
|
|
|
shutil.copytree(self.sys_dir, os.path.join(self.tmp_dir, 'sysa'),
|
2022-05-22 01:03:21 +01:00
|
|
|
ignore=shutil.ignore_patterns('tmp'))
|
2022-04-13 00:08:29 +01:00
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
def sysb(self):
|
|
|
|
"""Copy in sysb files for sysb."""
|
2022-04-24 04:59:17 +01:00
|
|
|
shutil.copytree(self.sysb_dir, os.path.join(self.tmp_dir, 'sysb'),
|
2022-05-22 01:03:21 +01:00
|
|
|
ignore=shutil.ignore_patterns('tmp'))
|
2021-07-06 01:52:10 +01:00
|
|
|
|
2022-05-23 06:56:18 +01:00
|
|
|
def sysc(self, create_initramfs):
|
2021-10-10 16:01:50 +01:00
|
|
|
"""Copy in sysc files for sysc."""
|
2023-03-06 06:30:09 +00:00
|
|
|
if create_initramfs or not self.external_sources:
|
2022-05-23 06:56:18 +01:00
|
|
|
ignore = shutil.ignore_patterns('tmp', 'distfiles')
|
|
|
|
else:
|
|
|
|
ignore = shutil.ignore_patterns('tmp')
|
2022-05-22 01:03:21 +01:00
|
|
|
shutil.copytree(self.sysc_dir, os.path.join(self.tmp_dir, 'sysc'),
|
2022-05-23 06:56:18 +01:00
|
|
|
ignore=ignore)
|
2021-10-10 16:01:50 +01:00
|
|
|
|
2021-04-06 00:12:02 +01:00
|
|
|
def stage0_posix(self):
|
2021-06-10 18:01:47 +01:00
|
|
|
"""Copy in all of the stage0-posix"""
|
2021-07-06 01:52:10 +01:00
|
|
|
stage0_posix_base_dir = os.path.join(self.sys_dir, 'stage0-posix', 'src')
|
2021-10-08 19:29:16 +01:00
|
|
|
copy_tree(stage0_posix_base_dir, self.tmp_dir)
|
|
|
|
|
2021-10-20 21:11:48 +01:00
|
|
|
arch = stage0_arch_map.get(self.arch, self.arch)
|
2021-10-08 19:29:16 +01:00
|
|
|
kaem_optional_seed = os.path.join(self.sys_dir, 'stage0-posix', 'src', 'bootstrap-seeds',
|
2021-10-20 21:11:48 +01:00
|
|
|
'POSIX', arch, 'kaem-optional-seed')
|
2021-04-06 00:12:02 +01:00
|
|
|
shutil.copy2(kaem_optional_seed, os.path.join(self.tmp_dir, 'init'))
|
|
|
|
|
2021-06-10 18:01:47 +01:00
|
|
|
# stage0-posix hook to continue running live-bootstrap
|
2021-12-28 11:51:15 +00:00
|
|
|
shutil.copy2(os.path.join(self.sys_dir, 'after.kaem'),
|
2021-06-10 18:01:47 +01:00
|
|
|
os.path.join(self.tmp_dir, 'after.kaem'))
|
2023-03-31 19:08:50 +01:00
|
|
|
|
|
|
|
def add_fiwix_files(self, file_list_path, dirpath):
|
|
|
|
"""Add files to the list to populate Fiwix file system"""
|
2023-04-03 19:12:01 +01:00
|
|
|
for root, dirs, filepaths in os.walk(dirpath):
|
|
|
|
if 'stage0-posix' in root:
|
|
|
|
continue
|
|
|
|
with open(file_list_path, 'a', encoding="utf-8") as file_list:
|
|
|
|
for filepath in filepaths:
|
|
|
|
file_list.write(f"/{os.path.join(root, filepath)}\n")
|
2023-03-31 19:08:50 +01:00
|
|
|
|
|
|
|
def create_fiwix_file_list(self):
|
|
|
|
"""Create a list of files to populate Fiwix file system"""
|
|
|
|
file_list_path = os.path.join(self.tmp_dir, 'sysa', 'lwext4-1.0.0-lb1',
|
|
|
|
'files', 'fiwix-file-list.txt')
|
|
|
|
shutil.copyfile(os.path.join(self.tmp_dir, 'sysa', 'lwext4-1.0.0-lb1',
|
|
|
|
'files', 'early-artifacts-needed-after-fiwix.txt'),
|
|
|
|
file_list_path)
|
|
|
|
|
|
|
|
save_cwd = os.getcwd()
|
|
|
|
self.add_fiwix_files(file_list_path, 'sysa')
|
|
|
|
self.add_fiwix_files(file_list_path, 'sysb')
|
|
|
|
self.add_fiwix_files(file_list_path, 'sysc')
|
|
|
|
os.chdir(save_cwd)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def output_dir(srcfs_file, dirpath):
|
|
|
|
"""Add a directory to srcfs file system"""
|
|
|
|
srcline = f"src 0 {dirpath}\n"
|
|
|
|
srcfs_file.write(srcline.encode())
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def output_file(srcfs_file, filepath):
|
|
|
|
"""Add a file to srcfs file system"""
|
|
|
|
srcline = f"src {os.path.getsize(filepath)} {filepath}\n"
|
|
|
|
srcfs_file.write(srcline.encode())
|
|
|
|
with open(filepath, 'rb') as srcfile:
|
|
|
|
srcfs_file.write(srcfile.read())
|
|
|
|
|
2023-04-03 19:12:01 +01:00
|
|
|
def output_tree(self, srcfs_file, treepath):
|
|
|
|
"""Add a tree of files to srcfs file system"""
|
|
|
|
self.output_dir(srcfs_file, treepath)
|
|
|
|
for root, dirs, files in os.walk(treepath):
|
|
|
|
if ".git" in root:
|
2023-03-31 19:08:50 +01:00
|
|
|
continue
|
2023-04-03 19:12:01 +01:00
|
|
|
for dirpath in dirs:
|
|
|
|
if ".git" in dirpath:
|
|
|
|
continue
|
|
|
|
self.output_dir(srcfs_file, os.path.join(root, dirpath))
|
2023-03-31 19:08:50 +01:00
|
|
|
|
2023-04-03 19:12:01 +01:00
|
|
|
for filepath in files:
|
|
|
|
if ".git" in filepath:
|
|
|
|
continue
|
|
|
|
self.output_file(srcfs_file, os.path.join(root, filepath))
|
2023-03-31 19:08:50 +01:00
|
|
|
|
|
|
|
def append_srcfs(self, image_file):
|
|
|
|
"""Append srcfs file system to sysa disk image"""
|
|
|
|
save_cwd = os.getcwd()
|
|
|
|
|
|
|
|
os.chdir(os.path.join(self.tmp_dir, 'sysa', 'stage0-posix', 'src'))
|
|
|
|
self.output_tree(image_file, '.')
|
|
|
|
|
|
|
|
os.chdir(self.tmp_dir)
|
|
|
|
shutil.move(os.path.join('sysa', 'stage0-posix'), '.')
|
|
|
|
self.output_tree(image_file, 'sysa')
|
|
|
|
self.output_tree(image_file, 'sysb')
|
|
|
|
self.output_tree(image_file, 'sysc')
|
|
|
|
shutil.move('stage0-posix', 'sysa')
|
|
|
|
shutil.copyfile(os.path.join('sysa', 'after.kaem'), 'after.kaem')
|
|
|
|
self.output_file(image_file, 'after.kaem')
|
|
|
|
|
|
|
|
# Add commands to kick off stage0-posix
|
|
|
|
cmd = ' '.join(['hex0',
|
|
|
|
'./bootstrap-seeds/POSIX/x86/hex0_x86.hex0'
|
|
|
|
'./bootstrap-seeds/POSIX/x86/hex0-seed\n'])
|
|
|
|
image_file.write(cmd.encode())
|
|
|
|
cmd = ' '.join(['hex0',
|
|
|
|
'./bootstrap-seeds/POSIX/x86/kaem-minimal.hex0',
|
|
|
|
'./bootstrap-seeds/POSIX/x86/kaem-optional-seed\n'])
|
|
|
|
image_file.write(cmd.encode())
|
|
|
|
cmd = ' '.join(['./bootstrap-seeds/POSIX/x86/kaem-optional-seed', './kaem.x86\n'])
|
|
|
|
image_file.write(cmd.encode())
|
|
|
|
|
|
|
|
os.chdir(save_cwd)
|
|
|
|
|
|
|
|
def create_builder_hex0_disk_image(self, image_file_name):
|
|
|
|
"""Create builder-hex0 disk image"""
|
|
|
|
run(os.path.join('sysa', 'stage0-posix', 'src',
|
|
|
|
'bootstrap-seeds', 'POSIX', 'x86', 'hex0-seed'),
|
|
|
|
os.path.join('kernel-bootstrap', 'builder-hex0-x86.hex0'),
|
|
|
|
image_file_name)
|
|
|
|
|
|
|
|
with open(image_file_name, 'ab') as image_file:
|
|
|
|
self.append_srcfs(image_file)
|
|
|
|
|
|
|
|
current_size = os.stat(image_file_name).st_size
|
|
|
|
|
|
|
|
megabyte = 1024 * 1024
|
|
|
|
# fill file with zeros up to next megabyte
|
|
|
|
extra = current_size % megabyte
|
|
|
|
round_up = megabyte - extra
|
|
|
|
with open(image_file_name, 'ab') as image_file:
|
|
|
|
image_file.write(b'\0' * round_up)
|
|
|
|
current_size += round_up
|
|
|
|
|
|
|
|
# fill file with zeros up to desired size, one megabyte at a time
|
|
|
|
with open(image_file_name, 'ab') as image_file:
|
|
|
|
while current_size < 1008 * megabyte:
|
|
|
|
image_file.write(b'\0' * megabyte)
|
|
|
|
current_size += megabyte
|