Charles Kozlers Personal Website

Tag: VirtualBox

[VirtualBox/Linux/Windows] Copied Linux Guests Increasing eth devices

by Chuck Kozler on Jan.13, 2010, under Computers, Linux/Unix, Ubuntu, Windows

I noticed that when you copy Linux guests (VBoxManage clonevdi) and then try to use them, your ethernet device adapters keep increasing by one (eth0,eth1,eth2..etc).

This happens because the OS is looking in /etc/udev/rules.d/70-persistent-net.rules to look for the MAC address and corresponding adapter. Since the MAC address on the now new guest has been changed by VirtualBox, obviously the Guest OS can not find this MAC address. Naturally, it disables eth0 and creates eth1 with the new VirtualBox generated MAC address.

This can become quite troublesome, especially if you make a copy of a copy of a copy and so on.

I wrote a quick script to swap these MAC addresses around for you. I had to write something like this because I had to deploy about 20 Virtualized Guests and I created a base VDI to copy over and over as I needed it. I placed this script in the /root directory so I could just “su” and execute it, restart, and my address was fixed.

#!/bin/bash
 
user=`whoami`
if [ $user != "root" ]; then
        echo "You must be root to run this.  Exiting."
        exit 300
fi
 
 
function swapMacs
{
        eth0_mac=`cat /etc/udev/rules.d/70-persistent-net.rules | grep -i "eth0" | cut -d ',' -f4 | cut -d '=' -f3 | cut -d '"' -f2 | sed -e 's/:/\\\\:/g'`
        if [ -z $eth0_mac ]; then
                echo " ! MAC Address for eth0 is empty.  Could not retrieve MAC from udev file."
                exit 100
        fi
        eth1_mac=`cat /etc/udev/rules.d/70-persistent-net.rules | grep -i "eth1" | cut -d ',' -f4 | cut -d '=' -f3 | cut -d '"' -f2 | sed -e 's/:/\\\\:/g'`
        if [ -z $eth1_mac ]; then
                echo " ! MAC Address for eth1 is empty.  Could not retrieve MAC from udev file. ( Are you sure the line exists? )"
                exit 100
        fi
 
        echo " * Find old mac: $eth0_mac"
        echo " * Replacing with: $eth1_mac"
 
        sed -i "s/$eth0_mac/$eth1_mac/g" /etc/udev/rules.d/70-persistent-net.rules
 
        if [ $? -eq 0 ]; then
                echo " + Successfully swapped mac addresses"
                echo " * Setting new udev file"
                new_rule=`cat /etc/udev/rules.d/70-persistent-net.rules | grep -i "eth0"`
 
                echo $new_rule > /etc/udev/rules.d/70-persistent-net.rules
                if [ $? -eq 0 ]; then
                        echo " + We have successfully updated the udev file"
                else
                        echo " ! Problem updating the udev file"
                        exit 100
                fi
 
        else
                echo " ! Problem swapping mac addresses"
                exit 100
        fi
}
 
swapMacs
Sphere: Related Content

Leave a Comment :, , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...