3

I'm running VMWare Workstation on Ubuntu 10.10. I have a series of virtual machines that I clone every week.

I've written a bash script that cycles through each of the virtual machines, softly suspends it, clones it, and then attempts to resume it.

The problem is, the VM's do not resume, so I have to manually resume them. I'm trying to figure out how I can modify the script I've written to ensure the VMs resume. I've included the script and a sample of the generated logfile showing the error message.

#!/bin/bash

##Author:   William Cooper
##Date: 2/15/2011
## Purpose:     Perform full backups of Virtual Machines
##      Running under VMWare Workstation

## Script won't run if the /nas directory doesn't exist
## The /nas directory is a mounted Buffalo Terastation
## Check /etc/fstab if this mountpoint is broken
   BACKUPDEST="/nas"

## No need to modify
   HOST=`hostname`
   DATEFILE=`/bin/date +%G%m%d`

## Run Command to find list of VM names:
## this will only be the name of the vm, no path and no .vmx
   VMLIST=`vmrun list | grep '/' | cut -d'/' -f3 | cut -d'.' -f1`

## Initialize Counter variable used to cycle through VMs in VMARRAY variable
   COUNTER=1

## Run Command to find list of VM Names and then store them in an Array:
## This will include full pathnames
   VMARRAY=( `vmrun list | grep '/'` )

#################################################################
## Functions

##
## Notify the starting time of backup in the log
##
function startScript {
   echo
   echo "----------------------------------------------------"
   echo "START - ${VM}"
   echo "Host: ${HOST}"
   echo "Date: `date`"
   echo
}

##
## Suspend VM
##
function suspendVM {
   ## Suspend the VM
   echo "Attempting to softly suspend "${VM}" . . ."
   vmrun suspend ${VMARRAY[${COUNTER}]} soft
   echo "${VM} Suspended on `date`"
   echo
}

##
## Backup VM
##
function doBackup {

   ## Check to see if the correct backup directory exists.
   if [ ! -d ${BACKUPDEST}/${VM} ]; then
      echo "${BACKUPDEST}/${VM} does not exist, creating . . ."
      mkdir "${BACKUPDEST}/${VM}"
      echo
   fi

   ## Backup VM (clone)
   echo "Backup of "${VM}" began: `date`"
   echo "Backup to ${BACKUPDEST}/${VM}/BACKUP/${DATEFILE} . . ."
   mkdir ${BACKUPDEST}/${VM}/BACKUP/${DATEFILE}
   vmrun clone ${VMARRAY[${COUNTER}]} ${BACKUPDEST}/${VM}/BACKUP/${DATEFILE}/${VM}.vmx full
   echo "Backup of "${VM}" ended: `date`"
   echo
}

##
## Start VM
##
function doStart {
   ## Resume VMs which were running [--type gui|sdl|vrdp|headless]
   echo "Attempting to resume "${VM}" . . ."
   vmrun start ${VMARRAY[${COUNTER}]}
   echo "${VM} Resumed on `date`"
   echo
}

##
## Notify the finishing time of backup in the log
##
function finishScript {
   echo "FINISH - ${VM}"
   echo "Host: ${HOST}"
   echo "Date: `date`"
   echo "----------------------------------------------------"
   echo
}

#################################################################
## Script

for VM in ${VMLIST}
do

   sleep 1
   ## StartScrip executes for logging
      startScript

   ## Suspend VM performs soft suspend
      suspendVM
      sleep 3

   ## Do Backup performs the VM clone
      doBackup
      sleep 3

   ## Start VM performs start vm
      doStart
      sleep 3

   ## FinishScript executes for logging
      finishScript

   ## Increment the Counter
      COUNTER=${COUNTER}+1

done | tee "${BACKUPDEST}/logs/VMClone_${DATEFILE}.log"

## Script
#################################################################
exit

An example of the generated logfile for each VM is shown below.

----------------------------------------------------
START - vmname
Host: servername
Date: Sat Feb 19 12:30:29 CST 2011

Attempting to softly suspend vmname . . .
vmname Suspended on Sat Feb 19 12:30:44 CST 2011

Backup of vmname began: Sat Feb 19 12:30:47 CST 2011
Backup to /nas/vmname/BACKUP/20110219 . . .
Backup of vmname ended: Sat Feb 19 12:43:16 CST 2011

Attempting to resume vmname . . .
Error: Cannot launch the UI because no display server is present in the current environment
vmname Resumed on Sat Feb 19 12:43:20 CST 2011

FINISH - vmname
Host: servername
Date: Sat Feb 19 12:43:23 CST 2011
----------------------------------------------------

Error: Cannot launch the UI because no display server is present in the current environment

Lucretius
  • 1,053
  • 1
  • 13
  • 26
  • 1
    Your counter increment works by way of a side effect of sorts. When you do `COUNTER=${COUNTER}+1` it builds an increasingly long string of the form `1+1+1+1+1` which gets arithmetically evaluated when it's used as an array subscript. I would recommend using `((COUNTER++))` instead. Also, you don't need all the decoration for array subscripts. You can use: `${VMARRAY[COUNTER]}` for example. Also, I recommend using `$()` instead of backticks for command substitution. They're more readable and can be more easily nested, etc. As for your question, can you provide more details of your environment? – Dennis Williamson Feb 23 '11 at 17:52
  • 1
    X11 needs to be running and the `DISPLAY` variable needs to have a meaningful value. – Dennis Williamson Feb 23 '11 at 17:53
  • Thanks for the tip on `((COUNTER++))`. `$()` does indeed feel cleaner, but aside from the code cleaning suggestions you've given I'm not sure why the DISPLAY global breaks? I run a cronjob that calls the script above every saturday. I think what I'm going to try now is just manually setting `DISPLAY=:0.0` in the script to see if that fixes it. Thanks. – Lucretius Feb 24 '11 at 16:37
  • The DISPLAY=:0.0 didn't work. The only way I could ensure this script succeeds is to close down VMWare Workstation and select for the jobs to "Run in the Background" without being displayed. – Lucretius Jun 11 '11 at 20:04
  • Are you doing this on the TTY's (like `Ctl-Alt-F1`) or in a terminal in ubuntu? With a TTY, I could never get `DISPLAY` working properly, but in a terminal it works fine. At least for me. – norcalli Jul 19 '11 at 10:24
  • Right now its a CronJob that runs a script (.sh) written in Notepad++ – Lucretius Jul 29 '11 at 16:33

4 Answers4

15

You could use the nogui paramater to the start command, e.g:

vmrun start yourmachine.vmx nogui

See: http://www.vmware.com/pdf/vix180_vmrun_command.pdf

Tom
  • 151
  • 1
  • 3
  • In Workstation 7 on Ubuntu it seemed to ignore that parameter. When I added it, it made no difference. We have since implimented vSphere and ESXi 5.0. Helpful answer though. – Lucretius Jan 25 '12 at 03:09
0

try doing echo $DISPLAY on your terminal TTY where vmware workstation runs. The echo $DISPLAY will return like :1000.0 you need to set export DISPLAY=":1000.0" in your script. then the script will resume the VM's.. i found this works when i use areca backup to resume my VM using postscript run in areca.

Johnbritto
  • 149
  • 2
  • 14
0

if the problem is because you are running vmrun from a ssh session, then another solution might be run a screen session on the server.

on the machine Workstation is running:

screen -S vmware

on the client machine,

ssh vmware-machine
screen -x vmware
vmrun start /path/to/vm.vmx
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
0

Since no solid answer has been found I'm just going to accept that under these specific circumstances there is no fix.

  • Ubuntu 10.10 x64
  • VMware Workstation 7

Solution: Close down VMware Workstation and choose for the VMs to "Run in the Background" Works flawlessly after that.

Lucretius
  • 1,053
  • 1
  • 13
  • 26