This repository provides Bash scripts to create, deploy, and remove cloud-init-enabled virtual machines using virt-install
. It has been tested on Ubuntu 24.04 LTS and works on other recent Ubuntu/Debian systems with a proper libvirt setup.
sudo apt update
sudo apt install libvirt-daemon-system libvirt-clients qemu-kvm virtinst wget cloud-image-utils
For non-root usage, add your user to the libvirt
and kvm
groups:
sudo usermod -aG libvirt,kvm $USER
Example templates are provided in the templates
directory:
cloud-config.yml-example
launch-vm.ini-example
To set up your configuration:
cp templates/cloud-config.yml-example templates/cloud-config.yml
cp templates/launch-vm.ini-example templates/launch-vm.ini
Edit:
launch-vm.ini
to configure network, CPUs, RAM, storage pool, etc.cloud-config.yml
to define the default user, password, and SSH key.
To support additional distributions, create an INI file in templates/
(e.g., debian12.ini
) with:
SOURCE=source-debian12.img
URL=https://cloud.debian.org/images/cloud/bookworm/daily/latest/debian-12-generic-amd64-daily.qcow2
OSVARIANT=debian12
VMPOOL=vm-pool
CONSOLE="pty,target_type=virtio"
This script stores base images and cloned VMs in a libvirt storage pool (vm-pool
by default). Create it if it doesn't exist:
virsh pool-define-as vm-pool dir - - - - "/var/lib/libvirt/images/vms"
virsh pool-build vm-pool
virsh pool-start vm-pool
virsh pool-autostart vm-pool
./bin/launch-vm.sh -d ubuntu22.04 -n MyUbuntuVM -c 2 -m 2048 -s 32
-d ubuntu22.04
→ Usestemplates/ubuntu22.04.ini
-n MyUbuntuVM
→ VM name-c 2
→ CPUs-m 2048
→ Memory (MB)-s 32
→ Disk size (GB)
Set the LIBVIRT_DEFAULT_URI
environment variable:
export LIBVIRT_DEFAULT_URI="qemu+ssh://user@hypervisor/system"
bin/remove-vm.sh -d <VM_NAME>
virsh vol-delete --pool vm-pool --vol source-ubuntu22.04.img
The next VM deployment will download a fresh image.
To resolve VM hostnames locally:
-
Install
libnss-libvirt
:sudo apt install libnss-libvirt
-
Edit
/etc/nsswitch.conf
, addinglibvirt libvirt_guest
to thehosts
line:hosts: files libvirt libvirt_guest dns
Now you can SSH to VMs using their hostname.
export LAUNCH_VM_INI="/path/to/custom-launch-vm.ini"
./launch-vm.sh -d ubuntu22.04 -n test-vm
Uses
/path/to/custom-launch-vm.ini
instead oftemplates/launch-vm.ini
.
export CLOUD_CONFIG="/custom/path/cloud-config.yml"
./launch-vm.sh -d ubuntu22.04 -n test-vm
Uses
/custom/path/cloud-config.yml
instead oftemplates/cloud-config.yml
.
- rotflol (Ronald Offerman) – Testing and contributions.