Problem solve Get help with specific problems with your technologies, process and projects.

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'/>

<source file='/var/lib/libvirt/images/vm1-disk2.img'/

<target dev='vdc'/>

</disk>

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.

This was last published in January 2018

Dig Deeper on Open source virtualization

PRO+

Content

Find more PRO+ content and other member only offers, here.

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

How do you use virsh commands in your environment?
Cancel

-ADS BY GOOGLE

SearchVMware

SearchWindowsServer

SearchCloudComputing

SearchVirtualDesktop

SearchDataCenter

Close