Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2029169
  • 博文数量: 593
  • 博客积分: 20034
  • 博客等级: 上将
  • 技术积分: 6779
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-06 14:07
文章分类

全部博文(593)

文章存档

2016年(1)

2011年(101)

2010年(80)

2009年(10)

2008年(102)

2007年(16)

2006年(283)

我的朋友

分类: LINUX

2011-03-27 13:48:36

I was searching for a solution to launch and suspend VMWare Workstation VMs on a CentOS 5-machine during boot/shutdown.
A script script for launching VirtualBox VMS during boot inspired me to create one for VMWare Workstation.
In this example I have teamed my VMs in a team /vms/my_team.vmtm, it could as well be a single VM (.vmx) that is being launched.
The VM Team will be launched with a different user account as VMWare not allows root to launch VMs.
During shutdown root however can suspend the VM Team, and this is also the action being used during system shutdown.

  1. Create a user vmuser

    useradd vmuser
    passwd vmuser

  2. Create the VMHeadless script. I have teamed all my VMS and will launch/suspend the team /vms/my_team.vmtm

    vi /etc/init.d/vmheadless

    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides:          vmheadless.sh
    # Required-Start:    $remote_fs $syslog
    # Required-Stop:     $remote_fs $syslog
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # chkconfig: 2345 99 20
    # Short-Description: Start VM
    # Description:       Start/stop the virtual machines
    ### END INIT INFO
    
    MACHINE_NAME="/vms/my_team.vmtm"
    VM_USER="vmuser"
    RETVAL=0
    LD_LIBRARY_PATH=/usr/lib/vmware:$LD_LIBRARY_PATH
    PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
    
    id=`id -u`
    if [ "$id" != "0" ]; then
       echo "This script must be run as root" 1>&2
       exit 1
    fi
    
    # Load the VERBOSE setting and other rcS variables
    [ -f /etc/default/rcS ] && . /etc/default/rcS
    
    # Load the VERBOSE setting and other rcS variables
    #. /lib/init/vars.sh
    
    # Define LSB log_* functions.
    # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
    . /lib/lsb/init-functions
    
    # Get function from functions library
    . /etc/init.d/functions
    
    # Load network defaults. Use this if there is any extra network stuff to do first.
    
    case "$1" in
    start)
       su $VM_USER -c "/usr/bin/vmrun start $MACHINE_NAME nogui"
            RETVAL=0
      echo "Started VM Headless RETVAL=($RETVAL)"
            ;;
    stop)
      /usr/bin/vmrun suspend "$MACHINE_NAME"
            RETVAL=$?
     echo "Suspended VM Headless RETVAL=($RETVAL)"
            ;;
    status)
      ps=`ps -ef | grep vmware-vmx | grep -v grep | wc -l`
      if [ "$ps" -gt 0 ] ; then
        echo "$ps VMS started"
        RETVAL=1
      else
        echo "VMS not running"
        RETVAL=0
      fi
            ;;
    restart)
      /usr/bin/vmrun suspend "$MACHINE_NAME" nogui
            RETVAL=$?
     echo "Suspended VM Headless RETVAL=($RETVAL)"
      su backup -c "/usr/bin/vmrun -T ws start $MACHINE_NAME nogui"
            RETVAL=$?
      echo "Started VM Headless RETVAL=($RETVAL)"
            ;;
    *)
            echo "Usage: $0 { start | stop }"
            RETVAL=1
            ;;
    esac
    exit $RETVAL
    
  3. Enable VMHeadless during boot

    chkconfig --add vmheadless
    chkonfig --level 2345 vmheadless on

  4. Launch the VMHeadless daemon, and thus launching the VM Team

    service vmheadless start

阅读(1005) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~