#!/usr/bin/env bash

# The script is created for starting an amd64 qemu virtual machine with specific parameters.

RESTORE=$(echo -en '\001\033[0m\002')
YELLOW=$(echo -en '\001\033[00;33m\002')

## Configuration
vcpu=8
memory=16
memory_append=`expr $memory \* 1024`
drive="$(ls *.qcow2)"
ssh_port=20000

cmd="qemu-system-x86_64 \
  -nographic -enable-kvm \
  -machine q35 -cpu max \
  -smp "$vcpu" -m "$memory"G \
  -kernel vmlinuz \
  -initrd initrd.img \
  -hda "$drive" \
  -object rng-random,filename=/dev/urandom,id=rng0 \
  -device virtio-rng-pci,rng=rng0 \
  -nic user,model=e1000,hostfwd=tcp::"$ssh_port"-:22 \
  -append 'root=/dev/sda1 rw console=ttyS0' \
  -device qemu-xhci -usb -device usb-kbd -device usb-tablet"

echo ${YELLOW}:: Starting VM...${RESTORE}
echo ${YELLOW}:: Using following configuration${RESTORE}
echo ""
echo ${YELLOW}vCPU Cores: "$vcpu"${RESTORE}
echo ${YELLOW}Memory: "$memory"G${RESTORE}
echo ${YELLOW}Disk: "$drive"${RESTORE}
echo ${YELLOW}SSH Port: "$ssh_port"${RESTORE}
echo ""
echo ${YELLOW}:: NOTE: Make sure ONLY ONE .qcow2 file is${RESTORE}
echo ${YELLOW}in the current directory${RESTORE}
echo ""

sleep 2

eval $cmd
