VMware Virtual Machine shutdown, startup, etc….

So I did some additional testing on the claim that vmware-cmd will not shutdown a VM that is locked at the console and I have not experienced the problem what I did find was the bash scripts that scripts that worked on my ESX 2.x server did not work on my VMware Server machine – it appears that _spaces_ in the directory structure and/or vm file name cause the script to fail.

These are examples of the original scripts:

getstate.sh
#!/bin/bash
echo “Getting running state of VM Guests…”
for vm in vmware-cmd -l
do
vmware-cmd “$vm” getstate $vm
done

stopall.sh
#!/bin/bash
echo “Stopping all running VM Guests…”
for vm in vmware-cmd -l
do
vmware-cmd “$vm” stop trysoft hard
done

The results on my ESX 2.x server for getstate.sh is the following:
getstate(/home/bocchrj/vmware/rh62_1/linux.vmx) = off
getstate(/home/bocchrj/vmware/rh62_2/linux.vmx) = off


On my VMware Server box this is the output of getstate.sh:
Getting running state of VM Guests…
/usr/bin/vmware-cmd: Could not connect to VM /Virtual_Machines/Windows
  (VMControl error -11: No such virtual machine: The config file /Virtual_Machines/Windows is not registered.
Please register the config file on the server.  For example:
vmware-cmd -s register “/Virtual_Machines/Windows”)
/usr/bin/vmware-cmd: Could not connect to VM XP
  (VMControl error -14: Unexpected response from vmware-authd: Invalid pathname: XP)
/usr/bin/vmware-cmd: Could not connect to VM Professional
  (VMControl error -14: Unexpected response from vmware-authd: Invalid pathname: Professional)
/usr/bin/vmware-cmd: Could not connect to VM SNMP
  (VMControl error -14: Unexpected response from vmware-authd: Invalid pathname: SNMP)
/usr/bin/vmware-cmd: Could not connect to VM Tools/Windows
  (VMControl error -14: Unexpected response from vmware-authd: Invalid pathname: Tools/Windows)
/usr/bin/vmware-cmd: Could not connect to VM XP
  (VMControl error -14: Unexpected response from vmware-authd: Invalid pathname: XP)
/usr/bin/vmware-cmd: Could not connect to VM Professional.vmx
etc…..

I took the time to write a slightly more robust perl script to stop and start VMs that appears to work well on both ESX 2.x and VMware Server (I only tested this on VMware server running on on a Linux host but it should work on a Windows host with perl installed).  If you would like an executable (exe) version for windows Email me and I can provide it to you.

#!/usr/bin/perl -w
#vmpower.pl
#RJB – 1/23/2007

use strict;
my $command;
my $switch;

if ($ARGV[0] eq “help”) {
    &usage;
    }
if ($ARGV[0] eq “getstate” || $ARGV[0] eq “stop” || $ARGV[0] eq “start” || $ARGV[0] eq “reset”) {
    &power;
    }
else{
    &error;
    }

sub power {
$command = “vmware-cmd -l”;
print “==> $command\n”;
if (system(“$command > .vmtmpfile”) == 0) {
  print ” success, exit status = $?\n”;
} else {
  print ” failure, exit status = $?\n”;
}

open (VM, ‘.vmtmpfile’);
while (<VM>) {
chomp;
$command = “vmware-cmd”;
$switch = $ARGV[0];
print “==> $command \”$_\” $ARGV[0]\n”;
system(“$command \”$_\” $switch”);
    if ( $? == 0 ) {
      print ” success, exit status = $?\n”;
    } else {
      print ” failure, exit status = $?\n”;
    }
}
close (VM);
system (“rm -f .vmtmpfile”);
}

sub usage {
system “clear”;
print “VM statup and shutdown script for ESX 2.x and VMware Server\n”;
print “vmpower.pl\n\n”;
print “Usage:  vmpower.pl [getstate|start|stop|reset]\n\n”;
exit;
}

sub error {
    print ” error\n”;
    print ” \”vmpower.pl help\” – for usage instructions\n\n”;
    exit;
}

Please follow and like us:

3 thoughts on “VMware Virtual Machine shutdown, startup, etc….

  1. Hello,

    nice script. For the shell script you could also use a

    IFS=”

    right after the #!/bin/bash. So the character separating the virtual machines would be a line break instead of a space, which is the default value. You can also see this in the scripts of my site: http://www.krausmueller.de/news/browse/1/article/backup-von-virtuellen-maschinen-mit-vmware-server-und-gsx-server.html?tx_ttnews%5BbackPid%5D=4&cHash=9260d35c3c (unfortunately it is written in german).

    Johannes

  2. Hello , for reasons about blank spaces in vmware machine name , i
    have modified the getstate script… #!/bin/bash echo “Getting
    running state of VM Guests…” vmware-cmd -l | while read LINE do
    echo “Running state of $LINE” vmware-cmd “${LINE}” getstate $vm
    done in this case , blank spaces are not a problem. Alex

  3. hI CAN U SEND ME THE STARTUP SHUTDOWN AND BACKUP SCRIPT OF VMWARE
    SERVER, MY HOST MACHINE IS UBUNTU AND GUEST IS ALSO UBUNTU AND MY
    GUEST FILE NAME IS UBUNTU-VIRTUAL.VMX

Leave a Reply