CentOS Linux Template Networking Issue


If you have every created a linux virtual machine template you’ve probably experienced the issue where a new ethernet device would be created on each VM instance created from that template. For those that have not found the resolution to this problem are in for a treat if your working with CentOS or RHEL. All thats needed is to delete the “/etc/udev/rules.d/70-persistent-net.rules” file right before you shutdown the vm and make it a template.

This can be easily forgotten if you manually have to do it so here’s a way to set it and forget. Here is what I found to work for me.

Save the code below in a file called refreshnetrules.

cp refreshnetrules /etc/rc.d/init.d/refreshnetrules

Use chkconfig to add the script to the appropriate run levels.

chkconfig --add refreshnetrules

Then all you need to do is reboot or shutdown the VM. Do this from within the OS because some virtualization products may power off the VM which bypasses the shutdown process. I had this issue with CloudStack which wasted my time as I troubleshoot what I thought was bad code. This script will run during shutdown without any intervention from you. “Set and forget”

Script code. Save as “refreshnetrules”.

#!/bin/sh
# /etc/rc.d/init.d/refreshnetrules

echo -n $"Starting refresh-netrules: "; echo

# Make sure rc.local.shutdown link exists:
if [ ! -h /etc/rc.d/rc.shutdown ]; then
   ln -s /etc/rc.d/init.d/refreshnetrules /etc/rc.d/rc.shutdown
fi

# Take care of start house keeping
if [ "$1" = "start" ]; then
   lockfile=/var/lock/subsys/refreshnetrules
   touch $lockfile
   /usr/bin/logger -p user.notice -t refreshnetrules " rc.shutdown touched $lockfile"
   exit 0
fi

# Make sure this script was called correctly
if [ "$1" != "stop" ]; then
   echo $"Usage: $0 {start|stop}"
   exit 2
fi

# Make a note in /var/log/messages that this script has been called:
/usr/bin/logger -p user.notice -t refresh-netrules " rc.local.shutdown has been invoked"

# Delete net rules file
rm -f /etc/udev/rules.d/70-persistent-net.rules

exit 0

Note the “rm -f /etc/udev/rules.d/70-persistent-net.rules” at the end of the file. You can add additional code here if you like but this script will only delete the net rules file on shutdown. I’d like to know if someone has a better way of doing this even though this works for me. Hope this helps.

1 comment

Add yours

Comments are closed.