Interactively manage existing config file

This commit is contained in:
Martin D Kealey 2022-09-25 13:45:02 +10:00
parent 15ea243818
commit 41212db551
2 changed files with 275 additions and 111 deletions

View File

@ -1,19 +1,31 @@
#!/bin/bash #!/bin/bash
# Purpose: Install Realtek USB WiFi adapter drivers. # Purpose: Build and Install Realtek USB WiFi adapter drivers.
# #
# This version of the installation script does not use dkms. # Only use this version if you don't have DKMS available.
# Build and install the driver on an LSB-compliant system.
SCRIPT_VERSION=20220923 SCRIPT_VERSION=20220925
options_file=88x2bu.conf options_file=88x2bu.conf
blacklist_file=rtw88_8822bu.conf blacklist_file=rtw88_8822bu.conf
installed_blacklist_file=/etc/modprobe.d/$blacklist_file
installed_options_file=/etc/modprobe.d/$options_file
# Start End
Tmks=$'\e[42;37;1m' Tmke=$'\e[22;39;49m' # Menu-Key
Twas=$'\e[33m' Twae=$'\e[39m' # WArning
Tins=$'\e[1m' Tine=$'\e[22m' # INstruction
Tdr=$'\e[47m\e[K\e[49m' # Diff-Ruler
Tcl=$'\r\e[K' # Clear-Line
# check to ensure sudo was used # check to ensure sudo was used
if (( EUID != 0 )) if (( EUID != 0 ))
then then
printf 'You must run this script with superuser (root) privileges.\n' printf '%sYou must run this script with superuser (root) privileges.%s\n' "$Twas" "$Twae"
printf 'Try: "sudo %s"\n' "$0" printf 'Try: "%ssudo %s%s"\n' "$Tins" "$0" "$Tine"
exit 1 exit 1
fi fi
@ -22,6 +34,7 @@ no_prompt=0
no_clean=0 no_clean=0
make_opts=() make_opts=()
make_build_opts=() make_build_opts=()
no_build=0
# get the options # get the options
for ((;$#;)) do for ((;$#;)) do
@ -34,6 +47,9 @@ for ((;$#;)) do
make_build_opts+=( "$1" ) ;; make_build_opts+=( "$1" ) ;;
-j) -j)
make_build_opts+=( "${@:1:2}" ) ; shift ;; # include second arg if present make_build_opts+=( "${@:1:2}" ) ; shift ;; # include second arg if present
-n|--dry-run|\
--nb|--no-build)
no_build=1 ;;
-h|--help|*) -h|--help|*)
cat <<- EndOfHelp cat <<- EndOfHelp
Usage: $0 [--no-prompt|-y] [--no-clean|-d] [--jobs[=N]|-jN] Usage: $0 [--no-prompt|-y] [--no-clean|-d] [--jobs[=N]|-jN]
@ -49,8 +65,63 @@ for ((;$#;)) do
shift shift
done done
# Install a config file, using a similar method to dpkg -i
managed_file_installation() {
local src=$1 installed=$2 desc=$3 old
if [[ -e $installed ]]
then
old=
else
old=/tmp/$$-$src
trap 'rm -f "$old"' EXIT
cp -Tf "$installed" "$old"
fi
while :
do
Kcont=XX Kdiff=XX Kedit=XX Krevt=XX Kinst=XX Kvimd=XX # any value that can't match a single byte
if [[ ! -e $installed ]]
then
menu=( 'Install new' Quit )
Kinst=i
else
menu=( Diff Edit Vimdiff Quit )
Kcont=c Kedit=e Kvimd=v
if [[ $old && -s $old ]] && ! cmp "$src" "$old"
then
menu=( 'Revert to old' "${menu[@]}" )
Krevt=r
fi
if d=$( diff "$src" "$installed" )
then
menu=( 'Continue' "${menu[@]}" )
else
menu=( 'Continue with existing' 'Install new' "${menu[@]}" )
Kdiff=d Kinst=i
printf '\n%sExisting %s file differs from the package default%s\n' "$Twas" "$desc" "$Twae"
fi
fi
printf '\nChoose:'
for c in "${menu[@]}" ; do printf ' %s' "$Tmks${c:0:1}$Tmke${c:1}" ; done
printf '? '
read -rs -N1 key || exit
printf %s "$Tcl"
case ${key,,} in
[qx]) exit ;;
$Kcont) break ;;
$Kdiff) printf '%s\n%s\n%s' "$Tdr" "$d" "$Tdr" ;;
$Kedit) ${EDITOR:-nano} "$installed" ;;
$Kinst) cp -Tfv "$src" "$installed" ;;
$Krevt) cp -Tfv "$old" "$installed" ;;
$Kvimd) vimdiff "$installed" "$src" ;;
esac
done
rm -f "$old" ; trap - EXIT
}
# information that helps with bug reports # information that helps with bug reports
clear
# displays script name and version # displays script name and version
printf 'Running %s version %s\n' "${0##*/}" "$SCRIPT_VERSION" printf 'Running %s version %s\n' "${0##*/}" "$SCRIPT_VERSION"
@ -62,54 +133,68 @@ uname -m
printf 'Starting installation...\n' printf 'Starting installation...\n'
# sets module parameters (driver options) if (( no_build ))
# blacklist the in-kernel module (driver) so that there is no conflict then
printf 'Copying options and blacklist files into /etc/modprobe.d\n' printf 'Build & install skipped.\n'
cp -fv "$options_file" "$blacklist_file" /etc/modprobe.d else
(( no_clean )) ||
make "${make_opts[@]}" clean
(( no_clean )) || make "${make_opts[@]}" "${make_build_opts[@]}" || {
make "${make_opts[@]}" clean status=$?
printf 'An error occurred. Error = %d\n' "$status"
printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ %ssudo %s%s\n' "$Tins" "${0/install/remove}" "$Tine"
exit "$status"
}
make "${make_opts[@]}" "${make_build_opts[@]}" || { make "${make_opts[@]}" install || {
status=$? status=$?
printf 'An error occurred. Error = %d\n' "$status" printf 'An error occurred. Error = %d\n' "$status"
printf 'Please report this error.\n' printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n' printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n' printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo ./remove-driver-no-dkms.sh\n' printf '$ %ssudo %s%s\n' "$Tins" "${0/install/remove}" "$Tine"
exit "$status" exit "$status"
} }
make "${make_opts[@]}" install || { printf 'The driver was installed successfully.\n'
status=$? fi
printf 'An error occurred. Error = %d\n' "$status"
printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo %s\n' "${*:0}"
exit "$status"
}
printf 'The driver was installed successfully.\n'
# unblock wifi # unblock wifi
rfkill unblock wlan rfkill unblock wlan
# Blacklist the in-kernel module (driver) so that there is no conflict
printf 'Installing module blacklist as %s\n' "$installed_blacklist_file"
cp -Tfv "$blacklist_file" "$installed_blacklist_file"
# Set module parameters (driver options)
# Install default if config file doesn't already exist, or overwrite it if
# non-interactive.
if (( no_prompt )) || [[ ! -e $installed_options_file ]]
then
printf 'Installing default options file as %s\n' "$installed_options_file"
cp -Tfv "$options_file" "$installed_options_file"
fi
# if NoPrompt is not used, ask user some questions to complete installation # if NoPrompt is not used, ask user some questions to complete installation
if (( ! no_prompt )) if (( ! no_prompt ))
then then
read -p 'Do you want to edit the driver options file now? [y/N] ' -n 1 -r || exit 1 managed_file_installation "$options_file" "$installed_options_file" 'Driver options'
printf '\n'
if [[ $REPLY = [Yy] ]]
then
${EDITOR:-nano} "/etc/modprobe.d/$options_file"
fi
read -p 'Do you want to reboot now? (recommended) [y/N] ' -n 1 -r || exit 1 read -p 'Do you want to reboot now? (recommended) [r/N] ' -n 1 -r || exit 1
printf '\n' printf %s "$Tcl"
if [[ $REPLY = [Yy] ]] if [[ $REPLY = [Rr] ]]
then then
reboot read -p 'Confirm reboot? [c/N] ' -n1 -r || exit 1
printf %s "$Tcl"
if [[ $REPLY = [Cc] ]]
then
reboot
fi
fi fi
fi fi

