ESXi Command Line Tools

From DFWLPiki
Jump to: navigation, search

Scope of This Document

I keep forgeting the command line syntax for ESXi console, so I document the commands I use most often here, hopefully so they will eventually be comittied to memory... if not I know where to quicly find them now.

Note that this document assumes you have Enabled SSH Techsupport mode.

Specifying the Default iSCSI Multipathing Mode

For Synology:

esxcli storage nmp satp set --default-psp=VMW_PSP_RR --satp=VMW_SATP_ALUA 

For Equallogic:

esxcli storage nmp satp set --default-psp=VMW_PSP_RR --satp=VMW_SATP_EQL

For Compellent:

esxcli storage nmp satp set --default-psp=VMW_PSP_RR --satp=VMW_SATP_DEFAULT_AA

VMWare ESXi Commands For Managing Guests

The main command is 'vim-cmd'. It has many uses via its subcommands and switches.

To get a list of all VM guests on a host, use:

vim-cmd vmsvc/getallvms

this will print a list of all VMs, their Vmid, common names, VM Guest OS, and file system locaion of the .vmx configuration file.

From that output, you take the Vmid of the host you want to manage, and check its power state like this:

vim-cmd vmsvc/power.getstate 2

The options for managing the power state of a guest VM are:

power.getstate                   
power.hibernate                  
power.off                        
power.on                         
power.reboot                     
power.reset                      
power.shutdown                   
power.suspend                    
power.suspendResume

To give a VM the command to gracefully shutdown, use:

vim-cmd vmsvc/power.shutdown 2

Moving .vmdk Files From Store to Store, Retain Thin Format

Power the VM Guest off. Remove from ESXi inventory.

Convert the vmdk file to thin:

vmkfstools -i SERVERNAME.vmdk -d thin SERVERNAME-thin.vmdk

Add the VM Guest back to inventory, and power on. Save the original disk file for just in case. After the system boots successfully, remove the thick vmdk file.

UPDATE - I have since found, that on the free version, if you create a target folder in the target file system, and then copy the contents of the actual Host folder (not the host folder itself), then the thin state will copy with the file. The above re-thining of the disk, was due to when copying the entire host folder, the disk would copy to thick format in the destination file system. One extra step, to pre-create the destination folder, but it saves a heck of a lot of time later when you dont have to re-thin several hundred gigs of vmdk files.  :)

3ware RAID Controller Managment

Command to view installed 3ware controllers:

# ./tw_cli show
Ctl   Model        (V)Ports  Drives   Units   NotOpt  RRate   VRate  BBU 
------------------------------------------------------------------------
c9    9650SE-16ML  16        4        1       0       1       1      -        

Encls         Slots  Drives  Fans  TSUnits  PSUnits  
-----------------------------------------------------
e0            16     4       0     0        0 

Command to view RAID units on c9:

# ./tw_cli /c9 show
Unit  UnitType  Status         %RCmpl  %V/I/M  Stripe  Size(GB)  Cache  AVrfy
------------------------------------------------------------------------------
u0    RAID-5    OK             -       -       256K    5587.9    RiW    ON     

VPort Status         Unit Size      Type  Phy Encl-Slot    Model
------------------------------------------------------------------------------
p0    OK             u0   1.82 TB   SATA  0   -            HUA722020ALA330     
p1    OK             u0   1.82 TB   SATA  1   -            HUA722020ALA330     
p4    OK             u0   1.82 TB   SATA  4   -            HUA722020ALA330     
p5    OK             u0   1.82 TB   SATA  5   -            HUA722020ALA330

Command to view status of RAID unit u0:

# ./tw_cli /c9/u0 show
Unit     UnitType  Status         %RCmpl  %V/I/M  Port  Stripe  Size(GB)
------------------------------------------------------------------------
u0       RAID-5    OK             -       -       -     256K    5587.9    
u0-0     DISK      OK             -       -       p0    -       1862.63   
u0-1     DISK      OK             -       -       p1    -       1862.63   
u0-2     DISK      OK             -       -       p4    -       1862.63   
u0-3     DISK      OK             -       -       p5    -       1862.63   
u0/v0    Volume    -              -       -       -     -       2048      
u0/v1    Volume    -              -       -       -     -       2048      
u0/v2    Volume    -              -       -       -     -       1491.9

