Skip to content

Instantly share code, notes, and snippets.

@devisnotnull
Last active May 29, 2018 21:21
Show Gist options
  • Save devisnotnull/18cf9b3422ad170a7aca2acba69b2758 to your computer and use it in GitHub Desktop.
Save devisnotnull/18cf9b3422ad170a7aca2acba69b2758 to your computer and use it in GitHub Desktop.
KVM virt-install Ubuntu 18.04
#!/bin/bash
if [ $# -ne 1 ] ; then
echo 'Please provide a name for the virtual machine '
exit 1
fi
############################################
function print_with_line_numbers {
local IFS=$'\n'
local lines=($1)
local i
echo ${lines[0]}
}
############################################
# Download boot iso
sudo wget http://releases.ubuntu.com/18.04/ubuntu-18.04-live-server-amd64.iso -O /var/lib/libvirt/boot/ubuntu-18.04.iso
NAME=$1
# Create new VM, Note that you can connect to this machine via ssh reverse proxy and the VNC client
virt-install --virt-type=kvm \
--name $NAME --ram 2048 --vcpus=1 \
--os-variant=ubuntu17.04 --cdrom=/var/lib/libvirt/boot/ubuntu-18.04-live-server-amd64.iso \
--graphics vnc --network bridge:vmbr2,model=virtio \
--disk path=/var/lib/libvirt/images/$NAME.qcow2,size=40,bus=virtio,format=qcow2 \
--noautoconsole
############################################
# You should get something similar to below
# <graphics type='vnc' port='5900' autoport='yes' listen='127.0.0.1'>
ITEM=$(virsh dumpxml $NAME | grep vnc)
############################################
RESULT="$(echo $ITEM | grep -oE '[0-9-]+')"
PORT=$(print_with_line_numbers "$RESULT")
# You will now need to forward the previous port number over ssh to your host; Change 5900 as required
# ssh root@167.2.2.2 -L 5900:127.0.0.1:5900
echo "VNC PORT TO CONNECT TO "$PORT
echo "Proxy command : ssh root@127.0.0.1 -L $PORT:127.0.0.1:$PORT"
# Now open the VNC client app, Create a new connection to 127.0.0.1:5900 and BOOSH your in
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment