// v run tests/busybox/boot.v INSTALLED_ROOT VINIX_ISO NEW_OUTPUT_DIRECTORY [MODE] // For ARM provide AARCH64_EFI_CODE and AARCH64_EFI_VARS, and CROSS_COMPILE for a cross GCC. // Set VINIX_QEMU_AMD64_DISK_BUS=nvme to use an NVMe disk for x86_64 fixtures. module main import crypto.sha256 import os import strconv fn run(argv []string) ! { result := os.exec(argv) if result.exit_code != 0 { return error('${argv}: exit ${result.exit_code}\n${result.output}') } } fn boot_image(root string, iso string, output string, mode string, fixture string, disk string) ! { deadline := strconv.atoi(os.getenv_opt('VINIX_QEMU_TIMEOUT') or { '120' }) or { return error('VINIX_QEMU_TIMEOUT must be a positive integer in seconds') } if deadline <= 0 { return error('VINIX_QEMU_TIMEOUT must be a positive integer in seconds') } shell := mode == '--shell' arm := mode.starts_with('--aarch64-') userland := mode in ['--userland', '--aarch64-userland'] || fixture != '' default_memory := if userland { '512' } else if arm { '384' } else { '1024' } memory := strconv.atoi(os.getenv_opt('VINIX_QEMU_MEMORY_MIB') or { default_memory }) or { return error('VINIX_QEMU_MEMORY_MIB must be a positive integer in MiB') } if memory <= 0 { return error('VINIX_QEMU_MEMORY_MIB must be a positive integer in MiB') } musl := mode in ['--musl', '--aarch64-musl'] machine := if arm { 'aarch64' } else { 'x86_64' } acceleration := os.getenv_opt('VINIX_QEMU_ACCEL') or { 'tcg' } if acceleration !in ['tcg', 'kvm'] || (acceleration == 'kvm' && os.uname().machine != machine) { return error('VINIX_QEMU_ACCEL must be tcg or kvm on a matching host') } cpu := if acceleration == 'kvm' { 'host' } else { 'max' } loader := '/lib/ld-musl-${machine}.so.1' amd64_disk_bus := os.getenv_opt('VINIX_QEMU_AMD64_DISK_BUS') or { 'ide' } if !arm && disk != '' && amd64_disk_bus !in ['ide', 'nvme'] { return error('VINIX_QEMU_AMD64_DISK_BUS must be ide or nvme') } if root == '/' || output == root || output.starts_with(root + '/') { return error('place the output outside a separate BusyBox package root') } if disk != '' { info := os.stat(disk)! if info.get_filetype() != .regular || !os.is_file(os.join_path(root, '.vinix-image-id')) { return error('disk fixtures require a regular image file and a matching initramfs seed') } } digest := sha256.sum(os.read_bytes(os.join_path(root, 'bin/busybox'))!).hex() helper := if userland { 'userland-check.sh' } else if shell { 'serial-line' } else { 'busybox-probe' } if os.exists(os.join_path(root, 'etc/inittab')) || os.exists(os.join_path(root, helper)) { return error('use a clean package root without a test helper or inittab') } os.mkdir(output)! boot := os.join_path(output, 'image/boot') additions := os.join_path(output, 'fixture') os.mkdir_all(boot)! os.mkdir_all(os.join_path(additions, 'etc'))! inittab := if shell { "com1::respawn:/bin/sh -c '" + r'while printf "vinix> " && line=$(/serial-line); do eval "$line"; done' + "'" } else if userland { console := if arm { '/dev/console' } else { '/dev/com1' } "::sysinit:/bin/sh -c 'exec /bin/sh /userland-check.sh >${console} 2>&1'" } else { '::sysinit:/busybox-probe' } os.write_file(os.join_path(additions, 'etc/inittab'), inittab + '\n')! mut loader_digest := '' if userland { source := if fixture != '' { fixture } else { os.join_path(os.dir(@FILE), '..', 'userland', 'check.sh') } os.cp(source, os.join_path(additions, helper))! } else { source := if shell { 'serial-line.v' } else { 'guest.v' } mut compiler := [@VEXE, '-prod', '-gc', 'none'] if musl { loader_digest = sha256.sum(os.read_bytes(os.join_path(root, loader.trim_left('/')))!).hex() specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs') target_cc := if arm { os.getenv('CROSS_COMPILE') + 'gcc' } else { 'gcc' } if arm { compiler << ['-os', 'linux', '-arch', 'arm64'] } compiler << ['-musl', '-d', 'packaged_musl', '-cc', target_cc, '-cflags', '--sysroot=' + os.quoted_path(root) + ' -specs=' + os.quoted_path(specs)] } else { compiler << ['-cc', 'musl-gcc', '-cflags', '-static'] } run([...compiler, '-o', os.join_path(additions, helper), os.join_path(os.dir(@FILE), source)])! } boot_files := if arm { ['boot/limine-uefi-cd.bin', 'EFI/BOOT/BOOTAA64.EFI'] } else { ['boot/limine-bios.sys', 'boot/limine-bios-cd.bin'] } run(['bsdtar', '-xf', iso, '-C', os.dir(boot), 'boot/vinix', ...boot_files])! disk_mode := if disk == '' { 'live' } else { 'legacy' } os.write_file(os.join_path(boot, 'limine.conf'), 'timeout: 0\nserial: yes\nverbose: yes\n/BusyBox\n' + ' protocol: limine\n kernel_path: boot():/boot/vinix\n resolution: 1024x768x32\n' + ' kaslr: no\n module_path: boot():/boot/initramfs.tar.gz\n cmdline: vinix.disk=${disk_mode}\n')! run(['tar', '--format=ustar', '--owner=0', '--group=0', '-czf', os.join_path(boot, 'initramfs.tar.gz'), '-C', root, '.', '-C', additions, './etc', './' + helper])! test_iso := os.join_path(output, if shell { 'busybox-shell.iso' } else { 'busybox.iso' }) cd_boot := if arm { ['-J', '--efi-boot', 'boot/limine-uefi-cd.bin', '-efi-boot-part', '--efi-boot-image', '--protective-msdos-label'] } else { ['-b', 'boot/limine-bios-cd.bin', '-no-emul-boot', '-boot-load-size', '4', '-boot-info-table'] } run(['xorriso', '-as', 'mkisofs', '-R', '-r', ...cd_boot, '-o', test_iso, os.dir(boot)])! if shell { println('Interactive serial shell image: ${test_iso}') return } log := os.join_path(output, 'serial.log') mut emulator := if arm { code := os.getenv('AARCH64_EFI_CODE') vars := os.join_path(output, 'vars.fd') os.cp(os.getenv('AARCH64_EFI_VARS'), vars)! ['qemu-system-aarch64', '-machine', 'virt,gic-version=3', '-device', 'ramfb', '-drive', 'if=pflash,format=raw,readonly=on,file=' + code, '-drive', 'if=pflash,format=raw,file=' + vars, '-device', 'virtio-scsi-pci,id=scsi0', '-drive', 'if=none,id=cd0,media=cdrom,readonly=on,file=' + test_iso, '-device', 'scsi-cd,drive=cd0,bus=scsi0.0,bootindex=1'] } else { ['qemu-system-x86_64', '-machine', 'q35', '-vga', 'std', '-cdrom', test_iso, '-boot', 'd'] } if disk != '' { drive := 'format=raw,snapshot=on,file=' + disk.replace(',', ',,') if arm { emulator << ['-drive', 'if=none,id=ports,' + drive, '-device', 'virtio-blk-device,drive=ports'] } else if amd64_disk_bus == 'nvme' { emulator << ['-drive', 'if=none,id=ports,' + drive, '-device', 'nvme,serial=vinix-fixture,drive=ports'] } else { emulator << ['-drive', 'if=ide,' + drive] } } emulator << ['-accel', acceleration, '-cpu', cpu, '-m', memory.str(), '-smp', '2', '-nic', 'none', '-display', 'none', '-monitor', 'none', '-serial', 'file:' + log, '-no-reboot'] run(['timeout', '${deadline}s', ...emulator])! lines := os.read_lines(log)!.map(it.trim_space()) if disk != '' { id := os.read_file(os.join_path(root, '.vinix-image-id'))!.trim_space() marker := 'VPAC DISK ROOT ${id}' // The kernel can start its exec diagnostic before the shell prints the newline. if id == '' || !lines.any(it == marker || it.starts_with(marker + 'exec: syscall ')) { return error('guest did not read the matching volume identity; see ${log}') } } if userland { pass := if fixture != '' { 'VPAC FIXTURE GUEST PASS' } else { 'VPAC USERLAND GUEST PASS' } if pass !in lines { return error('userland guest verification failed; see ${log}') } println('Vinix ${machine} userland checks passed. See ${log}') return } if '${digest} /bin/busybox' !in lines || 'VPAC BUSYBOX GUEST PASS' !in lines { return error('guest verification failed; see ${log}') } if musl && '${loader_digest} ${loader}' !in lines { return error('guest musl differs from the packaged loader; see ${log}') } println('Vinix booted the installed BusyBox ${digest}. See ${log}') } fn main() { mode := if os.args.len >= 5 { os.args[4] } else { '' } disk_mode := mode in ['--disk-fixture', '--aarch64-disk-fixture'] script_mode := mode in ['--fixture', '--aarch64-fixture'] if !(os.args.len == 4 || (os.args.len == 5 && mode in ['--shell', '--musl', '--aarch64-musl', '--userland', '--aarch64-userland']) || (os.args.len == 6 && script_mode) || (os.args.len == 7 && disk_mode)) { 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]') exit(2) } output := os.abs_path(os.args[3]) new_dir := os.join_path(os.real_path(os.dir(output)), os.file_name(output)) fixture := if script_mode || disk_mode { os.real_path(os.args[5]) } else { '' } disk := if disk_mode { os.abs_path(os.args[6]) } else { '' } boot_image(os.real_path(os.args[1]), os.abs_path(os.args[2]), new_dir, mode, fixture, disk) or { eprintln(err) exit(1) } }