How to Hotplug Virtio Disk in KVM/Libvirt Without Reboot


2 views

When attempting to hot-add a virtio disk to a running KVM virtual machine, two common problems arise:

  • The driver name gets incorrectly set to 'file' instead of 'qemu'
  • The disk isn't immediately visible in the guest OS without reboot

The correct virsh command for attaching a virtio disk should be:

virsh attach-disk vps_59 /home/cloud/vps_59/test.img vdd \
--driver qemu --subdriver raw --cache none --targetbus virtio --persistent

After proper attachment, your domain XML should show:

<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/home/cloud/vps_59/test.img'/>
  <target dev='vdd' bus='virtio'/>
  <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>

For Ubuntu 10.04 guests, you'll need to manually rescan the SCSI bus:

# First find the host number
ls /sys/class/scsi_host/
# Then trigger rescan (assuming host0)
echo "- - -" > /sys/class/scsi_host/host0/scan

Create a disk XML file (newdisk.xml):

<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/home/cloud/vps_59/test.img'/>
  <target dev='vdd' bus='virtio'/>
</disk>

Then attach it with:

virsh attach-device vps_59 newdisk.xml --persistent
  • Verify QEMU guest agent is running in the VM
  • Check dmesg output in the guest for disk detection messages
  • Ensure the virtio-scsi controller is present in the VM configuration

When working with KVM virtualization, properly hotplugging virtio disks requires precise XML configuration and guest OS cooperation. The key pain points emerge when:

  1. The libvirt XML gets incorrect driver specifications (file vs qemu)
  2. The guest kernel fails to recognize new devices without rescan

The original approach had incorrect driver declaration. Here's the proper virsh command:

virsh attach-disk vps_59 /home/cloud/vps_59/test.img vdd \
  --driver qemu --subdriver raw --targetbus virtio --persistent

This generates correct XML:


  
  
  
  

For Ubuntu guests, trigger device rescan with these steps:

# First check current PCI devices
ls /sys/bus/pci/devices/

# Find the new device (should appear as virtio-pci)
# Then trigger rescan:
echo 1 > /sys/bus/pci/rescan

# Alternative method for block devices:
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "- - -" > /sys/class/scsi_host/host1/scan

Create a complete hotplug script:

#!/bin/bash
VM_NAME="vps_59"
DISK_PATH="/home/cloud/vps_59/test.img"
DISK_NAME="vdd"

# Create disk if not exists
[[ ! -f $DISK_PATH ]] && \
  truncate -s 5G $DISK_PATH

# Attach disk
virsh attach-disk $VM_NAME $DISK_PATH $DISK_NAME \
  --driver qemu --subdriver raw --targetbus virtio --persistent

# Connect to guest and trigger rescan
virsh qemu-agent-command $VM_NAME \
  '{"execute":"guest-exec","arguments":{"path":"sh","arg":["-c","echo 1 > /sys/bus/pci/rescan"]}}'

For Windows guests, use the devcon utility to scan for hardware changes.

  • Verify QEMU guest agent is running in the VM
  • Check kernel messages with dmesg | grep virtio
  • Ensure the virtio-scsi controller is enabled in VM configuration
  • For file-based disks, verify SELinux contexts if applicable