busybox: boot the installed package on a diskless Vinix guest
@@ -0,0 +1,75 @@
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.
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, 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+
63+fn 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+}
@@ -0,0 +1,63 @@
1+// Started by the packaged BusyBox init through /etc/inittab on a diskless guest.
2+module main
3+
4+import os
5+
6+#include <fcntl.h>
7+#include <unistd.h>
8+#include <sys/reboot.h>
9+
10+fn C.open(&char, int, ...) int
11+fn C.dup2(int, int) int
12+fn C.close(int) int
13+fn C.reboot(int) int
14+
15+fn run(argv []string) !string {
16+ result := os.exec(argv)
17+ if result.exit_code != 0 {
18+ return error('${argv} exited ${result.exit_code}: ${result.output}')
19+ }
20+ return result.output.trim_space()
21+}
22+
23+fn check(argv []string, expected string) ! {
24+ actual := run(argv)!
25+ if actual != expected {
26+ return error('${argv}: expected ${expected}, got ${actual}')
27+ }
28+ println('${argv} -> ${actual}')
29+}
30+
31+fn verify() ! {
32+ for path in ['/sbin/init', '/bin/sh', '/bin/cat'] {
33+ if !os.is_link(path) || os.real_path(path) != '/bin/busybox' {
34+ return error('${path} must resolve to /bin/busybox')
35+ }
36+ }
37+ check(['/bin/busybox', 'uname', '-m'], 'x86_64')!
38+ check(['/bin/sh', '-c', r'echo $((6 * 7))'], '42')!
39+ check(['/bin/busybox', 'ash', '-c', r'echo $((3 * 7))'], '21')!
40+ check(['/bin/cat', '/etc/inittab'], '::sysinit:/busybox-probe')!
41+ println(run(['/bin/busybox', 'sha256sum', '/bin/busybox'])!)
42+}
43+
44+fn main() {
45+ if os.getppid() != 1 || run(['/bin/busybox', 'uname', '-s']) or { '' } != 'Vinix' {
46+ eprintln('requires a Vinix guest with the probe started directly by init')
47+ exit(2)
48+ }
49+ fd := C.open(c'/dev/com1', C.O_WRONLY)
50+ if fd < 0 || C.dup2(fd, 1) < 0 || C.dup2(fd, 2) < 0 {
51+ exit(2)
52+ }
53+ if fd > 2 {
54+ C.close(fd)
55+ }
56+ verify() or {
57+ eprintln('VPAC BUSYBOX GUEST FAIL ${err}')
58+ C.reboot(C.RB_POWER_OFF)
59+ exit(1)
60+ }
61+ println('VPAC BUSYBOX GUEST PASS')
62+ C.reboot(C.RB_POWER_OFF)
63+}
@@ -0,0 +1,43 @@
1+// Emit one edited serial line to ash. Vinix COM reads wait for exactly the
2+// requested byte count. The port does not supply a terminal line discipline.
3+module main
4+
5+#include <unistd.h>
6+
7+fn C.read(int, voidptr, uint) int
8+fn C.write(int, voidptr, uint) int
9+
10+fn main() {
11+ mut line := []u8{}
12+ mut byte := u8(0)
13+ for {
14+ if C.read(0, &byte, 1) != 1 {
15+ exit(1)
16+ }
17+ match byte {
18+ 3 {
19+ eprintln('^C')
20+ return
21+ }
22+ 4 {
23+ if line.len == 0 {
24+ exit(1)
25+ }
26+ break
27+ }
28+ 8, 127 {
29+ if line.len > 0 {
30+ line.delete_last()
31+ eprint('\b \b')
32+ }
33+ }
34+ 10, 13 { break }
35+ else {
36+ line << byte
37+ C.write(2, &byte, 1)
38+ }
39+ }
40+ }
41+ eprint('\r\n')
42+ println(line.bytestr())
43+}