It is a common use case for developers to change or provide some sort of configuration to their running apps. But what if the app is inside a virtual machine. Hypervisors provide encapsulation and isolation of the guest OS from the outside world. How do I communicate with my app then? Should I restart it? Well, for VMware virtualization infrastructure there is an easy way to achieve live configuration update. It is possible to provide information to a running guest OS in the form of key/value pairs using the official VMware vSphere API. One can make such API calls both against vSphere vCenter Server and VMware ESXi. Also, bindings for this API are supported and provided by VMware for a bunch of different programming languages. The examples for configuring VMware virtual machine in this post use the VMware vSphere API Python Bindings available on GitHub.
Let’s list all the virtual machines:
host = 'localhost' pwd = '' port = 443 user = 'root' vmname = 'test1' si = pyVim.connect.Connect(host=host, port=port, user=user, pwd=pwd, sslContext=ssl._create_unverified_context()) content = si.RetrieveContent() datacenter = content.rootFolder.childEntity vmFolder = datacenter.vmFolder vmList = vmFolder.childEntity
After that we can get the one we are interested in. In this example we search by
for i in vmList: if i.name == vmname: vm = i
myKey = 'guestinfo.exampleKey' myValue = 'exampleValue' cspec = pyVmomi.vim.vm.ConfigSpec() cspec.extraConfig = [pyVmomi.vim.option.OptionValue(key=myKey, value=myValue)]
A call to ReconfigVM_Task does the trick.
The key prefix ‘guestinfo.‘ is required for the guest OS to be able to read the key/value pair. If the key does not start with ‘guestinfo.’ it is not visible inside the guest OS.
It should be noted that all key/value pairs added to a running (powered on) VM will be lost on guest OS reboot. However, if the virtual machine is powered off in the time of ReconfigVM_Task then the key/value pair gets persisted into the corresponding .vmx file and VM’s reboot does not clear it.
$ vmware-rpctool "info-get guestinfo.exampleKey"
All of this operations are already conveniently scripted on GitHub. Of course it needs some customizations according to the concrete environment but it works great for quick experiments.