Scripted configuration of VMware ESX vswitch's, vmknic's and VLAN's

I believe there are better ways of doing this thru the Perl SDK but I’m yet to use it. However as a shell script this can also be included in kickstart scripts. Don’t rely on the NIC/vmk design below, it works, but may not be the best way to do it!

[[email protected] ~]$ cat test.sh 
#!/bin/sh 
# 
# Connectivity layout, HP blades with 10Gbps Virtual Connect NIC's, 
#  vmnic0 – 10Gbps 802.1Q trunk 
#  vmnic1 – 10Gbps 802.1Q trunk 
#    vTrunk0 MULTI – 10Gbps multi-VLAN 
#  vmnic2 – 10Gbps iSCSI LAN access 
#  vmnic3 – 10Gbps iSCSI LAN access 
#    vSwitch1 – iSCSI Access – VM network for VM iSCSI access 
#    vSwitch1 – vmk0 – vmkNIC for ESX access to iSCSI SAN (has 4 x 1Gbps interfaces) 
#    vSwitch1 – vmk1 – vmkNIC for ESX access to iSCSI SAN (has 4 x 1Gbps interfaces) 
#    vSwitch1 – vmk2 – vmkNIC for ESX access to iSCSI SAN (has 4 x 1Gbps interfaces) 
#    vSwitch1 – vmk3 – vmkNIC for ESX access to iSCSI SAN (has 4 x 1Gbps interfaces) 
#  vmnic4 – 10Gbps access, shared vMotion/FT network 
#  vmnic5 – 10Gbps access, shared vMotion/FT network 
#    vSwitch2 – vmk4 – vmkNIC for vMotion source/destination IP address 
#    vSwitch2 – vmk5 – vmkNIC for Fault Tolerance source/destination IP address 
#  vmnic6 – 1Gbps access, service console LAN 
#  vmnic7 – 1Gbps access, service console LAN 
#    vSwitch0 – Service Console – service console is isolated from all other networks. 
# 
  
 
TRUNK_VLANS="SOMETHING:11" 
VMKNIC_ISCSI_01="1.1.1.1/255.255.255.0" 
VMKNIC_ISCSI_02="1.1.1.2/255.255.255.0" 
VMKNIC_ISCSI_03="1.1.1.3/255.255.255.0" 
VMKNIC_ISCSI_04="1.1.1.4/255.255.255.0" 
VMOTION_LAN="2.2.2.10/255.255.255.0" 
FT_LAN="3.3.3.10/255.255.255.0" 
  
 
# Add last NIC to service console vSwitch0, vmnic6 should already be there it depends on your ESX installer boot options. Eg. use ksdevice=vmnic6 on boot line. 
echo esxcfg-vswitch -L vmnic7 vSwitch0 
  
 
# Create 802.1q trunked vswitch with MTU 9000 
echo esxcfg-vswitch -a vTrunk0 
echo esxcfg-vswitch -m 9000 vTrunk0 
echo esxcfg-vswitch -L vmnic0 vTrunk0 
echo esxcfg-vswitch -L vmnic1 vTrunk0 
  
 
for VLAN in ${TRUNK_VLANS} ; do 
 NAME=`echo ${VLAN} | cut -f1 -d':' -` 
 VLAN=`echo ${VLAN} | cut -f2 -d':' -` 
 echo esxcfg-vswitch -A "${NAME}" vTrunk0 
 echo esxcfg-vswitch -p "${NAME}" -v ${VLAN} vTrunk0 
