libvirt + QEMU + KVM + Debian

Check cpu virtualization access:

sudo virt-host-validate

egrep -c '(vmx|svm)' /proc/cpuinfo

sudo apt-get install cpu-checker
sudo kvm-ok

Check Debian source list sources.list:

deb jessie main non-free
deb-src jessie main non-free

Free up not used space on a qcow2-image-file:

# zero fill on guest
dd if=/dev/zero of=/some/file
rm /some/file

# shut down the VM

# on host
qemu-img convert -O qcow2 original_image.qcow2 deduplicated_image.qcow2


Show supported machines types:

qemu-system-x86_64 -machine help

QEMU config file - /etc/libvirt/qemu.conf. VNC, SPICE, etc. configuration.

Networking -


Images and snapshots -

qemu-img tool:

# Show information (size, backing files, snapshots)
qemu-img info <image.qcow2>

# Create a simple QCOW2 image file
qemu-img create -f qcow2 <image.qcow2> <max_capacity_in_gigabytes>g

# Create a QCOW2 image linked with base image
qemu-img create -b <base_image> -f qcow2 -l <new_image>

# List snapshots
qemu-img snapshot -l <imagename>

# Create snapshot
qemu-img snapshot -c <snapshot-name> <imagename>

# Apply snapshot
qemu-img snapshot -a <snapshot-name> <imagename>

# Delete snapshot
qemu-img snapshot -d <snapshot-name> <imagename>

# Clone image
qemu-img convert -p -f qcow2 <source_image> -O qcow2 <dest_image>

Nested virtualization

Check is enabled:

cat /sys/module/kvm_intel/parameters/nested
# Y or N

Enable nested module on host

sudo sh -c "echo 'options kvm-intel nested=Y' > /etc/modprobe.d/kvm-intel.conf"

# reboot or reload the kernel module
modprobe -r kvm_intel
modprobe kvm_intel

Configuration in virt-manager

Make sure your VM is shut down. Open virt-manager, go to the VM details page for that VM. Click on the Processor page. In the Configuration section, there are two options - either type host-passthrough into to Model field or enable Copy host CPU configuration checkbox (that fills host-model value into the Model field). Click Apply. The difference between those two values is complicated, some details are in bug 1055002. For nested virt, you’ll probably want to use host-passthrough until issues with host-model are worked out. Be aware though, that host-passthrough is not recommended for general usage, just for nested virt purposes.

Install KVM, libvirt

sudo apt-get update
sudo apt-get install qemu-kvm libvirt-bin qemu-utils
# adduser root libvirt
# adduser lee libvirt
/etc/libvirt/         # configs
/etc/libvirt/storage/ # storage configs
/var/lib/libvirt/     # images, snapshots, etc.


/var/lib/libvirt/dnsmasq/virbr0.status - MAC/IP/hostname of domains


# Port forwarding to machine

<domain type='kvm' xmlns:qemu=''>
<interface type='network'> -> <interface type='user'>
   <qemu:arg value='-redir'/>
   <qemu:arg value='tcp:2222::22'/>

# Custom loader::

    <loader readonly='yes' type='pflash'>/path_to/OVMF_CODE-pure-efi.fd</loader>

# Network

<interface type="network">
   <source network="default"/>


# connect via VNC
virt-viewer --connect qemu+ssh://<user>@<host>/system <domain>

Virsh CMD


# get list of snapshot
virsh snapshot-list <domain>

# get snapshot info
virsh snapshot-info --domain <domain> --snapshotname <snapshot>

# create snapshot
virsh snapshot-create-as <domain> --name <snap_name> --description <snap_description>

# delete snapshot
virsh snapshot-delete --domain <domain> --snapshotname <snap_name>

# revert to snapshot
virsh snapshot-revert <domain> --snapshotname <snap_name>

Domains management:

# define domain from XML (without run)
virsh define <file.xml>

# undefine domain from XML
virsh undefine <domain>
virsh undefine --nvram <domain>                 # also remove nvram
virsh undefine --remove-all-storage <domain>    # also remove storages

# show domains list
    virsh list          # run domains
    virsh list --all    # all domains

# generate xml from domain
    virsh dumpxml <domain> > <domain>.xml

# create domain and start VM
    virsh create <config.xml>

# forcefully shutdown VM
    virsh destroy alice

# shutdown VM
    virsh shutdown <domain>

# suspend/resume VM
    virsh suspend alice
    virsh resume alice

# add domain to autostart on host run
    virsh autostart <domain>

# get domain info
    virsh dominfo <domain>

    # edit domain xml
virsh edit <domain>

# get VNC port of domain
virsh vncdisplay <domain>

Pool management:

# show pools (storages) list
    virsh pool-list         # active polls
virsh pool-list --all   # all pools

    virsh pool-define-as <name_of_storage> dir --target /etc/libvirt/images/    # create pool
    virsh pool-autostart <name_of_storage>  # add pool to autostart
    virsh pool-start <name_of_storage>      # start pool

virsh pool-destroy <pool>
virsh pool-undefine <pool>


virt-clone -o <from_domain> -n <to_domain> --auto-clone --original-xml <from_domain_xml>


# show all networks
virsh net-list

# get network info
virsh net-info <network>

# print lease info for a given network
net-dhcp-leases <network>

# print networks of domain (MAC, type, etc.)
virsh domiflist <domain>

Bridge configuration

sudo apt-get update
sudo apt-get dist-upgrade

# install drivers
sudo apt-get install firmware-realtek firmware-linux

sudo apt-get install bridge-utils

nano /etc/sysctl.conf

Create bridge::

    sudo brctl addbr br0

Network configuration /etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth1
iface eth1 inet manual

auto br0
iface br0 inet static
        address <host_ip>
        netmask <mask>
        gateway <ip>
        dns-nameservers <ip1> <ip2>
        bridge_ports eth1
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

Restart and check network configuration:

sudo /etc/init.d/networking restart

Show bridges:

brctl show


Install latest version:

sudo wget -q -O - | sudo apt-key add -
sudo sh -c 'echo "deb wily-getdeb apps" >> /etc/apt/sources.list.d/getdeb.list'
sudo apt-get update && sudo apt-get dist-upgrade
apt-get install virt-manager

# config file virt-manager

Install latest version from GIT (prefer mode) [Ubuntu 16.04 - OK]:

git clone
cd virt-manager/
sudo apt-get install -y gobject-introspection gir1.2-libosinfo-1.0 intltool-debian intltool
sudo python install -v