Monday, 20 April 2015

vSphere Automation

List of VMware CLIs, SDKs and related tools:
http://www.virtuallyghetto.com/2015/01/list-of-vmware-clis-sdks-devops-tools.html

vSphere 6 CLI:
https://www.vmware.com/support/developer/vcli/

VMware 5.1 ESXi poster:
http://blogs.vmware.com/vsphere/files/2012/11/ESXi-5.1-Poster.pdf

vSphere vMA:
https://www.vmware.com/support/developer/vima/

vmkfstools - vSphere CLI for managing VMFS volumes:
http://pubs.vmware.com/vsphere-60/index.jsp#com.vmware.vcli.ref.doc/vmkfstools.html

vim-cmd ESXi CLI:
http://www.doublecloud.org/2013/11/vmware-esxi-vim-cmd-command-a-quick-tutorial/
http://www.doublecloud.org/2013/12/powerful-hacks-with-esxi-vim-cmd-command-together-with-shell-commands/
http://www.virtuallyghetto.com/vmware-vimsh-and-vim-cmd


Common VM related tasks with CLI:
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2012964
PowerCLI
vMA
cli
Register a VM
New-VM –vmfilepath “[datastore]path_to_vmx_on_datastore” –vmhostesxhost
vmware-cmd --server esxhost –s register path_to_vmx_file
vmware-cmd --server vcenter --vihostesxhost –s register path_to_vmx_file
vim-cmd solo/registervmpath_to_vmx_file
Unregister a VM
Remove-VM vm_name
vmware-cmd --server esxhost –s unregister path_to_vmx_file
vmware-cmd --server vcenter --vihostesxhost –s unregisterpath_to_vmx_file
vim-cmd
vmsvc/unregister vmid
Delete a VM
Remove-VM vm_name -deletepermanently
vmware-cmd --server esxhost –s unregister path_to_vmx_file
vmware-cmd --server vcenter --vihostesxhost –s unregisterpath_to_vmx_file vifs --serveresxhost --rm “[datastore]path_to_vmx_on_datastore
vim-cmd vmsvc/destroy vmid
Get a listing of VMs on a host
Get-VM –location esxhost
vmware-cmd –-server esxhost –-username root –l
vmware-cmd --server vcenter –-vihost esxhost -l
esxcli vm process list
vim-cmd vmsvc/getallvms

vim-cmd vmsvc/getallvms
Determine if a VM has a snapshot
Get-VM –name vm_name | Get-Snapshot
vmware-cmd --server esxhostpath_to_vmx_file hassnapshot
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file hassnapshot
vim-cmd vmsvc/get.snapshotvmid
Take a snapshot of a VM
Get-VM –name vm_name | New-Snapshot –name snapshot_name
vmware-cmd --server esxhostpath_to_vmx_file createsnapshotsnapshot_name
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_filecreatesnapshot snapshot_name
vim-cmd vmsvc/snapshot.create vmidsnapshot_name
Remove a snapshot of a VM
Get-VM –name vm_name | Get-Snapshot –name snapshot_name | Remove-Snapshot
vmware-cmd --server esxhostpath_to_vmx_file removesnapshots
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_fileremovesnapshots
vim-cmd vmsvc/snapshot.remove vmid
Get the current power state of a VM
Get-VM –name vm_name
vmware-cmd --server esxhostpath_to_vmx_file getstate
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file getstate
vim-cmd vmsvc/power.getstate vmid
Get the uptime for a VM
Get-Stat -entity vm_name -stat sys.uptime.latest -MaxSamples 1
vmware-cmd --server esxhostpath_to_vmx_file getuptime
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file getuptime
vim-cmd vmsvc/get.summaryvmid |grep uptimeSeconds
Power on a VM
Start-VM –vm vm_name
vmware-cmd --server esxhostpath_to_vmx_file start
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file start
vim-cmd vmsvc/power.on vmid
Shutdown a VM
Shutdown-VMGuest –vm vm_name
vmware-cmd --server esxhostpath_to_vmx_file stop soft
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file stop soft
vim-cmd vmsvc/power.shutdown vmid
Power off a VM
Stop-VM –vm vm_name
vmware-cmd --server esxhostpath_to_vmx_file stop hard
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file stop hard
esxcli vm process kill –wworld_id
vim-cmd vmsvc/power.offvmid
Reboot a VM
Restart-VMGuest –vm vm_name
vmware-cmd --server esxhostpath_to_vmx_file reset soft
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file reset soft
vim-cmd vmsvc/power.rebootvmid
Reset a VM
Restart-VM –vm vm_name
vmware-cmd --server esxhostpath_to_vmx_file reset hard
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_file reset hard
vim-cmd vmsvc/power.resetvmid
Upgrade VMware Tools in a VM
Update-Tools –vm vm_name
N/A
vim-cmd vmsvc/tools.upgradevmid
Display the IP address of a VM
Get-VMGuestNetworkInterface –vmvm_name -guestuserguest_admin_user -guestpasswordguest_admin_password
vmware-cmd --server esxhostpath_to_vmx_file getguestinfo ip
vmware-cmd --server vcenter --vihostesxhost path_to_vmx_filegetguestinfo ip
vim-cmd vmsvc/get.guestvmid |grep -m 1 "ipAddress = \""

