Add / Remove port groups and VLANs using the ESXi command line

Useful commands for modifying port groups and VLAN settings on ESXi. Can be used in scripts of course to do additions / removals / changes in bulk.

List vSwitches:
=============================================
esxcli network vswitch standard list

List port groups
=============================================
esxcli network vswitch standard portgroup list

Add port group named “VLAN-900” to vSwitch0
=============================================
esxcfg-vswitch --add-pg=VLAN-900 vSwitch0

Assign VLAN 900 to port group “VLAN-900”
=============================================
esxcfg-vswitch -v 900 -p VLAN-900 vSwitch0

Delete port group TEST from vSwitch0
=============================================
esxcfg-vswitch --del-pg=TEST vSwitch0

Restore XenServer VM snapshots from the CLI / command line

Taking a snapshot from the command line is trivial. Restoring the snapshot is not. From the XenCenter GUI application it’s easy of course, but sometimes you need to automate things. In this case I have an environment for testing which needs to be reverted to the same state after each test.

Each VM has only a single snapshot. Matching the snapshot UUID with the UUID of the virtual machine isn’t easy, but can be done by extracting the parameter called “children” from the snapshot.

To list the snapshots with their respective VM UUID’s, I use the following (i and j variables for snapshot UUID and VM UUID respectively):

[root@XenServer42 ~]# for i in `xe snapshot-list  | grep uuid | awk '{print$5}'`; do export j=$(xe snapshot-param-get uuid=$i  param-name=children); echo "VM UUID: $j - Snapshot UUID: $i"; done
VM UUID: 9211f2a2-4624-4254-543f-b6a99cce7760 - Snapshot UUID: 89ed788c-987e-75ad-9d72-84b2d06486de
VM UUID: 92bbf92c-57df-a6c3-fab2-366573ea3f29 - Snapshot UUID: 23da7e91-19d6-07d3-5fb3-818b834d6883
VM UUID: 3dd5209b-77cc-923a-d0da-4fb7aa013498 - Snapshot UUID: c91e63c6-6fd7-e49e-1a80-6215fecdda10
VM UUID: 29a28e86-dd60-0727-487c-12743b833a6b - Snapshot UUID: 4d21112b-b92d-4b1e-16c2-d1bcfb2d1a0b
VM UUID: 6bc1fd07-cfa7-6b00-dc90-dfe2f228db9c - Snapshot UUID: 44abca3c-eadc-c74f-2095-9c6198681305
VM UUID: c4348bab-cf34-217c-85be-3661a0e5cb60 - Snapshot UUID: af1859ae-675b-6a3d-f688-a0af65baba13

To restore the snapshots:
for i in `xe snapshot-list  | grep uuid | awk '{print$5}'`; do export j=$(xe snapshot-param-get uuid=$i  param-name=children); xe snapshot-revert uuid=$j snapshot-uuid=$i; done

NOTE: This works if there’s only ONE snapshot per VM and you want to restore them all. Otherwise more complex scripting is required to filter out the ones you need. It’s more than enough for our test systems though.

Some basic XenServer VM management from the CLI / command line

List some VMs we care about:
================================================
xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n

Take snapshot of the VMs
================================================
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`; do echo "Snapshotting $i"; xe vm-snapshot new-name-label="BASE vGPU IMAGE" vm=$i; done

Start the VMs
================================================
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`; do echo $i; xe vm-start vm=$i; done

Stop the VMs
================================================
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`; do echo $i; xe vm-shutdown vm=$i; done

Shutdown – Take snapshot – Start sequence:
================================================
On one line:
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`; do xe vm-shutdown vm=$i; echo "Snapshotting $i"; xe vm-snapshot new-name-label="BASE vGPU IMAGE" vm=$i; xe vm-start vm=$i; done

For use in a short shell script (prettier formatting):

#!/bin/sh

for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`
do 
    xe vm-shutdown vm=$i
    echo "Snapshotting $i"
    xe vm-snapshot new-name-label="BASE vGPU IMAGE" vm=$i
    xe vm-start vm=$i
done

Import VMs from directory:
================================================
for i in *.bkp; do xe vm-import filename=$i sr-uuid=`pvscan | grep Local | awk '{print $4}' | sed 's/-/ /' | awk '{print $2}'` preserve=true; done

Export VMs to current directory:
================================================
for i in `xe vm-list | grep na | grep -v Xen | awk '{print $4}' | sort -n`;do echo $i; xe vm-export filename=$i.bkp vm=$i; done

Error when installing the vSphere6.0 appliance: The file D:\vcsa-setup.html is not in a folder shared with the host and cannot be opened by the host.

When trying to launch the vSphere6.0 appliance installer I just got the following “The file D:\vcsa-setup.html is not in a folder shared with the host and cannot be opened by the host.”

vcsa-setup.html error

After having tried and received the same error after copying the ISO locally, emptying the contents into a folder, mounting it via vCenter5.5 to my VM, etc I simply dragged and dropped the file onto Firefox. That was it – it works.

Funny how these simple things can end up wasting time …