Menu |
On-demand virtual machine (VM) with VirtualBox and xinetdIn case you ever have the need to run a virtual machine (VM) that is remote controlled via (V)RDP on a linux host in an on-demand fashion, i.e. it is started automatically when a RDP connection is opened, here is a way to do it. VBoxHeadless - The VirtualBox VRDP server without GUIWith the VirtualBox installation comes VBoxHeadless, a daemon that runs a VM and provides the VRDP service to control it without opening any GUI on the host system. We will use it to run our on-demand VM. VBoxHeadless takes (among others) the name of the VM to start and optionally the TCP port number to listen on as command line arguments. So the following simple command could be used to start the VM, listening on the default port 3389: /usr/bin/VBoxHeadless -s VMNAME xinetdThe first solution to the on-demand problem that came to mind was xinetd, a super-server that listens for incoming connections and can spawn child processes to handle them as they arrive. So my first try was to simply set up a xinetd service on port 3389 that runs VBoxHeadless as specified above. This failed miserably because you cannot just run any server under xinetd. The server process must be designed for that. Basically there are two possible modes of operation for TCP servers under xinetd:
Obviously both modes cannot work with VBoxHeadless directly. A little shell script to the rescueSo I created a shell script that acts as a xinetd-compatible wrapper around VBoxHeadless. Here is the code: #!/bin/sh This script is intended to be run from xinetd and first checks the state of the VM with the VBoxManage command. If the VM is powered off, it starts VBoxHeadless in the background, but not on the default port. Instead, VBoxHeadless should listen on another unused port, I used 53389 here. Then we have to wait until VBoxHeadless is ready to accept a connection. I know that sleep 10 is ugly, but I already tried to improve that by reading from the temporary log file of VBoxHeadless and failed. That file always reads as empty until the VBoxHeadless process terminates, so it seems like its output is never flushed. After having made sure that the VM is running, we have to forward the incoming connection to the VBoxHeadless process. Here I am running the script in "wait=no" mode and so I can use the tool netcat (nc) to connect stdin and stdout of the running script to the started VBoxHeadless instance. Here is the xinetd config I used for this service (this goes somewhere in /etc/xinetd.conf or /etc/xinetd.d/... depending on your installation): # default: off The path in the "server" line must point to the script created above. And there you go:
|
| Modified: 2009-07-11 | Copyright © 2006-2010 Christian Hornung - chhornung@googlemail.com |