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