-- MikeGore - 2014-06-04

Running a VM in VMware Workstation as a service under Linux

  • The Attached script called vmware-run can be used to run a VM as a service under VMware workstation
  • You must set the VM variable for it to work

Usage

  • chmod 755 vmware-run
  • ./vmware-run install - installs vmware-run service with automatic startup run level 95 and stop run level 01
  • service vmware-run remove - removes vmware-run service
  • service vmware start - starts VM
  • service vmware stop - stops VM
  • service vmware status - VM status
  • service vmware restart - restarts VM
  • service vmware restart-force - force restarts VM

Script

  • Download script: * vmware-run: vmware-run script

#!/bin/bash
#
# Should-Start: vmware
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: VMware VM image starter
# ===========================================
# Author: Mike Gore 27 May 2014 
# What: This script will run a single VM as a service
# Uses: VMware Workstation 8,9,10
# ===========================================
# vmrun command
VMRUN=/usr/bin/vmrun
NAME=`basename $0`
# ===========================================
# Define the full pathname of your VM here
VM=
# Example: 
# VM=/home/cscf-adm/vmware/win7.vmx
# ===========================================
# Check that VM has been defined

if [ -z "$VM" ]
then
   echo "You must define VM"
    echo "Example:"
    echo "   VM=\"/home/cscf-adm/vmware/win7.vmx\""
    echo "Note: This is the full name and path to the VM"
   exit 1
fi

# Check that the VMRUN command exists
if [ ! -f "$VMRUN" ]
then
   echo "$VMRUN command is missing"
   exit 1
fi

case $1 in
    start)
      STATS=`"$VMRUN" -T ws list "$VM" 2>/dev/null | grep "$VM"`
      if [ -n "$STATS" ]
      then
         echo "$STATS is already running"
         exit 1
      fi
      echo "Starting $VM"
      "$VMRUN" -T ws start "$VM" nogui
    ;;

    stop)
      echo "Stopping $VM"
      "$VMRUN" -T ws stop "$VM" soft
    ;;

    restart-force)
      echo "Restarting, with hard option, $VM"
      "$VMRUN" -T ws stop "$VM" hard
    ;;

    restart)
      echo "Restarting $VM"
      service "$NAME" stop
      service "$NAME" start
    ;;

    status)
      "$VMRUN" -T ws list "$VM"
    ;;

    install)
      if [  -f /etc/init.d/"$NAME" ]   * *service vmware restart* - restarts VM
      then
         echo "$NAME already exists in /etc/init.d"
         echo "If you are SURE this is correct run $NAME remove first"
         exit 1
      fi
      # Install Service
      chmod 755 "$NAME"
      cp -p "$NAME" /etc/init.d
      # If it did exist remove it first
      update-rc.d -f "$NAME" remove
      # Install Srevice
      update-rc.d "$NAME" defaults 95 01
    ;;

    remove)
      service "$NAME" stop
      update-rc.d -f "$NAME" remove
    ;;
    *)
      echo "usage: `"$NAME"` {start|stop|restart|restart-force|status|install|remove}"
    ;;
esac
Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatext vmware-run r1 manage 2.0 K 2014-06-04 - 13:22 MikeGore vmware-run script
Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2014-06-04 - MikeGore
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback