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