libvirt Cheatsheet
virsh Commands
virsh
is a CLI tool for libvirt
.
# Check libvirt version
$ virsh version
# list virtual machines
$ virsh list
Id Name State
--------------------------
3 debian running
$ virsh -c qemu:///system list --all
# list networks
$ virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active no yes
Get more info about the network (replace default
with the actual network name)
$ virsh net-info default
Name: default
UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Active: yes
Persistent: yes
Autostart: no
Bridge: virbr0
Edit network config
$ virsh net-edit default
# define the default netowrk
$ virsh net-define /usr/share/libvirt/networks/default.xml
# enable autostart
$ virsh net-autostart default
# start default network
$ virsh net-start default
# verify it is active
$ virsh net-list
Name State Autostart Persistent
--------------------------------------------
default active yes yes
autostart
=started by the network manager. virsh
may report the interface as inactive
even if it is up.
How to find inactive networks?
Inactive networks are not shown in virsh net-list
, but in virsh net-list --all
:
# returns an empty list
$ virsh net-list
# use --all to see inactive networks
$ virsh net-list --all
Name State Autostart Persistent
----------------------------------------------
default inactive no yes
How to find the IP to ssh to VMs?
net-dhcp-leases
shows mac, ip, hostname, etc. E.g. to find the IPs on the default
network:
$ virsh net-dhcp-leases default
Then ssh username@ip
.
How to resolve "Network 'default' is not active"?
Check if /usr/share/libvirt/networks/default.xml
exists:
$ cat /usr/share/libvirt/networks/default.xml
<network>
<name>default</name>
<bridge name='virbr0'/>
<forward/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
virt-install Commands
Install virt-install
:
# Install virtinst (for `virt-install`)
$ sudo apt install virtinst
Use virt-install
to create a VM:
$ virt-install --virt-type kvm --name bookworm-amd64 \
--location https://deb.debian.org/debian/dists/bookworm/main/installer-amd64/ \
--os-variant debian12 \
--disk size=10 --memory 1024 \
--graphics none \
--console pty,target_type=serial \
--extra-args "console=ttyS0"
virt-manager Commands
start virt-manager:
$ sudo virt-manager
About libvirt
libvirt
itself is NOT a hypervisor; it is a C library that supports multiple hypervisors, including
- KVM
- Xen
- OpenVZ
- LXC
- VMware ESXi, VMware Workstation
- VirtualBox (Oracle)
- Hyper-V (Microsoft)
- PowerVM (IBM)
Where to find libvirt files?
- libvirtd config:
/etc/libvirt/libvirtd.conf
- qemu config:
/etc/libvirt/qemu.conf
- xml files can be found in
/etc/libvirt/qemu/VM_NAME.xml
How to install libvirtd?
# Install libvirt related packages
$ sudo apt install libvirt-clients libvirt-daemon-system
How to check if libvirtd is running?
$ systemctl status libvirtd
How to find libvirt IP rules?
$ iptables-save | grep LIBVIRT
What is virbr0?
virbr0
is the bridge interface created by libvirt.
What is vnet0?
vnet
interfaces (e.g. vnet0
, vnet2
) are tap
interfaces. They are attached to the process running the qemu-kvm emulator.
- what the process writes to that interface will appear as having been received on that interface by the host
- what the host transmits on that interface is available for reading by that process.
qemu typically uses it for its virtualized network interface in the guest. The vnet will be added to a bridge interface which means plugging the VM into a switch.
Notice the virbr0
in vnet0
row:
$ ip link
5: vnet0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master virbr0 state UNKNOWN group default qlen 1000
The virtual network is enabled by the combination of bridge and tun/tap:
$ nmcli connection show
NAME UUID TYPE DEVICE
virbr0 d2d51ac2-xxxx-xxxx-xxxx-xxxxxxxxxxxx bridge virbr0
vnet0 daf9cebd-xxxx-xxxx-xxxx-xxxxxxxxxxxx tun vnet0
vnet1 46f11de7-xxxx-xxxx-xxxx-xxxxxxxxxxxx tun vnet1