<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Alex Amiryan's Linux Blog &#187; script</title>
	<atom:link href="http://www.amiryan.org/tag/script/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.amiryan.org</link>
	<description>Just sharing my experiance in linux</description>
	<lastBuildDate>Tue, 10 Nov 2009 19:56:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>VirtualBox init.d service autostart script</title>
		<link>http://www.amiryan.org/2009/11/04/virtualbox-init-d-service-autostart-script/</link>
		<comments>http://www.amiryan.org/2009/11/04/virtualbox-init-d-service-autostart-script/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 16:38:34 +0000</pubDate>
		<dc:creator>Alex Amiryan</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[howtos]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[service]]></category>
		<category><![CDATA[startup]]></category>
		<category><![CDATA[VirtualBox]]></category>

		<guid isPermaLink="false">http://www.amiryan.org/?p=246</guid>
		<description><![CDATA[I&#8217;ve just installed started using VirtualBox on my Fedora 11 x86_64 and it works just perfectly. I&#8217;ve migrated from VMWare when it figured out that it is unable to work with latest kernel version. So I need to start some virtual machines in the background with system startup. I&#8217;ve made a lot of googling and [...]]]></description>
			<content:encoded><![CDATA[<!--Amazon_CLS_IM_START--><p>I&#8217;ve just installed started using VirtualBox on my Fedora 11 x86_64 and it works just perfectly. I&#8217;ve migrated from VMWare when it figured out that it is unable to work with latest kernel version. So I need to start some virtual machines in the background with system startup. I&#8217;ve made a lot of googling and found some dirty scripts that were not meeting my criterias. I&#8217;ve decided to write my own system startup service.</p>
<p>You need to create file named <strong>vbox</strong> in <strong>/etc/sysconfig/</strong> to list the virtual machine names that you want to start with system. Also when shutting down this service will save state to all running VMs. Here is the script:</p>
<p><em><strong>/etc/init.d/vbox</strong></em></p>
<pre class="brush: bash">
#!&#47;bin&#47;sh
#
# chkconfig: - 91 35
# description: Starts and stops vbox autostart VMs.

### BEGIN INIT INFO
# Provides: vbox
# Required-Start: $network $named $vboxdrv
# Required-Stop: $network $named
# Default-Start:
# Default-Stop: 0 1 2 3 4 5 6
# Short-Description: Autostart some Virtual Box VMs
# Description: Autostart some Virtual Box VMs that are mentioned in /etc/sysconfig/vbox file
### END INIT INFO

. /etc/rc.d/init.d/functions

MANAGE_CMD=VBoxManage

[ -r /etc/sysconfig/vbox ] &amp;&amp; . /etc/sysconfig/vbox

prog=$"Virtual Box Machines"

start()
{
	echo -n $"Starting $prog: "
	RETVAL=0

	for vbox_name in ${VBOX_AUTOSTART}
	do
	    SERVS=1
	    echo -n "${vbox_name} "
	    daemon $MANAGE_CMD startvm "${vbox_name}" -type vrdp &gt;/dev/null 2&gt;&amp;1
	    RETVAL=$?
	    [ "$RETVAL" -eq 0 ] || break
	done
	if [ -z "$SERVS" ]; then
	    echo -n "no virtual machines configured "
	    failure
	    RETVAL=6
	else
	    if [ "$RETVAL" -eq 0 ]; then
		success $"vbox startup"
		touch /var/lock/subsys/vbox
	    else
		failure $"vbox start"
	    fi
	fi
	echo
	return "$RETVAL"
}

stop()
{
	echo -n $"Shutting down $prog: "
	for vbox_name in ${VBOX_AUTOSTART}
	do
	    echo -n "${vbox_name} "
	    runuser root -c "$MANAGE_CMD -q controlvm "${vbox_name}" savestate" &gt;/dev/null 2>&#038;1
	done
	RETVAL=$?
	[ "$RETVAL" -eq 0 ] &amp;&amp; success $"vbox shutdown" || \
	    failure $"vbox shutdown"
	echo
	[ "$RETVAL" -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/vbox
	return "$RETVAL"
}

status()
{
	for vbox_name in ${VBOX_AUTOSTART}
	do
	    echo -n "${vbox_name} "
	    $MANAGE_CMD showvminfo "${vbox_name}"|grep "^State:\s*.*$"
	done
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  status)
	status
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|status}" >&#038;2
	exit 3
	;;
esac

:</pre>
<p>And here is configuration file:</p>
<p><em><strong>/etc/sysconfig/vbox</strong></em></p>
<pre class="brush:bash"># Virtual box machines to autostart
# Example to start 2 machines
#	VBOX_AUTOSTART = "MachineName1 MachineName2"

# VBOX_AUTOSTART=""</pre>
<p>Thats it. If you have any questions do not hesitate to contact me.</p>
<!--Amazon_CLS_IM_END-->]]></content:encoded>
			<wfw:commentRss>http://www.amiryan.org/2009/11/04/virtualbox-init-d-service-autostart-script/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
