vinix-ports / tests / busybox / boot.v
75 lines · 71 sloc · 3.4 KB
Raw
1// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]
2// Run on x86_64 Linux with musl-gcc, bsdtar, GNU tar, xorriso and QEMU installed.
3module main
4
5import crypto.sha256
6import os
7
8fn run(argv []string) ! {
9 result := os.exec(argv)
10 if result.exit_code != 0 {
11 return error('${argv}: exit ${result.exit_code}\n${result.output}')
12 }
13}
14
15fn boot_image(root string, iso string, output string, shell bool) ! {
16 if root == '/' || output == root || output.starts_with(root + '/') {
17 return error('place the output outside a separate BusyBox package root')
18 }
19 digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex()
20 helper := if shell { 'serial-line' } else { 'busybox-probe' }
21 if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) {
22 return error('use a clean root containing just the installed BusyBox package')
23 }
24 os.mkdir(output)!
25 boot := os.join_path(output, 'image/boot')
26 additions := os.join_path(output, 'fixture')
27 os.mkdir_all(boot)!
28 os.mkdir_all(os.join_path(additions, 'etc'))!
29 inittab := if shell {
30 "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'"
31 } else {
32 '::sysinit:/busybox-probe'
33 }
34 os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')!
35 source := if shell { 'serial-line.v' } else { 'guest.v' }
36 run([@VEXE, '-prod', '-gc', 'none', '-cc', 'musl-gcc', '-cflags', '-static', '-o',
37 os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])!
38 run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', 'boot/limine-bios.sys',
39 'boot/limine-bios-cd.bin'])!
40 os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\n/BusyBox\n' +
41 ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' +
42 ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=live\n')!
43 run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot,
44 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])!
45 test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' })
46 run(['xorriso', '-as', 'mkisofs', '-R', '-r', '-b', 'boot/limine-bios-cd.bin', '-no-emul-boot',
47 '-boot-load-size', '4', '-boot-info-table', '-o', test_iso, os.dir(boot)])!
48 if shell {
49 println('Interactive serial shell image: ${test_iso}')
50 return
51 }
52 log := os.join_path(output, 'serial.log')
53 run(['timeout', '90s', 'qemu-system-x86_64', '-machine', 'q35', '-accel', 'tcg', '-cpu', 'max',
54 '-m', '1024', '-smp', '2', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd', '-nic', 'none',
55 '-display', 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot'])!
56 lines := os.read_lines(log)!.map(it.trim_space())
57 if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines {
58 return error('guest verification failed; see ${log}')
59 }
60 println('Vinix booted the installed BusyBox ${digest}. See ${log}')
61}
62
63fn main() {
64 shell := os.args.len == 5 && os.args[4] == '--shell'
65 if os.args.len != 4 && !shell {
66 eprintln('usage: boot.v INSTALLED_ROOT VINIX_AMD64_ISO NEW_OUTPUT_DIRECTORY [--shell]')
67 exit(2)
68 }
69 output := os.abs_path(os.args[3])
70 new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output))
71 boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, shell) or {
72 eprintln(err)
73 exit(1)
74 }
75}
76