From bab248237a1eca59fa104857d6fe6b268b9d69e0 Mon Sep 17 00:00:00 2001 From: diviocity Date: Sun, 29 Nov 2026 22:56:35 +0100 Subject: [PATCH] busybox: boot the installed package on a diskless Vinix guest --- tests/busybox/boot.v | 75 +++++++++++++++++++++++++++++++++++++ tests/busybox/guest.v | 63 +++++++++++++++++++++++++++++++ tests/busybox/serial-line.v | 43 +++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 tests/busybox/boot.v create mode 100644 tests/busybox/guest.v create mode 100644 tests/busybox/serial-line.v diff --git a/tests/busybox/boot.v b/tests/busybox/boot.v new file mode 100644 index 0000000..7aba772 --- /dev/null +++ b/tests/busybox/boot.v @@ -0,0 +1,75 @@ +// 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) + } +} diff --git a/tests/busybox/guest.v b/tests/busybox/guest.v new file mode 100644 index 0000000..11bcd21 --- /dev/null +++ b/tests/busybox/guest.v @@ -0,0 +1,63 @@ +// Started by the packaged BusyBox init through /etc/inittab on a diskless guest. +module main + +import os + +#include +#include +#include + +fn C.open(&char, int, ...) int +fn C.dup2(int, int) int +fn C.close(int) int +fn C.reboot(int) int + +fn run(argv []string) !string { + result := os.exec(argv) + if result.exit_code != 0 { + return error('${argv} exited ${result.exit_code}: ${result.output}') + } + return result.output.trim_space() +} + +fn check(argv []string, expected string) ! { + actual := run(argv)! + if actual != expected { + return error('${argv}: expected ${expected}, got ${actual}') + } + println('${argv} -> ${actual}') +} + +fn verify() ! { + for path in ['/sbin/init', '/bin/sh', '/bin/cat'] { + if !os.is_link(path) || os.real_path(path) != '/bin/busybox' { + return error('${path} must resolve to /bin/busybox') + } + } + check(['/bin/busybox', 'uname', '-m'], 'x86_64')! + check(['/bin/sh', '-c', r'echo $((6 * 7))'], '42')! + check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')! + check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')! + println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!) +} + +fn main() { + if os.getppid() != 1 || run(['/bin/busybox', 'uname', '-s']) or { '' } != 'Vinix' { + eprintln('requires a Vinix guest with the probe started directly by init') + exit(2) + } + fd := C.open(c'/dev/com1', C.O_WRONLY) + if fd < 0 || C.dup2(fd, 1) < 0 || C.dup2(fd, 2) < 0 { + exit(2) + } + if fd > 2 { + C.close(fd) + } + verify() or { + eprintln('VPAC BUSYBOX GUEST FAIL ${err}') + C.reboot(C.RB_POWER_OFF) + exit(1) + } + println('VPAC BUSYBOX GUEST PASS') + C.reboot(C.RB_POWER_OFF) +} diff --git a/tests/busybox/serial-line.v b/tests/busybox/serial-line.v new file mode 100644 index 0000000..a3c173b --- /dev/null +++ b/tests/busybox/serial-line.v @@ -0,0 +1,43 @@ +// Emit one edited serial line to ash. Vinix COM reads wait for exactly the +// requested byte count. The port does not supply a terminal line discipline. +module main + +#include + +fn C.read(int, voidptr, uint) int +fn C.write(int, voidptr, uint) int + +fn main() { + mut line := []u8{} + mut byte := u8(0) + for { + if C.read(0, &byte, 1) != 1 { + exit(1) + } + match byte { + 3 { + eprintln('^C') + return + } + 4 { + if line.len == 0 { + exit(1) + } + break + } + 8, 127 { + if line.len > 0 { + line.delete_last() + eprint('\b \b') + } + } + 10, 13 { break } + else { + line << byte + C.write(2, &byte, 1) + } + } + } + eprint('\r\n') + println(line.bytestr()) +} -- 2.56.1