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