2021-04-06 00:12:02 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""
|
|
|
|
A helper application used to start bootstrapping process.
|
|
|
|
It has a few modes of operation, you can create initramfs with
|
|
|
|
binary seeds and sources that you can boot into or alternatively
|
|
|
|
you can run bootstap inside chroot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
|
2021-04-16 18:35:24 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Bastian Bittorf <bb@npl.de>
|
2021-05-18 12:37:34 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Melg Eight <public.melg8@gmail.com>
|
2021-07-06 01:52:10 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space>
|
2021-04-06 00:12:02 +01:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-04-16 18:35:24 +01:00
|
|
|
import shutil
|
2021-04-06 00:12:02 +01:00
|
|
|
|
|
|
|
from sysa import SysA
|
2021-07-06 01:52:10 +01:00
|
|
|
from sysb import SysB
|
|
|
|
from sysc import SysC
|
|
|
|
from lib.utils import run, umount
|
|
|
|
|
|
|
|
def create_configuration_file(args):
|
|
|
|
"""
|
|
|
|
Creates bootstrap.cfg file which would contain options used to
|
|
|
|
customize bootstrap.
|
|
|
|
"""
|
|
|
|
config_path = os.path.join('sysglobal', 'bootstrap.cfg')
|
|
|
|
with open(config_path, "w") as config:
|
|
|
|
config.write("FORCE_TIMESTAMPS=" + str(args.force_timestamps) + "\n")
|
|
|
|
config.write("CHROOT=" + str(args.chroot) + "\n")
|
|
|
|
config.write("DISK=hda1\n")
|
2021-04-06 00:12:02 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
A few command line arguments to customize bootstrap.
|
|
|
|
This function also creates SysA object which prepares directory
|
|
|
|
structure with bootstrap seeds and all sources.
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-a", "--arch", help="Bootstrap architecture",
|
|
|
|
default="x86")
|
|
|
|
parser.add_argument("-c", "--chroot", help="Run inside chroot",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("-p", "--preserve", help="Do not unmount temporary dir",
|
|
|
|
action="store_true")
|
|
|
|
parser.add_argument("-t", "--tmpdir", help="Temporary directory")
|
2021-05-18 12:37:34 +01:00
|
|
|
parser.add_argument("--force_timestamps",
|
|
|
|
help="Force all files timestamps to be 0 unix time",
|
|
|
|
action="store_true")
|
2021-04-06 00:12:02 +01:00
|
|
|
|
|
|
|
# QEMU arguments
|
|
|
|
parser.add_argument("-q", "--qemu-cmd", help="QEMU command",
|
|
|
|
default="qemu-system-x86_64")
|
|
|
|
parser.add_argument("-r", "--qemu-ram", help="Memory (in megabytes) allocated to QEMU VM",
|
|
|
|
default=8000)
|
|
|
|
parser.add_argument("-k", "--kernel", help="Kernel to use (default is ./kernel)",
|
|
|
|
default="kernel")
|
|
|
|
|
|
|
|
parser.add_argument("-m", "--minikernel", help="Use minikernel",
|
|
|
|
action="store_true")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.chroot and args.minikernel:
|
|
|
|
raise ValueError("chroot and minikernel options cannot be used simultaneously.")
|
|
|
|
|
|
|
|
if args.arch != "x86":
|
|
|
|
raise ValueError("Only x86 is supported at the moment.")
|
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
create_configuration_file(args)
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
system_b = SysB(arch=args.arch, preserve_tmp=args.preserve, tmpdir=args.tmpdir, chroot=args.chroot)
|
|
|
|
system_a = SysA(arch=args.arch, preserve_tmp=args.preserve, tmpdir=args.tmpdir, chroot=args.chroot, sysb_tmp=system_b.tmp_dir)
|
|
|
|
system_c = SysC(arch=args.arch, preserve_tmp=args.preserve, tmpdir=args.tmpdir, chroot=args.chroot)
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
bootstrap(args, system_a, system_b, system_c)
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-07-06 01:52:10 +01:00
|
|
|
def bootstrap(args, system_a, system_b, system_c):
|
2021-04-06 00:12:02 +01:00
|
|
|
"""Kick off bootstrap process."""
|
2021-07-06 01:52:10 +01:00
|
|
|
print("Bootstrapping %s -- SysA" % (args.arch))
|
2021-04-06 00:12:02 +01:00
|
|
|
if args.chroot:
|
2021-04-21 23:54:14 +01:00
|
|
|
find_chroot = """
|
|
|
|
import shutil
|
|
|
|
print(shutil.which('chroot'))
|
|
|
|
"""
|
|
|
|
chroot_binary = run('sudo', 'python3', '-c', find_chroot,
|
|
|
|
capture_output=True).stdout.decode().strip()
|
2021-07-06 01:52:10 +01:00
|
|
|
# sysa
|
2021-04-06 00:12:02 +01:00
|
|
|
init = os.path.join(os.sep, 'bootstrap-seeds', 'POSIX', args.arch, 'kaem-optional-seed')
|
2021-07-06 01:52:10 +01:00
|
|
|
run('sudo', 'env', '-i', 'PATH=/bin', chroot_binary, system_a.tmp_dir, init)
|
|
|
|
# Perform the steps for sysa -> sysc transition that would occur within
|
|
|
|
# qemu if we were running not in chroot
|
|
|
|
# We skip sysb as that is only pertinent to "hardware" (not chroot)
|
|
|
|
# system_c.chroot_transition(system_a.tmp_dir)
|
|
|
|
# sysc
|
|
|
|
print("Bootstrapping %s -- SysC" % (args.arch))
|
|
|
|
# init = os.path.join(os.sep, 'init')
|
|
|
|
# run('sudo', chroot_binary, system_c.tmp_dir, init)
|
|
|
|
|
|
|
|
elif args.minikernel:
|
2021-04-16 18:35:24 +01:00
|
|
|
if os.path.isdir('kritis-linux'):
|
|
|
|
shutil.rmtree('kritis-linux')
|
|
|
|
|
|
|
|
run('git', 'clone',
|
|
|
|
'--depth', '1', '--branch', 'v0.7',
|
2021-04-06 00:12:02 +01:00
|
|
|
'https://github.com/bittorf/kritis-linux.git')
|
2021-04-16 18:35:24 +01:00
|
|
|
run('kritis-linux/ci_helper.sh',
|
|
|
|
'--private',
|
|
|
|
'--multi', '1',
|
|
|
|
'--repeat', '1',
|
|
|
|
'--arch', args.arch,
|
|
|
|
'--qemucpu', '486',
|
|
|
|
'--kernel', '3.18.140',
|
|
|
|
'--features', 'kflock,highrestimers',
|
2021-07-06 01:52:10 +01:00
|
|
|
# Hack to add -hda /dev/blah
|
|
|
|
'--ramsize', str(args.qemu_ram) + 'M -hda ' + sysb.dev_name,
|
|
|
|
'--initrd', system_a.initramfs_path,
|
2021-04-16 18:35:24 +01:00
|
|
|
'--log', '/tmp/bootstrap.log')
|
2021-07-06 01:52:10 +01:00
|
|
|
|
|
|
|
else:
|
|
|
|
run(args.qemu_cmd,
|
|
|
|
'-enable-kvm',
|
|
|
|
'-m', str(args.qemu_ram) + 'M',
|
|
|
|
'-no-reboot',
|
|
|
|
'-hda', system_c.dev_name,
|
|
|
|
'-kernel', args.kernel,
|
|
|
|
'-initrd', system_a.initramfs_path,
|
|
|
|
'-display', 'curses')
|
2021-04-06 00:12:02 +01:00
|
|
|
|
2021-04-16 18:35:24 +01:00
|
|
|
if __name__ == "__main__":
|
2021-04-06 00:12:02 +01:00
|
|
|
main()
|