How to run a script on a host prior to instantiating VM

We have a small web service that tells us "what device is RDP (spice/VNC) into a running VM"
The problem is tcp_keepalive is not enabled in the spice and vnc services that are embedded in the VM’s runtime instantiation. So often times the host/hypervisor’s netstat shows a “dead/broken” connection of previous remote-viewer session.

As such we would like to leverage the host’s underlying tcp_keepalive feature by running the following command on the host/hypervisor prior to booting the VM.

$ \

… run VM…

Does anyone have any ideas how to do run a script on host just prior to invoking (booting/running) a selected VM?


Morning Pedro!,
I’d check OpenNebula hooks, that may do the trick.

You could create a VM hook that runs when it’s created or booted and set the remote value to yes so the hook is executed in the host where the VM was allocated. The hook can be a script in any language supported by the host (bash, python, ruby…)


I believe that what you want to do is change the QEMU executable environment instead of executing a script. You can specify environment in the libvirt domain xml with qemu:env. Take a look at:

These parameters can be added using RAW parameter in the VM template or setting it by default in /etc/one/vmm_exec/vmm_exec_kvm.conf. To set it by default it would be something similar to this:

RAW="  <qemu:commandline>
    <qemu:env name='LD_PRELOAD' value=''/>
    <qemu:env name='KEEPCNT' value='20'/>
    <qemu:env name='KEEPIDLE' value='180'/>
    <qemu:env name='KEEPINTVL' value='60'/>

There may be ways to set this up in libvirt configuration and that would a better solution.

Alternatively you can create a shell script that sets these variables and then call qemu-system-x86_64 executable. You’ll also have to change in that same file the EMULATOR value to point to the script.


Thanks for the quick reply. We have a number of HOSTS in the cluster, and one of these hosts is also running sunstone. I have found the /etc/one/vmm_exec/vmm_exec_kvm.conf file on only the one host that is running sunstone.

If I make the change in this one file, will it be pushed (executed on) to each hypervisor when a VM is deployed, or RUN? or both? I assume this change will apply to ALL VMs.

Thanks again for your support.

Thanks for the reply/support.
I have read the hooks link you provided, and I am not sure how to specify the custom state of “just before boot”.
Also, is it safe to assume that the script is executed in ths same shell as that which is booting up the VM? I ask this because this setting of environment variables must be executed within the same shell space as the VM boot up…(i think)
Any ideas or pointers are greatly appreciated.


That change will apply to all new VMs in all KVM hypervisors. Make sure that you restart OpenNebula after that change.