• Edit
  • Delete

libvirt / virsh cheatsheet (kvm)

Performance:  http://www.linux-kvm.org/page/Tuning_KVM
-> Raw images using "virtio" seem fine


stop qemu-kvm

start qemu-kvm

restart qemu-kvm


Virsh Commands




  • virsh dumpxml server.example.com > server.example.com.xml
  • virsh define /tmp/server.example.xml

 Add a disk

  • virsh attach-disk server.example.com /var/lib/libvirt/images/server.example.com-srv.img vdb --persistent


  • edit my_host_name
  • console my_host_name
    • Does not work out of the box!
  • net-list
  • net-info default
    • Bridge virbr0
  • net-edit default
    • Default example:
        <forward mode='nat'/>
        <bridge name='virbr0' stp='on' delay='0' />
        <ip address='' netmask=''>
            <range start='' end='' />
    • Route example from https://help.ubuntu.com/community/KVM/Networking
        <forward mode='route'/>
        <bridge name='virbr0' stp='on' delay='0' />
        <ip address='' netmask=''>
            <range start='' end='' />
            <host mac="00:11:22:33:44:55" name="guest.example.com" ip="" />
  • nwfilter-list
  • nwfilter-edit entry
    • Example: nwfilter-edit allow-arp
      <filter name='allow-arp' chain='arp'>
        <rule action='accept' direction='inout' priority='500'/>


Leases: /var/lib/libvirt/dnsmasq/default.leases

Create, add and rename disk images

Disk Image Types

Raw - maybe faster, direct

qcow2 - abstraction layers. allow snapshots etc


Handling images

  • qemu-img info image_file
  • show partitions for raw image
    • fdisk -lu image_file
  • mount a partition from a raw file locally:
    • fdisk -lu image_file
      • note the start of the desired partition e.g. 2048
    • mount image_file /mnt -o loop,offset=$((512*2048))
  • zerofree image
    • connect to vm console via virt-manager gui
    • Zero all empty disk space (call from within the vm)

    • apt-get install zerofree
    • reboot the vm
    • Hit "ESC" in grub menu -> Recovery -> drop to root
    • mount -o remount,ro /
    • zerofree -v /dev/sda1
  • convert lvm partition to raw image

Rsyncing images

  • rsync  --inplace 
    • This option only transmits the changed blocks of a file, not the whole file. But rsync can handle sparse files when passing the “--sparse” option. Unfortunately “--sparse” and “--inplace” cannot be used together.

      Solution: When copying the file the first time, which means it does not exist on the target server use “rsync --sparse“. This will create a sparse file on the target server and copies only the used data of the sparse file.
  • first time:
    • rsync --ignore-existing --sparse
  • subsequent times:
    • rsync --inplace

Sparse Files

  • Create
    • dd of=sparse-file bs=1k seek=5120 count=0
    • dd if=/dev/zero of=sparse-file bs=1 count=1 seek=1024k
  • Detect
    • ls -lhs
    • ls -lks sparse-file
    • du --block-size=1 sparse-file
  • Copying
    • Normal "cp" will detect sparsing
    • Copy normal file with nulls into sparse file
      • cp --sparse=always file1 file1_sparsed
    • rsync --sparse




Compress raw image into qcow2

  • qemu-img convert -c -f raw -O qcow2 input.img input.compressed.qcow2

 Rsync sparse file


fsck kvm raw image


On the hypervisor:

  • apt install libguestfs-tools
  • guestfish -a /var/lib/libvirt/images/example.img
    • run
    • list-filesystems
    • fsck ext4 /dev/sda1

Suspend and Resume all running machines

  • #!/bin/bash
    # virsh appends an empty line
    VMS=`virsh list --state-running --name | head -n -1`
    for VM in $VMS; do
      virsh suspend $VM
    echo "do something"
    for VM in $VMS; do
      virsh resume $VM