Manage KVM QEMU virtual machines with virt
Prerequisite
sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemonAdd user to these groups to not have to use sudo/root for managing machines
sudo adduser $USER libvirt
sudo adduser $USER kvmCreate
Choose network type
If you want network bridge, this will make your VMs a part of your LAN and not NAT through your hosy
auto br0
iface br0 inet dhcp
bridge_ports enp3s0 # Replace with your actual interface
bridge_stp off
bridge_fd 0Or if you are using systemd network. Create these 3 files in /etc/systemd/network/
25-br0.netdev
[NetDev]
Name=br0
Kind=bridge
MACAddress=18:31:bf:25:1d:5f30-enp10s0.network
[Match]
Name=enp10s0
[Network]
Bridge=br035-br0.network
[Match]
Name=br0
[Network]
DHCP=ipv4
IPv6AcceptRA=yes
Address=fda9:9699:faa:cda5::21/64
[IPv6AcceptRA]
UseDNS=yes
UseDomains=yesthen run
sudo systemctl restart systemd-networkdCreate file host-bridge.xml
<network>
<name>host-bridge</name>
<forward mode="bridge"/>
<bridge name="br0"/>
</network>virsh net-define host-bridge.xml
virsh net-start host-bridge
virsh net-autostart host-bridge
sudo mkdir -p /etc/qemu
echo "allow br0" | sudo tee /etc/qemu/bridge.conf
sudo chmod 0644 /etc/qemu/bridge.conf
sudo chmod u+s /usr/lib/qemu/qemu-bridge-helperTo stop/delete to start from scratch
virsh net-destroy host-bridge
virsh net-undefine host-bridgeCreate
virt-install \
--name k8s-master-01 \
--ram 4096 \
--vcpus 2 \
--disk path=/mnt/ssd1/vms/kvm/k8s-master-01.qcow2,size=20 \
--os-variant debian13 \
--network network=host-bridge \
--graphics vnc \
--location 'https://deb.debian.org/debian/dists/trixie/main/installer-amd64/' \
--virt-type=kvmthen you need something to connect to the session through VNC. Cockpit for example.
After you have contact with the VM, enable console properly:
sudo systemctl enable serial-getty@ttyS0.service
sudo systemctl start serial-getty@ttyS0.serviceThen connect from the host with
virsh console k8s-master-01Also add this to GRUB
GRUB_CMDLINE_LINUX_DEFAULT="console=tty0 console=ttyS0,115200n8"guest agents
sudo apt install -y qemu-guest-agent spice-vdagentdmaybe you need to a Enter-keypress before it launches login prompt
VM Operations
List all
virsh list --allStart machine
virsh start debian12Edit machine
virsh edit debian12Power off machine
virsh shutdown debian12Force shut power off
virsh destroy debian12Clone machine
virt-clone --original debian12 --name debian12-copy --file /var/lib/libvirt/images/debian12-copy.qcow2Snapshot
virsh snapshot-create-as --domain k8s-master-01 \
--name post_ingress_stable \
--description "Master node with working Ingress and MetalLB" \
--atomicNetwork
List
virsh net-list --allStart network, must be done before the VM can start
virsh net-start defaultStop network
virsh net-destroy <network-name>Delete
virsh net-undefine <network-name>Network info
virsh net-info defaultGet VMs IP addresses
virsh net-dhcp-leases defaultArticle on how to set up networks in KVM
Mount disk shared with host
This is in virt manager gui
- Set up shared memory
- Press "Add hardware", select "Filesystem"
- Select the host folder, and add a name for Target path like this "mount_tag_ssd1"
Typical error message: Unable to find a satisfying virtiofsd
make sure to install virtiofsd
sudo apt install virtiofsdAdd the path inside the xml right under driver tag:
<binary path="/run/current-system/sw/bin/virtiofsd"/>Inside the VM you mount in fstab
#virtiofs
mount_tag_ssd1 /mnt/ssd1 virtiofs rw,relatime 0 0via virsh edit
<domain type='kvm'>
...
<memoryBacking>
<access mode='shared'/>
</memoryBacking>
...
<devices>
<filesystem type='mount' accessmode='passthrough'>
<driver type='virtiofs'/>
<source dir='/mnt/raid1/your_folder'/>
<target dir='my_raid_mount'/>
</filesystem>
...
</devices>
</domain>Extend size of existing disk
Host:
sudo qemu-img resize debian-vm.qcow2 +20G
virsh blockresize debian-vm vda 30Ginside VM:
sudo apt install cloud-guest-utils -y
sudo growpart /dev/vda 1
sudo resize2fs /dev/sda1NFS
Server
sudo apt install nfs-kernel-server -y
Edit /etc/exports:
/mnt/your-raid-path 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
/mnt/md1/bak/k8s-cluster1 192.168.1.0/24(rw,sync,no_subtree_check,no_root_squash)
change /etc/nfs.conf
[mountd]
port=32767sudo systemctl restart nfs-kernel-server
open firewall
tcp dport { 111, 2049, 32767 } accept
udp dport { 111, 2049, 32767 } acceptClient
sudo apt install nfs-common -y
sudo showmount -e 192.168.1.21