done 
  
 
# Create ISCSI vSwitch 
echo esxcfg-vswitch -a vSwitch1 
echo esxcfg-vswitch -m 9000 vSwitch1 
echo esxcfg-vswitch -L vmnic2 vSwitch1 
echo esxcfg-vswitch -L vmnic3 vSwitch1 
echo esxcfg-vswitch -A "iSCSI Access" vSwitch1 
echo esxcfg-vswitch -A "iSCSI1" vSwitch1 
echo esxcfg-vswitch -A "iSCSI2" vSwitch1 
echo esxcfg-vswitch -A "iSCSI3" vSwitch1 
echo esxcfg-vswitch -A "iSCSI4" vSwitch1 
echo esxcfg-vmknic -a -i `echo "${VMKNIC_ISCSI_01}" | cut -f1 -d'/' -` -n `echo "${VMKNIC_ISCSI_01}" | cut -f2 -d'/' -` -m 9000 -p iSCSI1 vmk0 
echo esxcfg-vmknic -a -i `echo "${VMKNIC_ISCSI_02}" | cut -f1 -d'/' -` -n `echo "${VMKNIC_ISCSI_02}" | cut -f2 -d'/' -` -m 9000 -p iSCSI2 vmk1 
echo esxcfg-vmknic -a -i `echo "${VMKNIC_ISCSI_03}" | cut -f1 -d'/' -` -n `echo "${VMKNIC_ISCSI_03}" | cut -f2 -d'/' -` -m 9000 -p iSCSI3 vmk2 
echo esxcfg-vmknic -a -i `echo "${VMKNIC_ISCSI_04}" | cut -f1 -d'/' -` -n `echo "${VMKNIC_ISCSI_04}" | cut -f2 -d'/' -` -m 9000 -p iSCSI4 vmk3 
echo esxcfg-vswitch -p iSCSI1 -N vmnic3 vSwitch1 
echo esxcfg-vswitch -p iSCSI2 -N vmnic2 vSwitch1 
echo esxcfg-vswitch -p iSCSI3 -N vmnic3 vSwitch1 
echo esxcfg-vswitch -p iSCSI4 -N vmnic2 vSwitch1 
  
 
# Create vMotion and fault tolerance vswitch 
echo esxcfg-vswitch -a vSwitch2 
echo esxcfg-vswitch -m 9000 vSwitch2 
echo esxcfg-vswitch -L vmnic4 vSwitch2 
echo esxcfg-vswitch -L vmnic5 vSwitch2 
echo esxcfg-vswitch -A "vMotion" vSwitch2 
echo esxcfg-vswitch -A "FaultTolerance" vSwitch2 
echo esxcfg-vmknic -a -i `echo "${VMOTION_LAN}" | cut -f1 -d'/' -` -n `echo "${VMOTION_LAN}" | cut -f2 -d'/' -` -m 9000 -p vMotion vmk4 
echo esxcfg-vmknic -a -i `echo "${FT_LAN}" | cut -f1 -d'/' -` -n `echo "${FT_LAN}" | cut -f2 -d'/' -` -m 9000 -p FaultTolerance vmk5 
  
 
# EOF 
   
[[email protected] ~]$ sh test.sh
esxcfg-vswitch -L vmnic7 vSwitch0 
esxcfg-vswitch -a vTrunk0 
esxcfg-vswitch -m 9000 vTrunk0 
esxcfg-vswitch -L vmnic0 vTrunk0 
esxcfg-vswitch -L vmnic1 vTrunk0 
esxcfg-vswitch -A "SOMETHING" vTrunk0 
esxcfg-vswitch -p "SOMETHING" -v 11 vTrunk0 
esxcfg-vswitch -a vSwitch1 
esxcfg-vswitch -m 9000 vSwitch1 
esxcfg-vswitch -L vmnic2 vSwitch1 
esxcfg-vswitch -L vmnic3 vSwitch1 
esxcfg-vswitch -A "iSCSI Access" vSwitch1 
esxcfg-vswitch -A "iSCSI1" vSwitch1 
esxcfg-vswitch -A "iSCSI2" vSwitch1 
esxcfg-vswitch -A "iSCSI3" vSwitch1 
esxcfg-vswitch -A "iSCSI4" vSwitch1 
esxcfg-vmknic -a -i 1.1.1.1 -n 255.255.255.0 -m 9000 -p iSCSI1 vmk0 
esxcfg-vmknic -a -i 1.1.1.2 -n 255.255.255.0 -m 9000 -p iSCSI2 vmk1 
esxcfg-vmknic -a -i 1.1.1.3 -n 255.255.255.0 -m 9000 -p iSCSI3 vmk2 
esxcfg-vmknic -a -i 1.1.1.4 -n 255.255.255.0 -m 9000 -p iSCSI4 vmk3 
esxcfg-vswitch -p iSCSI1 -N vmnic3 vSwitch1 
esxcfg-vswitch -p iSCSI2 -N vmnic2 vSwitch1 
esxcfg-vswitch -p iSCSI3 -N vmnic3 vSwitch1 
esxcfg-vswitch -p iSCSI4 -N vmnic2 vSwitch1 
esxcfg-vswitch -a vSwitch2 
esxcfg-vswitch -m 9000 vSwitch2 
esxcfg-vswitch -L vmnic4 vSwitch2 
esxcfg-vswitch -L vmnic5 vSwitch2 
esxcfg-vswitch -A "vMotion" vSwitch2 
esxcfg-vswitch -A "FaultTolerance" vSwitch2 
esxcfg-vmknic -a -i 2.2.2.10 -n 255.255.255.0 -m 9000 -p vMotion vmk4 
esxcfg-vmknic -a -i 3.3.3.10 -n 255.255.255.0 -m 9000 -p FaultTolerance vmk5 
[[email protected] ~]$
Author image
About colin-stubbs