#!/usr/bin/env bash
# The virtual machine has no network or host device passthrough.
source "$(dirname -- "${BASH_SOURCE[0]}")/lib.sh"
[[ $# == 1 || ( $# == 2 && $2 == --shell ) ]] || die 'Usage: tools/run-vm TEST_DIRECTORY [--shell]'
work=$(realpath -e -- "$1")
[[ -f $work/rootfs.ext4 && -f $work/command-line ]] || die 'Run make init-test first'
source "$FDS_ROOT/config/vm-test.conf"
command_line=$(cat "$work/command-line")
[[ ${2:-} != --shell ]] || command_line+=' fds.vm_shell=1'
exec "$FDS_ROOT/tools/in-void" env TMPDIR="$work" qemu-system-aarch64 \
    -machine virt -cpu cortex-a72 -accel tcg -m 1024 -smp 2 \
    -nodefaults -display none -monitor none -serial stdio -nic none -no-reboot \
    -kernel "$FDS_ROOT/out/vm-kernel/boot/vmlinux-$VM_KERNEL_VERSION" \
    -append "$command_line" \
    -device ich9-ahci,id=ahci \
    -drive "file=$work/rootfs.ext4,if=none,id=system,format=raw,snapshot=on" \
    -device ide-hd,drive=system,bus=ahci.0
