vinix-ports / tests / busybox / boot.v
148 lines · 144 sloc · 6.25 KB · 5e65700089755718805fb16ecac9c1dfa2b4ece0
Raw
1// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_ISO NEW_OUTPUT_DIRECTORY [MODE]
2// For ARM provide AARCH64_EFI_CODE and AARCH64_EFI_VARS, and CROSS_COMPILE for a cross GCC.
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, mode string, fixture string) ! {
16 shell := mode == '--shell'
17 arm := mode.starts_with('--aarch64-')
18 userland := mode in ['--userland', '--aarch64-userland'] || fixture != ''
19 musl := mode in ['--musl', '--aarch64-musl']
20 machine := if arm { 'aarch64' } else { 'x86_64' }
21 loader := '/lib/ld-musl-${machine}.so.1'
22 if root == '/' || output == root || output.starts_with(root + '/') {
23 return error('place the output outside a separate BusyBox package root')
24 }
25 digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex()
26 helper := if userland {
27 'userland-check.sh'
28 } else if shell {
29 'serial-line'
30 } else {
31 'busybox-probe'
32 }
33 if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) {
34 return error('use a clean package root without a test helper or inittab')
35 }
36 os.mkdir(output)!
37 boot := os.join_path(output, 'image/boot')
38 additions := os.join_path(output, 'fixture')
39 os.mkdir_all(boot)!
40 os.mkdir_all(os.join_path(additions, 'etc'))!
41 inittab := if shell {
42 "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'"
43 } else if userland {
44 console := if arm { '/dev/console' } else { '/dev/com1' }
45 "::sysinit:/bin/sh -c 'exec /bin/sh /userland-check.sh >${console} 2>&1'"
46 } else {
47 '::sysinit:/busybox-probe'
48 }
49 os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')!
50 mut loader_digest := ''
51 if userland {
52 source := if fixture != '' { fixture } else { os.join_path(os.dir(@FILE), '..', 'userland', 'check.sh') }
53 os.cp(source, os.join_path(additions, helper))!
54 } else {
55 source := if shell { 'serial-line.v' } else { 'guest.v' }
56 mut compiler := [@VEXE, '-prod', '-gc', 'none']
57 if musl {
58 loader_digest = sha256.sum(os.read_bytes(os.join_path(root, loader.trim_left('/')))!).hex()
59 specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs')
60 target_cc := if arm { os.getenv('CROSS_COMPILE') + 'gcc' } else { 'gcc' }
61 if arm { compiler << ['-os', 'linux', '-arch', 'arm64'] }
62 compiler << ['-musl', '-d', 'packaged_musl', '-cc', target_cc, '-cflags',
63 '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)]
64 } else {
65 compiler << ['-cc', 'musl-gcc', '-cflags', '-static']
66 }
67 run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])!
68 }
69 boot_files := if arm {
70 ['boot/limine-uefi-cd.bin', 'EFI/BOOT/BOOTAA64.EFI']
71 } else {
72 ['boot/limine-bios.sys', 'boot/limine-bios-cd.bin']
73 }
74 run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', ...boot_files])!
75 os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\nverbose: yes\n/BusyBox\n' +
76 ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' +
77 ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=live\n')!
78 run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot,
79 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])!
80 test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' })
81 cd_boot := if arm {
82 ['-J', '--efi-boot', 'boot/limine-uefi-cd.bin', '-efi-boot-part', '--efi-boot-image',
83 '--protective-msdos-label']
84 } else {
85 ['-b', 'boot/limine-bios-cd.bin', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table']
86 }
87 run(['xorriso', '-as', 'mkisofs', '-R', '-r', ...cd_boot, '-o', test_iso, os.dir(boot)])!
88 if shell {
89 println('Interactive serial shell image: ${test_iso}')
90 return
91 }
92 log := os.join_path(output, 'serial.log')
93 mut emulator := if arm {
94 code := os.getenv('AARCH64_EFI_CODE')
95 vars := os.join_path(output, 'vars.fd')
96 os.cp(os.getenv('AARCH64_EFI_VARS'), vars)!
97 ['qemu-system-aarch64', '-machine', 'virt,gic-version=3', '-device', 'ramfb', '-drive',
98 'if=pflash,format=raw,readonly=on,file=' + code, '-drive',
99 'if=pflash,format=raw,file=' + vars, '-device', 'virtio-scsi-pci,id=scsi0', '-drive',
100 'if=none,id=cd0,media=cdrom,readonly=on,file=' + test_iso, '-device',
101 'scsi-cd,drive=cd0,bus=scsi0.0,bootindex=1']
102 } else {
103 ['qemu-system-x86_64', '-machine', 'q35', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd']
104 }
105 memory := if userland {
106 '512'
107 } else if arm {
108 '384'
109 } else {
110 '1024'
111 }
112 emulator << ['-accel', 'tcg', '-cpu', 'max', '-m', memory, '-smp', '2', '-nic', 'none', '-display',
113 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot']
114 run(['timeout', '120s', ...emulator])!
115 lines := os.read_lines(log)!.map(it.trim_space())
116 if userland {
117 pass := if fixture != '' { 'VPAC FIXTURE GUEST PASS' } else { 'VPAC USERLAND GUEST PASS' }
118 if pass !in lines {
119 return error('userland guest verification failed; see ${log}')
120 }
121 println('Vinix ${machine} userland checks passed. See ${log}')
122 return
123 }
124 if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines {
125 return error('guest verification failed; see ${log}')
126 }
127 if musl && '${loader_digest} ${loader}' !in lines {
128 return error('guest musl differs from the packaged loader; see ${log}')
129 }
130 println('Vinix booted the installed BusyBox ${digest}. See ${log}')
131}
132
133fn main() {
134 mode := if os.args.len >= 5 { os.args[4] } else { '' }
135 script_mode := mode in ['--fixture', '--aarch64-fixture']
136 if !(os.args.len == 4 || (os.args.len == 5 && mode in ['--shell', '--musl', '--aarch64-musl',
137 '--userland', '--aarch64-userland']) || (os.args.len == 6 && script_mode)) {
138 eprintln('usage: boot.v ROOT ISO NEW_DIRECTORY [--shell|--musl|--aarch64-musl|--userland|--aarch64-userland|--fixture SCRIPT|--aarch64-fixture SCRIPT]')
139 exit(2)
140 }
141 output := os.abs_path(os.args[3])
142 new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output))
143 fixture := if script_mode { os.real_path(os.args[5]) } else { '' }
144 boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode, fixture) or {
145 eprintln(err)
146 exit(1)
147 }
148}
149