libvirt / virsh cheatsheet (kvm)

-> Raw images using "virtio" seem fine


stop qemu-kvm

start qemu-kvm

restart qemu-kvm


Virsh Commands




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

 Add a disk

  • virsh attach-disk /var/lib/libvirt/images/ 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-dhcp-leases default
    • shows the ip
  • net-edit default
    • Default example:
        <forward mode='nat'/>
        <bridge name='virbr0' stp='on' delay='0' />
        <ip address='' netmask=''>
            <range start='' end='' />
    • Route example from
        <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="" 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


TODO: there are better tools

@see: kpartx for mounting images:

@see: fsck, sparsify:


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

Resize raw image

  • shutdown vm host


  • qemu-img resize example.root.img +25G
  • virsh start


assuming partition 1=root partition 2=swap, replace swap partition with swap file

  • swapoff -a
  • vi /etc/fstab
    • comment out swap partition
  • fdisk /dev/vda
    • p
      • isk /dev/vda: 50 GiB, 53687091200 bytes, 104857600 sectors
        Units: sectors of 1 * 512 = 512 bytes
        Sector size (logical/physical): 512 bytes / 512 bytes
        I/O size (minimum/optimal): 512 bytes / 512 bytes
        Disklabel type: dos
        Disk identifier: 0xc9cef2dc

        Device     Boot    Start      End  Sectors  Size Id Type
        /dev/vda1  *        2048 46483455 46481408 22.2G 83 Linux
        /dev/vda2       46483456 52426751  5943296  2.9G 82 Linux swap / Solaris

    • d
    • 2
    • d
    • n
    • p
    • 1
    • default (2048)
    • dfault (all space)
    • p
      • Device     Boot Start       End   Sectors Size Id Type
        /dev/vda1        2048 104857599 104855552  50G 83 Linux
    • w
  • reboot
  • resize2fs /dev/vda1
  • create swapfile instead of swap partition
    • fallocate -l 2G /swapfile
    • chmod 600 /swapfile
    • mkswap /swapfile
    • swapon /swapfile
    • swapon --show
    • vi /etc/fstab
      • /swapfile none swap sw 0 0