Friday, June 27, 2008

Fixing VBox guest lossing networking connection

When playing around with Virtual Box guests running Linux, the network connection my get lost on Debian Etch when the virtual interface's MAC changes. This is due to the fact that this version of linux is using udev and fortunately udev detects changes to MAC address. So basically, each new network interface with new MAC address gets its new ethx network interface device name. In my case, I got 3 ethx's already so I need to get rid of the unnecessary interfaces. As a root do this:
$: su
#: cd /etc/udev/rules.d
#: mkdir ~/bak
#: cp zxx_persistent-net-generator.rules ~/bak
#: rm zxx_persistent-net-generator.rules
#: /etc/init.d/udev stop
#: /etc/init.d/udev start
Note that we make a backup of the file just in case we need to go back. Also, for zxx_persistent-net-generator.rules, xx is any two digit number. So just go check the directory for a file that resembels *_persistent-net-generator.rules. Now to prevent udev to generate new interface for every new MAC address for Virtual Box virtual interface, do make the following changes:
#: nano -w /etc/udev/persistent-net-generator.rules
Then ensure that you have the line to ingore MAC address from Virtual Box virtual network interface:
# These rules generate rules to keep network interface names unchanged
# across reboots write them to /etc/udev/rules.d/z25_persistent-net.rules.
#
# The default name for this file is z45_persistent-net-generator.rules.

ACTION!="add",    GOTO="persistent_net_generator_end"
SUBSYSTEM!="net",   GOTO="persistent_net_generator_end"

# ignore the interface if a name has already been set
NAME=="?*",    GOTO="persistent_net_generator_end"

# ignore "secondary" raw interfaces of the madwifi driver
KERNEL=="ath*", ATTRS{type}=="802", GOTO="persistent_net_generator_end"

# provide nice comments for the generated rules
SUBSYSTEMS=="pci", \
ENV{COMMENT}="PCI device $attr{vendor}:$attr{device}"
SUBSYSTEMS=="usb", \
ENV{COMMENT}="USB device $attr{idVendor}:$attr{idProduct}"
SUBSYSTEMS=="ieee1394", \
ENV{COMMENT}="Firewire device $attr{host_id}"
SUBSYSTEMS=="xen", \
ENV{COMMENT}="Xen virtual device"
ENV{COMMENT}=="", \
ENV{COMMENT}="Unknown $env{SUBSYSTEM} device ($env{DEVPATH})"
ATTRS{driver}=="?*", \
ENV{COMMENT}="$env{COMMENT} ($attr{driver})"
#Ignore Virtual Box virtual network interface
ATTR{address}=="08:00:27:*", GOTO="persistent_net_generator_end"

# ignore interfaces without a driver link like bridges and VLANs
KERNEL=="eth*|ath*|wlan*|ra*|sta*", DRIVERS=="?*",\
IMPORT{program}="write_net_rules $attr{address}"

ENV{INTERFACE_NEW}=="?*", NAME="$env{INTERFACE_NEW}"

LABEL="persistent_net_generator_end"
Then delete /etc/udev/rules.d/zxx_persistent-net-generator.rules. In Debian Etch, default xx value is 25 but once you re-generate new rule it defaults xx to 45. Now once you restart udev or reboot the machine you should have a persistent interface name for Virtual Box virtual interface. A word of caution though. If you add another interface this may cause problem as udev might not assign new network interface for the additional NIC card since we have created an exception. So you might opt to use the first method of just regenerating the rules by deleting /etc/udev/rules.d/zxx_persistent-net-generator.rules and restarting udev or rebooting the machine.

References: http://forums.virtualbox.org/viewtopic.php?t=1616

Enjoy!

~TechnoS

No comments: