Ubuntu, Remote Desktop and Windows Remote desktop (2): How to auto start (and pause) a VirtualBox VM at boot time and shutdown

12/04/2014 at 7:23 am Plaats een reactie

This article is a continuation of my previous article: Ubuntu, Remote Desktop and Windows Remote desktop (1)

As soon as I decided to re-publish my WordPress blog (it had been offline for a couple of months), I noticed that one of the more popular articles is the article about X2Go . X2Go is a remote desktop solution for Linux, and is a fantastic product. Maybe I could have used it for my home network, but when doing this home network project, X2Go was not yet feature-rich enough.  As stated in my previous announcement, I would like to discuss the way I have organized my home network with Thin Client / remote solutions, with a combination of a couple of products. For a general overview, check out the picture in my previous article.

Ubuntu 12.04 Desktop version (LTSP) I personnally prefer Long Term Support versions (LTSP), since, generally speaking, these LTSP versions are more stable than the non-LTSP releases.

Virtualbox, which will auto-start a Windows 7 VM at boot time of the Ubuntu Desktop, and auto-pause this Windows 7 VM at Ubuntu Desktop shutdown.

Thinlinc . I believe that this product is, by far, the best commercial product for any Linux based thin client / remote solution, whether intended for a remote Linux or Windows Desktop. And best of all: it comes with a free 10 user version. I guess that this is more than sufficient for an average home usage…

Let’s first talk about some basics.

First I installed my Ubuntu “Desktop server”. It would be used, later on, as remote, say, Linux RDP server, with a virtual Windows 7 box inside. For the whole project I bought, an old HP xw6400 workstation, at less than 300 euro. It’s a 64-bit machine, with only 8 GB RAM, and a 1 TB disk, and it’s the HP XW6400 shown in my previous article.

On this machine, I installed Ubuntu 12.04 Desktop Edition. As shown in the screenshot, I did a custom partitioning of the disk:

 

Image

– A boot partition (/boot) of type ext2
– a swap file partition of 2 GB
– an extended 900 GB partition (“/”; this would contain the user data)
– an extra /vms partition (this would contain the files for VirtualBox VM’s up to 100 GB)

The VirtualBox part

Okay, now let’s get started with VirtualBox: how to auto-start and auto-pause a (Windows) VirtualBox VM at boot time and at shutdown. This Windows VM will be used, later on, in the Thinlinc Remote Desktop solution.

Basically, I simply installed VirtualBox. If I remember well, I did not use the Ubuntu Software Centre to install it. At that time, I used virtualbox-4.2_4.2.6-82870~Ubuntu~precise_amd64.deb from https://www.virtualbox.org/wiki/Downloads. I guess there’s a more recent version now, check it out at Virtualbox.When defining the location of the files for the Windows VM, I chose /vms (which I created earlier). For the rest, I simply did a normal install of a VirtualBox Windows VM, so I am not going into details about that.

But now, here comes the first trick: how to auto-start and auto-pause the VM at boot and at shutdown time.

First, you need a script that I found on  sourceforge.net. I tried to find it back for reference, but It seems to be gone from SourceForge. However, the original script can still be found at pastebin.com.   Here is the content of my /etc/init.d/  windows script: vbox-windows7ultimate.

Mind your VirtualBox VM name: stick to lower case to avoild errors, and avoid space in the name of the VM.

Since the file is self-explanatory, I simply copy / paste the content here:

#! /bin/sh
#
# chkconfig: 2345 90 10
### BEGIN INIT INFO
# Provides: vbox-windows7ultimate
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: ‘windows7ultimate’ virtual machine
# Description: Starts and stops a VirtualBox host as a service.
### END INIT INFO

# Author: Brendan Kidwell <brendan@glump.net>
# License: GPL 3 <http://opensource.org/licenses/GPL-3.0&gt;
#
# Based on /etc/init.d/skeleton from Ubuntu 12.04.

#——————————————————————————-
#
# CONFIGURATION
#
# What is the name of the VM exactly as it appears in the VirtualBox control
# panel? This is the ‘name’ and the ‘long name’ of the VM.
#
# Does ‘name’ contain spaces or other special characters? If so, you must
# make up some other value for ‘name’ that doesn’t have spaces.
#
# Setup:
#
# 1. Copy this file to /etc/init.d/vbox-‘name’. The filename must start with
# the prefix “vbox-“. Make sure you set the ‘x’ bit on the file to make it
# executable: Note by JanV: open a terminal session, sudo sulogin, and cd into /etc/init.d and chmod +x filename.
#
# 2. Edit ‘Provides’, above, to match the filename.
#
# 3. Edit ‘Short-Description’ to describe the function of the VM.
#
# 4. If ‘long name’ is different from ‘name’ fill it in below, otherwise leave
# LONGNAME as an empty string.
VM_LONG_NAME=””
#
# 5. What user owns the virtual machine?
VM_OWNER=jvoort
#
# 6. Which stop command? “hibernate” or “powerbutton”
VM_STOP=hibernate
#
# 7. For the ‘start-wait’ command — waiting until network is up, what is the
# VM’s hostname?
VM_HOSTNAME=””
#
#——————————————————————————-

# Do NOT “set -e”

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
# Pull DESC from file header
DESC=`grep –max-count=1 “^# Short-Description:” $(readlink -f $0)|cut –delimiter=’ ‘ –field=3-|sed ‘s/^ *//’`
# Pull NAME from file header
NAME=`grep –max-count=1 “^# Provides:” $(readlink -f $0)|cut –delimiter=’ ‘ –field=3-|sed ‘s/^ *//’`
SCRIPTNAME=/etc/init.d/$NAME

MANAGE_CMD=VBoxManage

# Get VM_SHORT_NAME from service name
VM_SHORT_NAME=`echo $NAME|cut –delimiter=’-‘ –field=2-`
# Actual filename of VM is VM_SHORT_NAME, or if VM_LONG_NAME is set, use that
if [ ! “$VM_LONG_NAME” ] ; then VM_LONG_NAME=$VM_SHORT_NAME ; fi

# Do not use ‘sudo’ if this script is actually running as VM_OWNER already
if [ `whoami` = $VM_OWNER ] ; then SUDO_CMD=”” ; else SUDO_CMD=”sudo -H -u $VM_OWNER” ; fi

# Set VBoxManage command for stop action
if [ $VM_STOP = powerbutton ] ; then VM_STOP_CMD=acpipowerbutton ; else VM_STOP_CMD=savestate ; fi

# If VM_HOSTNAME isn’t set (for ‘start-wait’ command) use VM_SHORTNAME
if [ ! “$VM_HOSTNAME” ] ; then VM_HOSTNAME=$VM_SHORT_NAME; fi

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

get_vm_state()
{
# possible SHORT_STATE values: running, paused, aborted, powered off

VMINFO=$($SUDO_CMD $MANAGE_CMD showvminfo “$VM_LONG_NAME” 2>/dev/null)
if [ $? = 0 ] ; then
# No error retriving state string
LONG_STATE=$(echo “$VMINFO”|grep –max-count=1 “^State:”|cut –delimiter=’ ‘ \
–fields=2-|sed ‘s/^ *//’)
SHORT_STATE=$(echo $LONG_STATE|cut –delimiter=”(” –fields=1|sed ‘s/ *$//’)
# Fix for syntax highlighting in KomodoEdit for previous line
[ 0 = 1 ] && NOOP=$(echo “)”)
else
# VM must be missing
LONG_STATE=missing
SHORT_STATE=missing
fi
}

do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started

get_vm_state

if [ “$SHORT_STATE” = “missing” ] ; then
echo Could not access VM \”$VM_LONG_NAME\”.
return 2
fi

if [ “$SHORT_STATE” = “running” ] ; then
echo VM \”$VM_LONG_NAME\” is already running.
return 1
fi

$SUDO_CMD $MANAGE_CMD startvm “$VM_LONG_NAME” -type vrdp || {
echo Failed to start VM \”$VM_LONG_NAME\”.
return 2
}

# No status report; VBoxManage said if it worked.
return 0
}

do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred

get_vm_state

if [ “$SHORT_STATE” = “missing” ] ; then
echo Could not access VM \”$VM_LONG_NAME\”.
return 3
fi

if [ ! “$SHORT_STATE” = “running” ] ; then
echo VM \”$VM_LONG_NAME\” is already stopped.
return 1
fi

$SUDO_CMD $MANAGE_CMD controlvm “$VM_LONG_NAME” $VM_STOP_CMD || {
echo Failed to hibernate VM \”$VM_LONG_NAME\”.
return 2
}

echo Waiting for \”$VM_LONG_NAME\” to complete shutdown…
while [ “$SHORT_STATE” = “running” ] ; do
sleep 1
get_vm_state
done

echo The VM \”$VM_LONG_NAME\” has been stopped \($VM_STOP\).
return 0
}

