Changing libvirt settings from PHP on the fly
To provide the performance and high availability that Copernica.com customers expect, we run a cluster of (many) physical servers. With a few exceptions, all these servers are multi-CPU blades with a large amount of memory, that are the host of even more virtual servers. Those virtual servers are controlled through PHP scripts that make use of a customized libvirt PHP extension.
Why use virtualization?
We have many different services we need our hardware to provide. A few examples of these are hosting images displayed in mailings, handling SOAP requests and executing batch jobs (like rebuilding selections or personalizing a mailing).
Since these services have different peak hours and it is very rare that they all need processing power at the same time it makes no sense to provide overcapacity for each and every one of them. Instead we virtualize these services so that when one service needs additional capacity it can use idle capacity from other services. Additionally, some of these services do not scale well on large, SMP systems and work better when run on many, smaller systems. Using virtualization one physical server can provide up to eight virtual servers.
Managing virtual servers
To manage all these virtual servers we use libvirt. Libvirt is a library used to control virtualized machines (or, in their terminology: "domains"). It provides the API to add, remove or modify servers.
As we are mostly a PHP-shop (although we nowadays do more and more with C++, and have some code running on node.js as well), we use the libvirt-php extension, which gives access to most of the libvirt API. With this libvirt extension, we can use PHP scripts to change the configuration of our virtual machines.
Several of the functions exposed by the libvirt-php API, however, can only change a domain/virtual machine that is not running. This means that we would first have to stop a virtual machine, before a PHP script can change its configuration. This is not ideal, as we frequently need to change the disks assigned to running machines. To overcome this, we have modified the libvirt PHP extension ourselves, so that changes can be made to a running domain as well, without the need to first bring that virtual machine down.
We have completely rewritten the libvirt_domain_disk_add and libvirt_domain_disk_remove methods in the libvirt-php code to allow hot-pluggable disks, as well as control some advanced settings such as caching and io modes. If you would like to try out this new functionality, you can find the modified code at https://github.com/martijnotto/libvirt-php.