หน้าเว็บ

วันจันทร์ที่ 4 กุมภาพันธ์ พ.ศ. 2556

VirtualBox : Run as Service (Linux)



ที่มา : http://www.glump.net/howto/virtualbox_as_a_service
http://intranet.sci.com/blog.php?u=3&b=669

1.Setup VirtualBox ให้เรียบร้อย
2.สร้าง Config ไฟล์


sudo touch /etc/default/virtualbox
sudo nano /etc/default/virtualbox

  1. SHUTDOWN_USERS="user1 user2" # space-delimited list of users who might have runnings vms. Excemple "sa"
  2. SHUTDOWN=savestate           # if any are found, suspend them to disk

3.สร้าง script สำหรับเรียกใช้งาน (ที่มา https://github.com/bkidwell/vbox-service-template)
*ตั้งชื่อไฟล์ว่า vbox-ตามด้วยชื่อของ VM เช่น vbox-OCS-SERVER แล้วแก้ไขข้อมูลใน script ให้ตรงกับสภาพแวดล้อมของ VM
  1. #! /bin/sh
  2. #
  3. # chkconfig: 2345 90 10
  4. ### BEGIN INIT INFO
  5. # Provides:          vbox-service-template
  6. # Required-Start:    $remote_fs $syslog
  7. # Required-Stop:     $remote_fs $syslog
  8. # Default-Start:     2 3 4 5
  9. # Default-Stop:      0 1 6
  10. # Short-Description: 'service-template' virtual machine
  11. # Description:       Starts and stops a VirtualBox host as a service.
  12. ### END INIT INFO
  13.  
  14. # Author: Brendan Kidwell <brendan@glump.net>
  15. # License: GPL 3 <http://opensource.org/licenses/GPL-3.0>
  16. #
  17. # Based on /etc/init.d/skeleton from Ubuntu 12.04.
  18.  
  19. #-------------------------------------------------------------------------------
  20. #
  21. #  CONFIGURATION
  22. #
  23. #  What is the name of the VM exactly as it appears in the VirtualBox control
  24. #  panel? This is the 'name' and the 'long name' of the VM.
  25. #
  26. #     Does 'name' contain spaces or other special characters? If so, you must
  27. #     make up some other value for 'name' that doesn't have spaces.
  28. #
  29. #  Setup:
  30. #
  31. #  1. Copy this file to /etc/init.d/vbox-'name'. The filename must start with
  32. #     the prefix "vbox-". Make sure you set the 'x' bit on the file to make it
  33. #     executable.
  34. #
  35. #  2. Edit 'Provides', above, to match the filename.
  36. #
  37. #  3. Edit 'Short-Description' to describe the function of the VM.
  38. #
  39. #  4. If 'long name' is different from 'name' fill it in below, otherwise leave
  40. #     LONGNAME as an empty string.
  41.      VM_LONG_NAME=""   # Exemple "NameVM", Exemple "Proxy"
  42. #
  43. #  5. What user owns the virtual machine?
  44.      VM_OWNER=???   # Insert name by number 1. Exemple "sa"
  45. #
  46. #  6. Which stop command? "hibernate" or "powerbutton"
  47.      VM_STOP=hibernate
  48. #
  49. #  7. For the 'start-wait' command -- waiting until network is up, what is the
  50. #     VM's hostname?
  51.       VM_HOSTNAME=""
  52. #
  53. #-------------------------------------------------------------------------------
  54.  
  55. # Do NOT "set -e"
  56.  
  57. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  58. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  59. # Pull DESC from file header
  60. DESC=`grep --max-count=1 "^# Short-Description:" $(readlink -f $0)|cut --delimiter=' ' --field=3-|sed 's/^ *//'`
  61. # Pull NAME from file header
  62. NAME=`grep --max-count=1 "^# Provides:" $(readlink -f $0)|cut --delimiter=' ' --field=3-|sed 's/^ *//'`
  63. SCRIPTNAME=/etc/init.d/$NAME
  64.  
  65. MANAGE_CMD=VBoxManage
  66.  
  67. # Get VM_SHORT_NAME from service name
  68. VM_SHORT_NAME=`echo $NAME|cut --delimiter='-' --field=2-`
  69. # Actual filename of VM is VM_SHORT_NAME, or if VM_LONG_NAME is set, use that
  70. if [ ! "$VM_LONG_NAME" ] ; then VM_LONG_NAME=$VM_SHORT_NAME ; fi
  71.  
  72. # Do not use 'sudo' if this script is actually running as VM_OWNER already
  73. if [ `whoami` = $VM_OWNER ] ; then SUDO_CMD="" ; else SUDO_CMD="sudo -H -u $VM_OWNER" ; fi
  74.  
  75. # Set VBoxManage command for stop action
  76. if [ $VM_STOP = powerbutton ] ; then VM_STOP_CMD=acpipowerbutton ; else VM_STOP_CMD=savestate ; fi
  77.  
  78. # If VM_HOSTNAME isn't set (for 'start-wait' command) use VM_SHORTNAME
  79. if [ ! "$VM_HOSTNAME" ] ; then VM_HOSTNAME=$VM_SHORT_NAME; fi
  80.  
  81. # Load the VERBOSE setting and other rcS variables
  82. . /lib/init/vars.sh
  83.  
  84. # Define LSB log_* functions.
  85. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
  86. # and status_of_proc is working.
  87. . /lib/lsb/init-functions
  88.  
  89. get_vm_state()
  90. {
  91.    # possible SHORT_STATE values: running, paused, aborted, powered off
  92.  
  93.    VMINFO=$($SUDO_CMD $MANAGE_CMD showvminfo "$VM_LONG_NAME" 2>/dev/null)
  94.    if [ $? = 0 ] ; then
  95.        # No error retriving state string
  96.        LONG_STATE=$(echo "$VMINFO"|grep --max-count=1 "^State:"|cut --delimiter=' ' \
  97.            --fields=2-|sed 's/^ *//')
  98.        SHORT_STATE=$(echo $LONG_STATE|cut --delimiter="(" --fields=1|sed 's/ *$//')
  99.        # Fix for syntax highlighting in KomodoEdit for previous line
  100.        [ 0 = 1 ] && NOOP=$(echo ")")
  101.    else
  102.        # VM must be missing
  103.        LONG_STATE=missing
  104.        SHORT_STATE=missing
  105.    fi
  106. }
  107.  
  108. do_start()
  109. {
  110.    # Return
  111.    #   0 if daemon has been started
  112.    #   1 if daemon was already running
  113.    #   2 if daemon could not be started
  114.  
  115.    get_vm_state
  116.  
  117.    if [ "$SHORT_STATE" = "missing" ] ; then
  118.        echo Could not access VM \"$VM_LONG_NAME\".
  119.        return 2
  120.    fi
  121.  
  122.    if [ "$SHORT_STATE" = "running" ] ; then
  123.        echo VM \"$VM_LONG_NAME\" is already running.
  124.        return 1
  125.    fi
  126.  
  127.    $SUDO_CMD $MANAGE_CMD startvm "$VM_LONG_NAME" -type vrdp || {
  128.        echo Failed to start VM \"$VM_LONG_NAME\".
  129.        return 2
  130.    }
  131.  
  132.    # No status report; VBoxManage said if it worked.
  133.    return 0
  134. }
  135.  
  136. do_stop()
  137. {
  138.    # Return
  139.    #   0 if daemon has been stopped
  140.    #   1 if daemon was already stopped
  141.    #   2 if daemon could not be stopped
  142.    #   other if a failure occurred
  143.  
  144.    get_vm_state
  145.  
  146.    if [ "$SHORT_STATE" = "missing" ] ; then
  147.        echo Could not access VM \"$VM_LONG_NAME\".
  148.        return 3
  149.    fi
  150.  
  151.    if [ ! "$SHORT_STATE" = "running" ] ; then
  152.        echo VM \"$VM_LONG_NAME\" is already stopped.
  153.        return 1
  154.    fi
  155.  
  156.    $SUDO_CMD $MANAGE_CMD controlvm "$VM_LONG_NAME" $VM_STOP_CMD || {
  157.        echo Failed to hibernate VM \"$VM_LONG_NAME\".
  158.        return 2
  159.    }
  160.  
  161.    echo Waiting for \"$VM_LONG_NAME\" to complete shutdown...
  162.    while [ "$SHORT_STATE" = "running" ] ; do
  163.        sleep 1
  164.        get_vm_state
  165.    done
  166.  
  167.    echo The VM \"$VM_LONG_NAME\" has been stopped \($VM_STOP\).
  168.    return 0
  169. }
  170.  
  171. do_status()
  172. {
  173.    get_vm_state
  174.    if [ "$SHORT_STATE" = "missing" ] ; then
  175.        echo Could not access VM \"$VM_LONG_NAME\".
  176.        return 2
  177.    fi
  178.    echo Status of VM \"$VM_LONG_NAME\": $LONG_STATE
  179.  
  180.    if [ "$SHORT_STATE" = "running" ] ; then return 1 ; else return 0 ; fi
  181. }
  182.  
  183. do_reload() {
  184.    VM_STOP=powerbutton
  185.    VM_STOP_CMD=acpipowerbutton
  186.    do_stop && do_start
  187. }
  188.  
  189. do_wait_for_online() {
  190.    echo Waiting for \"$VM_LONG_NAME\" to come up on the network...
  191.    while [ ! "$result" = "0" ] ; do
  192.        sleep 1
  193.        ping -c 1 $VM_HOSTNAME >/dev/null 2>/dev/null
  194.        result=$?
  195.    done
  196.    echo Ready.
  197. }
  198.  
  199. case "$1" in
  200.  start)
  201.    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  202.    do_start
  203.    case "$?" in
  204.        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  205.        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  206.    esac
  207.    ;;
  208.  start-wait)
  209.    do_start && do_wait_for_online
  210.    ;;
  211.  stop)
  212.    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  213.    do_stop
  214.    case "$?" in
  215.        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  216.        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  217.    esac
  218.    ;;
  219.  status)
  220.    do_status && exit 0 || exit $?
  221.    ;;
  222.  restart|force-reload)
  223.    #
  224.    # If the "reload" option is implemented then remove the
  225.    # 'force-reload' alias
  226.    #
  227.    [ "$VERBOSE" != no ] && log_daemon_msg "Restarting $DESC" "$NAME"
  228.    VM_STOP=powerbutton
  229.    VM_STOP_CMD=acpipowerbutton
  230.    do_stop
  231.    case "$?" in
  232.      0|1)
  233.        do_start
  234.        case "$?" in
  235.            0) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  236.            1) [ "$VERBOSE" != no ] && log_end_msg 1 ;; # Old process is still running
  237.            *) [ "$VERBOSE" != no ] && log_end_msg 1 ;; # Failed to start
  238.        esac
  239.        ;;
  240.      *)
  241.        # Failed to stop
  242.        [ "$VERBOSE" != no ] && log_end_msg 1
  243.        ;;
  244.    esac
  245.    ;;
  246.  restart-wait)
  247.    VM_STOP=poweroff
  248.    VM_STOP_CMD=acpipowerbutton
  249.    do_stop
  250.    do_start && do_wait_for_online
  251.    ;;
  252.  *)
  253.    echo "Usage: $SCRIPTNAME {start|start-wait|stop|status|restart|restart-wait|force-reload}" >&2
  254.    exit 3
  255.    ;;
  256. esac
  257.  
  258. :
  259.  
  260.  

4.ทดสอบ script (chmod u+x ก่อน) by use command in terminal "chmod u+x /etc/init.d/vbox-Name"
/etc/init.d/vbox-NAME status # Is the VM running?
/etc/init.d/vbox-NAME start # Start the VM
/etc/inid.d/vbox-NAME stop # Stop the VM

5.ติดตั้ง script ให้ทำงานทุกครั้งตอน boot
sudo update-rc.d vbox-NAME defaults 90
Last edited by Suwit_J on Tue 11/09/2012 13:53:50, edited 1 time in total.               

ไม่มีความคิดเห็น:

แสดงความคิดเห็น