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