Configure VM (cloud-init)
Choose a name
[bash]: Set temporary environment variables
NAME=debian-dev
Choose hardware sizes
[bash]: Set temporary environment variables
MEMORY=1024
CPUS=2
DISK_SIZE=50
Choose cloud image
You can choose any standard cloud image that supports cloud-init.
Debian 12
[bash]: Set temporary environment variables
OS_VARIANT=debian12
CLOUD_IMAGE=https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2
Tip
On slighly older versions of libvirt, you may need to set OS_VARIANT differently, but the image should still work:
[bash]: Set temporary environment variables
OS_VARIANT=debian11
Fedora 40
[bash]: Set temporary environment variables
OS_VARIANT=fedora40
CLOUD_IMAGE=https://download.fedoraproject.org/pub/fedora/linux/releases/40/Cloud/x86_64/images/Fedora-Cloud-Base-Generic.x86_64-40-1.14.qcow2
Find the default subnet (virbr0
)
[bash]: Run this on your workstation:
ip route | grep virbr0 | cut -d " " -f 1
(stdout)
192.168.122.0/24
Configure IP Address and MAC address
[bash]: Set temporary environment variables
IP_ADDRESS=192.168.122.2
MAC_ADDRESS=$(printf '00:60:2F:%02X:%02X:%02X\n' $[RANDOM%256] $[RANDOM%256] | tr '[:upper:]' '[:lower:]')
Tip
You need to choose a valid IP_ADDRESS in the range of your subnet,
although on every machine I’ve tried this on so far, the default has
been 192.168.122.0/24
. The MAC address will be randomized to create
a static lease.
Create static DHCP lease
[bash]: Run this on your workstation:
sudo virsh net-update default add-last ip-dhcp-host "<host mac='${MAC_ADDRESS}' name='${NAME}' ip='${IP_ADDRESS}' />" --live --config --parent-index 0
Tip
You can edit the file manually to do more cleanup. After editing, you must stop (destroy) and restart the network.
[bash]: Run this on your workstation:
sudo virsh net-edit default
sudo virsh net-destroy default
sudo rm /var/lib/libvirt/dnsmasq/virbr0.status
sudo virsh net-start default
sudo virsh net-dhcp-leases default
Create env file to store main config settings
[bash]: Run this on your workstation:
TMP_ENV=$(mktemp)
cat << EOF > ${TMP_ENV}
export NAME=${NAME}
export OS_VARIANT=${OS_VARIANT}
export IP_ADDRESS=${IP_ADDRESS}
export MAC_ADDRESS=${MAC_ADDRESS}
export CLOUD_IMAGE=${CLOUD_IMAGE}
export MEMORY=${MEMORY}
export CPUS=${CPUS}
export DISK_SIZE=${DISK_SIZE}
export USER_DATA=~/libvirt/cloud-init/${NAME}.yaml
EOF
chmod a+r ${TMP_ENV}
sudo su ${VM_ADMIN:-libvirt-admin} -c \
"mkdir -p ~/libvirt && cp ${TMP_ENV} ~/libvirt/${NAME}.env"
Tip
This will create a new config file in the libvirt-admin user’s home directory
~/libvirt/${NAME}.env
.