KVM and Libvirt - How do I hotplug a new virtio disk?
I'd like to start with a note that you should avoid using virsh attach-disk
with its limited amount of options. Instead, I'd suggest to specify the exact disk format you prefer in a separate, temporary XML file or by using the virt-manager GUI application (for the latter, skip the first step).
-
Create a temporary file with a disk definition like this one below.
adjust the properties to fit your situation
<disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/path/to/disk-image.img'/> <target dev='vdb' bus='virtio'/> </disk>
Tip: Peek into your current XML domain configuration and copy a
<disk>
section from there.virsh dumpxml <domainname>
-
Now, before adding the disk to a current domain, make sure the required hotplug kernel modules are loaded in the guest.
Some Linux distributions like recent CentOS/RHEL/Fedora have this built-in in the kernel. In this case, check for
CONFIG_HOTPLUG_PCI_ACPI
. If it'sy
, then you're all set and you can skip this step.modprobe acpiphp modprobe pci_hotplug
Consider adding these two modules to
/etc/modules
if you want them to be loaded on boot by default. -
Add the disk it to the running VM using
virsh attach-device <domain name> /path/to/disk.xml
Optionally, add the
--persistent
option to let Libvirt update the domain XML definition 'persistent'. -
Finally, check inside the guest if the disk was indeed hotplug-inserted. The kernel should be triggered, as can be checked with
dmesg
:[ 321.946440] virtio-pci 0000:00:06.0: using default PCI settings [...] [ 321.952782] vdb: vdb1 vdb2
In the above example I've added a disk as
vdb
with two partitions in the partition table.
References
- Linux-KVM wiki - Using PCI Hotplug Support
- similar question "Adding Virtio block devices at runtime in Libvirt KVM"
Related videos on Youtube
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Admin
Updated on September 18, 2022Comments
-
Admin almost 2 years
I am trying to hot-add a file-based disk to a running KVM virtual server. I've created a new disk from scratch using the command
dd of=/home/cloud/vps_59/test.img bs=1 seek=5G count=0
and I was hoping to get it hot-added to the guest by doing this in the virsh shell:
virsh # attach-disk vps_59 /home/cloud/vps_59/test.img \ vdd --driver=file --subdriver=raw
The XML definition of the domain then becomes:
<disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/home/cloud/vps_59/root.img'/> <target dev='vda' bus='virtio'/> </disk> <disk type='file' device='disk'> <driver name='file' type='raw'/> <source file='/home/cloud/vps_59/test.img'/> <target dev='vdd' bus='virtio'/> </disk>
As you can see, the driver name becomes wrong, it should be
driver name='qemu'
as the existingvda
disk. I have tried with--drive=qemu
but it states it is unsupported.Secondly, I only "see" the newly added drive once I reboot the virtual machine running Ubuntu 10.04.4 LTS. How can I make the drive "hotplug"? I want the virtual machine to "see" the new drive immediately without a reboot.
-
chutz over 11 yearsShouldn't you also add
--persistent
to make sure this configuration does not get lost when you power off the virtual machine?