mirror of
https://github.com/keylase/nvidia-patch.git
synced 2024-11-23 05:57:21 +00:00
linux: refactor patch.sh in order to add new operation modes
This commit is contained in:
parent
9d889d6371
commit
f25b0fd056
108
patch.sh
108
patch.sh
@ -6,11 +6,10 @@ set -euo pipefail ; # <- this semicolon and comment make options apply
|
|||||||
|
|
||||||
backup_path="/opt/nvidia/libnvidia-encode-backup"
|
backup_path="/opt/nvidia/libnvidia-encode-backup"
|
||||||
silent_flag=''
|
silent_flag=''
|
||||||
rollback_flag=''
|
|
||||||
|
|
||||||
print_usage() { printf '
|
print_usage() { printf '
|
||||||
SYNOPSIS
|
SYNOPSIS
|
||||||
patch.sh [OPTION]...
|
patch.sh [-s] [-r|-h]
|
||||||
|
|
||||||
DESCRIPTION
|
DESCRIPTION
|
||||||
The patch for Nvidia drivers to increase encoder sessions
|
The patch for Nvidia drivers to increase encoder sessions
|
||||||
@ -22,12 +21,15 @@ DESCRIPTION
|
|||||||
'
|
'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2209
|
||||||
|
opmode="patch"
|
||||||
|
|
||||||
while getopts 'rsh' flag; do
|
while getopts 'rsh' flag; do
|
||||||
case "${flag}" in
|
case "${flag}" in
|
||||||
r) rollback_flag='true' ;;
|
r) opmode="${opmode}rollback" ;;
|
||||||
s) silent_flag='true' ;;
|
s) silent_flag='true' ;;
|
||||||
*) print_usage
|
h) opmode="${opmode}help" ;;
|
||||||
exit 1 ;;
|
*) echo "Incorrect option specified in command line" ; exit 2 ;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
@ -105,54 +107,65 @@ declare -A object_list=(
|
|||||||
["435.21"]='libnvcuvid.so'
|
["435.21"]='libnvcuvid.so'
|
||||||
)
|
)
|
||||||
|
|
||||||
NVIDIA_SMI="$(command -v nvidia-smi)"
|
patch_common () {
|
||||||
|
NVIDIA_SMI="$(command -v nvidia-smi || true)"
|
||||||
if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then
|
if [[ ! "$NVIDIA_SMI" ]] ; then
|
||||||
echo 'Something went wrong. Check nvidia driver'
|
echo 'nvidia-smi utility not found. Probably driver is not installed.'
|
||||||
exit 1;
|
exit 1
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Detected nvidia driver version: $driver_version"
|
|
||||||
|
|
||||||
if [[ ! "${patch_list[$driver_version]+isset}" || ! "${object_list[$driver_version]+isset}" ]]; then
|
|
||||||
echo "Patch for this ($driver_version) nvidia driver not found." 1>&2
|
|
||||||
echo "Available patches for: " 1>&2
|
|
||||||
for drv in "${!patch_list[@]}"; do
|
|
||||||
echo "$drv" 1>&2
|
|
||||||
done
|
|
||||||
exit 1;
|
|
||||||
fi
|
|
||||||
|
|
||||||
patch="${patch_list[$driver_version]}"
|
|
||||||
object="${object_list[$driver_version]}"
|
|
||||||
|
|
||||||
declare -a driver_locations=(
|
|
||||||
'/usr/lib/x86_64-linux-gnu'
|
|
||||||
'/usr/lib/x86_64-linux-gnu/nvidia/current/'
|
|
||||||
'/usr/lib64'
|
|
||||||
"/usr/lib/nvidia-${driver_version%%.*}"
|
|
||||||
)
|
|
||||||
|
|
||||||
dir_found=''
|
|
||||||
for driver_dir in "${driver_locations[@]}" ; do
|
|
||||||
if [[ -e "$driver_dir/$object.$driver_version" ]]; then
|
|
||||||
dir_found='true'
|
|
||||||
break
|
|
||||||
fi
|
fi
|
||||||
done
|
|
||||||
|
|
||||||
[[ "$dir_found" ]] || { echo "ERROR: cannot detect driver directory"; exit 1; }
|
if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then
|
||||||
|
echo 'Something went wrong. Check nvidia driver'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ $rollback_flag ]]; then
|
echo "Detected nvidia driver version: $driver_version"
|
||||||
|
|
||||||
|
if [[ ! "${patch_list[$driver_version]+isset}" || ! "${object_list[$driver_version]+isset}" ]]; then
|
||||||
|
echo "Patch for this ($driver_version) nvidia driver not found." 1>&2
|
||||||
|
echo "Available patches for: " 1>&2
|
||||||
|
for drv in "${!patch_list[@]}"; do
|
||||||
|
echo "$drv" 1>&2
|
||||||
|
done
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
patch="${patch_list[$driver_version]}"
|
||||||
|
object="${object_list[$driver_version]}"
|
||||||
|
|
||||||
|
declare -a driver_locations=(
|
||||||
|
'/usr/lib/x86_64-linux-gnu'
|
||||||
|
'/usr/lib/x86_64-linux-gnu/nvidia/current/'
|
||||||
|
'/usr/lib64'
|
||||||
|
"/usr/lib/nvidia-${driver_version%%.*}"
|
||||||
|
)
|
||||||
|
|
||||||
|
dir_found=''
|
||||||
|
for driver_dir in "${driver_locations[@]}" ; do
|
||||||
|
if [[ -e "$driver_dir/$object.$driver_version" ]]; then
|
||||||
|
dir_found='true'
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
[[ "$dir_found" ]] || { echo "ERROR: cannot detect driver directory"; exit 1; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
rollback () {
|
||||||
|
patch_common
|
||||||
if [[ -f "$backup_path/$object.$driver_version" ]]; then
|
if [[ -f "$backup_path/$object.$driver_version" ]]; then
|
||||||
cp -p "$backup_path/$object.$driver_version" \
|
cp -p "$backup_path/$object.$driver_version" \
|
||||||
"$driver_dir/$object.$driver_version"
|
"$driver_dir/$object.$driver_version"
|
||||||
echo "Restore from backup $object.$driver_version"
|
echo "Restore from backup $object.$driver_version"
|
||||||
else
|
else
|
||||||
echo "Backup not found. Try to patch first."
|
echo "Backup not found. Try to patch first."
|
||||||
exit 1;
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
}
|
||||||
|
|
||||||
|
patch () {
|
||||||
|
patch_common
|
||||||
if [[ ! -f "$backup_path/$object.$driver_version" ]]; then
|
if [[ ! -f "$backup_path/$object.$driver_version" ]]; then
|
||||||
echo "Attention! Backup not found. Copy current $object to backup."
|
echo "Attention! Backup not found. Copy current $object to backup."
|
||||||
mkdir -p "$backup_path"
|
mkdir -p "$backup_path"
|
||||||
@ -165,4 +178,11 @@ else
|
|||||||
sha1sum "${PATCH_OUTPUT_DIR-$driver_dir}/$object.$driver_version"
|
sha1sum "${PATCH_OUTPUT_DIR-$driver_dir}/$object.$driver_version"
|
||||||
ldconfig
|
ldconfig
|
||||||
echo "Patched!"
|
echo "Patched!"
|
||||||
fi
|
}
|
||||||
|
|
||||||
|
case "${opmode}" in
|
||||||
|
patch) patch ;;
|
||||||
|
patchrollback) rollback ;;
|
||||||
|
patchhelp) print_usage ; exit 2 ;;
|
||||||
|
*) echo "Incorrect combination of flags"; exit 2 ;;
|
||||||
|
esac
|
||||||
|
Loading…
Reference in New Issue
Block a user