# Will bring in (is a simple wrapper for) `qemu-system-x86` and
# `qemu-system-common`. Some guides install `qemu` but this brings in
# `qemu-system` (which in turn brings in *all* `qemu-system-*`!) and
# `qemu-user`, which we don't need.
sudo apt-get install qemu-kvm

# Brings in `libvirt-daemon-system` (which brings in `libvirt-daemon` and
# configures it as a system service) and `libvirt-clients` (which includes the
# binary `virsh`).
sudo apt-get install libvirt-bin

# Add user to the `kvm` and `libvirt` groups.
sudo adduser "$USER" -a -G "kvm,libvirt"

# Create, manage and display the graphical console of virtual machines from
# GUI.
# sudo apt-get install virt-manager

# Create virtual machines from CLI.
sudo apt-get install virtinst

# Add support for `virt-install --os-variant "auto"`.
sudo apt-get install libosinfo-bin

# If virt-manager is not used, but one wants to connect to the virtual machines
# console, virt-viewer can be used instead. Recommended by `virtinst`.
sudo apt-get install virt-viewer

# Enable the network "default" on the "qemu:///system" domain.
# <https://wiki.debian.org/KVM#Libvirt_default_network> (note that this link,
# at the time of writing, mistakenly say `iptables` is required while it is in
# fact `ebtables` that is required).
sudo apt-get install dnsmasq-base bridge-utils ebtables
sudo systemctl restart libvirtd
virsh net-list --all
sudo virsh --connect="qemu:///system" net-start "default"
sudo virsh --connect="qemu:///system" net-autostart "default"
virsh net-list --all

# Optional Vagrant integration.
sudo apt-get install vagrant-libvirt
# sudo apt-get install vagrant-mutate

# Netboot
# location="http://ftp.debian.org/debian/dists/stable/main/installer-amd64/"
# location="http://archive.ubuntu.com/ubuntu/dists/bionic-updates/main/installer-amd64/"
# Netinst
# https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-10.3.0-amd64-netinst.iso
# location="debian-10.3.0-amd64-netinst.iso"
# Live
# cdrom="https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/debian-live-10.3.0-amd64-standard.iso"
# cdrom="https://cdimage.debian.org/images/unofficial/non-free/images-including-firmware/current-live/amd64/iso-hybrid/debian-live-10.3.0-amd64-standard+nonfree.iso"
# cdrom="http://releases.ubuntu.com/bionic/ubuntu-18.04.4-desktop-amd64.iso"
# cdrom="http://releases.ubuntu.com/bionic/ubuntu-18.04.4-live-server-amd64.iso"

name="$(
    echo "$location" \
    | sed -n 's#^https\?://[^/]\+/\([^/]\+\)/dists/\([^/]\+\)/.*/installer-\([^/]\+\)/$#\1-\2-\3#p'
)"
# name="$(basename "$cdrom")"

virt-install \
    --hvm --virt-type "kvm" \
    --noreboot \
    --os-type "linux" --os-variant "generic" \
    --name "$name" \
    --location "$location" \
    --filesystem="source=$(pwd),target=/vagrant" \
    --memory "1024" \
    --disk "none" \
    --graphics "none" --extra-args "console=ttyS0" \
|| virsh destroy "$name" ; virsh undefine "$name"

virsh list --all --name \
| xargs -n 1 sh -c 'virsh destroy "$1" ; virsh undefine "$1"' _
virsh list --all

virt-install --debug \

--location "$location" \
--cdrom "$cdrom" \

--graphics "none" --extra-args "console=ttyS0" \
--extra-args "console=tty0 console=ttyS0" \
--graphics "none" --extra-args "console=tty0 console=ttyS0" \

--extra-args="preseed=https://preseed.$(dnsdomainname)/preseed.cfg" \
--extra-args="preseed=file:/preseed.cfg" --initrd-inject="preseed.cfg" \

# https://wiki.qemu.org/Documentation/9psetup#Mounting_the_shared_path
# sudo apt-get install linux-image-extra-virtual 9mount
--filesystem="source=$(pwd),target=/vagrant" \
mkdir -p "/tmp/host_files"
mount -t 9p -otrans=virtio,version=9p2000.L "/vagrant" "/tmp/host_files"