#!/bin/sh # Author: Noah Friedman # Created: 2011-10-02 # Public domain # $Id: eth0-fix,v 1.5 2015/02/25 02:06:19 friedman Exp $ ### BEGIN INIT INFO # Provides: eth0-fix # Required-Start: # Required-Stop: # X-Start-Before: $network $netdaemons # Default-Start: 2 3 4 5 # Default-Stop: # Short-Description: detect changed MAC for eth0 # Description: detects changed MAC for eth0 and reconfigures ### END INIT INFO # chkconfig: 2345 01 99 # description: detects changed MAC for eth0 and reconfigures # To add/remove using chkconfig: # chkconfig --add eth0-fix # chkconfig --del eth0-fix # To add/remove using insserv: # insserv --default eth0-fix # insserv --remove eth0-fix # Using update-rc.d: # update-rc.d eth0-fix defaults # update-rc.d -f eth0-fix remove # To launch from upstart(8), create /etc/init/eth0-fix.conf with the # following lines: # description "detect changed MAC for eth0" # start on net-device-added INTERFACE=eth1 # exec /etc/init.d/eth0-fix start() { if [ -e /sys/class/net/eth1 ] && ! [ -e /sys/class/net/eth0 ]; then rules=/etc/udev/rules.d/70-persistent-net.rules omac=`sed -ne '/eth0/s/.*{address}=="\([^"]*\)".*/\1/p' $rules` read nmac < /sys/class/net/eth1/address logger -p daemon.info -t ${0##*/} -i -s \ Primary ethernet MAC has changed from $omac to $nmac\; \ renaming eth1 to eth0. sed -i \ -e '/eth0/s/\({address}=="\)[^"]*/\1'"$nmac/" \ -e '/eth1/d' \ -e '/# PCI device/d' \ $rules # CentOS 6.x udev generates new rules here before root filesystem # is mounted read/write, then the udev-post init script (which runs # after this one) appends them to the existing rules. Delete this # one now so that doesn't happen. rm -f /dev/.udev/tmp-rules--${rules##*/} # Fedora/RHEL/CentOS cfg=/etc/sysconfig/network-scripts/ifcfg-eth0 if [ -f $cfg ] && grep "^HWADDR=" $cfg > /dev/null then sed -i -e "/^HWADDR=/d" $cfg fi ip link set eth1 name eth0 udevadm control --reload-rules fi } case $1 in start | '' ) start ;; esac # eof