#!/usr/bin/env bash

# Usage: $0 /path/to/unix-tutor.qcow2

set -x

IMAGE=$1
MEMORY_IN_MB=${MEMORY_IN_MB:-4096} # 6144 = 4096 + 2048
NUMBER_OF_CPUS=${NUMBER_OF_CPUS:-2}

################################################################################
#
# Using plain QEMU
#
# SSH access
# Run `$0 unix-tutor.qcow2 -device e1000,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22`
# to enable SSH access from the host via `ssh -p 2222 alice@localhost`.
# (See https://wiki.qemu.org/Documentation/Networking)

# File sharing
# When SSH access is enabled (see above), `sshfs` is a possible solution.
#
# Alternatively, use QEMU's "-virtfs" option:
# `$0 unix-tutor.qcow2 -virtfs local,path=/path/on/host,security_model=none,mount_tag=shared
# The mount_tag "shared" is specifically recognized by the system and you will
# have a mountpoint at /shared set up automatically.

exec qemu-system-x86_64 -machine accel=kvm:tcg -cpu max \
  -name "UNIX Tutor" \
  -m "$MEMORY_IN_MB" \
  -smp "$NUMBER_OF_CPUS" \
  "$IMAGE" \
  "${@:2}"

#
#
################################################################################
#
# Using libvirt / virt-install / virt-manager / virsh
#
# Install the image:
# virt-install --connect qemu:///session --name UNIX-Tutor --memory 4096 --osinfo nixos-unknown --import --disk $IMAGE --autoconsole none --autostart
# (Note that this will use the local user's session insteadof the system
# sesstion. You may need to launch `virt-manager` with "-c qemu:///session" to
# see the VM or use the system URI in the first place, i.e. "qemu:///system")
#
# SSH access
# Append "--network passt,portForward=2222:22" during install to forward the
# host's port 2222 to guest port 22. This allows to connect to the VM via
# `ssh -p 2222 alice@localhost`.
#
# File sharing
# When SSH access is enabled (see above), `sshfs` is a possible solution.
#
# Alternatively, append "--filesystem '/path/on/host,shared'" during
# install. The mount_tag "shared" is specifically recognized by the system and
# you will have a mountpoint at /shared set up automatically.
