// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell] // Run on x86_64 Linux with musl-gcc, bsdtar, GNU tar, xorriso and QEMU installed. module main import crypto.sha256 import os fn run(argv []string) ! { result := os.exec(argv) if result.exit_code != 0 { return error('${argv}: exit ${result.exit_code}\n${result.output}') } } fn boot_image(root string, iso string, output string, shell bool) ! { if root == '/' || output == root || output.starts_with(root + '/') { return error('place the output outside a separate BusyBox package root') } digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex() helper := if shell { 'serial-line' } else { 'busybox-probe' } if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) { return error('use a clean root containing just the installed BusyBox package') } os.mkdir(output)! boot := os.join_path(output, 'image/boot') additions := os.join_path(output, 'fixture') os.mkdir_all(boot)! os.mkdir_all(os.join_path(additions, 'etc'))! inittab := if shell { "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'" } else { '::sysinit:/busybox-probe' } os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')! source := if shell { 'serial-line.v' } else { 'guest.v' } run([@VEXE, '-prod', '-gc', 'none', '-cc', 'musl-gcc', '-cflags', '-static', '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])! run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', 'boot/limine-bios.sys', 'boot/limine-bios-cd.bin'])! os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\n/BusyBox\n' + ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' + ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=live\n')! run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot, 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])! test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' }) run(['xorriso', '-as', 'mkisofs', '-R', '-r', '-b', 'boot/limine-bios-cd.bin', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table', '-o', test_iso, os.dir(boot)])! if shell { println('Interactive serial shell image: ${test_iso}') return } log := os.join_path(output, 'serial.log') run(['timeout', '90s', 'qemu-system-x86_64', '-machine', 'q35', '-accel', 'tcg', '-cpu', 'max', '-m', '1024', '-smp', '2', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd', '-nic', 'none', '-display', 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot'])! lines := os.read_lines(log)!.map(it.trim_space()) if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines { return error('guest verification failed; see ${log}') } println('Vinix booted the installed BusyBox ${digest}. See ${log}') } fn main() { shell := os.args.len == 5 && os.args[4] == '--shell' if os.args.len != 4 && !shell { eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]') exit(2) } output := os.abs_path(os.args[3]) new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output)) boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, shell) or { eprintln(err) exit(1) } }