Managing virtual machine storage with virsh commands

The virsh edit command offers more options than the basic ones offered by Virtual Machine Manager when it comes to storage.

If you're just beginning your virtualization project, a graphical tool like Virtual Machine Manager is a great...

starting point and provides excellent services, as it is easy to use and presents all the options you need. For more advanced administration, the virsh shell provides even more options.

There are many different management tasks that can be applied to storage devices. To start with, you may need to add storage devices to your virtual machine (VM). When adding them, different device properties may be altered to fit your needs. To get full access to the devices on your VM, use the virsh edit command on the VM you want to modify, as in virsh edit node1. This gives you the complete configuration of the VM in XML. In the code listing below, you can see what the output can look like for disk configuration.

Listing 1: Disk configuration as seen from virsh edit

    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/vgvms/lvonenodecluster'/>
      <target dev='vda' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    <disk type='block' device='disk'>
      <driver name='qemu' type='raw'/>
      <source dev='/dev/vgvms/drbdn1'/>
      <target dev='vdb' bus='virtio'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/isos/sles11sp364.iso'/>
      <target dev='hda' bus='ide'/>
      <address type='drive' controller='0' bus='0' target='0' unit='0'/>

Adding a disk from this interface is relatively easy. To start with, you'll need to define the disk type. On a typical configuration, that will be a "block" disk type, referring to a block device on the KVM host that is used as the storage back end for your disk. Another common option is the "file" disk type, which refers to anything that uses a file as the storage back end. Think, for instance, of a sparse file or an ISO file for a CD-ROM.

After specifying the disk type, you'll need to identify the type of device the VM is accessing. Following that, you'll specify the driver and type within that driver that are used. For KVM disk access, the QEMU driver offers raw access to the underlying storage back end at the host level. Following the driver specification, you can also add a cache attribute to specify where caching is handled. Different options are available: none, writethrough, writeback, directsync and unsafe. All of these are used to define how caching is synchronized between the KVM host and the VM, but for the best possible performance, switch off caching on the host completely using the cache=none option.

Next, you'll specify the source that is being used for the device. If the disk type is "block," the source will be a device (dev). If the disk type is file, the source will be a file also.

Following that, you'll indicate which target is used. This specifies which type of hardware the virtual device will recognize. For disks, typically the vd devices are used (vda for the first disk, vdb for the second disk and so on). On KVM, the virtio bus is used, which allows direct and non-emulated access to the disk devices from the VM, thus guaranteeing a minimal amount of overhead while addressing the device. Other peripheral devices, like optical drives, typically use the IDE bus, although different buses may be used as well, such as SCSI. If the device needs any specific properties, you can choose them on a separate line. In the above example code listing, you can see that the CD-ROM device is addressed as read-only, which makes perfect sense for a device that is based on an ISO image.

The last line in the configuration specifies the address of the device within the VM. As an administrator, you can just skip this line -- it will be added automatically the first time you restart the VM that is using the device.

After specifying the device properties, restart the VM and it will pick up the new configuration automatically and also auto-generate the configuration line that is required to address the new device.

This was first published in March 2014

Dig Deeper on Virtual server backup and storage



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



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: