aboutsummaryrefslogtreecommitdiffstats
path: root/run-openbsd.image.sh
blob: 7f16897f8e01958119145ce603aaaf9397e2c92b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 "$@"