Hotadd pci devices

From KVM

Using PCI Hotplug Support

PCI Hotplug support is now available in kvm.git, here is how to use it.

Install PCI Hotplug drivers in the guest (Linux)

I loaded the following modules to get pci hotplug working:

  • acpiphp
  • pci_hotplug

You can either add these to your distro's module list to load on boot, or run a command like this.

for m in acpiphp pci_hotplug; do sudo modprobe ${m}; done

Use QEMU monitor to hot add device

(qemu) drive_add 0 file=/storage/10G.img,format=qcow2,id=drive-virtio-disk1,if=none 
(qemu) device_add virtio-blk-pci,scsi=on,drive=drive-virtio-disk1
  • check attached PCI devices
(qemu) info pci
  • notice
    • pci_add is old style, instead use device_add

Use QEMU monitor to hot add devices

The QEMU monitor has a new command, pci_add, which is used to hotadd the pci devices.


QEMU 0.9.1 monitor - type 'help' for more information
(qemu) help pci_add 
pci_add bus nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... -- hot-add PCI device


Add a NIC

The following command in the QEMU monitor hot-adds a nic to the guest.

  • pci_add auto nic model=e1000

You can replace model=e1000 with either model=rtl8139, model=virtio, or model=ne2k_pci. Note that model=ne2000 isn't pci-based but rather ISA-based and cannot be used for hotplug.

(qemu) pci_add auto nic  model=e1000 WARNING: PCI region size must be pow2 type=0x0, size=0x60000  OK bus 0, slot 5, function 0 (devfn 40)
                                                                                               

In the guest we can check if the device was added by looking at dmesg.

[   91.808265] acpiphp: Slot [30] registered                          
[   91.808276] acpiphp: Slot [31] registered                          
[  110.828130] PCI: Ignore bogus resource 6 [0:0] of 0000:00:02.0     
[  110.828333] decode_hpp: Could not get hotplug parameters. Use defaults
[  110.903259] Intel(R) PRO/1000 Network Driver - version 7.3.20-k2-NAPI 
[  110.903264] Copyright (c) 1999-2006 Intel Corporation.                
[  110.903489] ACPI: PCI Interrupt Link [LNKA] enabled at IRQ 10         
[  110.903492] ACPI: PCI Interrupt 0000:00:05.0[A] -> Link [LNKA] -> GSI 10 (level, high) -> IRQ 10
[  111.238713] e1000: 0000:00:05.0: e1000_probe: (PCI:33MHz:32-bit) 52:54:00:12:34:57              
[  111.292550] e1000: eth1: e1000_probe: Intel(R) PRO/1000 Network Connection                      


Add a disk

Hot-adding a disk means hot-adding a PCI device, so you need PCI hotplug support in the guest for this to work.

The following command in the QEMU monitor will add a scsi disk to the guest:

 * pci_add auto storage file=/home/rharper/work/images/empty.img,if=scsi

You can replace if=scsi with if=virtio, but IDE is not hotpluggable.


QEMU 0.9.1 monitor - type 'help' for more information

(qemu) pci_add auto storage file=/home/rharper/work/images/empty.img,if=scsi

OK bus 0, slot 5, function 0 (devfn 40)                                  
                                                                 

In the guest we see via dmesg that the device was added.

[  150.581752] PCI: Enabling device 0000:00:05.0 (0000 -> 0003)
[  150.582634] ACPI: PCI Interrupt Link [LNKA] enabled at IRQ 10
[  150.582637] ACPI: PCI Interrupt 0000:00:05.0[A] -> Link [LNKA] -> GSI 10 (level, high) -> IRQ 10
[  150.585099] sym0: <895a> rev 0x0 at pci 0000:00:05.0 irq 10
[  150.594628] sym0: No NVRAM, ID 7, Fast-40, LVD, parity checking
[  150.607861] sym0: SCSI BUS has been reset.
[  150.607883] scsi2 : sym-2.2.3
[  153.608433] scsi 2:0:0:0: Direct-Access     QEMU     QEMU HARDDISK    0.9. PQ: 0 ANSI: 3                                                     
[  153.608462]  target2:0:0: tagged command queuingenabled, command queue depth 16. 
[  153.608484]  target2:0:0: Beginning Domain Validation
[  153.609517]  target2:0:0: Domain Validation skipping write tests
[  153.609522]  target2:0:0: Ending Domain Validation
[  153.618880] scsi 2:0:0:0: Attached scsi generic sg1 type 0
[  153.749967] Driver 'sd' needs updating - please use bus_type methods
[  153.750204] sd 2:0:0:0: [sda] 20971520 512-byte hardware sectors (10737 MB)
[  153.750253] sd 2:0:0:0: [sda] Write Protect is off
[  153.750255] sd 2:0:0:0: [sda] Mode Sense: 14 00 00 00
[  153.750354] sd 2:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  153.750478] sd 2:0:0:0: [sda] 20971520 512-byte hardware sectors (10737 MB)
[  153.750253] sd 2:0:0:0: [sda] Write Protect is off
[  153.750255] sd 2:0:0:0: [sda] Mode Sense: 14 00 00 00
[  153.750354] sd 2:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  153.750478] sd 2:0:0:0: [sda] 20971520 512-byte hardware sectors (10737 MB)
[  153.750534] sd 2:0:0:0: [sda] Write Protect is off
[  153.750536] sd 2:0:0:0: [sda] Mode Sense: 14 00 00 00
[  153.750634] sd 2:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
[  153.750637]  sda: unknown partition table
[  153.759275] sd 2:0:0:0: [sda] Attached SCSI disk