aboutsummaryrefslogtreecommitdiffstats
path: root/run-openbsd.image.sh
diff options
context:
space:
mode:
Diffstat (limited to 'run-openbsd.image.sh')
-rwxr-xr-xrun-openbsd.image.sh98
1 files changed, 98 insertions, 0 deletions
diff --git a/run-openbsd.image.sh b/run-openbsd.image.sh
new file mode 100755
index 0000000..7f16897
--- /dev/null
+++ b/run-openbsd.image.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+set -o nounset
+set -o errexit
+
+_SCRIPT_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
+. "$_SCRIPT_DIR/lib.sh"
+
+CPU="$(get_ncpus)"
+MEM=$(($(get_memsize 'mb') / 2))
+DISPLAY=''
+TMP=''
+
+_ARCH_X86="x86_64"
+_ARCH_ARM="arm64"
+ARCH="$_ARCH_X86"
+
+_DISKFMT_RAW="raw"
+_DISKFMT_QCOW2="qcow2"
+DISKFMT="$_DISKFMT_RAW"
+
+main() {
+ parse_args "$@"
+
+ _ARCH_LABEL=amd64
+ [ "$ARCH" = "$_ARCH_ARM" ] && _ARCH_LABEL=arm64
+ if [ "$DISKFMT" = "$_DISKFMT_RAW" ]; then
+ DRIVE="file=$_SCRIPT_DIR/build/openbsd-7.9-$_ARCH_LABEL.raw,if=virtio,format=raw"
+ elif [ "$DISKFMT" = "$_DISKFMT_QCOW2" ]; then
+ DRIVE="file=$_SCRIPT_DIR/build/openbsd-7.9-$_ARCH_LABEL.qcow2,if=virtio,format=qcow2"
+ fi
+
+ if [ "$ARCH" = "$_ARCH_X86" ]; then
+ # shellcheck disable=SC2086
+ qemu-system-x86_64 \
+ -machine type=q35,accel=tcg \
+ -cpu max \
+ -m "$MEM" \
+ -smp "$CPU" \
+ ${TMP:+-snapshot} \
+ ${DISPLAY:+-display cocoa} \
+ -netdev user,id=net0,hostfwd=tcp::2222-:22 \
+ -device virtio-net,netdev=net0 \
+ -drive $DRIVE
+ fi
+}
+
+parse_args() {
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ -h | --help)
+ usage
+ exit 0
+ ;;
+ --mem)
+ MEM="$2"
+ shift 2
+ ;;
+ --cpu)
+ CPU="$2"
+ shift 2
+ ;;
+ --arch)
+ ARCH="$2"
+ shift 2
+ ;;
+ --disk-fmt)
+ DISKFMT="$2"
+ shift 2
+ ;;
+ --display)
+ DISPLAY=1
+ shift 1
+ ;;
+ --tmp)
+ TMP=1
+ shift 1
+ ;;
+ *)
+ echo "Unknown option: $1"
+ shift
+ ;;
+ esac
+ done
+
+ _parse_args_arch_options="$_ARCH_X86 $_ARCH_ARM"
+ if ! validate_enum "$ARCH" "$_parse_args_arch_options"; then
+ echo "[ERROR] Expected one of: '$_parse_args_arch_options'. Got '$ARCH'." >&2
+ 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
+ return 1
+ fi
+}
+
+main "$@"