Dario Lo Presti - Fotolia
Expand KVM storage with virsh commands
With a few steps -- the creation of files, the addition of XML code and the execution of commands -- you can use the virsh command-line interface to add storage to existing VMs.
Storage customization is a key part of KVM's flexibility. The ability to customize storage to further optimize performance is an important driver of KVM adoption.
The virsh command-line interface manages KVM VMs, and with it, you can expand KVM storage and add on to an existing VM. Doing so requires the creation of a preallocated disk file, the addition of XML code that identifies the disk and the execution of virsh commands to attach the disk to the device.
Make a disk file
The first step to KVM storage expansion requires the creation of a disk file. A file on a disk often represents storage devices in KVM VMs. Although it's possible to connect a KVM VM to an actual storage device, it doesn't make sense in most scenarios because it's not flexible or scalable.
When you add disk files, you need to decide if this is going to be a sparse file or a file with a fixed size. Sparse files are space-efficient, as not all the required storage will be immediately allocated. This enables you to efficiently manage available KVM storage. Sparse files aren't ideal, however, because new data blocks will require allocation on the underlying physical device before the VM can use the data, which will hurt performance.
To create a preallocated disk file, use:
dd if=/dev/zero of=/var/lib/libvirt/images/vm1-disk2.img bs=1G count=10.
This command adds a disk file with a size of 10 GB to the /var/lib/libvirt/images directory, which is the default disk images directory on Red Hat and related platforms -- your distribution might use a different directory.
Use XML to connect to KVM
Add XML code that identifies the disk so KVM knows about the storage device you created. To make configuration updates easier, put the specification in a separate file. You can see what such a file should look like below:
<disk type='file' device='disk'>
<driver name='qemu' type='raw' cache='none'/>
In this file, identify the disk type as file-based storage, as you'll use this file as the storage back end. This file needs to be presented as a disk within the VM. Alternatively, you could, for instance, use option device='cdrom' to present the disk file as an optical drive within the VM.
Next, using a raw file type, identify the driver as the qemu disk driver. In this context, raw means that there's no metadata about compression or other additions to the disk device. It's a big, empty file. Alternatively, you can add a qcow2 file to identify this disk type.
The next line identifies where to find this disk, which refers to the exact location where you created the disk file earlier. The target device within the VM is then identified as vdc, the VM device type that uses the virtio driver for the most efficient disk access from within the VM.
Attach disk to device
Use the virsh attach-device command and config vm1 vm1-disk2.xml to add to the disk in order to expand KVM volume. Notice that, in this command, vm1 matches the name of the VM as shown by the virsh list command, and vm1-disk2.xml refers to the XML file that defines the disk. You might have to use a complete path specification to make this approach work.
The virsh attach-device command connects a new disk device. Virsh attach-device requires you to use an XML configuration file to define the new disk's properties, and this command might be necessary in some situations.
If you don't need to specify any options for the new disk device, you can use virsh attach-disk as an alternative for KVM storage expansion. This command offers a simpler syntax, but lacks some options. You would then use virsh attach-disk vm1 /var/lib/libvirt/images/vm1-disk2.img vdc --cache none to give you the same result.