diff options
Diffstat (limited to 'run-openbsd.image.sh')
| -rwxr-xr-x | run-openbsd.image.sh | 108 |
1 files changed, 85 insertions, 23 deletions
diff --git a/run-openbsd.image.sh b/run-openbsd.image.sh index 2b578c1..8226380 100755 --- a/run-openbsd.image.sh +++ b/run-openbsd.image.sh @@ -2,8 +2,8 @@ set -o nounset set -o errexit -_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) -. "$_SCRIPT_DIR/lib.sh" +SCRIPT_DIR="$(realpath "$(dirname "$0")")" +. "$SCRIPT_DIR/lib.sh" CPU="$(get_ncpus)" MEM=$(($(get_memsize 'mb') / 2)) @@ -12,23 +12,22 @@ TMP='' _ARCH_X86="x86_64" _ARCH_ARM="arm64" -ARCH="$_ARCH_X86" +ARCH="$_ARCH_ARM" -_DISKFMT_RAW="raw" -_DISKFMT_QCOW2="qcow2" -DISKFMT="$_DISKFMT_RAW" +_DISK_FORMAT_RAW="raw" +_DISK_FORMAT_QCOW2="qcow2" +DISK_FORMAT="$_DISK_FORMAT_QCOW2" + +VERSION="8.0" +EDK2_DIR="/opt/homebrew/share/qemu" +SSH_PORT="2222" +WORKSPACE_DIR="$SCRIPT_DIR/openbsd-image" +BUILD_DIR="$WORKSPACE_DIR/build" +EFIVARS_RUN="$BUILD_DIR/efivars.fd" +EDK2_CODE="$EDK2_DIR/edk2-aarch64-code.fd" main() { parse_args "$@" - - _ARCH_LABEL=amd64 - [ "$ARCH" = "$_ARCH_ARM" ] && _ARCH_LABEL=arm64 - if [ "$DISKFMT" = "$_DISKFMT_RAW" ]; then - DRIVE="file=$_SCRIPT_DIR/build/openbsd-8.0-$_ARCH_LABEL.raw,if=virtio,format=raw" - elif [ "$DISKFMT" = "$_DISKFMT_QCOW2" ]; then - DRIVE="file=$_SCRIPT_DIR/build/openbsd-8.0-$_ARCH_LABEL.qcow2,if=virtio,format=qcow2" - fi - if [ "$ARCH" = "$_ARCH_X86" ]; then # shellcheck disable=SC2086 qemu-system-x86_64 \ @@ -37,10 +36,25 @@ main() { -m "$MEM" \ -smp "$CPU" \ ${TMP:+-snapshot} \ - ${DISPLAY:+-display cocoa} \ - -netdev user,id=net0,hostfwd=tcp::2222-:22 \ + -netdev user,id=net0,hostfwd=tcp::${SSH_PORT}-:22 \ -device virtio-net,netdev=net0 \ - -drive $DRIVE + $(get_display_flag) \ + $(get_drive_flags) + elif [ "$ARCH" = "$_ARCH_ARM" ]; then + # shellcheck disable=SC2086 + qemu-system-aarch64 \ + -machine type=virt,accel=hvf \ + -cpu host \ + -m "$MEM" \ + -smp "$CPU" \ + ${TMP:+-snapshot} \ + -device qemu-xhci,id=xhci \ + -device usb-kbd,bus=xhci.0 \ + -device virtio-gpu-pci \ + -netdev user,id=net0,hostfwd=tcp::${SSH_PORT}-:22 \ + -device virtio-net,netdev=net0 \ + $(get_display_flag) \ + $(get_drive_flags) fi } @@ -63,8 +77,8 @@ parse_args() { ARCH="$2" shift 2 ;; - --disk-fmt) - DISKFMT="$2" + --disk-format) + DISK_FORMAT="$2" shift 2 ;; --display) @@ -88,11 +102,59 @@ parse_args() { return 1 fi - _parse_args_diskfmt_options="$_DISKFMT_RAW $_DISKFMT_QCOW2" - if ! validate_enum "$DISKFMT" "$_parse_args_diskfmt_options"; then - echo "[ERROR] Expected one of: '$_parse_args_diskfmt_options'. Got '$DISKFMT'." >&2 + _parse_args_disk_format_options="$_DISK_FORMAT_RAW $_DISK_FORMAT_QCOW2" + if ! validate_enum "$DISK_FORMAT" "$_parse_args_disk_format_options"; then + echo "[ERROR] Expected one of: '$_parse_args_disk_format_options'. Got '$DISK_FORMAT'." >&2 return 1 fi } +usage() { + cat <<EOF +Usage: $(basename "$0") [OPTIONS] + +Run a built OpenBSD image with QEMU. + +Options: + --mem MEMORY Memory in MB to allocate to the VM (default: $MEM) + --cpu NUM Num of CPUs to allocate to the VM (default: $CPU) + --arch ARCH Target architecture (must match built image): amd64 or arm64 (default: $ARCH) + --disk-format FORMAT Format of the VM image (default: $DISK_FORMAT) + --display Show a GUI display + --tmp Use a temporary disk that is wiped when the VM is stopped + -h, --help Show this help message +EOF +} + +get_display_flag() { + if [ -n "$DISPLAY" ]; then + echo "-display cocoa" + else + echo "-display none" + fi +} + +get_drive_flags() ( + base="$BUILD_DIR/openbsd-$VERSION-$(get_arch_label)" + + case "$DISK_FORMAT" in + "$_DISK_FORMAT_RAW") + echo "-drive file=$base.raw,if=virtio,format=raw" + ;; + "$_DISK_FORMAT_QCOW2") + echo "-drive file=$base.qcow2,if=virtio,cache=writeback,discard=ignore,format=qcow2" \ + "-drive file=$EDK2_CODE,if=pflash,unit=0,format=raw,readonly=on" \ + "-drive file=$EFIVARS_RUN,if=pflash,unit=1,format=raw" + ;; + esac +) + +get_arch_label() { + if [ "$ARCH" = "$_ARCH_ARM" ]; then + echo "aarch64" + else + echo "amd64" + fi +} + main "$@" |