Implement kernel bootstrap up to building Linux.

This commit is contained in:
rick-masters 2023-03-31 18:08:50 +00:00
parent 5ea8dd3136
commit ecf99ab08b
52 changed files with 4898 additions and 139 deletions

131
sysa.py
View file

@ -13,6 +13,7 @@ import shutil
import tarfile
from lib.sysgeneral import SysGeneral, stage0_arch_map
from lib.utils import run
# pylint: disable=consider-using-with
# pylint: disable=too-many-instance-attributes
@ -37,7 +38,7 @@ class SysA(SysGeneral):
self.tmp_dir = tmpdir.add_sys("sysa")
def prepare(self, create_initramfs):
def prepare(self, create_initramfs, kernel_bootstrap=False):
"""
Prepare directory structure for System A.
We create an empty tmp directory, unpack stage0-posix.
@ -59,6 +60,11 @@ class SysA(SysGeneral):
self.sysc(create_initramfs)
if kernel_bootstrap:
self.create_fiwix_file_list()
self.create_builder_hex0_disk_image(os.path.join(self.tmp_dir, 'sysa.img'))
return
if self.repo_path:
repo_dir = os.path.join(self.tmp_dir, 'usr', 'src', 'repo-preseeded')
shutil.copytree(self.repo_path, repo_dir)
@ -101,3 +107,126 @@ class SysA(SysGeneral):
# stage0-posix hook to continue running live-bootstrap
shutil.copy2(os.path.join(self.sys_dir, 'after.kaem'),
os.path.join(self.tmp_dir, 'after.kaem'))
def find_tree(self, dirpath):
"""Find all sub dirs and files in a path"""
subdirs, files = [], []
for path in os.scandir(dirpath):
if path.is_dir():
subdirs.append(path.path)
if path.is_file():
files.append(path.path)
for subdir in list(subdirs):
more_dirs, more_files = self.find_tree(subdir)
subdirs.extend(more_dirs)
files.extend(more_files)
return subdirs, files
def add_fiwix_files(self, file_list_path, dirpath):
"""Add files to the list to populate Fiwix file system"""
_, filepaths = self.find_tree(dirpath)
with open(file_list_path, 'a') as file_list:
for filepath in filepaths:
if 'stage0-posix' in filepath:
continue
file_list.write(f"/{filepath}\n")
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())
def output_tree(self, srcfs_file, filepath):
self.output_dir(srcfs_file, filepath)
dirs, files = self.find_tree(filepath)
for dirpath in dirs:
if ".git" in dirpath:
continue
self.output_dir(srcfs_file, dirpath)
for filepath in files:
if ".git" in filepath:
continue
self.output_file(srcfs_file, filepath)
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