#!/bin/bash

isofile=$1
shift

vnc=$(ip addr show dev eth0 | grep -oE 'inet [0-9.]+'  | cut -d' ' -f 2):1

[[ -z "$isofile" || ! -e "$isofile" ]] && exit
basedir=$(dirname $isofile)
diskfile=$basedir/disk.img

[[ ! -e "$diskfile" ]] && qemu-img create -f qcow2 $diskfile 5g

qemu-system-x86_64 \
    -enable-kvm \
    -name "deepin-test" \
    -cpu host \
    -smp 2 \
    -m 1024 \
    -drive file=$diskfile,cache=writeback,if=none,media=disk,id=virt-disk0,aio=native -device virtio-blk-pci,drive=virt-disk0 \
    -netdev user,id=hostnet0 -device virtio-net-pci,netdev=hostnet0 \
    -localtime \
    -daemonize \
    -snapshot \
    -vnc $vnc \
    -cdrom $isofile \
    -boot once=d \
    $@

echo "Connect with:"
echo "    gvncviewer $vnc"
