Migrate RHEL VMware Interfaces to AHV Interfaces

July 19, 2021

by Ed McAndrew

Intended Audience Level: Beginner/Intro

Code Sample Type: Snippet

Nutanix Technologies: General

Minimum Product Version: All Move & Dial Versions

Script/Code Language: Bash Shell

REST API Sample? No

REST API Version: N/A

This script will migrate “ensXX” interfaces to “ethXX” interfaces after a Move VM Migration or Dial Cluster Conversion from ESX to AHV.

Code Sample Details

This section may be empty if additional code sample details are not available.
#!/bin/bash
#
#.NOTES
##############################################################################
#	Migrate RHEL VMware Interfaces to AHV Interfaces
#	Filename: rhel_migrate_interfaces.sh
#	Script Version: 1.0.1
##############################################################################
#.SYNOPSIS
#	This script will migrate "ensXX" interfaces to "ethXX" interfaces after a Move VM Migration or Dial Cluster Conversion from ESX to AHV.
#	This also handles sub-interfaces (ensXX:X) in their original order.
#.PREREQUISITES
#	1. Console access to RHEL machine.
#	2. Install script as root user
#		a. [root@localhost ~]# chmod +x rhel_migrate_interfaces.sh
#	3. Create @reboot cron job
#		a. [root@localhost ~]# crontab -e
#		b. @reboot /root/scripts/rhel_migrate_interfaces.sh >/dev/null 2>&1
#		c. Save and Quit
#	4. Proceed with Move migration or Dial conversion.
#		a. Please select "Manual" VM preparation option if using this script with Move.
#.DISCLAIMER
#	This code is intended as a standalone example.  Subject to licensing restrictions defined on nutanix.dev, this can be downloaded, copied and/or modified in any way you see fit.
#	Please be aware that all public code samples provided by Nutanix are unofficial in nature, are provided as examples only, are unsupported and will need to be heavily scrutinized and potentially modified before they can be used in a production environment.  All such code samples are provided on an as-is basis, and Nutanix expressly disclaims all warranties, express or implied.
#
#	All code samples are © Nutanix, Inc., and are provided as-is under the MIT license. (https://opensource.org/licenses/MIT)
##############################################################################
# SET VARIABLES BELOW
##############################################################################
my_log_path='/root/scripts'
my_startup_wait_time_in_seconds=30 # Default: 30 seconds is typically enough of a delay for the network stack to come up.
##############################################################################
#////////////////////////////////////////////////////////////////////////////////////////////////
# CHANGE NOTHING BELOW HERE!
#////////////////////////////////////////////////////////////////////////////////////////////////
##############################################################################
my_orig_ifs=$IFS
IFS=$'\n';
for i in $(/usr/sbin/lspci | /usr/bin/grep Ethernet);do
	my_nic_vendor="$i"
	if [[ $my_nic_vendor == *"VMware"* ]];then
		# If NIC Vendor is VMware, exit the script as we're on the Source side of the migration/conversion.
		IFS=$my_orig_ifs
		exit 1
	fi
done
IFS=$my_orig_ifs
/bin/sleep $my_startup_wait_time_in_seconds
# Build array of ethXX interfaces, sorted naturally lowest to highest.
# Build array of previous ensXXX configuration files, sorting by number, lowest to highest.
# If this script has already run successfully, there will be no ifcfg-ens* files other than the .bak copies; subsequently, the my_ens_files array will be empty, not enumerable and the script won't process anything.
my_ens_files=($(ls -b /etc/sysconfig/network-scripts/ifcfg-ens* | /bin/sort -t '-' -k2 | /usr/bin/grep -v bak))
my_eth_interfaces=($(/sbin/ip addr show | /bin/grep 'state UP' | /bin/awk '{print $2}' | /bin/tr -d \: | /bin/sort -n))
my_last_interface_name="none"
my_interface_index=0
my_logfile="$my_log_path/migrate_interfaces.log"
# Loop array of configuration files to update interfaces.
for ((i=0;i<${#my_ens_files[@]};i++));do
	if [ ! -f $my_logfile ]; then
	    /bin/mkdir -p $my_log_path && /bin/touch $my_logfile
	fi
	# Build required variables
	my_filename=${my_ens_files[$i]}
	my_filebase="$(basename $my_filename)"
	my_this_interface=$(/bin/echo $my_filebase | awk -F ':' '{print $1}')
	if [ "$my_this_interface" != "$my_last_interface_name" ] && [ "$my_last_interface_name" != "none" ];then my_interface_index=$((my_interface_index+1));fi
	my_old_interface_name="$(/bin/echo $my_filebase | /bin/awk -F '-' '{print $NF}')"
	my_new_interface_name="${my_eth_interfaces[$my_interface_index]}"
	if [[ "$my_filename" == *":"* ]]; then
		my_sub_interface_index="$(/bin/echo $my_old_interface_name | /bin/awk -F ':' '{print $NF}')"
		my_new_interface_name="$my_new_interface_name:$my_sub_interface_index"
	fi
	my_new_filename="ifcfg-$my_new_interface_name"
	# Do the work!
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]: Migrating $my_filebase to $my_new_interface_name." >> $my_logfile
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]:    Backing up $my_filebase config file." >> $my_logfile
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]:    Modifying $my_filebase config file." >> $my_logfile
	# SED the file and create a backup copy of the original in the proccess.
	/bin/sed -i.bak "s/${my_old_interface_name}/${my_new_interface_name}/g" $my_filename
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]:    Moving config file to $my_new_interface_name." >> $my_logfile
	# Rename the file to the appropriate name.
	/bin/mv "$my_filename" "/etc/sysconfig/network-scripts/$my_new_filename"
	my_last_interface_name=$(/bin/echo $my_filebase | awk -F ':' '{print $1}')
done
if [ "${#my_ens_files[@]}" -gt "0" ]; then
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]: Restarting Network Service..." >> $my_logfile
	/bin/systemctl restart network >> $my_logfile 2>&1
	/bin/echo "[$(/bin/date '+%m-%d-%Y %H:%M:%S')]: Done..." >> $my_logfile
fi