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