Some notes on dynamic variables

Creating pointers / variable variables is required to loop through any number of vSwitches listed in the host configuration file. The easiest way is to normally use the “${!ReferenceVariable}” syntax, or arrays/associative arrays, but as mentioned in part 1 these simply don’t work in the Busybox Ash shell. The only way I’ve managed to get this to work is “$(eval echo “\${ReferenceVariable”)” – this seems to work OK, and I’ll stick with it until I come across something better.

vSwitches

All fairly straight forward – loop through the vSwitch variables set in the host configuration file and configure accordingly. I’ve set these up with standard vSwitches with

  • Uplinks
  • Failover / failback
  • Switch notify
  • Loadbalancing
  • Number of ports
  • MTU
  • etc

but this can be expanded to include any config required.

########################################################################################################################
# Configure networking, iterate through host configuration file to determine which vswitches and port groups and
# vmkernel interfaces to configure
intVswitchCounter=0;
while :
do
	echo "INFO  ${DELIMITER1}" >> ${strLogfile};
	# Check if vswitch has been specified in host configuration file
	strVswitchName=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_NAME}");
	if [ ! "${strVswitchName}" ]; then
		echo "INFO  No further vSwitches found in host configuration file." >> ${strLogfile};
		break;
	else
		echo "INFO  Configuring vSwitch${intVswitchCounter}." >> ${strLogfile};

		# Parse variable variables
		strVswitchUplink1=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_UPLINK1}");
		strVswitchUplink2=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_UPLINK2}");
		strVswitchActive=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_ACTIVE}");
		strVswitchStandby=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_STANDBY}");
		strVswitchFailback=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_FAILBACK}");
		strVswitchFailuredetection=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_FAILUREDETECTION}");
		strVswitchNotify=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_NOTIFY}");
		strVswitchLoadbal=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_LOADBAL}");
		strVswitchPorts=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_PORTS}");
		strVswitchMTU=$(eval echo "\${CFG_VSWITCH${intVswitchCounter}_MTU}");

		# Configure vswitch
		echo "INFO  Creating ${strVswitchName} with ${strVswitchPorts} ports." >> ${strLogfile};
		esxcli network vswitch standard add --ports=${strVswitchPorts} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		echo "INFO  Adding uplink ${strVswitchUplink1} to ${strVswitchName}." >> ${strLogfile};
		esxcli network vswitch standard uplink add --uplink-name=${strVswitchUplink1} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		echo "INFO  Adding uplink ${strVswitchUplink2} to ${strVswitchName}." >> ${strLogfile};
		esxcli network vswitch standard uplink add --uplink-name=${strVswitchUplink2} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		echo "INFO  Configuring ${strVswitchName} with MTU ${strVswitchMTU}." >> ${strLogfile};
		esxcli network vswitch standard set --mtu=${strVswitchMTU} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;

		# vSwitch failover policy
		if [ "${strVswitchStandby}" == "" ]; then
			echo "INFO  Configuring ${strVswitchName} with active uplinks ${strVswitchActive} and no standby uplinks." >> ${strLogfile};
			esxcli network vswitch standard policy failover set --active-uplinks=${strVswitchActive} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		else
			echo "INFO  Configuring ${strVswitchName} with active uplink ${strVswitchActive} and passive uplink ${strVswitchStandby}." >> ${strLogfile};
			esxcli network vswitch standard policy failover set --active-uplinks=${strVswitchActive} --standby-uplinks=${strVswitchStandby} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		fi

		# Failback
		if [ "${strVswitchFailback}" ]; then
			echo "INFO  Configuring ${strVswitchName} failback policy set to ${strVswitchFailback}." >> ${strLogfile};
			esxcli network vswitch standard policy failover set --failback=${strVswitchFailback} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		fi

		# Failure detection
		echo "INFO  Configuring ${strVswitchName} with ${strVswitchFailuredetection} failure detection." >> ${strLogfile};
		esxcli network vswitch standard policy failover set --failure-detection=${strVswitchFailuredetection} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;

		# Notify switches
		if [ "${strVswitchNotify}" ]; then
			echo "INFO  Configuring ${strVswitchName} switch notify policy set to ${strVswitchNotify}." >> ${strLogfile};
			esxcli network vswitch standard policy failover set --notify-switches=${strVswitchNotify} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;
		fi

		# Loadbal
		echo "INFO  Configuring ${strVswitchName} with ${strVswitchLoadbal} load balancing." >> ${strLogfile};
		esxcli network vswitch standard policy failover set --load-balancing=${strVswitchLoadbal} --vswitch-name=${strVswitchName} >> ${strLogfile} 2>&1;

	fi #Checking for existance of vSwitch config entries

	# Next vSwitch
	intVswitchCounter=$((intVswitchCounter + 1));
done

Portgroups

Continues in the same fashion. Configures port group on specified vSwitch, sets VLAN and vmmic override if need be (e.g. for iSCSI).

########################################################################################################################
# Configure portgroups
intPortgroupCounter=0;
while :
do
	echo "INFO  ${DELIMITER1}" >> ${strLogfile};

	# Check if portgroup has been specified in host configuration file
	strPortgroupName=$(eval echo "\${CFG_PORTGROUP${intPortgroupCounter}_NAME}");
	if [ ! "${strPortgroupName}" ]; then
		echo "INFO  No further portgroups found in host configuration file." >> ${strLogfile};
		break;
	else
		echo "INFO  Configuring portgroup ${intPortgroupCounter}." >> ${strLogfile};

		# Parse variables
		strPortgroupVswitch=$(eval echo "\${CFG_PORTGROUP${intPortgroupCounter}_VSWITCH}");
		strPortgroupVLAN=$(eval echo "\${CFG_PORTGROUP${intPortgroupCounter}_VLAN}");
		strPortgroupActive=$(eval echo "\${CFG_PORTGROUP${intPortgroupCounter}_ACTIVE}");

		echo "INFO  Configuring portgroup ${strPortgroupName} on ${strPortgroupVswitch}." >> ${strLogfile};
		esxcli network vswitch standard portgroup add --portgroup-name="${strPortgroupName}" --vswitch-name=${strPortgroupVswitch} >> ${strLogfile} 2>&1;
		echo "INFO  Configuring portgroup ${strPortgroupName} to use VLAN ${strPortgroupVLAN}." >> ${strLogfile};
		esxcli network vswitch standard portgroup set --portgroup-name="${strPortgroupName}" --vlan-id=${strPortgroupVLAN} >> ${strLogfile} 2>&1;

		if [ "${strPortgroupActive}" ]; then
			echo "INFO  Configuring portgroup ${strPortgroupName} with active uplink ${strPortgroupActive}." >> ${strLogfile};
			esxcli network vswitch standard portgroup policy failover set --portgroup-name=${strPortgroupName} --active-uplinks=${strPortgroupActive} >> ${strLogfile} 2>&1;
		fi

	fi #Checking for existance of portgroup config entries

	# Next portgroup
	intPortgroupCounter=$((intPortgroupCounter + 1));
done

Interfaces

Vmkernel interfaces, set IP details, MTU and tags.

########################################################################################################################
# Configure vmkernel interfaces
intVmkCounter=0;
while :
do
	echo "INFO  ${DELIMITER1}" >> ${strLogfile};

	# Check if portgroup has been specified in host configuration file
	strVmkName=$(eval echo "\${CFG_VMK${intVmkCounter}_NAME}");
	if [ ! "${strVmkName}" ]; then
		echo "INFO  No further interfaces found in host configuration file." >> ${strLogfile};
		break;
	else
		echo "INFO  Configuring vmk${intVmkCounter}." >> ${strLogfile};

		# Parse variable variables
		strVmkPortgroup=$(eval echo "\${CFG_VMK${intVmkCounter}_PORTGROUP}");
		strVmkType=$(eval echo "\${CFG_VMK${intVmkCounter}_TYPE}");
		strVmkIP=$(eval echo "\${CFG_VMK${intVmkCounter}_IP}");
		strVmkNetmask=$(eval echo "\${CFG_VMK${intVmkCounter}_NETMASK}");
		strVmkMTU=$(eval echo "\${CFG_VMK${intVmkCounter}_MTU}");
		strVmkTags=$(eval echo "\${CFG_VMK${intVmkCounter}_TAGS}");

		# Add interface
		echo "INFO  Configuring interface ${strVmkName} on portgroup ${strVmkPortgroup}." >> ${strLogfile};
		esxcli network ip interface add --interface-name=${strVmkName} --portgroup-name="${strVmkPortgroup}" >> ${strLogfile} 2>&1;
		if [ "${strVmkType}" == "static" ]; then
			echo "INFO  Configuring static IP ${strVmkIP}/${strVmkNetmask}." >> ${strLogfile};
			esxcli network ip interface ipv4 set --interface-name=${strVmkName} --ipv4=${strVmkIP} --netmask=${strVmkNetmask} --type=${strVmkType} >> ${strLogfile} 2>&1;
		elif [ "${strVmkType}" == "dhcp" ]; then
			echo "INFO  Configuring DHCP." >> ${strLogfile};
			esxcli network ip interface ipv4 set --interface-name=${strVmkName} --type=${strVmkType} >> ${strLogfile} 2>&1;
		fi

		# Enable interface
		echo "INFO  Enabling ${strVmkName}." >> ${strLogfile};
		esxcli network ip interface set --enabled=true --interface-name=${strVmkName} >> ${strLogfile} 2>&1;

		# Set MTU
		echo "INFO  Configuring ${strVmkName} with MTU ${strVmkMTU}." >> ${strLogfile};
		esxcli network ip interface set --mtu=${strVmkMTU} --interface-name=${strVmkName} >> ${strLogfile} 2>&1;

		# Tag network
		for strVmkTag in $(echo ${strVmkTags} | sed 's/,/\ /g');
		do
			echo "INFO  Tagging ${strVmkName} with tag ${strVmkTag}." >> ${strLogfile};
			esxcli network ip interface tag add --interface-name=${strVmkName} --tagname=${strVmkTag} >> ${strLogfile} 2>&1;
		done

	fi #Checking for existance of vmk config entries

	# Next portgroup
	intVmkCounter=$((intVmkCounter + 1));
done

Next – storage, general system settings and tidy up.

Posted by Dag

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s