View File

@ -1,18 +1,45 @@
#!/bin/bash #!/bin/bash
# Purpose: Install Realtek USB WiFi adapter drivers. # Purpose: Build and Install Realtek USB WiFi adapter drivers.
# #
# This version of the installation script uses dkms. # Use Dynamic Kernel Module Support (DKMS) to build and install the driver.
SCRIPT_VERSION=20220923 SCRIPT_VERSION=20220925
options_file=88x2bu.conf options_file=88x2bu.conf
blacklist_file=rtw88_8822bu.conf blacklist_file=rtw88_8822bu.conf
drv_name=rtl88x2bu drv_name=rtl88x2bu
drv_version=5.13.1 drv_version=5.14.0
drv_dir=$PWD installed_blacklist_file=/etc/modprobe.d/$blacklist_file
installed_options_file=/etc/modprobe.d/$options_file
installed_driver=/usr/src/$drv_name-$drv_version
# Start End
Tmks=$'\e[42;37;1m' Tmke=$'\e[22;39;49m' # Menu-Key
Twas=$'\e[33m' Twae=$'\e[39m' # WArning
Tins=$'\e[1m' Tine=$'\e[22m' # INstruction
Tdr=$'\e[47m\e[K\e[49m' # Diff-Ruler
Tcl=$'\r\e[K' # Clear-Line
# check to ensure sudo was used
if (( EUID != 0 ))
then
printf '%sYou must run this script with superuser (root) privileges.%s\n' "$Twas" "$Twae"
printf 'Try: "%ssudo %s%s"\n' "$Tins" "$0" "$Tine"
exit 1
fi
# check for previous installation
if [[ -d "$installed_driver" ]]
then
printf 'It appears that this driver may already be installed.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '%s$ sudo ./remove-driver.sh\%sn'
exit 1
fi
# support for NoPrompt allows non-interactive use of this script # support for NoPrompt allows non-interactive use of this script
no_prompt=0 no_prompt=0
@ -27,6 +54,9 @@ for ((;$#;)) do
no_clean=1 ;; no_clean=1 ;;
-j*) -j*)
printf '(dkms build ignores "%s" option)\n' "$1" ;; printf '(dkms build ignores "%s" option)\n' "$1" ;;
-n|--dry-run|\
--nb|--no-build)
no_build=1 ;;
-h|--help|*) -h|--help|*)
cat <<- EndOfHelp cat <<- EndOfHelp
Usage: $0 [--no-prompt|-y] [--no-clean|-d] Usage: $0 [--no-prompt|-y] [--no-clean|-d]
@ -42,22 +72,58 @@ for ((;$#;)) do
shift shift
done done
# check to ensure sudo was used # Install a config file, using a similar method to dpkg -i
if (( EUID != 0 )) managed_file_installation() {
then local src=$1 installed=$2 desc=$3 old
printf 'You must run this script with superuser (root) privileges.\n' if [[ -e $installed ]]
printf 'Try: "sudo %s"\n' "$0" then
exit 1 old=
fi else
old=/tmp/$$-$src
# check for previous installation trap 'rm -f "$old"' EXIT
if [[ -d "/usr/src/$drv_name-$drv_version" ]] cp -Tf "$installed" "$old"
then fi
printf 'It appears that this driver may already be installed.\n' while :
printf 'You will need to run the following before reattempting installation.\n' do
printf '$ sudo ./remove-driver.sh\n' Kcont=XX Kdiff=XX Kedit=XX Krevt=XX Kinst=XX Kvimd=XX # any value that can't match a single byte
exit 1 if [[ ! -e $installed ]]
fi then
menu=( 'Install new' Quit )
Kinst=i
else
menu=( Diff Edit Vimdiff Quit )
Kcont=c Kedit=e Kvimd=v
if [[ $old && -s $old ]] && ! cmp "$src" "$old"
then
menu=( 'Revert to old' "${menu[@]}" )
Krevt=r
fi
if d=$( diff "$src" "$installed" )
then
menu=( 'Continue' "${menu[@]}" )
else
menu=( 'Continue with existing' 'Install new' "${menu[@]}" )
Kdiff=d Kinst=i
printf '\n%sExisting %s file differs from the package default%s\n' "$Twas" "$desc" "$Twae"
fi
fi
printf '\nChoose:'
for c in "${menu[@]}" ; do printf ' %s' "$Tmks${c:0:1}$Tmke${c:1}" ; done
printf '? '
read -rs -N1 key || exit
printf '\r\e[K'
case ${key,,} in
[qx]) exit ;;
$Kcont) break ;;
$Kdiff) printf '%s\n%s\n%s' "$Tdr" "$d" "$Tdr" ;;
$Kedit) ${EDITOR:-nano} "$installed" ;;
$Kinst) cp -Tfv "$src" "$installed" ;;
$Krevt) cp -Tfv "$old" "$installed" ;;
$Kvimd) vimdiff "$installed" "$src" ;;
esac
done
rm -f "$old" ; trap - EXIT
}
# information that helps with bug reports # information that helps with bug reports
@ -74,68 +140,81 @@ uname -m
printf 'Starting installation...\n' printf 'Starting installation...\n'
# the add command requires source in "/usr/src/$drv_name-$drv_version" if (( no_build ))
printf 'Copying source files to: %s\n' "/usr/src/$drv_name-$drv_version" then
cp -rf "$drv_dir" "/usr/src/$drv_name-$drv_version" printf 'Build & install skipped.\n'
else
# the add command requires source in "/usr/src/$drv_name-$drv_version"
printf 'Copying source files into %s\n' "$installed_driver/"
cp -rf "./" "$installed_driver/"
(( no_clean )) ||
find "$installed_driver" '(' -name '*.o' -o -name '*.ko' -o -name '*.o.cmd' ')' -delete
(( no_clean )) || dkms add -m "$drv_name" -v "$drv_version" || {
find "/usr/src/$drv_name-$drv_version" '(' -name '*.o' -o -name '*.ko' -o -name '*.o.cmd' ')' -delete status=$?
printf 'An error occurred. dkms add error = %d\n' "$status"
printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo ./remove-driver.sh\n'
exit "$status"
}
# sets module parameters (driver options) dkms build -m "$drv_name" -v "$drv_version" || {
# blacklist the in-kernel module (driver) so that there is no conflict status=$?
printf 'Copying options and blacklist files into: %s\n' "/etc/modprobe.d" printf 'An error occurred. dkms build error = %d\n' "$status"
cp -fv "$options_file" "$blacklist_file" /etc/modprobe.d printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ %ssudo %s%s\n' "$Tins" "${0/install/remove}" "$Tine"
exit "$status"
}
dkms add -m "$drv_name" -v "$drv_version" || { dkms install -m "$drv_name" -v "$drv_version" || {
status=$? status=$?
printf 'An error occurred. dkms add error = %d\n' "$status" printf 'An error occurred. dkms install error = %d\n' "$status"
printf 'Please report this error.\n' printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n' printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n' printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo ./remove-driver.sh\n' printf '$ %ssudo %s%s\n' "$Tins" "${0/install/remove}" "$Tine"
exit "$status" exit "$status"
} }
dkms build -m "$drv_name" -v "$drv_version" || { printf 'The driver was installed successfully.\n'
status=$? fi
printf 'An error occurred. dkms build error = %d\n' "$status"
printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo ./remove-driver.sh\n'
exit "$status"
}
dkms install -m "$drv_name" -v "$drv_version" || {
status=$?
printf 'An error occurred. dkms install error = %d\n' "$status"
printf 'Please report this error.\n'
printf 'Please copy all screen output and paste it into the report.\n'
printf 'You will need to run the following before reattempting installation.\n'
printf '$ sudo ./remove-driver.sh\n'
exit "$status"
}
printf 'The driver was installed successfully.\n'
# unblock wifi # unblock wifi
rfkill unblock wlan rfkill unblock wlan
# Blacklist the in-kernel module (driver) so that there is no conflict
printf 'Installing module blacklist as %s\n' "$installed_blacklist_file"
cp -Tfv "$blacklist_file" "$installed_blacklist_file"
# Set module parameters (driver options)
# Install default if config file doesn't already exist, or overwrite it if
# non-interactive.
if (( no_prompt )) || [[ ! -e $installed_options_file ]]
then
printf 'Installing default options file as %s\n' "$installed_options_file"
cp -Tfv "$options_file" "$installed_options_file"
fi
# if NoPrompt is not used, ask user some questions to complete installation # if NoPrompt is not used, ask user some questions to complete installation
if (( ! no_prompt )) if (( ! no_prompt ))
then then
read -p 'Do you want to edit the driver options file now? [y/N] ' -n 1 -r || exit 1 managed_file_installation "$options_file" "$installed_options_file" 'Driver options'
printf '\n'
if [[ $REPLY = [Yy] ]]
then
${EDITOR:-nano} "/etc/modprobe.d/$options_file"
fi
read -p 'Do you want to reboot now? (recommended) [y/N] ' -n 1 -r || exit 1 read -p 'Do you want to reboot now? (recommended) [r/N] ' -n 1 -r || exit 1
printf '\n' printf %s "$Tcl"
if [[ $REPLY = [Yy] ]] if [[ $REPLY = [Rr] ]]
then then
reboot read -p 'Confirm reboot? [c/N] ' -n1 -r || exit 1
printf %s "$Tcl"
if [[ $REPLY = [Cc] ]]
then
reboot
fi
fi fi
fi fi