Other useful CLI commands:
chkconfig -l
Shows daemons running on hypervisor. Can also be used for configuration.

esxtop
Same as linux top for vmware

vmkerrcode -l
List of vmkernel errors

esxcfg-info
Lists a LOT of information about the esx host

esxcfg-vmknic -l
esxcfg-nics -l
Lists information about NICs. Can also be used for configuration.

esxcfg-vswitch -l
Lists information about virtual switching. Can also be used for configuration.

dcui
Provides console screen to ssh session

vsish
Vmware interactive shell

decodeSel /var/log/ipmi_sel.raw
Read System Event Log of server

https://github.com/zorangagic/auto-create/blob/master/create.sh
vmkfstools -c "${SIZE}"G -a lsilogic $NAME/$NAME.vmdk
#Adding Virtual Machine to VM register - modify your path accordingly!!
MYVM=`vim-cmd solo/registervm /vmfs/volumes/datastore1/${NAME}/${NAME}.vmx`
#Powering up virtual machine:
vim-cmd vmsvc/power.on $MYVM

Create new VM from template using CLI:
http://www.kmggroup.ch/?p=353
# First configure ssh key
ssh root@myESXiHost
vi /etc/ssh/keys-root/authorized_keys

# Then automate VM creation from template
newHostName=testbox-v003fry
templateDir=/mnt/synology/files/vmware/datastores/dev/template
datastoreDir=/mnt/synology/files/vmware/datastores/dev
cd $datastoreDir
mkdir $newHostName
cat $templateDir/template.vmx | sed -e 's/XXX_HOST_NAME_XXX/'$newHostName'/' > $datastoreDir/$newHostName/$newHostName.vmx
ssh root@192.168.2.204 vmkfstools -c 16g /vmfs/volumes/NFSDev/$newHostName/$newHostName.vmdk -a lsilogic
vmID=$(ssh root@192.168.2.204 vim-cmd solo/registervm /vmfs/volumes/NFSDev/$newHostName/$newHostName.vmx $newHostName pool0)
#-- turn on VM
ssh root@192.168.2.204 vim-cmd vmsvc/power.on $vmID &
sleep 1
#-- check there is a message and choose 2 (default, moved it)
[ -z "`ssh root@192.168.2.204 vim-cmd vmsvc/message $vmID _vmx1 | grep 'No message'`" ] && ssh root@192.168.2.204 vim-cmd vmsvc/message $vmID _vmx1 2

Deploy VM from template using CLI:
https://github.com/finalduty/enl/blob/058b080cd669c3488b377c2906bb6b5f34456348/esx/deploy_vm_from_template.sh
## Clone and Deploy VMWare Machine

## Set Source Template and Destination Name
dstvm='nfs-a01.finalduty.me'
srcvm='_template-centos'
srcpath='/vmfs/volumes/datastore0'
dstpath='/vmfs/volumes/datastore0'

src="$srcpath/$srcvm/"
dst="$dstpath/$dstvm/"

## Copy Files and Rename for New Guest
cp -a $src $dst && rm $dst/vmware*
vmkfstools -E $dst/$srcvm.vmdk $dst/$dstvm.vmdk
for i in `ls $dst | grep $srcvm`; do mv $dst/$i $dst/`echo $i | sed "s|$srcvm|$dstvm|"`; done
sed -i "s|$srcvm|$dstvm|"g $dst/$dstvm.vmx
vmid=$(vim-cmd solo/registervm $dst/$dstvm.vmx)
vim-cmd vmsvc/power.on $vmid &
sleep 1; vim-cmd vmsvc/message $vmid _vmx1 2
vim-cmd vmsvc/getallvms | grep $vmid
vim-cmd vmsvc/power.getstate $vmid | grep Powered

VM from template:
https://github.com/sgomezsaez/CoreOS_VMWare/blob/master/deploy_coreos_on_esxi.sh

Move a VM from one ESXi host to another ESXi host using CLI:
http://www.routereflector.com/2015/01/moving-a-powered-off-vm-from-cli/

Clone a VM using CLI:
http://virtuallyhyper.com/2012/04/cloning-a-vm-from-the-command-line/
http://kb.vmware.com/selfservice/microsites/search.do?cmd=displayKC&docType=kc&externalId=1027876&sliceId=1&docTypeID=DT_KB_1_1&dialogID=936786325&stateId=1%200%20936792991

Converting an image with QEMU:
http://cri.ch/linux/docs/sk0020.html
Create disk image file by qemu-img create -f raw <image> <size (say. 4G)>
Convert it to vmdk qemu-img convert -f raw <qemu-image> -O <vmplayer-image (.vmdk ext)>
Create a .vmx file

Old but still great overview:

No comments:

Post a Comment