SHARE
TWEET

vbox-headless start-stop script

a guest Sep 1st, 2011 406 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          vbox-headless
  4. # Required-Start:    $syslog $vboxdrv $network
  5. # Required-Stop:    
  6. # Default-Start:     2 3 4 5
  7. # Default-Stop:      0 1 6
  8. # Short-Description: Start VMs in headless mode.
  9. # Description:       This script runs VMs for the default VirtualBox user in
  10. #                    headless mode. Make sure all VMs are using different RDP
  11. #                    ports.
  12. ### END INIT INFO
  13.  
  14. # Author: Nicolas Tessore <n.tessore@gmail.com>
  15.  
  16. ####
  17. # VirtualBox settings
  18. ####
  19.  
  20. # The user which owns the VMs
  21. VBOX_USER=username
  22.  
  23. # The list of VMs to run. Leave empty to run all registered VMs.
  24. VBOX_LIST=""
  25.  
  26. # VirtualBox executables
  27. VBOX_MANAGE=/usr/bin/vboxmanage
  28. VBOX_HEADLESS=/usr/bin/vboxheadless
  29.  
  30. ####
  31. # End VirtualBox settings
  32. ####
  33.  
  34. # Do NOT "set -e"
  35.  
  36. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  37. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  38. DESC="VirtualBox daemon"
  39. NAME=vbox-headless
  40. DAEMON=$VBOX_HEADLESS
  41. DAEMON_ARGS=""
  42. PIDFILE=/var/run/$NAME.pid
  43. SCRIPTNAME=/etc/init.d/$NAME
  44.  
  45. # Exit if the package is not installed
  46. [ -x "$DAEMON" ] || exit 0
  47.  
  48. # Read configuration variable file if it is present
  49. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  50.  
  51. # Load the VERBOSE setting and other rcS variables
  52. . /lib/init/vars.sh
  53.  
  54. # Define LSB log_* functions.
  55. # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
  56. . /lib/lsb/init-functions
  57.  
  58.  
  59. vm_init_list()
  60. {
  61.         # get registered VMs
  62.         LIST_VMS=`sudo -H -u $VBOX_USER $VBOX_MANAGE --nologo list vms | cut -d ' ' -f 1 | tr -d '"'`
  63.        
  64.        # check for list of VMs
  65.        if [ -z "$VBOX_LIST" ]
  66.        then
  67.           # all registered VMs for user
  68.           VBOX_LIST=$LIST_VMS
  69.        else
  70.           # check that VMs exist
  71.           for VM in $VBOX_LIST
  72.           do
  73.              case $LIST_VMS in
  74.              "$VM")
  75.                 continue
  76.                 ;;
  77.              *)
  78.                 log_failure_msg "ERROR: VM '$VM' is not registered!"
  79.                 exit 1
  80.                 ;;
  81.              esac
  82.           done
  83.        fi
  84.     }
  85.  
  86.     # get uuid for vm
  87.     vm_get_uuid()
  88.     {
  89.        vm=$1
  90.        hwuuid=`sudo -H -u $VBOX_USER $VBOX_MANAGE --nologo showvminfo --machinereadable "$vm" | grep 'hardwareuuid='`
  91.        echo $hwuuid | cut -d '=' -f 2 | tr -d '"'
  92.     }
  93.  
  94.     # control running vm
  95.     vm_ctrl()
  96.     {
  97.        sudo -H -u $VBOX_USER $VBOX_MANAGE --nologo controlvm $1 $2 > /dev/null 2>&1
  98.     }
  99.  
  100.     #
  101.     # Function that starts the daemon/service
  102.     #
  103.     do_start()
  104.     {
  105.        vm_init_list
  106.        
  107.        # Return
  108.        #   0 if daemon has been started
  109.        #   1 if daemon was already running
  110.        #   2 if daemon could not be started
  111.        RETVAL=0
  112.        
  113.        # Start all VMs
  114.        for VM in $VBOX_LIST
  115.        do
  116.           VM_UUID=`vm_get_uuid $VM`
  117.           VM_PIDFILE="$PIDFILE.$VM_UUID"
  118.           VM_DAEMON="$DAEMON"
  119.           VM_DAEMON_ARGS="$DAEMON_ARGS --startvm $VM_UUID"
  120.          
  121.           log_action_begin_msg "Starting VM '$VM'"
  122.          
  123.           # test for running VM
  124.           USER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \
  125.              --start \
  126.              --quiet \
  127.              --pidfile $VM_PIDFILE \
  128.              --startas $VM_DAEMON \
  129.              --test \
  130.              > /dev/null
  131.          
  132.           # VM already running
  133.           if [ "$?" != 0 ]
  134.           then
  135.              # report VM is running
  136.              log_warning_msg "VM '$VM' already running"
  137.              [ "$RETVAL" = 0 ] && RETVAL=1
  138.              continue
  139.           fi
  140.          
  141.           # start VM
  142.           USER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \
  143.              --start \
  144.              --quiet \
  145.              --pidfile $VM_PIDFILE \
  146.              --make-pidfile \
  147.              --background \
  148.              --chuid $VBOX_USER \
  149.              --startas $VM_DAEMON \
  150.              -- $VM_DAEMON_ARGS
  151.          
  152.           log_action_end_msg "$?"
  153.          
  154.           # check if start failed
  155.           if [ "$?" != 0 ]
  156.           then
  157.              # report error
  158.              log_failure_msg "Error starting VM '$VM'"
  159.              RETVAL=2
  160.           fi
  161.        done
  162.        
  163.        if [ "$RETVAL" -lt 2 ]
  164.        then
  165.           log_daemon_msg "VirtualBox daemon started successfully"
  166.        else
  167.           log_daemon_msg "VirtualBox daemon started with errors"
  168.        fi
  169.        
  170.        return "$RETVAL"
  171.     }
  172.  
  173.     #
  174.     # Function that stops the daemon/service
  175.     #
  176.     do_stop()
  177.     {
  178.        vm_init_list
  179.        
  180.        # Return
  181.        #   0 if daemon has been stopped
  182.        #   1 if daemon was already stopped
  183.        #   2 if daemon could not be stopped
  184.        #   other if a failure occurred
  185.        RETVAL=0
  186.        
  187.        for VM in $VBOX_LIST
  188.        do
  189.           VM_UUID=`vm_get_uuid $VM`
  190.           VM_PIDFILE="$PIDFILE.$VM_UUID"
  191.          
  192.           log_action_begin_msg "Stopping VM '$VM'"
  193.          
  194.           # try savestate halt
  195.           vm_ctrl $VM savestate
  196.          
  197.           # stop daemon
  198.           USER=$VBOX_USER LOGNAME=$VBOX_USER start-stop-daemon \
  199.              --stop \
  200.              --quiet \
  201.              --retry=TERM/30/KILL/5 \
  202.              --pidfile $VM_PIDFILE
  203.          
  204.           case "$?" in
  205.           0)
  206.              log_action_end_msg 0
  207.              ;;
  208.           1)
  209.              log_warning_msg "VM '$VM' already stopped"
  210.              [ "$RETVAL" = 0 ] && RETVAL=1
  211.              ;;
  212.           2)
  213.              log_action_end_msg 1
  214.              log_failure_msg "ERROR: Could not stop VM '$VM'"
  215.              RETVAL=2
  216.              continue
  217.              ;;
  218.           esac
  219.          
  220.           rm -f $VM_PIDFILE
  221.        done
  222.        
  223.        if [ "$RETVAL" -lt 2 ]
  224.        then
  225.           log_daemon_msg "VirtualBox daemon stopped successfully"
  226.        else
  227.           log_daemon_msg "VirtualBox daemon stopped with errors"
  228.        fi
  229.        
  230.        return "$RETVAL"
  231.     }
  232.  
  233.     #
  234.     # Function that sends a SIGHUP to the daemon/service
  235.     #
  236.     do_reload() {
  237.        #
  238.        # If the daemon can reload its configuration without
  239.        # restarting (for example, when it is sent a SIGHUP),
  240.        # then implement that here.
  241.        #
  242.        start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
  243.        return 0
  244.     }
  245.  
  246.     case "$1" in
  247.       start)
  248.        log_daemon_msg "Starting $DESC" "$NAME"
  249.        do_start
  250.        case "$?" in
  251.           0|1) log_end_msg 0 ;;
  252.           2) log_end_msg 1 ;;
  253.        esac
  254.        ;;
  255.       stop)
  256.        log_daemon_msg "Stopping $DESC" "$NAME"
  257.        do_stop
  258.        case "$?" in
  259.           0|1) log_end_msg 0 ;;
  260.           2) log_end_msg 1 ;;
  261.        esac
  262.        ;;
  263.       status)
  264.            status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
  265.            ;;
  266.       #reload|force-reload)
  267.        #
  268.        # If do_reload() is not implemented then leave this commented out
  269.        # and leave 'force-reload' as an alias for 'restart'.
  270.        #
  271.        #log_daemon_msg "Reloading $DESC" "$NAME"
  272.        #do_reload
  273.        #log_end_msg $?
  274.        #;;
  275.       restart|force-reload)
  276.        #
  277.        # If the "reload" option is implemented then remove the
  278.        # 'force-reload' alias
  279.        #
  280.        log_daemon_msg "Restarting $DESC" "$NAME"
  281.        do_stop
  282.        case "$?" in
  283.          0|1)
  284.           do_start
  285.           case "$?" in
  286.              0) log_end_msg 0 ;;
  287.              1) log_end_msg 1 ;; # Old process is still running
  288.              *) log_end_msg 1 ;; # Failed to start
  289.           esac
  290.           ;;
  291.          *)
  292.             # Failed to stop
  293.           log_end_msg 1
  294.           ;;
  295.        esac
  296.        ;;
  297.       *)
  298.        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  299.        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  300.        exit 3
  301.        ;;
  302.     esac
  303.  
  304.     :
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top