{"id":2284,"date":"2011-09-01T07:48:12","date_gmt":"2011-09-01T06:48:12","guid":{"rendered":"http:\/\/www.bdjl.de\/localhost\/?p=2284"},"modified":"2017-03-01T12:03:48","modified_gmt":"2017-03-01T11:03:48","slug":"virtualbox-start-stop-script","status":"publish","type":"post","link":"https:\/\/www.bdjl.de\/localhost\/?p=2284","title":{"rendered":"VirtualBox Start Stop Script"},"content":{"rendered":"<p>Um auf einem Wirtsrechner mehrere Instanzen von unter Virtualbox laufenden VMs mit dem Wirt selbst zu starten und zu stoppen, brauchten wir entsprechende start-stop-Skripte. F\u00fcndig wurden wir im Forum von Virtualbox:<\/p>\n<p><a href=\"http:\/\/forums.virtualbox.org\/viewtopic.php?f=7&amp;t=34790\" target=\"_blank\">http:\/\/forums.virtualbox.org\/viewtopic.php?f=7&amp;t=34790<\/a><\/p>\n<p>Eine Alternative sahen wir uns ebenfalls noch an, entschieden dann aber f\u00fcr das Skript von Nicolas Tessore. Trotzdem &#8211; wenigstens noch der Link: <a href=\"http:\/\/www.glump.net\/howto\/virtualbox_as_a_service\" target=\"_blank\">http:\/\/www.glump.net\/howto\/virtualbox_as_a_service<\/a><\/p>\n<p>Das Skript von N.Tessore passten wir ein klein wenig bei <em>Require-Start<\/em> an und dokumentieren dies nun hier. Es l\u00e4uft reibungslos auf einem Ubuntu Server 10.04 LTS 64 bit und Virtualbox 4.1 &#8211; f\u00fcr andere VBox- und Ubuntu-Versionen sollte es anpassbar sein:<\/p>\n<blockquote><p>#! \/bin\/sh<br \/>\n### BEGIN INIT INFO<br \/>\n# Provides: vbox-headless<br \/>\n# Required-Start: $syslog $vboxdrv $network<br \/>\n# Required-Stop:<br \/>\n# Default-Start: 2 3 4 5<br \/>\n# Default-Stop: 0 1 6<br \/>\n# Short-Description: Start VMs in headless mode.<br \/>\n# Description: This script runs VMs for the default VirtualBox<br \/>\n#\u00a0user in headless mode. Make sure all VMs are using different<br \/>\n#\u00a0 RDP ports.<br \/>\n### END INIT INFO<\/p>\n<p># Author: Nicolas Tessore &lt;n.tessore@gmail.com&gt;<\/p>\n<p>####<br \/>\n# VirtualBox settings<br \/>\n####<\/p>\n<p># The user which owns the VMs<br \/>\nVBOX_USER=username<\/p>\n<p># The list of VMs to run. Leave empty to run all registered VMs.<br \/>\nVBOX_LIST=&#8220;&#8220;<\/p>\n<p># VirtualBox executables<br \/>\nVBOX_MANAGE=\/usr\/bin\/vboxmanage<br \/>\nVBOX_HEADLESS=\/usr\/bin\/vboxheadless<\/p>\n<p>####<br \/>\n# End VirtualBox settings<br \/>\n####<\/p>\n<p># Do NOT &#8222;set -e&#8220;<\/p>\n<p># PATH should only include \/usr\/* if it runs after the mountnfs.sh script<br \/>\nPATH=\/sbin:\/usr\/sbin:\/bin:\/usr\/bin<br \/>\nDESC=&#8220;VirtualBox daemon&#8220;<br \/>\nNAME=vbox-headless<br \/>\nDAEMON=$VBOX_HEADLESS<br \/>\nDAEMON_ARGS=&#8220;&#8220;<br \/>\nPIDFILE=\/var\/run\/$NAME.pid<br \/>\nSCRIPTNAME=\/etc\/init.d\/$NAME<\/p>\n<p># Exit if the package is not installed<br \/>\n[ -x &#8222;$DAEMON&#8220; ] || exit 0<\/p>\n<p># Read configuration variable file if it is present<br \/>\n[ -r \/etc\/default\/$NAME ] &amp;&amp; . \/etc\/default\/$NAME<\/p>\n<p># Load the VERBOSE setting and other rcS variables<br \/>\n. \/lib\/init\/vars.sh<\/p>\n<p># Define LSB log_* functions.<br \/>\n# Depend on lsb-base (&gt;= 3.0-6) to ensure that this file is present.<br \/>\n. \/lib\/lsb\/init-functions<\/p>\n<p>vm_init_list()<br \/>\n{<br \/>\n# get registered VMs<br \/>\nLIST_VMS=`sudo -H -u $VBOX_USER $VBOX_MANAGE &#8211;nologo list vms | cut -d &#8218; &#8218; -f 1 | tr -d &#8218;&#8220;&#8218;`<\/p>\n<p># check for list of VMs<br \/>\nif [ -z &#8222;$VBOX_LIST&#8220; ]<br \/>\nthen<br \/>\n# all registered VMs for user<br \/>\nVBOX_LIST=$LIST_VMS<br \/>\nelse<br \/>\n# check that VMs exist<br \/>\nfor VM in $VBOX_LIST<br \/>\ndo<br \/>\ncase $LIST_VMS in<br \/>\n&#8222;$VM&#8220;)<br \/>\ncontinue<br \/>\n;;<br \/>\n*)<br \/>\nlog_failure_msg &#8222;ERROR: VM &#8218;$VM&#8216; is not registered!&#8220;<br \/>\nexit 1<br \/>\n;;<br \/>\nesac<br \/>\ndone<br \/>\nfi<br \/>\n}<\/p>\n<p># get uuid for vm<br \/>\nvm_get_uuid()<br \/>\n{<br \/>\nvm=$1<br \/>\nhwuuid=`sudo -H -u $VBOX_USER $VBOX_MANAGE &#8211;nologo showvminfo &#8211;machinereadable &#8222;$vm&#8220; | grep &#8218;hardwareuuid=&#8217;`<br \/>\necho $hwuuid | cut -d &#8218;=&#8216; -f 2 | tr -d &#8218;&#8220;&#8218;<br \/>\n}<\/p>\n<p># control running vm<br \/>\nvm_ctrl()<br \/>\n{<br \/>\nsudo -H -u $VBOX_USER $VBOX_MANAGE &#8211;nologo controlvm $1 $2 &gt; \/dev\/null 2&gt;&amp;1<br \/>\n}<\/p>\n<p>#<br \/>\n# Function that starts the daemon\/service<br \/>\n#<br \/>\ndo_start()<br \/>\n{<br \/>\nvm_init_list<\/p>\n<p># Return<br \/>\n#\u00a0\u00a0 0 if daemon has been started<br \/>\n#\u00a0\u00a0 1 if daemon was already running<br \/>\n#\u00a0\u00a0 2 if daemon could not be started<br \/>\nRETVAL=0<\/p>\n<p># Start all VMs<br \/>\nfor VM in $VBOX_LIST<br \/>\ndo<br \/>\nVM_UUID=`vm_get_uuid $VM`<br \/>\nVM_PIDFILE=&#8220;$PIDFILE.$VM_UUID&#8220;<br \/>\nVM_DAEMON=&#8220;$DAEMON&#8220;<br \/>\nVM_DAEMON_ARGS=&#8220;$DAEMON_ARGS &#8211;startvm $VM_UUID&#8220;<\/p>\n<p>log_action_begin_msg &#8222;Starting VM &#8218;$VM'&#8220;<\/p>\n<p># test for running VM<br \/>\nUSER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \\<br \/>\n&#8211;start \\<br \/>\n&#8211;quiet \\<br \/>\n&#8211;pidfile $VM_PIDFILE \\<br \/>\n&#8211;startas $VM_DAEMON \\<br \/>\n&#8211;test \\<br \/>\n&gt; \/dev\/null<\/p>\n<p># VM already running<br \/>\nif [ &#8222;$?&#8220; != 0 ]<br \/>\nthen<br \/>\n# report VM is running<br \/>\nlog_warning_msg &#8222;VM &#8218;$VM&#8216; already running&#8220;<br \/>\n[ &#8222;$RETVAL&#8220; = 0 ] &amp;&amp; RETVAL=1<br \/>\ncontinue<br \/>\nfi<\/p>\n<p># start VM<br \/>\nUSER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \\<br \/>\n&#8211;start \\<br \/>\n&#8211;quiet \\<br \/>\n&#8211;pidfile $VM_PIDFILE \\<br \/>\n&#8211;make-pidfile \\<br \/>\n&#8211;background \\<br \/>\n&#8211;chuid $VBOX_USER \\<br \/>\n&#8211;startas $VM_DAEMON \\<br \/>\n&#8212; $VM_DAEMON_ARGS<\/p>\n<p>log_action_end_msg &#8222;$?&#8220;<\/p>\n<p># check if start failed<br \/>\nif [ &#8222;$?&#8220; != 0 ]<br \/>\nthen<br \/>\n# report error<br \/>\nlog_failure_msg &#8222;Error starting VM &#8218;$VM'&#8220;<br \/>\nRETVAL=2<br \/>\nfi<br \/>\ndone<\/p>\n<p>if [ &#8222;$RETVAL&#8220; -lt 2 ]<br \/>\nthen<br \/>\nlog_daemon_msg &#8222;VirtualBox daemon started successfully&#8220;<br \/>\nelse<br \/>\nlog_daemon_msg &#8222;VirtualBox daemon started with errors&#8220;<br \/>\nfi<\/p>\n<p>return &#8222;$RETVAL&#8220;<br \/>\n}<\/p>\n<p>#<br \/>\n# Function that stops the daemon\/service<br \/>\n#<br \/>\ndo_stop()<br \/>\n{<br \/>\nvm_init_list<\/p>\n<p># Return<br \/>\n#\u00a0\u00a0 0 if daemon has been stopped<br \/>\n#\u00a0\u00a0 1 if daemon was already stopped<br \/>\n#\u00a0\u00a0 2 if daemon could not be stopped<br \/>\n#\u00a0\u00a0 other if a failure occurred<br \/>\nRETVAL=0<\/p>\n<p>for VM in $VBOX_LIST<br \/>\ndo<br \/>\nVM_UUID=`vm_get_uuid $VM`<br \/>\nVM_PIDFILE=&#8220;$PIDFILE.$VM_UUID&#8220;<\/p>\n<p>log_action_begin_msg &#8222;Stopping VM &#8218;$VM'&#8220;<\/p>\n<p># try savestate halt<br \/>\nvm_ctrl $VM savestate<\/p>\n<p># stop daemon<br \/>\nUSER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \\<br \/>\n&#8211;stop \\<br \/>\n&#8211;quiet \\<br \/>\n&#8211;retry=TERM\/30\/KILL\/5 \\<br \/>\n&#8211;pidfile $VM_PIDFILE<\/p>\n<p>case &#8222;$?&#8220; in<br \/>\n0)<br \/>\nlog_action_end_msg 0<br \/>\n;;<br \/>\n1)<br \/>\nlog_warning_msg &#8222;VM &#8218;$VM&#8216; already stopped&#8220;<br \/>\n[ &#8222;$RETVAL&#8220; = 0 ] &amp;&amp; RETVAL=1<br \/>\n;;<br \/>\n2)<br \/>\nlog_action_end_msg 1<br \/>\nlog_failure_msg &#8222;ERROR: Could not stop VM &#8218;$VM'&#8220;<br \/>\nRETVAL=2<br \/>\ncontinue<br \/>\n;;<br \/>\nesac<\/p>\n<p>rm -f $VM_PIDFILE<br \/>\ndone<\/p>\n<p>if [ &#8222;$RETVAL&#8220; -lt 2 ]<br \/>\nthen<br \/>\nlog_daemon_msg &#8222;VirtualBox daemon stopped successfully&#8220;<br \/>\nelse<br \/>\nlog_daemon_msg &#8222;VirtualBox daemon stopped with errors&#8220;<br \/>\nfi<\/p>\n<p>return &#8222;$RETVAL&#8220;<br \/>\n}<\/p>\n<p>#<br \/>\n# Function that sends a SIGHUP to the daemon\/service<br \/>\n#<br \/>\ndo_reload() {<br \/>\n#<br \/>\n# If the daemon can reload its configuration without<br \/>\n# restarting (for example, when it is sent a SIGHUP),<br \/>\n# then implement that here.<br \/>\n#<br \/>\nstart-stop-daemon &#8211;stop &#8211;signal 1 &#8211;quiet &#8211;pidfile $PIDFILE &#8211;name $NAME<br \/>\nreturn 0<br \/>\n}<\/p>\n<p>case &#8222;$1&#8220; in<br \/>\nstart)<br \/>\nlog_daemon_msg &#8222;Starting $DESC&#8220; &#8222;$NAME&#8220;<br \/>\ndo_start<br \/>\ncase &#8222;$?&#8220; in<br \/>\n0|1) log_end_msg 0 ;;<br \/>\n2) log_end_msg 1 ;;<br \/>\nesac<br \/>\n;;<br \/>\nstop)<br \/>\nlog_daemon_msg &#8222;Stopping $DESC&#8220; &#8222;$NAME&#8220;<br \/>\ndo_stop<br \/>\ncase &#8222;$?&#8220; in<br \/>\n0|1) log_end_msg 0 ;;<br \/>\n2) log_end_msg 1 ;;<br \/>\nesac<br \/>\n;;<br \/>\nstatus)<br \/>\nstatus_of_proc &#8222;$DAEMON&#8220; &#8222;$NAME&#8220; &amp;&amp; exit 0 || exit $?<br \/>\n;;<br \/>\n#reload|force-reload)<br \/>\n#<br \/>\n# If do_reload() is not implemented then leave this commented out<br \/>\n# and leave &#8218;force-reload&#8216; as an alias for &#8218;restart&#8216;.<br \/>\n#<br \/>\n#log_daemon_msg &#8222;Reloading $DESC&#8220; &#8222;$NAME&#8220;<br \/>\n#do_reload<br \/>\n#log_end_msg $?<br \/>\n#;;<br \/>\nrestart|force-reload)<br \/>\n#<br \/>\n# If the &#8222;reload&#8220; option is implemented then remove the<br \/>\n# &#8218;force-reload&#8216; alias<br \/>\n#<br \/>\nlog_daemon_msg &#8222;Restarting $DESC&#8220; &#8222;$NAME&#8220;<br \/>\ndo_stop<br \/>\ncase &#8222;$?&#8220; in<br \/>\n0|1)<br \/>\ndo_start<br \/>\ncase &#8222;$?&#8220; in<br \/>\n0) log_end_msg 0 ;;<br \/>\n1) log_end_msg 1 ;; # Old process is still running<br \/>\n*) log_end_msg 1 ;; # Failed to start<br \/>\nesac<br \/>\n;;<br \/>\n*)<br \/>\n# Failed to stop<br \/>\nlog_end_msg 1<br \/>\n;;<br \/>\nesac<br \/>\n;;<br \/>\n*)<br \/>\n#echo &#8222;Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}&#8220; &gt;&amp;2<br \/>\necho &#8222;Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}&#8220; &gt;&amp;2<br \/>\nexit 3<br \/>\n;;<br \/>\nesac<\/p>\n<p>:<\/p><\/blockquote>\n<p>In diesem Pastebin liegt es auch noch rum, sollte Copy and Paste aus WordPress mal wieder zickig sein:<\/p>\n<p><a href=\"http:\/\/pastebin.com\/4i8W0FB2\" target=\"_blank\">http:\/\/pastebin.com\/4i8W0FB2<\/a><\/p>\n<p>Mit Hilfe von<\/p>\n<blockquote><p>update-rc.d\u00a0 vbox-headless start 99 2 3 4 5 . stop 99 0 1 6 .<\/p><\/blockquote>\n<p>konnte das Skript dann zwar in die richtigen Ordner<\/p>\n<blockquote><p>\/etc\/rc2.d\/S99vbox-headless<br \/>\n\/etc\/rc3.d\/S99vbox-headless<br \/>\n\/etc\/rc4.d\/S99vbox-headless<br \/>\n\/etc\/rc5.d\/S99vbox-headless<\/p><\/blockquote>\n<p>eingef\u00fcgt werden, aber &#8211; komisch, komisch &#8211; nicht an der 99. Stelle, sondern immer nur an der 20. Diese Anpassung musste also von Hand vorgenommen werden.<\/p>\n<p>Das wirklich coole an N.Tessores Skript ist, dass es die VMs nicht herunterf\u00e4hrt, sondern bei einem Serverreboot schlicht pausieren l\u00e4sst. So sind die VMs nach dem Start des Wirtsrechners sofort wieder da.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Um auf einem Wirtsrechner mehrere Instanzen von unter Virtualbox laufenden VMs mit dem Wirt selbst zu starten und zu stoppen, brauchten wir entsprechende start-stop-Skripte. F\u00fcndig wurden wir im Forum von Virtualbox: http:\/\/forums.virtualbox.org\/viewtopic.php?f=7&amp;t=34790 Eine Alternative sahen wir uns ebenfalls noch an, entschieden dann aber f\u00fcr das Skript von Nicolas Tessore. Trotzdem &#8211; wenigstens noch der Link: [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[22,181],"class_list":["post-2284","post","type-post","status-publish","format-standard","hentry","category-linux","category-schule","tag-script","tag-virtualbox"],"_links":{"self":[{"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/posts\/2284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2284"}],"version-history":[{"count":7,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/posts\/2284\/revisions"}],"predecessor-version":[{"id":4952,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=\/wp\/v2\/posts\/2284\/revisions\/4952"}],"wp:attachment":[{"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bdjl.de\/localhost\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}