Search This Blog

Wednesday, July 17, 2013

Linux CentOS 6 KVM hot add device

First of all, you should avoid using virsh attach-disk with its limited amount of options. Instead, I suggest to specify the exact disk format you prefer in a separate, temporary XML file, like this: 

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2' cache='writeback'/>
  <source file='/home/gert/kvm/testdomain-vdb.img'/>
  <target dev='vdb' bus='virtio'/>
</disk>


Before adding it, make sure the hotplug kernel modules are loaded in the guest: 

modprobe acpiphp
modprobe pci_hotplug


Some distributions, including recent CentOS/RHEL/Fedora have this built-in in the kernel. In this case, check for CONFIG_HOTPLUG_PCI_ACPI. If it's y, then you're all set.

Finally, add it to the running VM using 

virsh # attach-device [domain] /path/to/disk.xml
(optionally, add the --persistent option to let Libvirt update the domain XML definition 'persistent'.)

In the guest, the kernel should now 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

This also works perfectly using the GUI-enabled virt-manager application.

source

How to clear memory (caches) on Linux

Since 2.6.16 you need only echo to clear cached memory on Linux box.

This will clear all cached memory.


echo 3 > /proc/sys/vm/drop_caches
You can clear only part of it by using 1 or 2. All options:
  • 1 to clear cached pages.
  • 2 to clear dentries and inodes. 
  • 3 to clear all cached memory.