libvirt / virsh cheatsheet (kvm)

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

Service

stop qemu-kvm

start qemu-kvm

restart qemu-kvm

 

Virsh Commands

Basics

virsh

 

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

Snapshots:

 Add a disk

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

Advanced

  • 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:
      <network>
        <name>default</name>
        <uuid>6936af66-85ec-41da-3829-e3b42dea52b2</uuid>
        <forward mode='nat'/>
        <bridge name='virbr0' stp='on' delay='0' />
        <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>
    • Route example from https://help.ubuntu.com/community/KVM/Networking
      <network>
        <name>default</name>
        <uuid>12345678-1234-1234-1234-123456789abc</uuid>
        <forward mode='route'/>
        <bridge name='virbr0' stp='on' delay='0' />
        <ip address='192.168.122.1' netmask='255.255.255.0'>
          <dhcp>
            <range start='192.168.122.100' end='192.168.122.254' />
            <host mac="00:11:22:33:44:55" name="guest.example.com" ip="192.168.122.99" />
          </dhcp>
        </ip>
      </network>
  • nwfilter-list
  • nwfilter-edit entry
    • Example: nwfilter-edit allow-arp
      <filter name='allow-arp' chain='arp'>
        <uuid>f4601583-80fa-dd2f-83bb-98c9d93bdfdf</uuid>
        <rule action='accept' direction='inout' priority='500'/>
      </filter>
  •  

DHCP

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

https://people.gnome.org/~markmc/qcow-image-format.html

 

TODO: there are better tools

@see: kpartx for mounting images: https://www.ullright.org/ullWiki/show/mount-disk-image-files-kpartx

@see: fsck, sparsify: https://www.ullright.org/ullWiki/show/libguestfs-tools-virtual-machine-image-tools

 

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

Virtio

http://www.linux-kvm.org/page/Virtio

Live-Backup

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

https://serverfault.com/questions/380186/how-to-run-fsck-on-guest-vms-from-kvm

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
    done
    
    echo "do something"
    
    for VM in $VMS; do
      virsh resume $VM
    done

Resize raw image

  • shutdown vm host

hypervisor

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

host

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