do_status()
{
get_vm_state
if [ “$SHORT_STATE” = “missing” ] ; then
echo Could not access VM \”$VM_LONG_NAME\”.
return 2
fi
echo Status of VM \”$VM_LONG_NAME\”: $LONG_STATE

if [ “$SHORT_STATE” = “running” ] ; then return 1 ; else return 0 ; fi
}

do_reload() {
VM_STOP=powerbutton
VM_STOP_CMD=acpipowerbutton
do_stop && do_start
}

do_wait_for_online() {
echo Waiting for \”$VM_LONG_NAME\” to come up on the network…
while [ ! “$result” = “0” ] ; do
sleep 1
ping -c 1 $VM_HOSTNAME >/dev/null 2>/dev/null
result=$?
done
echo Ready.
}

case “$1” in
start)
[ “$VERBOSE” != no ] && log_daemon_msg “Starting $DESC” “$NAME”
do_start
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
start-wait)
do_start && do_wait_for_online
;;
stop)
[ “$VERBOSE” != no ] && log_daemon_msg “Stopping $DESC” “$NAME”
do_stop
case “$?” in
0|1) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
2) [ “$VERBOSE” != no ] && log_end_msg 1 ;;
esac
;;
status)
do_status && exit 0 || exit $?
;;
restart|force-reload)
#
# If the “reload” option is implemented then remove the
# ‘force-reload’ alias
#
[ “$VERBOSE” != no ] && log_daemon_msg “Restarting $DESC” “$NAME”
VM_STOP=powerbutton
VM_STOP_CMD=acpipowerbutton
do_stop
case “$?” in
0|1)
do_start
case “$?” in
0) [ “$VERBOSE” != no ] && log_end_msg 0 ;;
1) [ “$VERBOSE” != no ] && log_end_msg 1 ;; # Old process is still running
*) [ “$VERBOSE” != no ] && log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
[ “$VERBOSE” != no ] && log_end_msg 1
;;
esac
;;
restart-wait)
VM_STOP=poweroff
VM_STOP_CMD=acpipowerbutton
do_stop
do_start && do_wait_for_online
;;
*)
echo “Usage: $SCRIPTNAME {start|start-wait|stop|status|restart|restart-wait|force-reload}” >&2
exit 3
;;
esac

:

Now when this script is inside the /etc/init.d directory, run these commands inside a terminal:

sudo sulogin (enter your password)
cd /etc/init.d
update-rc.d scriptname defaults 90

With this command, you ensure that the script is used at boot time of your Ubuntu computer.

All right, basically that’s all. If you want to check if your VM is running:

terminal
sudo sulogin (enter your password)
service scriptname status

 

Or your just check it out within VirtualBox:

 

Image

Check it out…and have fun!!!

More about my home network configuration in an upcoming article: Thinlinc

 

Advertenties

Entry filed under: Uncategorized.

Ubuntu, Remote Desktop and Windows Remote desktop (1) Ubuntu, Remote Desktop and Windows Remote desktop (3): ThinLinc

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit / Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit / Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit / Bijwerken )

Google+ photo

Je reageert onder je Google+ account. Log uit / Bijwerken )

Verbinden met %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Posted previously


%d bloggers liken dit: