vinix-ports / tests / busybox / boot.v
212 lines · 208 sloc · 8.98 KB · 8fa8a4aa90d888df853d3ebf72b579e1745994dd
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.
3// Set VINIX_QEMU_AMD64_DISK_BUS=nvme to use an NVMe disk for x86_64 fixtures.
4// Set VINIX_QEMU_VCPUS=1 to reproduce a single-vCPU guest.
5module main
6
7import crypto.sha256
8import os
9import strconv
10
11fn run(argv []string) ! {
12 result := os.exec(argv)
13 if result.exit_code != 0 {
14 return error('${argv}: exit ${result.exit_code}\n${result.output}')
15 }
16}
17
18fn boot_image(root string, iso string, output string, mode string, fixture string, disk string) ! {
19 deadline := strconv.atoi(os.getenv_opt('VINIX_QEMU_TIMEOUT') or { '120' }) or {
20 return error('VINIX_QEMU_TIMEOUT must be a positive integer in seconds')
21 }
22 if deadline <= 0 {
23 return error('VINIX_QEMU_TIMEOUT must be a positive integer in seconds')
24 }
25 shell := mode == '--shell'
26 arm := mode.starts_with('--aarch64-')
27 userland := mode in ['--userland', '--aarch64-userland'] || fixture != ''
28 default_memory := if userland {
29 '512'
30 } else if arm {
31 '384'
32 } else {
33 '1024'
34 }
35 memory := strconv.atoi(os.getenv_opt('VINIX_QEMU_MEMORY_MIB') or { default_memory }) or {
36 return error('VINIX_QEMU_MEMORY_MIB must be a positive integer in MiB')
37 }
38 if memory <= 0 {
39 return error('VINIX_QEMU_MEMORY_MIB must be a positive integer in MiB')
40 }
41 vcpus := strconv.atoi(os.getenv_opt('VINIX_QEMU_VCPUS') or { '2' }) or {
42 return error('VINIX_QEMU_VCPUS must be a positive integer')
43 }
44 if vcpus <= 0 {
45 return error('VINIX_QEMU_VCPUS must be a positive integer')
46 }
47 musl := mode in ['--musl', '--aarch64-musl']
48 machine := if arm { 'aarch64' } else { 'x86_64' }
49 acceleration := os.getenv_opt('VINIX_QEMU_ACCEL') or { 'tcg' }
50 if acceleration !in ['tcg', 'kvm'] || (acceleration == 'kvm' && os.uname().machine != machine) {
51 return error('VINIX_QEMU_ACCEL must be tcg or kvm on a matching host')
52 }
53 cpu := if acceleration == 'kvm' { 'host' } else { 'max' }
54 loader := '/lib/ld-musl-${machine}.so.1'
55 amd64_disk_bus := os.getenv_opt('VINIX_QEMU_AMD64_DISK_BUS') or { 'ide' }
56 if !arm && disk != '' && amd64_disk_bus !in ['ide', 'nvme'] {
57 return error('VINIX_QEMU_AMD64_DISK_BUS must be ide or nvme')
58 }
59 if root == '/' || output == root || output.starts_with(root + '/') {
60 return error('place the output outside a separate BusyBox package root')
61 }
62 if disk != '' {
63 info := os.stat(disk)!
64 if info.get_filetype() != .regular || !os.is_file(os.join_path(root, '.vinix-image-id')) {
65 return error('disk fixtures require a regular image file and a matching initramfs seed')
66 }
67 }
68 digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex()
69 helper := if userland {
70 'userland-check.sh'
71 } else if shell {
72 'serial-line'
73 } else {
74 'busybox-probe'
75 }
76 if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) {
77 return error('use a clean package root without a test helper or inittab')
78 }
79 os.mkdir(output)!
80 boot := os.join_path(output, 'image/boot')
81 additions := os.join_path(output, 'fixture')
82 os.mkdir_all(boot)!
83 os.mkdir_all(os.join_path(additions, 'etc'))!
84 inittab := if shell {
85 "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'"
86 } else if userland {
87 console := if arm { '/dev/console' } else { '/dev/com1' }
88 "::sysinit:/bin/sh -c 'exec /bin/sh /userland-check.sh >${console} 2>&1'"
89 } else {
90 '::sysinit:/busybox-probe'
91 }
92 os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')!
93 mut loader_digest := ''
94 if userland {
95 source := if fixture != '' {
96 fixture
97 } else {
98 os.join_path(os.dir(@FILE), '..', 'userland', 'check.sh')
99 }
100 os.cp(source, os.join_path(additions, helper))!
101 } else {
102 source := if shell { 'serial-line.v' } else { 'guest.v' }
103 mut compiler := [@VEXE, '-prod', '-gc', 'none']
104 if musl {
105 loader_digest = sha256.sum(os.read_bytes(os.join_path(root, loader.trim_left('/')))!).hex()
106 specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs')
107 target_cc := if arm { os.getenv('CROSS_COMPILE') + 'gcc' } else { 'gcc' }
108 if arm { compiler << ['-os', 'linux', '-arch', 'arm64'] }
109 compiler << ['-musl', '-d', 'packaged_musl', '-cc', target_cc, '-cflags',
110 '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)]
111 } else {
112 compiler << ['-cc', 'musl-gcc', '-cflags', '-static']
113 }
114 run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])!
115 }
116 boot_files := if arm {
117 ['boot/limine-uefi-cd.bin', 'EFI/BOOT/BOOTAA64.EFI']
118 } else {
119 ['boot/limine-bios.sys', 'boot/limine-bios-cd.bin']
120 }
121 run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', ...boot_files])!
122 disk_mode := if disk == '' { 'live' } else { 'legacy' }
123 os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\nverbose: yes\n/BusyBox\n' +
124 ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' +
125 ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=${disk_mode}\n')!
126 run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot,
127 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])!
128 test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' })
129 cd_boot := if arm {
130 ['-J', '--efi-boot', 'boot/limine-uefi-cd.bin', '-efi-boot-part', '--efi-boot-image',
131 '--protective-msdos-label']
132 } else {
133 ['-b', 'boot/limine-bios-cd.bin', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table']
134 }
135 run(['xorriso', '-as', 'mkisofs', '-R', '-r', ...cd_boot, '-o', test_iso, os.dir(boot)])!
136 if shell {
137 println('Interactive serial shell image: ${test_iso}')
138 return
139 }
140 log := os.join_path(output, 'serial.log')
141 mut emulator := if arm {
142 code := os.getenv('AARCH64_EFI_CODE')
143 vars := os.join_path(output, 'vars.fd')
144 os.cp(os.getenv('AARCH64_EFI_VARS'), vars)!
145 ['qemu-system-aarch64', '-machine', 'virt,gic-version=3', '-device', 'ramfb', '-drive',
146 'if=pflash,format=raw,readonly=on,file=' + code, '-drive',
147 'if=pflash,format=raw,file=' + vars, '-device', 'virtio-scsi-pci,id=scsi0', '-drive',
148 'if=none,id=cd0,media=cdrom,readonly=on,file=' + test_iso, '-device',
149 'scsi-cd,drive=cd0,bus=scsi0.0,bootindex=1']
150 } else {
151 ['qemu-system-x86_64', '-machine', 'q35', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd']
152 }
153 if disk != '' {
154 drive := 'format=raw,snapshot=on,file=' + disk.replace(',', ',,')
155 if arm {
156 emulator << ['-drive', 'if=none,id=ports,' + drive, '-device',
157 'virtio-blk-device,drive=ports']
158 } else if amd64_disk_bus == 'nvme' {
159 emulator << ['-drive', 'if=none,id=ports,' + drive, '-device',
160 'nvme,serial=vinix-fixture,drive=ports']
161 } else {
162 emulator << ['-drive', 'if=ide,' + drive]
163 }
164 }
165 emulator << ['-accel', acceleration, '-cpu', cpu, '-m', memory.str(), '-smp', vcpus.str(),
166 '-nic', 'none', '-display', 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot']
167 run(['timeout', '${deadline}s', ...emulator])!
168 lines := os.read_lines(log)!.map(it.trim_space())
169 if disk != '' {
170 id := os.read_file(os.join_path(root, '.vinix-image-id'))!.trim_space()
171 marker := 'VPAC DISK ROOT ${id}'
172 // The kernel can start its exec diagnostic before the shell prints the newline.
173 if id == '' || !lines.any(it == marker || it.starts_with(marker + 'exec: syscall ')) {
174 return error('guest did not read the matching volume identity; see ${log}')
175 }
176 }
177 if userland {
178 pass := if fixture != '' { 'VPAC FIXTURE GUEST PASS' } else { 'VPAC USERLAND GUEST PASS' }
179 if pass !in lines {
180 return error('userland guest verification failed; see ${log}')
181 }
182 println('Vinix ${machine} userland checks passed. See ${log}')
183 return
184 }
185 if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines {
186 return error('guest verification failed; see ${log}')
187 }
188 if musl && '${loader_digest} ${loader}' !in lines {
189 return error('guest musl differs from the packaged loader; see ${log}')
190 }
191 println('Vinix booted the installed BusyBox ${digest}. See ${log}')
192}
193
194fn main() {
195 mode := if os.args.len >= 5 { os.args[4] } else { '' }
196 disk_mode := mode in ['--disk-fixture', '--aarch64-disk-fixture']
197 script_mode := mode in ['--fixture', '--aarch64-fixture']
198 if !(os.args.len == 4 || (os.args.len == 5 && mode in ['--shell', '--musl', '--aarch64-musl',
199 '--userland', '--aarch64-userland']) || (os.args.len == 6 && script_mode)
200 || (os.args.len == 7 && disk_mode)) {
201 eprintln('usage: boot.v ROOT ISO NEW_DIRECTORY [--shell|--musl|--aarch64-musl|--userland|--aarch64-userland|--fixture SCRIPT|--aarch64-fixture SCRIPT|--disk-fixture SCRIPT DISK|--aarch64-disk-fixture SCRIPT DISK]')
202 exit(2)
203 }
204 output := os.abs_path(os.args[3])
205 new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output))
206 fixture := if script_mode || disk_mode { os.real_path(os.args[5]) } else { '' }
207 disk := if disk_mode { os.abs_path(os.args[6]) } else { '' }
208 boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode, fixture, disk) or {
209 eprintln(err)
210 exit(1)
211 }
212}
213