Skip to content
Geek and I
Go back

Set up a VMware vSwitch - the vMA way

Updated:
Mike Horwath2 min read

Have a set of new VMware hosts you need to configure matching vSwitch configurations on?

Have a cluster that needs updating with some new VLAN additions on a standard vSwitch?

If you answered ‘yes’ to either of those questions then continue reading!

In this script, we will loop through requesting the password from the user for each host (if each host has differing root passwords), or if you have vifp set up then use the vifptarget command for credentials.

Create a text file with either host names or IP addresses like so:

Terminal window
192.168.31.14
vmhost01.geekandi.net
172.28.91.194

and we will use this as input to the for loop.

Terminal window
#!/bin/sh
# set username for authentication, normally this is 'root'
VI_USERNAME=root ; export VI_USERNAME
# within the for loop it will prompt you for each host's
# root password. This section could be commented out if
# you have already set up vifp parameters
for host in `cat that-text-file.txt` ; do
# if using the vifp then comment out this section
echo -n "Enter ${host} root password: "
read VI_PASSWORD ; export VI_PASSWORD
# if using vifp then uncomment out this section
# vifptarget --set ${i}
# now the meat of the script
# example: add a port group for a new NFS segment on VLAN 200
esxcfg-vswitch --server ${i} --add-pg NFS_VLAN200 vSwitch0
esxcfg-vswitch --server ${i} --vlan=200 --pg=NFS_VLAN200 vSwitch0
# example: add a port group to set up a vMotion segment on VLAN 300
esxcfg-vswitch --server ${i} --add-pg vMotion_VLAN300 vSwitch0
esxcfg-vswitch --server ${i} --vlan=300 --pg=vMotion_VLAN300 vSwitch0
done

But we still need vmkernel interfaces added and this isn’t as scriptable unless you have your servers set up with a known numbering scheme and it is easy to figure out the pattern to script, here are the command lines used to add vmkernel interfaces for the port groups above.

Terminal window
esxcfg-vmknic --add --ip 172.17.29.131 --netmask 255.255.0.0 --server <server_name> NFS_VLAN200
esxcfg-vmknic --add --ip 172.18.29.131 --netmask 255.255.0.0 --server <server_name> vMotion_VLAN300

Easy and simple to edit for your needs.

I used the above script fragments to create standard port groups on the default vSwitch0 when I rolled out my last VMware cluster then converted the vmkernel networking from standard to vDS via the vSphere client.

If you want to do the same for a vSphere Distributed Switch then you’ll need to hop on over to PowerCLI and load a 3rd party module (as of 5.x that is, where x < 5 at the time of this article, or using PowerCLI 5.1 R2 which was recently released). I have used this multiple times for another customer to set up their networking for VXLAN + VLAN + vCloud Director configuration(s).

Leave me a comment if you would like to see the same script via PowerCLI.



Related Posts

Previous Post
nginx - conversion complete
Next Post
SYSPRO6 SRSPRTWN.int Error when printing