Adding Virtio block devices at runtime in Libvirt KVM

10,987

Solution 1

These days virsh(1) has all the command-line options, you can simply run e.g.:

sudo virsh attach-disk \
           --domain guestname \
           --source /dev/volume_group/logical_volume \
           --target vdb \
           --driver qemu \
           --subdriver raw \
           --cache none \
           --io native \
           --targetbus virtio \
           --config \
           --live

virsh attach-disk --help shows it all.

Solution 2

I find the commandline way of specifying the options quite limited. Try using the attach-device action and specify the disk configuration in an XML file.

virsh # attach-device [MACHINE] /tmp/new-disk.xml

with the new-disk.xml file containing the five lines you would add using edit.

Add --persistent to have it update your machine's XML definition for you.

update

Make sure to have the hotplug kernel modules loaded in the guest before adding the device:

modprobe acpiphp
modprobe pci_hotplug

You should then see the kernel throwing some debug messages in dmesg, like this:

[  321.946440] virtio-pci 0000:00:06.0: using default PCI settings
[...]
[  321.952782]  vdb: vdb1 vdb2
Share:
10,987

Related videos on Youtube

aef
Author by

aef

Erisian pope, Free software advocate, Domain-driven organization/software architect, Elixir/Ruby programmer, GNU/Linux administrator, Scrum/Nexus master, GDPR data protection officer, Gamer

Updated on September 18, 2022

Comments

  • aef
    aef almost 2 years

    I'm running Debian Wheezy Beta 4 with KVM based guest systems which run the same operating system. I'm using LibVirt to manage the virtualisation.

    What I would like to do is to attach an LVM based block device to a running guest system through Virtio. If I would configure it through virsh edit [MACHINE] it would look like this:

    <disk type='block' device='disk'>
      <driver name='qemu' type='raw' cache='none' io='native'/>
      <source dev='/dev/volume_group/logical_volume'/>
      <target dev='vdb' bus='virtio'/>
    </disk>
    

    I tried to find out how to do this with virsh attach-disk. So far I figured the following:

    virsh attach-disk guest /dev/volume_group/logical_volume vdb --driver qemu --type raw --cache none --persistent
    

    How can I specify the target's bus and driver's io field? I really need these options to be exactly as specified in the XML.

  • aef
    aef over 11 years
    I'm always getting the following message: error: Failed to attach device from new-disk.xml and then error: End of file while reading data: Input/output error. Afterwards the libvirt daemon process is gone and I need to restart it. The exact same XML snippet works fine if I paste it manually in virsh edit [MACHINE]
  • gertvdijk
    gertvdijk over 11 years
    "End of file while reading data: Input/output error" -- sufficient permissions on the file for the user which Libvirtd runs as? and "the libvirt daemon process is gone and I need to restart it" seems a bug to me.
  • aef
    aef over 11 years
    The file is world-readable. I guess it's some kind of bug.