public
Last active — forked from Ahtenus/switchmods

Updated version of switchmods

  • Download Gist
switchmods12.04
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#!/bin/bash
#
# DESCRIPTION
#
# This is a script to automate the switch from r8169 to r8168.
#
# AUTHORSHIP
# Jameson Williams
# www.jamesonwilliams.com
# Taras Taday
#
# CHANGELOG
#
# Apr 15, 2012: Updated code to work with Ubuntu 12.04
#
# May 16, 2008: Corrected a typo on line 43 which prevented the r8169
# module from being renamed. Thanks to linuxnotes.blogspot.com
# for spotting it.
#
# Jun 1, 2008: Added a bunch of checks to beef up the script a bit.
# Removed all reliance upon the internet, as it is more than
# likely the case that this script is being run without
# connectivity. Added better output in case of failures.
#
# Jun 2, 2008: Simplified many of the if statements to simpler checks.
# Added a few more, and created the die() function.
#
# Jun 4, 2008: Moved the stuff to removfe r8169 to the beginning. This
# makes the depmod command behave correctly, finally! Also fixed
# the insmod check so that it no longer incorrectly dies. (Good
# news!) Updated the die command to delete rogue $TMP folders in
# case the script dies before completion. Also added more output
# to help with debugging.
#
# Aug 13, 2008: Updated the code to work with version 8.008.00 of the
# Realtek driver. The new code no longer needs to be patched,
# which was sort of the *magic* of the previous versions of this
# script. Still a useful quick-fix script, though.
#
# Nov 7, 2008: Updated package for 8.009.00
#
# Aug 17, 2009: Updated package for 8.013.00, added a .ko, etc.
#
# Oct 18, 2009: Updated package for 8.0.14.00, including a patch for
# kernels >= 2.6.31.
#
# Oct 26, 2011: Changed so that it works for linux 3.0 (Ahtenus)
#
#
 
VERSION=r8168-8.028.00
 
# setup a temporary directory, and save some environmental infos
PREVWD=$(pwd)
RUNPATH=$(dirname $0)
TMP=$(mktemp -d)
MODULEPATH=/lib/modules/`uname -r`/kernel/drivers/net/ethernet/realtek
 
function die {
# $1 is an error message to print to stderr.
echo $1 1>&2;
if [[ -d $TMP ]]; then
rm -rf $TMP2
fi
if [[ -f $MODULEPATH/r8169.ko.bak ]] && \
[[ ! -f $MODULEPATH/r8169.ko ]] && \
[[ $(id -u) -eq 0 ]]; then
cp $MODULEPATH/r8169.ko.bak $MODULEPATH/r8169.ko
fi
exit 1
}
 
# check to make sure that this is running as root!
[[ $(id -u) -eq 0 ]] || die "You must run this script as root."
 
echo "Attempting to remove running r8168 and r8169 modules if loaded..."
[[ $(lsmod | grep -c r8169) -eq 0 ]] || rmmod r8169
[[ $(lsmod | grep -c r8168) -eq 0 ]] || rmmod r8168
 
echo "Attempting to move $MODULEPATH/r8169.ko to $MODULEPATH/r8169.ko.bak."
[[ ! -f $MODULEPATH/r8169.ko ]] || \
mv $MODULEPATH/r8169.ko $MODULEPATH/r8169.ko.bak
 
echo "Blacklisting r8169 in /etc/modprobe.d/blacklist..."
if [[ $(grep -c "blacklist r8169" /etc/modprobe.d/blacklist.conf) -lt 1 ]]; then
echo -e "\nblacklist r8169\n" >> /etc/modprobe.d/blacklist.conf
fi
 
DRVFILE=${VERSION}.tar.bz2
 
echo "Creating a tmp dir in which to build the module..."
cp "$RUNPATH"/"$DRVFILE" $TMP
cd $TMP
tar xjf "$DRVFILE"
cd ./${VERSION}/src
 
echo -n "Checking for gcc and linux-headers-$(uname -r)..."
[[ $(dpkg -l | grep -c linux-headers-$(uname -r )) -gt 0 ]] || \
die "You need to install package linux-headers-$(uname -r )."
 
[[ $(dpkg -l | grep -c gcc) -gt 0 ]] || \
die "You need to install the GNU C Compiler."
echo -e "\tOK"
 
echo -n "Attempting to build the module..."
{
cd ..
make clean
make modules
make install # this copies the module to the appropriate /lib/modules
# dir.
} &>/dev/null
 
[[ -f $MODULEPATH/r8168.ko ]] || die "Could not build module."
echo -e "\tOK"
 
cd $PREVWD
 
rm -r $TMP
 
echo -n "Inserting the module into running kernel code..."
insmod $MODULEPATH/r8168.ko
[[ $? ]] || die "Failed to insert module."
echo -e "\tOK"
 
depmod
update-initramfs -u
 
echo -n "All done! You should now have a "
echo "working wired ethernet connection, persistant at boot."
 
exit 0

Hi gist,

I'm trying out your script here, I got into trouble with "You need to install package linux-headers-". I'm a newbie and such a lazy ass :) I'll try to google it though after this, but just wondering if that could be automated to into the script :D

Regards,
Erik

just realized that was a stupid suggestion hahaha!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.