KVM for fun

This post is inspired by Remy van Elst’s post on how to build a few VMs with virt-install scripts for KVM.

We use VT (Virtualisation Technology) alot at work and I wanted to take the opportunity to play with KVM (libvirt) and kick the tires a little. Here is the setup tutorial for Ubuntu with a few virt-install scripts ready to use.

Installation on Ubuntu 18.04 (LTS).

apt-get install qemu-kvm libvirt-bin bridge-utils virtinst libguestfs-tools libosinfo-bin cloud-image-utils

You can also follow the official documentation from Ubuntu.

Make sure your network is properly configured the way to want it. If you are looking to forward some ports you can too.

Now for the magic, here is few one liners with virt-install.

Ubuntu 18.04 (kickstart/preseed)

Kickstart example file: ks.cfg

virt-install \
    --name=ubuntu18 \
    --disk path=/var/lib/libvirt/images/ubuntu18.img,bus=virtio,size=10 \
    --graphics none \
    --vcpus=2 --ram=2048 \
    --network bridge=virbr0,model=virtio \
    --location=http://ca.archive.ubuntu.com/ubuntu/dists/bionic/main/installer-amd64 \
    --os-type linux \
    --os-variant ubuntu18.04 \
    --console pty,target_type=serial \
    --extra-args 'ks=http://example.net/ks.cfg hostname=host domain=example.com console=ttyS0,115200n8 serial'

Ubuntu 18.04 (cloud-init removed)

# Download image.
wget https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img -O /var/lib/libvirt/images/ubuntu-cloud.img

# Resize it to 10G.
qemu-img resize /var/lib/libvirt/images/ubuntu-cloud.img 10G

# Remove cloud-init and manually set the root password.
virt-customize -a /var/lib/libvirt/images/ubuntu-cloud.img --root-password password:change_me --uninstall cloud-init

virt-install \
    --name ubuntu-cloud \
    --vcpus 1 --memory 1024 \
    --disk /var/lib/libvirt/images/ubuntu-cloud.img,device=disk,bus=virtio \
    --os-type linux \
    --os-variant ubuntu18.04 \
    --virt-type kvm \
    --graphics none \
    --network network=default,model=virtio \

CentOS 7 (with cloud-init)

# Download image.
wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-1907.qcow2 -O /var/lib/libvirt/images/centos-cloud.img

# Create user-data file.
cat << EOT >> user-data
password: change_me
chpasswd: { expire: False }
ssh_pwauth: True
hostname: box

# Generate seed.iso file.
cloud-localds -v /var/lib/libvirt/images/seed.iso user-data

virt-install \
    --name centos-cloud \
    --vcpus 1 --memory 1024 \
    --disk /var/lib/libvirt/images/centos-cloud.qcow2,device=disk,bus=virtio \
    --disk /var/lib/libvirt/images/seed.img,device=cdrom \
    --os-type linux \
    --os-variant centos7.0 \
    --virt-type kvm \
    --graphics none \
    --network default \

Kali Linux

virt-install \
    --name kali \
    --vcpus 2 --memory 2048 \
    --disk /var/lib/libvirt/images/kali.img,device=disk,bus=virtio,size=30 \
    --disk /var/lib/libvirt/images/kali-linux-2019.2-amd64.iso,device=cdrom \
    --os-type linux \
    --os-variant debiantesting \
    --virt-type kvm \
    --graphics vnc,port=5901 \
    --network network=default,model=virtio \
    --boot cdrom,hd,menu=on \

KVM cheetsheet.

virsh list --all
virsh shutdown box --mode acpi
virsh destroy box
virsh undefine box
virsh domiflist box
osinfo-query os | grep -i fedora
arp -an # find guests

And finally, some useful links: