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