Merge pull request #163 from Snawoot/opmodes

Automation-friendly patch.sh
This commit is contained in:
Snawoot 2019-09-28 00:17:57 +03:00 committed by GitHub
commit 8efe984c2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 118 additions and 56 deletions

View File

@ -49,14 +49,17 @@ Requirements:
# bash ./patch.sh -h # bash ./patch.sh -h
SYNOPSIS SYNOPSIS
patch.sh [OPTION]... patch.sh [-s] [-r|-h|-c VERSION|-l]
DESCRIPTION DESCRIPTION
The patch for Nvidia drivers to increase encoder sessions The patch for Nvidia drivers to remove NVENC session limit
-s Silent mode (No output) -s Silent mode (No output)
-r Rollback to original (Restore lib from backup) -r Rollback to original (Restore lib from backup)
-h Print this help message -h Print this help message
-c VERSION Check if version VERSION supported by this patch.
Returns true exit code (0) if version is supported.
-l List supported driver versions
``` ```

View File

@ -6,28 +6,35 @@ 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|-c VERSION|-l]
DESCRIPTION DESCRIPTION
The patch for Nvidia drivers to increase encoder sessions The patch for Nvidia drivers to remove NVENC session limit
-s Silent mode (No output) -s Silent mode (No output)
-r Rollback to original (Restore lib from backup) -r Rollback to original (Restore lib from backup)
-h Print this help message -h Print this help message
-c VERSION Check if version VERSION supported by this patch.
Returns true exit code (0) if version is supported.
-l List supported driver versions
' '
} }
while getopts 'rsh' flag; do # shellcheck disable=SC2209
opmode="patch"
while getopts 'rshc:l' 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 ;; c) opmode="${opmode}checkversion" ; checked_version="$OPTARG" ;;
l) opmode="${opmode}listversions" ;;
*) echo "Incorrect option specified in command line" ; exit 2 ;;
esac esac
done done
@ -105,22 +112,37 @@ declare -A object_list=(
["435.21"]='libnvcuvid.so' ["435.21"]='libnvcuvid.so'
) )
NVIDIA_SMI="$(command -v nvidia-smi)" check_version_supported () {
local ver="$1"
[[ "${patch_list[$ver]+isset}" && "${object_list[$ver]+isset}" ]]
}
get_supported_versions () {
for drv in "${!patch_list[@]}"; do
[[ "${object_list[$drv]+isset}" ]] && echo "$drv"
done | sort -t. -n
return 0
}
patch_common () {
NVIDIA_SMI="$(command -v nvidia-smi || true)"
if [[ ! "$NVIDIA_SMI" ]] ; then
echo 'nvidia-smi utility not found. Probably driver is not installed.'
exit 1
fi
if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then if ! driver_version=$("$NVIDIA_SMI" --query-gpu=driver_version --format=csv,noheader,nounits | head -n 1) ; then
echo 'Something went wrong. Check nvidia driver' echo 'Something went wrong. Check nvidia driver'
exit 1; exit 1
fi fi
echo "Detected nvidia driver version: $driver_version" echo "Detected nvidia driver version: $driver_version"
if [[ ! "${patch_list[$driver_version]+isset}" || ! "${object_list[$driver_version]+isset}" ]]; then if ! check_version_supported "$driver_version" ; then
echo "Patch for this ($driver_version) nvidia driver not found." 1>&2 echo "Patch for this ($driver_version) nvidia driver not found."
echo "Available patches for: " 1>&2 echo "Patch is available for versions: "
for drv in "${!patch_list[@]}"; do get_supported_versions
echo "$drv" 1>&2 exit 1
done
exit 1;
fi fi
patch="${patch_list[$driver_version]}" patch="${patch_list[$driver_version]}"
@ -143,18 +165,31 @@ done
[[ "$dir_found" ]] || { echo "ERROR: cannot detect driver directory"; exit 1; } [[ "$dir_found" ]] || { echo "ERROR: cannot detect driver directory"; exit 1; }
if [[ $rollback_flag ]]; then }
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
}
patch () {
patch_common
if [[ -f "$backup_path/$object.$driver_version" ]]; then
bkp_hash="$(sha1sum "$backup_path/$object.$driver_version" | cut -f1 -d\ )"
drv_hash="$(sha1sum "$driver_dir/$object.$driver_version" | cut -f1 -d\ )"
if [[ "$bkp_hash" != "$drv_hash" ]] ; then
echo "Backup exists and driver file differ from backup. Skipping patch."
return 0
fi fi
else else
if [[ ! -f "$backup_path/$object.$driver_version" ]]; then echo "Attention! Backup not found. Copying current $object to backup."
echo "Attention! Backup not found. Copy current $object to backup."
mkdir -p "$backup_path" mkdir -p "$backup_path"
cp -p "$driver_dir/$object.$driver_version" \ cp -p "$driver_dir/$object.$driver_version" \
"$backup_path/$object.$driver_version" "$backup_path/$object.$driver_version"
@ -165,4 +200,28 @@ 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!"
}
query_version_support () {
if check_version_supported "$checked_version" ; then
echo "SUPPORTED"
exit 0
else
echo "NOT SUPPORTED"
exit 1
fi fi
}
list_supported_versions () {
get_supported_versions
}
case "${opmode}" in
patch) patch ;;
patchrollback) rollback ;;
patchhelp) print_usage ; exit 2 ;;
patchcheckversion) query_version_support ;;
patchlistversions) list_supported_versions ;;
*) echo "Incorrect combination of flags. Use option -h to get help."
exit 2 ;;
esac