Install KVM guest on LVM-managed host with libvirt

Install KVM guest on LVM-managed host with libvirt

Goal: use logical volumes on a KVM host as virtual disks on KVM guests. The key in achieving the goal is to define a logical volume pool and then let libvirtd allocate virtual volumes for guests from that pool. The logical volume pool will be backed up by a LVM logical volume group.

  1. Create a volume group for the logical volume pool. If you already have one, you can use it and skip this step.

    vgcreate 'virtvg' '/dev/virtpv' # substitute /dev/virtpv with any block device suitable for your needs
  2. Define a volume pool and make it start automatically when libvirtd starts so that it is always availabe for your guests.

    virsh 'pool-define-as'     \
        --name 'virtvg'        \
        --type 'logical'       \
        --source-format 'lvm2' \
        --target '/dev/virtvg'
    virsh 'pool-autostart' 'virtvg'
    virsh 'pool-start' 'virtvg'
  3. Start the default network device:

    virsh 'net-start' 'default'
  4. Install a guest machine which uses a virtual volume from the newly created pool. If you would like to provide multiple volumes for your guest, just provide multiple --disk arguments. Note that the size of each volume must be explicitly given.

    virt-install                                 \
        --hvm                                    \
        --name 'myvirtwheezy'                    \
        --ram '1024'                             \
        --disk 'pool=virtvg,bus=virtio,size=12'  \
        --network 'network=default,model=virtio' \
        --os-type 'linux'                        \
        --location ''

From the guest's point of view, the logical volume (/dev/virtg/myvirtwheezy.img1) in host's virtvg volume group looks like an ordinary virtual volume (/dev/vda). The guest is free to use it as it sees appropriate, even managing it with LVM. Which would result in a nested LVM, extremly useful for creating runtime-resizable guests.