Configuration Backup and Restore of a Single Host

Syncronize all settings:

vim-cmd hostsvc/firmware/sync_config

Dump all the settings:

vim-cmd hostsvc/firmware/backup_config

Upon completion, the host will print out:

Bundle can be downloaded at : http://*/downloads/configBundle-[FQDN].tgz.  

Enter the URL into a browser and substitute in the FQDN for the * and download the file to your system.

To restore from a previously saved config, scp the backup file from your system to the host, move it to the /tmp folder, and rename the file to configBundle.tgz.

mv configBundle-[FQDN].tgz /tmp/configBundle.tgz

Put the host in maintenance mode.

vim-cmd hostsvc/maintenance_mode_enter

Restore the previous configuration.

vim-cmd hostsvc/firmware/restore_config /tmp/configBundle.tgz

Upon completion of the reload, the host will reboot. Note, that if you rebuild the ESXi server, you will need to reinstall the same version, or patch up to the version that was running when the config dump was made.

Migrate a VM From One Host to Another

note: ensure that the VM that is being moved does not have any .iso file mounted to the cdrom drive. ie... anything it cannot take with it in the migration.

note also: if youre are moving a RHEL6 system, it would be helpful to remove the /etc/udev/rules.d/70-persistent-net.rules before you do the final shutdown on the VM you are going to move.

ovftool -ds=DEST-DATASTORE vi://root@SOURCE-ESXi/VMNAME vi://root@DEST-ESXi

Updating the Hypervisor Using .zip File From Command Line

Today I needed to update from 5.1-Update1 to 5.1-Update2, to hopefully eliminate a stability issue i have been having. I have purple-screen-of-deathed many times in the past month.

First, scp the file into the file system on the hypervisor, into a directory I created.

scp ESXi510-201410001.zip root@dlp-esxi02:/vmfs/volumes/Local_SATA_160GB/Patch/

Then ssh into the hypervisor, and run this command:

esxcli software vib update -d "/vmfs/volumes/Local_SATA_160GB/ESXi510-201410001.zip"

The output should say:

Installation Result
  Message: The update completed successfully, but the system needs to be rebooted for the changes to be effective.
  Reboot Required: true

Killing a stuck VM

esxcli vm process list
esxcli vm process kill --type=force --world-id=10561

Installing 3ware 96550SE Driver in ESXi 5.1

First, I copied the driver to the path below, and attempted to run the command against the .vib file.

/vmfs/volumes/5335f7d7-a4d74ff0-24f9-c80aa9881e26/DRIVER # esxcli software vib install -v ./scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib 
[VibDownloadError]
('./scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib', , "[Errno 4] IOError: <urlopen error [Errno 2] No such file or directory: '/var/log/vmware/scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib'>")
       url = ./scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib
Please refer to the log file for more details.

Since it seemed to be expecting the file to be in /var/log/vmware (dont ask me why), i copied the file there.

/vmfs/volumes/5335f7d7-a4d74ff0-24f9-c80aa9881e26/DRIVER # cp scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib /var/log/vmware/
/ vmfs/volumes/5335f7d7-a4d74ff0-24f9-c80aa9881e26/DRIVER # esxcli software vib install -v ./scsi-3w-9xxx-2.27.08.036-1OEM.500.0.0.472560.x86_64.vib 
Installation Result
  Message: The update completed successfully, but the system needs to be rebooted for the changes to be effective.
  Reboot Required: true
  VIBs Installed: LSI_bootbank_scsi-3w-9xxx_2.27.08.036-1OEM.500.0.0.472560
  VIBs Removed: 
  VIBs Skipped: 

Finally, I reboot the host.

/vmfs/volumes/5335f7d7-a4d74ff0-24f9-c80aa9881e26/DRIVER # reboot

Setting SNMP

esxcli system snmp set -c mycomunity
esxcli system snmp set -l warning
esxcli system snmp set -e yes

SSH keys

SSH keys should be placed here:

/etc/ssh/keys-root/authorized_keys