Advertisement
kiedtink

Debian VMware Autostart

Feb 20th, 2018
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.61 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. ### BEGIN INIT INFO
  4. # Provides: vmwareautostart
  5. # Required-Start: $vmware $network $syslog
  6. # Required-Stop: $vmware $network $syslog
  7. # X-Start-Before:
  8. # X-Stop-After:
  9. # Default-Start: 2 3 4 5
  10. # Default-Stop: 0 1 6
  11. # Short-Description: This service auto-starts and stops VMware guests
  12. ### END INIT INFO
  13.  
  14. # To use this script, follow installation instructions at
  15. # http://www.atrixnet.com/autostart-vmware-virtual-machines-on-boot-in-linux
  16. # ...and then customize it below
  17.  
  18. # ======== USER CONFIGURABLE VARIABLES (CUSTOMIZE THESE PLEASE) ==========
  19.  
  20. # unless you have weird characters or shell escapes in your varable values
  21. # there is no need to muddy up the shell code by using excessive quotes
  22. # and punctuation.  For this reason, you'll see the example variable values
  23. # below are simple and clean.  Don't put spaces between variables, values,
  24. # and equal signs (=).  You can't do that in shell scripts.
  25.  
  26. # number of seconds to wait between startup of multiple VMs.  The faster
  27. # your disk storage, the lower this number can be.  The idea is to not
  28. # start more VMs at one time than your system can handle and still
  29. # remain stable/responsive
  30.  
  31. VM_wait_between=30
  32.  
  33. # max number of seconds to wait for a virtual machine to gracefully shut
  34. # down before taking it down hard.  Allow more time for app servers and
  35. # windows virtual machines.
  36.  
  37. VM_max_stop_wait=30
  38.  
  39. # name the system user who runs your virtual machines.  you should not be
  40. # running virtual machines as root, in the event that one gets compromised
  41. # that could be a security liability.  I recommend that you consider
  42. # creating an unprivileged system account that does nothing else but run
  43. # virtual machines in vmware
  44.  
  45. VM_user=tommy
  46.  
  47. # list your virtual machines below, with each on its own line within the
  48. # perenthesis block, as shown.  Make sure each VM is a fully-qualified
  49. # path to the .vmx file for the virtual machine you want to auto-start
  50.  
  51. VM_list=(
  52.    /mdlvmraid/vmware/vmachines/private/netmon/netmon.vmx
  53.    /mdlvmraid/vmware/vmachines/private/somevm/somevm.vmx
  54.    /mdlvmraid/vmware/vmachines/private/openvpn/openvpn.vmx
  55. );
  56.  
  57. # ======== THE REST OF THIS CODE IS NOT CONFIGURABLE ==========
  58.  
  59. export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  60.  
  61. if [[ $( id -u ) -ne 0 ]];
  62. then
  63.    echo You must run this script as root or with sudo
  64.  
  65.    exit 1
  66. fi
  67. if [[ "$( getent passwd $VM_user )" == "" ]];
  68. then
  69.    echo Could not locate specified VM user "'$VM_user'" on the system.  Abort.
  70.  
  71.    exit 1
  72. fi
  73.  
  74. VM_user_group=$( id -gn $VM_user );
  75.  
  76. VM_cmd_exec="sudo -u $VM_user -g $VM_user_group vmrun"
  77.  
  78. case "$1"
  79. in
  80.    start)
  81.       VM_iter=0
  82.       VM_list_length=${#VM_list[@]};
  83.  
  84.       for vm in "${VM_list[@]}";
  85.       do
  86.          if [[ $( vmrun list 2>/dev/null | grep $vm | wc -l ) -ne 0 ]];
  87.          then
  88.             echo VM "$vm" is already running
  89.  
  90.             continue;
  91.          fi
  92.  
  93.          echo Starting up VM "$vm" ...
  94.  
  95.          $VM_cmd_exec start "$vm" nogui >/dev/null 2>&1
  96.  
  97.          VM_iter=$(( $VM_iter + 1 ));
  98.  
  99.          if [[ $VM_iter -lt $VM_list_length ]];
  100.          then
  101.             echo -n ...waiting $VM_wait_between seconds before starting next VM
  102.  
  103.             for tick in $( seq 1 $VM_wait_between );
  104.             do
  105.                echo -n .
  106.  
  107.                sleep 1
  108.             done
  109.  
  110.             echo
  111.          fi
  112.       done
  113.  
  114.       $0 status
  115.    ;;
  116.  
  117.    stop)
  118.       for vm in "${VM_list[@]}";
  119.       do
  120.  
  121.          if [[ $( vmrun list 2>/dev/null | grep "$vm" | wc -l ) -eq 0 ]];
  122.          then
  123.             echo VM "$vm" is not running
  124.  
  125.             continue;
  126.          fi
  127.          echo Stopping "$vm"...
  128.  
  129.          $VM_cmd_exec stop "$vm" soft >/dev/null 2>&1 &
  130.  
  131.          VM_stop_pid=$!
  132.  
  133.          VM_stop_waited=0;
  134.  
  135.          echo -n ...Waiting $VM_max_stop_wait seconds for it to stop
  136.  
  137.          while kill -0 $VM_stop_pid >/dev/null 2>&1 ;
  138.          do
  139.             echo -n .
  140.  
  141.             sleep 1
  142.  
  143.             VM_stop_waited=$(( $VM_stop_waited + 1 ));
  144.  
  145.             if [[ $VM_stop_waited -gt $VM_max_stop_wait ]];
  146.             then
  147.                echo
  148.                echo -n ...Timeout reached while waiting for graceful shutdown.
  149.                echo -n Hard shutdown forced...
  150.  
  151.                $VM_cmd_exec stop "$vm" hard >/dev/null 2>&1;
  152.             fi
  153.          done
  154.  
  155.          echo
  156.          echo ...VM "$vm" stopped.
  157.       done
  158.  
  159.       $0 status
  160.    ;;
  161.  
  162.    status)
  163.       $VM_cmd_exec list
  164.    ;;
  165.  
  166.    restart)
  167.       $0 stop && $0 start
  168.    ;;
  169.  
  170.    *)
  171.       echo Usage: $0 '{start|stop|status|restart}'
  172.  
  173.       exit 1
  174.    ;;
  175.  
  176. esac
  177.  
  178. # vim: set ft=sh :
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement