
Linux and KVM, Part 5: Templates and Cloud-Init, Minute Clones, Snapshots, and Backups
🌐 Machine-translated from Russian. Show original
In this part:
Template from a cloud image#
Installing every VM with an ISO is a long time. Ubuntu releases ready-made cloud images that are configured at first launch via cloud-init: name, user, SSH key, network, packages.
cd /var/lib/libvirt/images
sudo wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
sudo qemu-img info noble-server-cloudimg-amd64.img
Image catalog: cloud-images.ubuntu.com/noble/current. Analogs exist in Debian (cloud.debian.org/images/cloud) and AlmaLinux.
Step 1. VM disk over the template#
sudo qemu-img create -f qcow2 -F qcow2 \
-b /var/lib/libvirt/images/noble-server-cloudimg-amd64.img \
/var/lib/libvirt/images/app01.qcow2 30G
The new disk stores only differences from the base image — created instantly. The base image cannot be changed or deleted after that.
Step 2. User-data file#
#cloud-config
hostname: app01
timezone: Asia/Almaty
users:
- name: ivan
groups: [sudo]
shell: /bin/bash
sudo: "ALL=(ALL) NOPASSWD:ALL"
ssh_authorized_keys:
- ssh-ed25519 AAAA...ваш_публичный_ключ ivan@laptop
package_update: true
packages: [qemu-guest-agent, htop]
runcmd:
- systemctl enable --now qemu-guest-agent
Step 3. Launch#
virt-install \
--name app01 --memory 2048 --vcpus 2 \
--disk /var/lib/libvirt/images/app01.qcow2,bus=virtio \
--import --os-variant ubuntu24.04 \
--network bridge=br0,model=virtio \
--cloud-init user-data=./user-data \
--graphics none --noautoconsole
The VM is ready in a minute. Find out the address: virsh domifaddr app01 --source agent. A new VM is changing hostname in the file and the same command. If you want dozens of VMs with one command, the next step: Terraform with the dmacvicar/libvirt provider or Ansible with the community.libvirtmodule.
Clone of an existing VM#
virsh shutdown web01
virt-clone --original web01 --name web02 --auto-clone
The clone has new MAC addresses, but inside it is the same OS with the same name and machine-id. Before cloning Linux-VM, make it "clean":
sudo apt install -y guestfs-tools
sudo virt-sysprep -d web01 --operations defaults,-ssh-userdir
For Windows, the equivalent is sysprep inside the VM (C:\Windows\System32\Sysprep\sysprep.exe /generalize /oobe /shutdown). Without this, Windows clones conflict within the domain.
Snapshots#
virsh snapshot-create-as web01 before-update --description "до обновления 1С"
virsh snapshot-list web01
virsh snapshot-revert web01 before-update
virsh snapshot-delete web01 before-update
⚠️ A snapshot is not a backup. It lies on the same drive and will die with it. Hold snapshots for hours or days, not months: the chain grows and slows down the drive.
Backup#
Option 1. Turn off and copy#
The most reliable, but with downtime:
virsh shutdown db01 && sleep 60
cp --sparse=always /vm/db01.qcow2 /backup/db01-$(date +%F).qcow2
virsh dumpxml db01 > /backup/db01-$(date +%F).xml
virsh start db01
Option 2. Nonstop — external snapshot#
A qemu-guest-agent is needed in the VM: it "freezes" the file system for a second to ensure the copy is consistent.
VM=db01; D=$(date +%F)
virsh snapshot-create-as $VM bk-$D --disk-only --atomic --no-metadata --quiesce
# основной файл больше не меняется — копируем
cp --sparse=always /vm/$VM.qcow2 /backup/$VM-$D.qcow2
virsh dumpxml $VM > /backup/$VM-$D.xml
# сливаем изменения обратно и переключаемся на основной файл
virsh blockcommit $VM vda --active --pivot --wait
rm /vm/$VM.bk-$D
Check the path to the overlay file using the command virsh domblklist $VM before and after the snapshot.
Option 3. Ready-made tools (free)#
- virtnbdbackup — incremental backups of the libvirt VM via the built-in
virsh backup-beginmechanism: github.com/abbbi/virtnbdbackup. - Proxmox Backup Server — if you switch to Proxmox.
- Borg or restic — for deduplication and encryption of copies to another server or S3-compatible storage.
The 3-2-1 rule: three copies, on two different media, one outside the office. Once a month, restore VMs from backup to the test bench: a backup from which you have never been restored is hope, not a backup.
Scheduling automation#
sudo tee /etc/systemd/system/vm-backup.timer >/dev/null <<'EOF'
[Timer]
OnCalendar=*-*-* 02:00
Persistent=true
[Install]
WantedBy=timers.target
EOF
The timer requires a vm-backup.service with the same name as your script. Send the result of each backup to monitoring — specifically in the "Monitoring: Zabbix, Prometheus, and Grafana" series.
Next comes the professional level: Linux and KVM, part 6: performance, device throughput, security, and live migration.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.