«

»

Nutanix AHV – Change Virtual Network For Multiple VMs

About 1.5 years ago I wrote a blog post about how to Change Nutanix AHV Virtual Machine Network Placement. This blog post is similar but the major difference is that you can virtual network for multiple VMs same time.

This his is not a UI feature, it’s a script me and my colleague Art Perkins created a while ago and you need to run it from a Nutanix Controller Virtual Machine (CVM).

To change VM virtual network placement for multiple VM (iIn fact you can use it for a single VM as well but might not make a lot of sense) just:

  • Download the script from my GitHub repository or from the script section in the blog post, and copy/upload to a CVM and make it executable
  • Run the script

Information about the script:

  • When specifying VM name(s) you can use regular expressions
  • You can list available networks is decided when running the script as you can see in the below example.
  • Each VM that is target for the reconfiguration is listed with Name and NIC MAC address so you get a chance to cancel the reconfiguration if needed.
  • All VM NICs, if a VM got more than one NIC, will be reconfigured.

The following sequence shows when I changed virtual network for 3 of 4 selected VMs (db1-1, db1-2, db1-3, db1-4) to virtual network vlan201, db1-4 was powered on and cannot be changed.

  • List VM name and VM virtual network configuration by running the following command:
    • acli vm.get db1-* | grep name
  • Run the script by running the command:
    • sh AHV-VM-change_virtual_nic.sh
  • Run the verification command and make sure the VMs were moved to the specified destination virtual network, in my case vlan201. DB1-4 won’t be changed as previously mentioned.
    • acli vm.get db1-* | grep name

Script

#!/bin/bash
# Script to change powered off VM Virtual Network.
# Authors: Magnus Andersson & Arthur Perkins @Nutanix
#
# Version 2.0 -  List Virtual Networksm Virtual Network name (including spaces) management, functionality to manage multiple NICs per VM
# Version 1.0 - Initial release
#
IFS=$'\n'
clear
echo "Enter the name of the VM(s) you want to change network for, regex is accepted."
echo "Important: Single entry only and the VM(s) name is case sensitive."
echo
read -ep "Enter VM Name Search Term:  " "vmname"
echo "-----------------------------------------------"
echo "Do you want to list available Virtual Networks?"
read -ep "Available options are Y or N:" "list"
if [ $list == "N" ] || [ $list == "n" ]
	then
		echo "Important: The Virtual Network name is case sensitive!"
		read -ep "Enter the Virtual Network name:" "vmnet"
	else
                echo "Available Virtual Networks:"
		/usr/local/nutanix/bin/acli net.list | awk  -F'[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]+-[a-zA-Z0-9]' '{print $1}' | sed -n '1!p'
		echo
		echo "Important: The Virtual Network name is case sensitive!"
		read -ep "Enter the Virtual Network name:" "vmnet"
	fi
echo
echo "The VM(s) will be connected to the following Virtual Network when the script is completed:" $vmnet
echo
echo "These VM(s) with their NIC(s) associated MAC address will be changed:"
vmn=`/usr/local/nutanix/bin/acli vm.list | awk '{print $1}' | grep -e $vmname`
for i in ${vmn[@]}; do
	echo -------------------------------------------------
    	echo $i
    	mac1=`/usr/local/nutanix/bin/acli vm.get $i | grep "mac_addr" | awk '{print $2}'`
    	echo $mac1
done
read -p "Press Enter to Continue or Ctrl-C to quit"
echo
for i in $vmn; do
echo
echo -------------------------------------------------
echo "Changing Virtual Network for VM $i"
vmstate=`/usr/local/nutanix/bin/acli vm.get $i | grep -i state | awk -F "\"" '{print $2}'`
	if [ $vmstate == "kOn" ] ; then
		echo "Cannot change Virtual Network for VM $i since it's powered on"
			else
    		mac2=`/usr/local/nutanix/bin/acli vm.get $i | grep "mac_addr" | awk '{print $2}'`
		for x in $mac2; do
			/usr/local/nutanix/bin/acli vm.nic_update $i $x network=$vmnet
		done
	fi
done

The script has been tested on AOS 5.8 and AHV 20170830.151

 

2 pings

Comments have been disabled.