Advertisement
Guest User

Proper Arch Linux /etc/init.d/vmware

a guest
Apr 17th, 2014
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.99 KB | None | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright 1998-2008 VMware, Inc.  All rights reserved.
  4. #
  5. # This script manages the services needed to run VMware software.
  6. #
  7.  
  8. # VMWARE_INIT_INFO
  9.  
  10. ETCDIR=/etc/vmware
  11.  
  12. . $ETCDIR/bootstrap
  13. libdir="$LIBDIR"/vmware
  14.  
  15. . "$libdir"/scripts/util.sh
  16.  
  17. load_settings "$libdir" || exit 1
  18.  
  19. VNETLIB_LOG=/var/log/vnetlib
  20. PRODUCT_NAME="VMware VMX"
  21. COMPONENT_NAME="vmware-vmx"
  22.  
  23. # This comment is a hack to prevent RedHat distributions from outputing
  24. # "Starting <basename of this script>" when running this startup script.
  25. # We just need to write the word daemon followed by a space
  26.  
  27. # This defines echo_success() and echo_failure() on RedHat
  28. if [ -r "$INITSCRIPTDIR"'/functions' ]; then
  29.    . "$INITSCRIPTDIR"'/functions'
  30. fi
  31.  
  32. # This defines $rc_done and $rc_failed on S.u.S.E.
  33. if [ -f /etc/rc.config ]; then
  34.    # Don't include the entire file: there could be conflicts
  35.    rc_done=`(. /etc/rc.config; echo "$rc_done")`
  36.    rc_failed=`(. /etc/rc.config; echo "$rc_failed")`
  37. else
  38.    # Make sure the ESC byte is literal: Ash does not support echo -e
  39.    rc_done=' done'
  40.    rc_failed='failed'
  41. fi
  42.  
  43. subsys=vmware
  44. driver=vmmon
  45. vnet=vmnet
  46. vmblock=vmblock
  47. vmci=vmci
  48. vmci_alias='pci:v000015ADd00000740sv*sd*bc*sc*i*'
  49. vmhgfs=vmhgfs
  50. vsock=vsock
  51. vsock_alias=vmware_vsock
  52.  
  53. vmciNode=vmci
  54. vsockNode=vsock
  55.  
  56. # SHM settings
  57. shmmaxPath=/proc/sys/kernel/shmmax
  58. shmmaxMinValue=268435456 # 256MB
  59.  
  60. #
  61. # Are we running in a VM?
  62. #
  63. vmwareInVM() {
  64.    "$BINDIR"/checkvm >/dev/null 2>&1
  65. }
  66.  
  67. #
  68. # Report a positive number if there are any VMs running.
  69. # May not be the actual vmmon reference count.
  70. #
  71. vmmonUseCount() {
  72.    local count
  73.    # Beware of module dependencies here. An exact match is important
  74.    count=`/sbin/lsmod | awk 'BEGIN {n = 0} {if ($1 == "'"$driver"'") n = $3} END {print n}'`
  75.    # If CONFIG_MODULE_UNLOAD is not set in the kernel, lsmod prints '-' instead of the
  76.    # reference count, so ask vmrun, or if we don't have vmrun, look for running vmx processes
  77.    if [ x${count} = "x-" ]
  78.    then
  79.       type vmrun > /dev/null 2>&1
  80.       if [ $? -eq 0 ]
  81.       then
  82.          count=`vmrun list | awk 'BEGIN {n=0} /^Total running VMs:/ {n = $4} END {print n}'`
  83.       else
  84.          count=`ps -afe | grep "/bin/vmware-vmx" | grep -v grep | wc -l`
  85.       fi
  86.    fi
  87.    echo $count
  88. }
  89.  
  90. # Is a given module loaded?
  91. isLoaded() {
  92.    local module="$1"
  93.  
  94.    /sbin/lsmod | awk 'BEGIN {n = "no";} {if ($1 == "'"$module"'") n = "yes";} END {print n;}'
  95. }
  96.  
  97. # Build a Linux kernel integer version
  98. kernelVersionInteger() {
  99.    echo $(((($1 * 256) + $2) * 256 + $3))
  100. }
  101.  
  102. # Get the running kernel integer version
  103. getVersionInteger() {
  104.    local version_uts
  105.    local v1
  106.    local v2
  107.    local v3
  108.  
  109.    version_uts=`uname -r`
  110.  
  111.    # There is no double quote around the back-quoted expression on purpose
  112.    # There is no double quote around $version_uts on purpose
  113.    set -- `IFS='.'; echo $version_uts`
  114.    v1="$1"
  115.    v2="$2"
  116.    v3="$3"
  117.    # There is no double quote around the back-quoted expression on purpose
  118.    # There is no double quote around $v3 on purpose
  119.    set -- `IFS='-ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'; echo $v3`
  120.    v3="$1"
  121.  
  122.    kernelVersionInteger "$v1" "$v2" "$v3"
  123. }
  124.  
  125. vmwareLoadModule() {
  126.    /sbin/modprobe "$1" || exit 1
  127. }
  128.  
  129. vmwareUnloadModule() {
  130.    [ "`isLoaded "$1"`" = 'no' ] && return 0
  131.    # if we ran depmod after removing the modules modprobe -r will not work:
  132.    /sbin/modprobe -r "$1" || /sbin/rmmod $1 || exit 1
  133. }
  134.  
  135. # Start the virtual machine monitor kernel service
  136. vmwareStartVmmon() {
  137.    vmwareLoadModule $driver
  138. }
  139.  
  140. # Stop the virtual machine monitor kernel service
  141. vmwareStopVmmon() {
  142.    vmwareUnloadModule $driver
  143. }
  144.  
  145. # Start the virtual ethernet kernel service
  146. vmwareStartVmnet() {
  147.    vmwareLoadModule $vnet
  148.    "$BINDIR"/vmware-networks --start >> $VNETLIB_LOG 2>&1
  149. }
  150.  
  151. # Stop the virtual ethernet kernel service
  152. vmwareStopVmnet() {
  153.    "$BINDIR"/vmware-networks --stop >> $VNETLIB_LOG 2>&1
  154.    vmwareUnloadModule $vnet
  155. }
  156.  
  157. # Returns 0 if networking is enabled, otherwise 1
  158. vmwareIsNetworkingEnabled() {
  159.   [ "$vmdb_NETWORKING" = 'yes' ]
  160.   return
  161. }
  162.  
  163. vmwareRealModName() {
  164.    # modprobe might be old and not understand the -R option, or
  165.    # there might not be an alias. In both cases we assume
  166.    # that the module is not upstreamed.
  167.    mod=$1
  168.    mod_alias=$2
  169.  
  170.    modname=$(/sbin/modprobe -R ${mod_alias} 2>/dev/null)
  171.    if [ $? = 0 -a "$modname" != "" ] ; then
  172.     echo $modname
  173.    else
  174.     echo $mod
  175.    fi
  176. }
  177.  
  178. # Start the virtual machine communication interface kernel service
  179. vmwareStartVmci() {
  180.    mod=$(vmwareRealModName $vmci $vmci_alias)
  181.  
  182.    # only load vmci if it's not already loaded
  183.    if [ "`isLoaded "$mod"`" = 'no' ]; then
  184. #      vmwareLoadModule "$mod"
  185.       vmwareLoadModule "$vmci"
  186.    fi
  187.    vmware_rm_stale_node "$vmciNode"
  188.    if [ ! -e "/dev/$vmciNode" ]; then
  189.       local minor=`cat /proc/misc | grep $vmciNode | awk '{print $1}'`
  190.       mknod --mode=666 "/dev/$vmciNode" c 10 "$minor"
  191.    else
  192.       chmod 666 "/dev/$vmciNode"
  193.    fi
  194.  
  195.    return 0
  196. }
  197.  
  198. # Make sure the system has enough shared memory available to cover shmmaxMinValue.
  199. # To handle overflow/wrapping, check that shmmax is greater than 1 since any overflow
  200. # will make shmmax look negative.  At least until shmmax or shmmaxMinValue wrap around
  201. # again.
  202. vmwareCheckSharedMemory() {
  203.    if [ -f "$shmmaxPath" ]; then
  204.       shmmax=`cat $shmmaxPath`
  205.       # Account for numbers that are too large that they wrap around and alias
  206.       # to a smaller number or they are outright set to -1.  If "1 < XXXX" fails
  207.       # then the XXX value is # out of bounds.  The only acceptable combo is that
  208.       # both values satisfy that condition, else report that the max value the
  209.       # system supports may not satisfy this programs requirements.
  210.       if  ((  $shmmax < 1 )) || (( $shmmaxMinValue < 1 )) \
  211.        || (( $shmmax < $shmmaxMinValue )) ; then
  212.          echo "$shmmaxMinValue" > "$shmmaxPath"
  213.          echo ""
  214.          echo "Setting the max shared memory the system will allow to $shmmaxMinValue."
  215.          echo ""
  216.       fi
  217.    fi
  218.    return 0
  219. }
  220.  
  221.  
  222. # Stop the virtual machine communication interface kernel service
  223. vmwareStopVmci() {
  224.    # Hosted now has to interface with Tools.  vmhgfs could possibly be loaded, which
  225.    # will interfere with the removal of vmci.  Only unload it if it's already
  226.    # loaded.
  227.    if [ "`isLoaded "$vmhgfs"`" = 'yes' ]; then
  228.       vmwareUnloadModule "$vmhgfs"
  229.    fi
  230.  
  231.    mod=$(vmwareRealModName $vmci $vmci_alias)
  232.  
  233.    # only unload vmci if it's already loaded
  234.    if [ "`isLoaded "${mod}"`" = 'yes' ]; then
  235. #      vmwareUnloadModule "${mod}"
  236.       vmwareUnloadModule "${vmci}"
  237.    fi
  238.    rm -f "/dev/$vmciNode"
  239. }
  240.  
  241. isVmciNeeded() {
  242.    if [ "$vmdb_VMCI_CONFED" = 'yes' ]; then
  243.       echo yes
  244.    else
  245.       echo no
  246.    fi
  247. }
  248.  
  249. # starts after vmci is loaded
  250. vmwareStartVsock() {
  251.    mod=$(vmwareRealModName $vsock $vsock_alias)
  252.    # only load vsock if it's not already loaded
  253.    if [ "`isLoaded "$mod"`" = 'no' ]; then
  254. #      vmwareLoadModule "$mod"
  255.       vmwareLoadModule "$vsock"
  256.    fi
  257.    vmware_rm_stale_node "$vsockNode"
  258.    # Give udev 5 seconds to create our node
  259.    vmware_delay_for_node "/dev/$vsockNode" 5
  260.    if [ ! -e "/dev/$vsockNode" ]; then
  261.       local minor=`cat /proc/misc | grep $vsockNode | awk '{print $1}'`
  262.       mknod --mode=666 "/dev/$vsockNode" c 10 "$minor"
  263.    else
  264.       chmod 666 "/dev/$vsockNode"
  265.    fi
  266.  
  267.    return 0
  268. }
  269.  
  270. # unloads before vmci
  271. vmwareStopVsock() {
  272.    mod=$(vmwareRealModName $vsock $vsock_alias)
  273.    # only unload vsock if it's already loaded
  274.    if [ "`isLoaded "$mod"`" = 'yes' ]; then
  275. #     vmwareUnloadModule "$mod"
  276.      vmwareUnloadModule "$vsock"
  277.    fi
  278.    rm -f /dev/vsock
  279. }
  280.  
  281. isVsockNeeded() {
  282.    if [ "$vmdb_VSOCK_CONFED" = 'yes' ]; then
  283.       echo yes
  284.    else
  285.       echo no
  286.    fi
  287. }
  288.  
  289. vmware_start_authdlauncher() {
  290.    vmware_bg_exec "`vmware_product_name` Authentication Daemon" \
  291.       "$SBINDIR/vmware-authdlauncher"
  292. }
  293.  
  294. vmware_stop_authdlauncher() {
  295.    local launcherpid=`pidof vmware-authdlauncher`
  296.    if [ -n "$launcherpid" ]; then
  297.       vmware_synchrone_kill $launcherpid "TERM"
  298.    fi
  299. }
  300.  
  301. vmwareService() {
  302.    case "$1" in
  303.       start)
  304.          if vmwareInVM; then
  305.             # Refuse to start services in a VM: they are useless
  306.             exit 1
  307.          fi
  308.  
  309.          echo 'Starting VMware services:'
  310.          exitcode='0'
  311.  
  312.          vmware_exec 'Virtual machine monitor' vmwareStartVmmon
  313.          exitcode=$(($exitcode + $?))
  314.  
  315.          if [ "`isVmciNeeded`" = 'yes' ]; then
  316.             vmware_exec 'Virtual machine communication interface' vmwareStartVmci
  317.             exitcode=$(($exitcode + $?))
  318.          fi
  319.  
  320.          # vsock needs vmci started first
  321.          if [ "`isVsockNeeded`" = 'yes' ]; then
  322.             vmware_exec 'VM communication interface socket family' vmwareStartVsock
  323.             # a vsock failure to load shouldn't cause the init to fail completely.
  324.          fi
  325.  
  326.          if [ "`is_vmblock_needed`" = 'yes' ] ; then
  327.             vmware_exec 'Blocking file system' vmware_start_vmblock
  328.             exitcode=$(($exitcode + $?))
  329.          fi
  330.  
  331.          # Try to load parport_pc.
  332.          /sbin/modprobe parport_pc >/dev/null 2>&1
  333.  
  334.          if vmwareIsNetworkingEnabled; then
  335.             vmware_exec 'Virtual ethernet' vmwareStartVmnet
  336.             exitcode=$(($exitcode + $?))
  337.          fi
  338.  
  339.          vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
  340.  
  341.          if [ "$exitcode" -gt 0 ]; then
  342.             exit 1
  343.          fi
  344.  
  345.          [ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
  346.          touch /var/lock/subsys/"$subsys"
  347.  
  348.          vmware_exec "Shared Memory Available"  vmwareCheckSharedMemory
  349.       ;;
  350.  
  351.       stop)
  352.          echo 'Stopping VMware services:'
  353.          exitcode='0'
  354.  
  355.          vmware_exec 'VMware Authentication Daemon' vmware_stop_authdlauncher
  356.  
  357.          # If the 'K' version of this script is running, the system is
  358.          # stoping services not because the user is running vmware-config.pl
  359.          # or running the initscript directly but because the user wants to
  360.          # shutdown.  Suspend all VMs.
  361.          if [ "`echo $BASENAME | sed -ne '/^K[0-9].vmware/p'`" ] ; then
  362.             if [ -x "$BINDIR"/vmrun ] ; then
  363.                for i in `pidof vmware-vmx` ; do
  364.                   "$BINDIR"/vmrun suspend `ps -p $i -f | \
  365.                        sed -ne '/vmware/s/.* \(\/.*\.vmx\)/\1/p'` 2> /dev/null
  366.                done
  367.             fi
  368.  
  369.          fi
  370.  
  371.          if [ "`vmmonUseCount`" -gt 0 ]; then
  372.             echo 'At least one instance of '"$PRODUCT_NAME"' is still running.' 1>&2
  373.             echo 'Please stop all running instances of '"$PRODUCT_NAME"' first.' 1>&2
  374.             echo " " >&2
  375.  
  376.             # Since we stopped authdlauncher to prevent new connections before disabling
  377.             # any vmxs, need to restart it here to restore the environment back to
  378.             # what it was before this init script ran.
  379.             vmware_exec 'VMware Authentication Daemon' vmware_start_authdlauncher
  380.  
  381.             # The unconfigurator handle this exit code differently
  382.             exit 2
  383.          fi
  384.  
  385.          # vmci is used by vsock so the module can't unload until vsock does.
  386.          if [ "`isVsockNeeded`" = 'yes' ]; then
  387.             vmware_exec 'VM communication interface socket family' vmwareStopVsock
  388.             exitcode=$(($exitcode + $?))
  389.          fi
  390.  
  391.          if [ "`isVmciNeeded`" = 'yes' ]; then
  392.             vmware_exec 'Virtual machine communication interface' vmwareStopVmci
  393.             exitcode=$(($exitcode + $?))
  394.          fi
  395.  
  396.          vmware_exec 'Virtual machine monitor' vmwareStopVmmon
  397.          exitcode=$(($exitcode + $?))
  398.  
  399.          if [ "`is_vmblock_needed`" = 'yes' ] ; then
  400.             vmware_exec 'Blocking file system' vmware_stop_vmblock
  401.             exitcode=$(($exitcode + $?))
  402.          fi
  403.  
  404.          # Try to unload parport_pc. Failure is allowed as it does not
  405.          # exist on kernels 2.0, and some other process could be using
  406.          # it.
  407.          /sbin/modprobe -r parport_pc >/dev/null 2>&1
  408.  
  409.          if vmwareIsNetworkingEnabled; then
  410.         vmwareStopVmnet
  411.          fi
  412.  
  413.          # The vmware and vmware-tray processes don't terminate automatically
  414.          # when the other services are shutdown.  They persist after calling
  415.          # 'init.d/vmware stop' and will happily keep going through an init
  416.          # start command, continuing to minimally function, blissfully ignorant.
  417.          # Time for a buzzkill.
  418.          for i in `pidof vmware vmware-tray` ; do
  419.             vmware_synchrone_kill $i "INT"
  420.          done
  421.  
  422.          if [ "$exitcode" -gt 0 ]; then
  423.             exit 1
  424.          fi
  425.  
  426.          rm -f /var/lock/subsys/"$subsys"
  427.       ;;
  428.  
  429.       status)
  430.          if [ "`vmmonUseCount`" -gt 0 ]; then
  431.             echo 'At least one instance of '"$PRODUCT_NAME"' is still running.'
  432.             echo
  433.             if [ "$2" = "vmcount" ]; then
  434.                exit 2
  435.             fi
  436.          fi
  437.          if [ "$2" = "vmcount" ]; then
  438.                exit 0
  439.          fi
  440.  
  441.          exitcode='0'
  442.  
  443.          echo -n "Module $driver "
  444.          [ "`isLoaded "$driver"`" = 'yes' ] && echo loaded || echo "not loaded"
  445.          if vmwareIsNetworkingEnabled; then
  446.             echo -n "Module $vnet "
  447.             [ "`isLoaded "$vnet"`" = 'yes' ] && echo loaded || echo "not loaded"
  448.          fi
  449.  
  450.          if [ "$exitcode" -gt 0 ]; then
  451.             exit 1
  452.          fi
  453.       ;;
  454.  
  455.       restart)
  456.          "$SCRIPTNAME" stop && "$SCRIPTNAME" start
  457.       ;;
  458.  
  459.       # Called to make sure script is in a runnable state.
  460.       validate)
  461.          exit 100
  462.       ;;
  463.  
  464.       stoppable)
  465.      [ "`vmmonUseCount`" -lt 1 ]
  466.      exit
  467.       ;;
  468.  
  469.       *)
  470.          echo "Usage: "$BASENAME" {start|stop|status|restart|stoppable}"
  471.          exit 1
  472.    esac
  473. }
  474.  
  475. SCRIPTNAME="$0"
  476. BASENAME=`basename "$SCRIPTNAME"`
  477.  
  478. # Check permissions
  479. if [ "`id -ur`" != '0' ]; then
  480.    echo 'Error: you must be root.'
  481.    echo
  482.    exit 1
  483. fi
  484.  
  485. vmwareService "$1"
  486.  
  487. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement