Monday, August 29, 2011

Powering on and off virtual machines via VMware ESX / ESXi 4.x console or SSH commands

An old colleague of mine reached out to me the other day when he ran into an issue with an vSphere environment where he was not able to connect to vCenter or the host via the vSphere Client.  The challenge he had was that he needed to get the virtual machines up as soon as possible but the only way he was able to access the hosts was through the console.  To make a long story short, the first thing that came to my mind was the “vim-cmd vmsvc” command so I went ahead and walked him through it.  Since I know this is bound to come up again sometime in the future, I thought I should write a blog post that will serve as a reference to people who ask me about performing operations in the console in the future.

The “vim-cmd vmsvc” command provides administrators the ability to configure and execute most operations that the vSphere Client provides.  The following are the available commands:

acquiremksticket          get.configoption          power.on
acquireticket             get.datastores            power.reboot
connect                   get.disabledmethods       power.reset
convert.toTemplate        get.environment           power.shutdown
convert.toVm              get.filelayout            power.suspend
createdummyvm             get.guest                 power.suspendResume
destroy                   get.guestheartbeatStatus  queryftcompat
device.connection         get.managedentitystatus   reload
device.connusbdev         get.networks              setscreenres
device.disconnusbdev      get.runtime               snapshot.create
device.diskadd            get.snapshotinfo          snapshot.dumpoption
device.diskaddexisting    get.summary               snapshot.get
device.diskremove         get.tasklist              snapshot.remove
device.getdevices         getallvms                 snapshot.removeall
device.toolsSyncSet       gethostconstraints        snapshot.revert
device.vmiadd             login                     snapshot.setoption
device.vmiremove          logout                    tools.cancelinstall
devices.createnic         message                   tools.install
get.capability            power.getstate            tools.upgrade
get.config                power.hibernate           unregister
get.config.cpuidmask                 upgrade


Since the example we’ll be using is to power on a virtual machine that is off, the commands we’ll be used are:

  1. getallvms
  2. power.getstate
  3. power.on

The first information we’ll need to power on a virtual machine is to obtain the Vmid which is a unique identifier that the ESXi host uses to identify the virtual machine.  This information can be retrieved via the getallvms command:

~ # vim-cmd vmsvc/getallvms
Vmid     Name                       File                          Guest OS         Version
112    CONTOSOMNG06   [VMFS Volume 2] CONTOSOMNG06/CONTOSOMNG06.vmx   winLonghornGuest      vmx-07
128    CONTOSODB01    [VMFS Volume 2] CONTOSODB01/CONTOSODB01.vmx     winNetStandardGuest   vmx-07
16     CONTOSOCFL01   [VMFS Volume 1] CONTOSOCFL01/CONTOSOCFL01.vmx   winNetStandardGuest   vmx-07
192    CONTOSOVDR01   [VMFS Volume 4] CONTOSOVDR01/CONTOSOVDR01.vmx   rhel5_64Guest         vmx-07
208    CONTOSOMNG10   [VMFS Volume 4] CONTOSOMNG10/CONTOSOMNG10.vmx   winLonghorn64Guest    vmx-07
64     CONTOSOMX03    [VMFS Volume 1] CONTOSOMX03/CONTOSOMX03.vmx     winNetStandardGuest   vmx-07
80     CONTOSOMNG01   [VMFS Volume 2] CONTOSOMNG01/CONTOSOMNG01.vmx   winNetStandardGuest   vmx-07

Note the Vmid column above.  For this example, the virtual machine we would like to power on will be CONTOSOMX03 with Vmid 64.

The next command we should run which serves more as a sanity check is the power.getstate command to check whether the virtual machine is powered on or off:

~ # vim-cmd vmsvc/power.getstate 64
Retrieved runtime info
Powered off

Now that we know the virtual machine is indeed powered off, we can proceed to power on the virtual machine with the command power.on:

~ # vim-cmd vmsvc/power.on 64
Powering on VM:
~ #


As shown in the initial list of commands available, the vim-cmd vmsvc/ provides administrators with the ability to either manually or automatically (via scripts) to operate on virtual machines without having to vSphere Client over to the host.

No comments: