VBoxHeadless-Just one machine at a time?

Discussion in 'HOWTO-Related Questions' started by ricardoc, Apr 21, 2010.

  1. ricardoc

    ricardoc New Member

    Hi Falco,

    I've been following all your articles dedicated to running VMs with VirtualBox using VBoxHeadless. They are an interesting proposal. I have noticed though that invariably you talk about one connection at a time. Since the RDP protocol is used with the host IP and not the guest, how can we set up simultaneous connections to more than one guest running on a headless Linux box with VirtualBox?

    Also, how can VirtualBox itself be managed remotely on a headless server? Is there a tool for that?

    Thanks,

    Ricardo.
     
  2. dfed

    dfed New Member

    I have some things I've noted running this at home you may be interested in, but let's start with the vrdp servers.

    You can set the creation of each machine to have a vrdp server enabled on guest, rather than on the host itself.

    I manage my server via ssh and some (admittedly poorly written) hacks. I can detail them here soon (I am just about to head into work, so I can post more when I get there.)

    running an ssh service on the host allows you to su - to the user that runs virtualbox (or ssh in as that user, if you allow it) and run VBoxManage and VBoxHeadless commands. If you are looking for a graphical hypervisor, ssh -X to the user that runs VirtualBox and execute the command VirtualBox and you'll get the familiar graphical VirtualBox interface.

    In the meantime, I'll post the command-line scripts I use to control the guests in a short while.
     
  3. ricardoc

    ricardoc New Member

    Hi dfed,

    Thank you for replying so fast. I would like to get a reference on how to set the vrdp server enabled guests. I would also like to take a look at your scripts (even if they are "poorly" written hacks); you have to start somewhere, don't you? ;)

    Regarding the graphical hypervisor, don't you need the X windows installed in the host to reach it through ssh? Will that mean that you have to add X to the headless server? Sorry for the silly questions but I'm not what you will call a Linux average user; I'm pretty new to this world.

    Regards,
     
  4. dfed

    dfed New Member

    Sure. As for scripts, I'll post them in a bit. First, if you want to enable vrdp per guest you can get familiar with the command VBoxManage. Read up here:

    http://www.virtualbox.org/manual/ch08.html

    ETA or, more specifically, this:

    http://linux-tips.org/article/74/enabling-remote-desktop-on-a-virtualbox-machine

    But for enabling vrdp servers on each guest, you'd do something like:

    Code:
    VBoxManage modifyvm MachineName -vrdpport <port>
    
    To answer your question about X being installed on the host, if you have installed VirtualBox via the how-to here, most likely apt already grabbed the Qt and X libraries as a dependency. This means you shouldn't have to install anything to ssh -X (user@host) then run the VirtualBox command.

    Some tips for automating some scripts from an ssh session to more easily control your VM's based on what I have done:

    1) set up two directories in the home directory of the user you are running VirtualBox from. One is ~/bin and one is ~/config.

    From there, you want to be able to use those directories for executable scripts (in ~/bin) that source configurations you can either automatically or manually gather (located in ~/config).

    For example, in the crontab for the user running VirtualBox, I have this job:

    Code:
    # Regenerate list of installed Virtual Guests:
    0,5,10,15,20,25,30,35,40,45,50,55 * * * * ls ~/.VirtualBox/Machines | sort -u > ~/config/hosts
    That generates a list of the hosts in a configuration file for me. Mine does it every five minutes, your choice of interval may vary. I build and destroy VM's rather often, as I use this box as a development and testing environment.

    Also, depending on your network configurations (ie: home network on cable modem or broadband, etc.) you may want to consider this tutorial for your host:

    http://www.howtoforge.com/installing-powerdns-with-mysql-backend-and-poweradmin-on-ubuntu-9.10

    What this can do is give you a DNS resolution for the network for each of your guests (again, I am assuming this is a home network, not a corporate sandbox that may or may not already have DNS.)

    With those two in place, you can then add a couple of other scripts. One would manage the startup and shutdown of your VM's either by individual name or all at a time. I wrote one for this, as seen below:

    Code:
    #!/bin/bash
    #Written by me with no guarantee this pile of crap will work.
    #init.d script to start/stop/restart all VM's installed on system via VirtualBox
    #
    # If we've not had 2 variables passed in the command line, failboat has sailed.
    if [[ "${#}" -lt 2 ]]; then
        echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
        exit 1;
    elif [[ "${#}" -gt 2 ]]; then
        echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
        exit 1;
    fi
    # First, declare some arrays to use later:
    declare -a list
    	list=($(ls ~/.VirtualBox/Machines/ | sort -u))
    declare -a running
    	running=($(ps -ef | grep VBoxHeadless | grep -v grep | awk '{print $10}' | sort -u))
    # Sanity check on ${1}:
    if [[ "${1}" == "stop" ]]; then
    	com=${1}
    elif [[ "${1}" == "start" ]]; then
    	com=${1}
    elif [[ "${1}" == "restart" ]]; then
    	com=${1}
    else echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2; exit 1;
    fi
    # Sanity check on ${2}:
    checkvm=($(for z in ${list[@]}; do echo ${z} | grep ${2}; done))
    if [[ "${2}" == ${checkvm} ]]; then
    	vm=${2}
    elif [[ "${2}" == "all" ]]; then 
    	vm=${list[@]}
    else echo "Virtual Machine ${2} not found.  Exiting." >&2; exit 1;
    fi
    echo "Working to ${com} ${vm[@]}"
    # Let's do some work.
    if [[ "${com}" == "start" ]]; then 
    	for x in ${vm[@]}; do nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Started VM ${x}"
    	done
    elif [[ "${com}" == "stop" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null
    		echo "Stopped VM ${x}"
    	done
    elif [[ "${com}" == "restart" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null && nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Restarted VM ${x}"
    	done
    fi
    exit 0
    
    Now this is a work in progress, as I have an extra array in there I don't use (yet) that checks which VM's are running (this will be for a future enhancement that checks for VM's that have died and restarts them.) I should note that I do this the hard way, as I could tell that line to use the VBoxManage command to give me running virtiuals. At it's basic this script will get you the ability to start/stop a single VM by name or all at once. It may duplicate some functions of the VirtualBox commands, but it's easier for me to remember to type. This can also be adapted to be used as an init.d script at start up to automatically boot your VM's.

    One more I use is the following:

    Code:
    #!/bin/bash
    source ~/.bashrc
    command=$1
    for x in `cat ~/config/hosts`
    do
    	ssh $x "uname -n; ${command}"
    done
    
    This is so I can send commands via ssh to each VM at once. This would be handy, if for example, I wanted to check disk usage on each machine. I could then just do a vcmd "df -h" (include the double quotes) and it would give me a readout of the response from each server.

    Now this assumes you run SSH on each guest. Also, it assumes that each guest may have similar linux/unix operating systems. If you wanted to parse between BSD or Linux guests, you could set up something like the following:

    Code:
    #!/bin/bash
    for x in `cat ~/config/hosts`; do ssh ${x} 'uname -nvo'; done > ~/config/host-os
    
    This would give you a configuration file with the list of host names and their OS flavors. You could then adapt command scripts based on just those like so:

    Code:
    #!/bin/bash
    source ~/.bashrc
    command=$1
    for x in `cat ~/config/host-os | grep "Ubuntu" | awk '{print $1}'`
    do
    	ssh $x "uname -n; ${command}"
    done
    
    This gives me the ability to run aptitude on all the ubuntu guests.

    You get the idea. If you have bash scripting questions I can post some links to help learn more about that.
     
    Last edited: Apr 22, 2010
  5. dfed

    dfed New Member

    Also, forgot to add:

    The basic funcitonality of my scripts are mostly covered in VBoxManage/VBoxHeadless commands. The extras I added are simple convenience and may not be written very effectively. I do like being able to execute a command to all (insert linux/UNIX flavor here) guests for keeping housekeeping duties easy.

    The big one, though, is the script to start/stop all or one VM by name. This specifically can be used to start VM's automatically at boot and is helpful in case of unexpected outage on the host.
     
  6. dfed

    dfed New Member

    Apologies for the many posts, but I often nitpick my own ideas and something just struck me.

    My script to determine the OS of each guests and give a config file to use in other scripts is inelegant. It also fails when a windows guest is introduced, because that wouldn't have an ssh service.

    Something much more elegant would be like the following:

    Code:
    #!/bin/bash
    for x in `ls ~/.VirtualBox/Machines/ | sort -u`; do VBoxManage showvminfo ${x} | sed -n 5,6p; done
    
    Of course this is off the top of my head. You could use various awk/sed magicks to get the needed info. Once gotten, you could tailor scripts to the various OS platforms your guests run (automating yum vs. automating aptitude, etc.)

    Helpful commands to use in scripts I've just sussed out:

    Show VM operating systems:

    for x in `ls ~/.VirtualBox/Machines/ | sort -u`; do VBoxManage showvminfo ${x} | sed -n 5,6p; done

    list running vms:

    VBoxManage list runningvms | sed '5d' | awk '{print $1}' | grep -v "All" | grep -v "(C)" | sed 's/"//' | sed 's/"//' | sort -u

    List all vms:

    VBoxManage list vms | sed '5d' | awk '{print $1}' | grep -v "All" | grep -v "(C)" | sed 's/"//' | sed 's/"//' | sort -u


    (ETA: Yeah these need to be combed over by me more thoroughly, but you get the idea.)
     
    Last edited: Apr 22, 2010
  7. ricardoc

    ricardoc New Member

    Hi dfed,

    Wow man!, That's lots of information and help. Thank you very much. I'll take some time to digest and try the vrdp set up with the VMs and also check your scripts to accommodate them to my environment. You're correct in thinking this is for a home network. Also your tip about using a better script when windows guests are introduced is right on since I have a couple of those.

    It will take me a few days before I can post back with results but if in the meantime you do more nitpicking on your ideas keep them coming; I'll we be checking this post every day.

    Have a nice one!
    Ricardo.
     
  8. dfed

    dfed New Member

    Ok. One line to output VM name and os:

    Code:
    for x in `VBoxManage list vms | grep '"' | cut -d'"' -f2 2>/dev/null`; do VBoxManage showvminfo "${x}" | egrep -e "Name:" -e "Guest OS:" |cut -b 10-64 |tr -d '\n'| awk '{print $1" "$2" "$3" "$4" "$5" "$6}'; done
    
    which outputs like this:

    Code:
    redhatest Red Hat (64 bit) 
    windowstest Windows XP (64 bit) 
    opensolaristest OpenSolaris (64 bit)  
    anothertest Windows 7 (64 bit) 
    archtest Arch Linux (64 bit) 
    bsdtest FreeBSD (64 bit)  
    warptest OS/2 Warp 4.5 
    Working on the portion to determine which is running and append that file with that.
     
  9. dfed

    dfed New Member

    Ok got it. It's messy and horrible, and I am sure there is a better way to do this, but here's one way:

    Code:
    for x in `VBoxManage list vms | grep '"' | cut -d'"' -f2 | sort -u 2>/dev/null`; do VBoxManage showvminfo "${x}" | egrep -e "Name:" -e "Guest OS:" -e "State:" | cut -b 10-64 | tr -d '\n' | awk '{print $1" "$2" " $3" " $4" "$5" "$6" "$7" "$8" "$9" "$10}'| awk -F"\(since" '{print $1}'; done
    which outputs:

    Code:
    chat Ubuntu (64 bit) running 
    db Ubuntu (64 bit) running 
    mail Ubuntu (64 bit) running 
    nas Ubuntu (64 bit) running 
    shell Ubuntu (64 bit) running 
    web Ubuntu (64 bit) running 
    
    (on this machine, it's all ubuntu guests. the one I was on earlier has more variety. Both return correct answers.)

    That allows you to have scripts that parse via grep and awk based on name, OS and whether the guest is running or not.

    I should mention that if you create machine names with spaces in them, this breaks. I should also mention that this is all done in bash, which is much more clunky than if I did this in perl. I'll maybe look into rewriting it in perl later.

    ETA:

    Also modified the first script to start/stop/restart:

    Code:
    #!/bin/bash
    #Written by dfed with no guarantee this pile of crap will work.
    #script to start/stop/restart one or all VM's installed on system via VirtualBox
    #
    # If we've not had 2 variables passed in the command line, failboat has sailed.
    if [[ "${#}" -lt 2 ]]; then
        echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
        exit 1;
    elif [[ "${#}" -gt 2 ]]; then
        echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2
        exit 1;
    fi
    # First, declare some arrays to use later:
    declare -a list
    	list=($(VBoxManage list vms | grep '"' | cut -d'"' -f2 | sort -u 2>/dev/null))
    # Sanity check on ${1}:
    if [[ "${1}" == "stop" ]]; then
    	com=${1}
    elif [[ "${1}" == "start" ]]; then
    	com=${1}
    elif [[ "${1}" == "restart" ]]; then
    	com=${1}
    else echo "Usage: ${0} <start|stop|restart> <(vm name)|all>" >&2; exit 1;
    fi
    # Sanity check on ${2}:
    checkvm=($(for z in ${list[@]}; do echo ${z} | grep ${2}; done))
    if [[ "${2}" == ${checkvm} ]]; then
    	vm=${2}
    elif [[ "${2}" == "all" ]]; then 
    	vm=${list[@]}
    else echo "Virtual Machine ${2} not found.  Exiting." >&2; exit 1;
    fi
    echo "Working to ${com} ${vm[@]}"
    # Let's do some work.
    if [[ "${com}" == "start" ]]; then 
    	for x in ${vm[@]}; do nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Started VM ${x}"
    	done
    elif [[ "${com}" == "stop" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null
    		echo "Stopped VM ${x}"
    	done
    elif [[ "${com}" == "restart" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null && nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Restarted VM ${x}"
    	done
    fi
    exit 0
     
    Last edited: May 1, 2010
  10. dfed

    dfed New Member

    Ok I've tweaked it out as far as I am going to with bash. I'll redo some scripts in perl because I've thought of some additions to functionality i want to do, but that's for later.

    For now, here's the breakdown:

    Three scripts, one to start/stop, one to gather info, and one to ssh remote commands to unixy guests. This works with any type of OS VBox allows, and won't fail with windows guests.

    First, throw this in cron at whatever interval you like:

    ~/bin/refreshvmconfig :
    Code:
    #!/bin/bash
    for x in `VBoxManage list vms | grep '"' | cut -d'"' -f2 | sort -u`; do VBoxManage showvminfo "${x}" --machinereadable | egrep -e "name=" -e "ostype=" -e "VMState=" | cut -d'"' -f2 | cut -d'_' -f1 | xargs echo; done > ~/config/hosts.conf
    
    make sure you have ~/bin and ~/config directories made int he home directory of whatever user is running VBox. Also, you'll want to exchange ssh keys with this user and root or a privileged user on the guest.

    Next, this will allow you to start/stop based on VM name, all or OS name. This means if you want to shut down all Windows7 guests at once, you can:

    ~/bin/vgsvc :
    Code:
    #!/bin/bash
    #Written by dfed with no guarantee this pile of crap will work.
    #script to start/stop/restart one or all VM's installed on system via VirtualBox
    #
    # If we've not had 2 variables passed in the command line, failboat has sailed.
    if [[ "${#}" -lt 1 ]]; then
        echo "Usage: ${0} <start|stop|restart|list> <VM Name|OS Name|all>" >&2
        exit 1;
    elif [[ "${#}" -gt 2 ]]; then
        echo "Usage: ${0} <start|stop|restart|list> <VM Name|OS Name|all>" >&2
        exit 1;
    fi
    # First, declare some arrays to use later:
    declare -a list
    	list=($(cat ~/config/hosts.conf | awk '{print $1}'))
    declare -a os
            os=($(cat ~/config/hosts.conf | awk '{print $2}' | sort -u))
    # Sanity check on ${1}:
    if [[ "${1}" == "list" ]]; then
            echo "Virtuals currently installed are:"
    		cat ~/config/hosts.conf | sort -u; 
    	echo "Usage: ${0} <start|stop|restart|list> <VM Name|OS Name|all>" >&2; exit 1;
    elif [[ "${1}" == "stop" ]]; then
    	com=${1}
    elif [[ "${1}" == "start" ]]; then
    	com=${1}
    elif [[ "${1}" == "restart" ]]; then
    	com=${1}
    else echo "Usage: ${0} <start|stop|restart|list> <VM Name|OS Name|all>" >&2; exit 1;
    fi
    # Sanity check on ${2}:
    checkvm=($(for z in ${list[@]}; do echo ${z} | grep ${2}; done))
    checkos=($(for y in ${os[@]}; do echo ${y} | grep ${2}; done))
    if [[ "${2}" == ${checkvm} ]]; then
    	vm=${2}
    elif [[ "${2}" == ${checkos} ]]; then
    	declare -a vm
    		vm=($(cat ~/config/hosts.conf | grep ${2} | awk '{print $1}'))
    elif [[ "${2}" == "all" ]]; then 
    	vm=${list[@]}
    else echo "Virtual Machine OS/Name ${2} not found.  Use ${0} \"list\" to determine VM names or operating systems installed." >&2; exit 1;
    fi
    echo "Working to ${com} ${vm[@]}"
    # Let's do some work.
    if [[ "${com}" == "start" ]]; then 
    	for x in ${vm[@]}; do nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Started VM ${x}"
    	done
    elif [[ "${com}" == "stop" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null
    		echo "Stopped VM ${x}"
    	done
    elif [[ "${com}" == "restart" ]]; then
    	for x in ${vm[@]}; do VBoxManage controlvm ${x} poweroff 2>&1 > /dev/null && nohup VBoxHeadless --startvm ${x} 2> /dev/null > /dev/null &
    		echo "Restarted VM ${x}"
    	done
    fi
    /opt/virtuals/bin/refreshvmconfig
    exit 0
    
    That will also refresh your config file with the state of whatever you start/stopped. This should be called from whatever init.d script you come up with to start vm's automatically at boot.

    Next, issue commands over ssh to unixy guests:

    ~/bin/vcmd-ssh :
    Code:
    #!/bin/bash
    #Written by dfed with no guarantee this pile of crap will work.
    #script to issue commands via ssh to guests based on OS
    #
    source ~/.bashrc
    # If we've not had some variables passed in the command line, failboat has sailed.
    if [[ "${#}" -lt 1 ]]; then
        echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2
            exit 1;
    elif [[ "${#}" -gt 2 ]]; then
        echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2
            exit 1;
    fi
    # Some arrays to use later:
    declare -a vms
    	vms=($(cat ~/config/hosts.conf | awk '{print $1}' | sort -u))
    declare -a os
            os=($(cat ~/config/hosts.conf | awk '{print $2}' | sort -u))
    declare -a hosts
    	hosts=($(cat ~/config/hosts.conf | grep "running" | grep -v "Windows" | grep -v "OS2" | grep -v "DOS" | grep -v "Other" | grep -v "Netware" | awk '{print $1" "$2}'| sort -u))
    checkvm=($(for z in ${vms[@]}; do echo ${z} | grep "${1}"; done))
    checkos=($(for y in ${os[@]}; do echo ${y} | grep "${1}"; done))
    # Sanity checks on ${1}
    if [[ "${1}" == "all" ]]; then
    	first="all"
    elif [[ "${1}" == "list" ]]; then
    	echo "Virtuals currently running are:"
    		cat ~/config/hosts.conf | grep "running" | awk -F"running" '{print $1}' | sort -u 
    	echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2; exit 1
    elif [[ "${1}" == ${checkvm} ]]; then
    	checkssh=($(for x in ${hosts[@]}; do echo ${x} | grep "${checkvm}"; done))
    	if [[ ${checkvm} == ${checkssh} ]]; then
    		first="vm" 
    	else echo "Sorry, "${checkvm}" is not an ssh-capable VM or is not running."
    	echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2; exit 1
    	fi
    elif [[ "${1}" == ${checkos} ]]; then
    	checkssh=($(for x in ${hosts[@]}; do echo ${x} | grep "${checkos}"; done))
    	if [[ "${checkos}" == ${checkssh} ]]; then
    		first="os"
    	else echo "Sorry, "${checkos}" is not an ssh-capable OS or is not running."
    	echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2; exit 1
    	fi
    else echo "I'm sorry, "${1}" is not recognized.  Please use \"vcmd list\" to determine host or operating systems currently running."
    	echo "Usage: ${0} <OS Name|VM Name|all|list> <Command (in quotes)>" >&2
    	exit 1;
    fi
    # Let's do some work:
    if [[ "${first}" == "all" ]]; then
    	for w in `cat ~/config/hosts.conf | grep "running" | grep -v "Windows" | grep -v "OS2" | grep -v "DOS" | grep -v "Other" | grep -v "Netware" | awk '{print $1}'`; do ssh ${w} "uname -n; $2"; done
    elif [[ "${first}" == "os" ]]; then
    	for w in `cat ~/config/hosts.conf | grep "running" | grep ${1} | awk '{print $1}'`; do ssh ${w} "uname -n; $2"; done
    elif [[ "${first}" == "vm" ]]; then
            ssh ${1} "uname -n; $2"
    fi
    exit 0
    
    That will weed out non-unixy guests (or at least those not known to have ssh running out of the box.) it will also allow you to issue commands based on OS specific stuff, so you can yum upgrade the RedHats and aptitude upgrade the Debians.

    Take note, I found that the VBoxManage showvminfo (vmname) command has a trigger you can append to use machine-readable outputs. So this:

    Code:
    VBoxManage showvminfo vmname --machinereadable 
    gives you something easier to parse via awk/sed than what I posted before.

    Of course, all of the above assumes you have either set hostnames based on VM name with static IPs for guests on the host in /etc/hosts or you have DNS running and assign static ips manually or via dhcp server to the guest. If you are running guests with bridged nics I have yet to find a way to determine the IP of those without DNS/static assignments. If you are running natted or host only, you can use the VBoxManage command to determine the IP like so:

    Code:
    VBoxManage guestproperty enumerate (vmname)
    Pipe to grep "Net" and you'll find the line.

    That's it. Enjoy. If you find a way to script commands to Windows boxen from linux command line (via mono, or whatever, or using expect scripts and rdesktop) you can base a vcmd-win off the vcmd-ssh script or you can remove the grep for windows and run cygwin.

    Cheers.
     
    Last edited: May 9, 2010

Share This Page