Path: blob/master/ALFA-W1F1/RTL8814AU/install-driver.sh
1306 views
#!/bin/sh12# Purpose: Install Realtek out-of-kernel USB WiFi adapter drivers.3#4# Supports dkms and non-dkms installations.5#6# To make this file executable:7#8# $ chmod +x install-driver.sh9#10# To execute this file:11#12# $ sudo ./install-driver.sh13#14# or15#16# $ sudo sh install-driver.sh17#18# Copyright(c) 2023 Nick Morrow19#20# This program is free software; you can redistribute it and/or modify21# it under the terms of version 2 of the GNU General Public License as22# published by the Free Software Foundation.23#24# This program is distributed in the hope that it will be useful, but25# WITHOUT ANY WARRANTY; without even the implied warranty of26# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the27# GNU General Public License for more details.2829# Need to install30apt install debhelper dpkg-dev dkms libelf-dev bc -y3132SCRIPT_NAME="install-driver.sh"33SCRIPT_VERSION="20230227"34MODULE_NAME="8814au"35DRV_VERSION="5.8.5.1"3637KARCH="$(uname -m)"38KVER="$(uname -r)"39MODDESTDIR="/lib/modules/${KVER}/kernel/drivers/net/wireless/"4041DRV_NAME="rtl${MODULE_NAME}"42DRV_DIR="$(pwd)"43OPTIONS_FILE="${MODULE_NAME}.conf"4445# check to ensure sudo was used to start the script46if [ "$(id -u)" -ne 0 ]; then47echo "You must run this script with superuser (root) privileges."48echo "Try: \"sudo ./${SCRIPT_NAME}\""49exit 150fi5152# support for the NoPrompt option allows non-interactive use of this script53NO_PROMPT=054# get the script options55while [ $# -gt 0 ]56do57case $1 in58NoPrompt)59NO_PROMPT=1 ;;60*h|*help|*)61echo "Syntax $0 <NoPrompt>"62echo " NoPrompt - noninteractive mode"63echo " -h|--help - Show help"64exit 165;;66esac67shift68done6970# ensure /usr/sbin is in the PATH so iw can be found71if ! echo "$PATH" | grep -qw sbin; then72export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin73fi7475# check to ensure gcc is installed76if ! command -v gcc >/dev/null 2>&1; then77echo "A required package is not installed."78echo "Please install the following package: gcc"79echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""80exit 181fi8283# check to ensure bc is installed84if ! command -v bc >/dev/null 2>&1; then85echo "A required package is not installed."86echo "Please install the following package: bc"87echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""88exit 189fi9091# check to ensure make is installed92if ! command -v make >/dev/null 2>&1; then93echo "A required package is not installed."94echo "Please install the following package: make"95echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""96exit 197fi9899# check to see if the correct header files are installed100if [ ! -d "/lib/modules/$(uname -r)/build" ]; then101echo "Your kernel header files aren't properly installed."102echo "Please consult your distro documentation or user support forums."103echo "Once the header files are properly installed, please run \"sudo ./${SCRIPT_NAME}\""104exit 1105fi106107# check to ensure iw is installed108if ! command -v iw >/dev/null 2>&1; then109echo "A required package is not installed."110echo "Please install the following package: iw"111echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""112exit 1113fi114115# check to ensure rfkill is installed116if ! command -v rfkill >/dev/null 2>&1; then117echo "A required package is not installed."118echo "Please install the following package: rfkill"119echo "Once the package is installed, please run \"sudo ./${SCRIPT_NAME}\""120exit 1121fi122123DEFAULT_EDITOR="$(cat default-editor.txt)"124# try to find the user's default text editor through the EDITORS_SEARCH array125for TEXT_EDITOR in "${VISUAL}" "${EDITOR}" "${DEFAULT_EDITOR}" vi; do126command -v "${TEXT_EDITOR}" >/dev/null 2>&1 && break127done128# fail if no editor was found129if ! command -v "${TEXT_EDITOR}" >/dev/null 2>&1; then130echo "No text editor found (default: ${DEFAULT_EDITOR})."131echo "Please install ${DEFAULT_EDITOR} or edit the file 'default-editor.txt' to specify your editor."132echo "Once complete, please run \"sudo ./${SCRIPT_NAME}\""133exit 1134fi135136echo ": ---------------------------"137138# displays script name and version139echo ": ${SCRIPT_NAME} v${SCRIPT_VERSION}"140141# information that helps with bug reports142143# display architecture144echo ": ${KARCH} (architecture)"145146SMEM=$(LANG=C free | awk '/Mem:/ { print $2 }')147sproc=$(nproc)148# avoid Out of Memory condition in low-RAM systems by limiting core usage149if [ "$sproc" -gt 1 ]; then150if [ "$SMEM" -lt 1400000 ]151then152sproc=2153fi154if [ "$SMEM" -lt 700000 ]155then156sproc=1157fi158fi159160# display number of in-use processing units / total processing units161echo ": ${sproc}/$(nproc) (in-use/total processing units)"162163# display total system memory164echo ": ${SMEM} (total system memory)"165166# display kernel version167echo ": ${KVER} (kernel version)"168169# display gcc version170gcc_ver=$(gcc --version | grep -i gcc)171echo ": ""${gcc_ver}"172173# display dkms version if installed174if command -v dkms >/dev/null 2>&1; then175dkms_ver=$(dkms --version)176echo ": ""${dkms_ver}"177fi178179# display secure mode status180if command -v mokutil >/dev/null 2>&1; then181if mokutil --sb-state | grep -i enabled >/dev/null 2>&1; then182echo ": SecureBoot enabled"183fi184if mokutil --sb-state | grep -i disabled >/dev/null 2>&1; then185echo ": SecureBoot disabled"186fi187if mokutil --sb-state | grep -i EFI >/dev/null 2>&1; then188echo ": EFI variables are not supported on this system"189fi190else191echo ": mokutil not installed"192fi193194195echo ": ---------------------------"196echo197198echo "Checking for previously installed drivers."199200# check for and remove non-dkms installations201# standard naming202if [ -f "${MODDESTDIR}${MODULE_NAME}.ko" ]; then203echo ": ---------------------------"204echo205echo "Removing a non-dkms installation: ${MODDESTDIR}${MODULE_NAME}.ko"206rm -f "${MODDESTDIR}"${MODULE_NAME}.ko207/sbin/depmod -a "${KVER}"208echo "Removing ${OPTIONS_FILE} from /etc/modprobe.d"209rm -f /etc/modprobe.d/${OPTIONS_FILE}210echo "Removing source files from /usr/src/${DRV_NAME}-${DRV_VERSION}"211rm -rf /usr/src/${DRV_NAME}-${DRV_VERSION}212make clean >/dev/null 2>&1213echo "Removal complete."214fi215216# check for and remove non-dkms installations217# with rtl added to module name (PClinuxOS)218if [ -f "${MODDESTDIR}rtl${MODULE_NAME}.ko" ]; then219echo ": ---------------------------"220echo221echo "Removing a non-dkms installation: ${MODDESTDIR}rtl${MODULE_NAME}.ko"222rm -f "${MODDESTDIR}"rtl${MODULE_NAME}.ko223/sbin/depmod -a "${KVER}"224echo "Removing ${OPTIONS_FILE} from /etc/modprobe.d"225rm -f /etc/modprobe.d/${OPTIONS_FILE}226echo "Removing source files from /usr/src/${DRV_NAME}-${DRV_VERSION}"227rm -rf /usr/src/${DRV_NAME}-${DRV_VERSION}228make clean >/dev/null 2>&1229echo "Removal complete."230fi231232# check for and remove non-dkms installations233# with compressed module in a unique non-standard location (Armbian)234# Example: /usr/lib/modules/5.15.80-rockchip64/kernel/drivers/net/wireless/rtl8821cu/8821cu.ko.xz235# Dear Armbiam, this is a really bad idea.236if [ -f "/usr/lib/modules/${KVER}/kernel/drivers/net/wireless/${DRV_NAME}/${MODULE_NAME}.ko.xz" ]; then237echo ": ---------------------------"238echo239echo "Removing a non-dkms installation: /usr/lib/modules/${KVER}/kernel/drivers/net/wireless/${DRV_NAME}/${MODULE_NAME}.ko.xz"240rm -f /usr/lib/modules/"${KVER}"/kernel/drivers/net/wireless/${DRV_NAME}/${MODULE_NAME}.ko.xz241/sbin/depmod -a "${KVER}"242echo "Removing ${OPTIONS_FILE} from /etc/modprobe.d"243rm -f /etc/modprobe.d/${OPTIONS_FILE}244echo "Removing source files from /usr/src/${DRV_NAME}-${DRV_VERSION}"245rm -rf /usr/src/${DRV_NAME}-${DRV_VERSION}246make clean >/dev/null 2>&1247echo "Removal complete."248fi249250# check for and remove dkms installations251if command -v dkms >/dev/null 2>&1; then252if dkms status | grep -i ${DRV_NAME}; then253echo ": ---------------------------"254echo255# need to add code here to delete any DRV_VERSION256echo "Removing a dkms installation."257dkms remove -m ${DRV_NAME} -v ${DRV_VERSION} --all258echo "Removing ${OPTIONS_FILE} from /etc/modprobe.d"259rm -f /etc/modprobe.d/${OPTIONS_FILE}260echo "Removing source files from /usr/src/${DRV_NAME}-${DRV_VERSION}"261rm -rf /usr/src/${DRV_NAME}-${DRV_VERSION}262echo "Removal complete."263fi264fi265266# sets module parameters (driver options) and blacklisted modules267echo ": ---------------------------"268echo269echo "Starting installation."270echo "Installing ${OPTIONS_FILE} to /etc/modprobe.d"271cp -f ${OPTIONS_FILE} /etc/modprobe.d272273# determine if dkms is installed and run the appropriate installation routines274if ! command -v dkms >/dev/null 2>&1; then275echo "The non-dkms installation routines are in use."276277make clean >/dev/null 2>&1278279make -j"$(nproc)"280RESULT=$?281282if [ "$RESULT" != "0" ]; then283echo "An error occurred: ${RESULT}"284echo "Please report this error."285echo "Please copy all screen output and paste it into the problem report."286echo "You will need to run the following before reattempting installation."287echo "$ sudo ./remove-driver.sh"288exit $RESULT289fi290291# if secure boot is active, use sign-install292if command -v mokutil >/dev/null 2>&1; then293if mokutil --sb-state | grep -i enabled >/dev/null 2>&1; then294echo ": SecureBoot enabled - read FAQ about SecureBoot"295make sign-install296RESULT=$?297else298make install299RESULT=$?300fi301else302make install303RESULT=$?304fi305306if [ "$RESULT" = "0" ]; then307make clean >/dev/null 2>&1308echo "The driver was installed successfully."309echo ": ---------------------------"310echo311else312echo "An error occurred: ${RESULT}"313echo "Please report this error."314echo "Please copy all screen output and paste it into the problem report."315echo "You will need to run the following before reattempting installation."316echo "$ sudo ./remove-driver.sh"317exit $RESULT318fi319else320echo "The dkms installation routines are in use."321322# the dkms add command requires source in /usr/src/${DRV_NAME}-${DRV_VERSION}323echo "Copying source files to /usr/src/${DRV_NAME}-${DRV_VERSION}"324cp -rf "${DRV_DIR}" /usr/src/${DRV_NAME}-${DRV_VERSION}325326dkms add -m ${DRV_NAME} -v ${DRV_VERSION}327RESULT=$?328329# RESULT will be 3 if the DKMS tree already contains the same module/version330# combo. You cannot add the same module/version combo more than once.331if [ "$RESULT" != "0" ]; then332if [ "$RESULT" = "3" ]; then333echo "This driver may already be installed."334echo "Run the following and then reattempt installation."335echo "$ sudo ./remove-driver.sh"336exit $RESULT337else338echo "An error occurred. dkms add error: ${RESULT}"339echo "Please report this error."340echo "Please copy all screen output and paste it into the problem report."341echo "Run the following before reattempting installation."342echo "$ sudo ./remove-driver.sh"343exit $RESULT344fi345else346echo "The driver was added to dkms successfully."347echo ": ---------------------------"348fi349350if command -v /usr/bin/time >/dev/null 2>&1; then351/usr/bin/time -f "Compile time: %U seconds" dkms build -m ${DRV_NAME} -v ${DRV_VERSION}352else353dkms build -m ${DRV_NAME} -v ${DRV_VERSION}354fi355RESULT=$?356357if [ "$RESULT" != "0" ]; then358echo "An error occurred. dkms build error: ${RESULT}"359echo "Please report this error."360echo "Please copy all screen output and paste it into the problem report."361echo "Run the following before reattempting installation."362echo "$ sudo ./remove-driver.sh"363exit $RESULT364else365echo "The driver was built by dkms successfully."366echo ": ---------------------------"367fi368369dkms install -m ${DRV_NAME} -v ${DRV_VERSION}370RESULT=$?371372if [ "$RESULT" != "0" ]; then373echo "An error occurred. dkms install error: ${RESULT}"374echo "Please report this error."375echo "Please copy all screen output and paste it into the problem report."376echo "Run the following before reattempting installation."377echo "$ sudo ./remove-driver.sh"378exit $RESULT379else380echo "The driver was installed by dkms successfully."381echo ": ---------------------------"382echo383fi384fi385386# provide driver upgrade information387echo "Info: Upgrade this driver with the following commands as needed:"388echo "$ git pull"389echo "$ sudo sh install-driver.sh"390echo "Note: Upgrades to this driver should be performed before distro upgrades."391echo "Note: Upgrades can be performed as often as you like."392echo "Note: Work on this driver is continuous."393echo ": ---------------------------"394echo395396# unblock wifi397if command -v rfkill >/dev/null 2>&1; then398rfkill unblock wlan399else400echo "Unable to run $ rfkill unblock wlan"401fi402403# if NoPrompt is not used, ask user some questions404if [ $NO_PROMPT -ne 1 ]; then405printf "Do you want to edit the driver options file now? (recommended) [Y/n] "406read -r yn407case "$yn" in408[nN]) ;;409*) ${TEXT_EDITOR} /etc/modprobe.d/${OPTIONS_FILE} ;;410esac411412printf "Do you want to apply the new options by rebooting now? (recommended) [Y/n] "413read -r yn414case "$yn" in415[nN]) ;;416*) reboot ;;417esac418fi419420421