/
nvidia-installer-update-db
executable file
·195 lines (168 loc) · 6.63 KB
/
nvidia-installer-update-db
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
#
# Updates NVIDIA database for nvidia-installer and nvidia-installer-dkms.
#
# https://www.nvidia.com/en-us/drivers/unix/
# https://download.nvidia.com/XFree86/Linux-x86_64/435.21/README/supportedchips.html
# https://download.nvidia.com/XFree86/Linux-x86_64/435.21/README/index.html
GetNvidiaBranch() {
local string="$1"
local series="$2"
local data="$(wget -q --timeout=10 -O- https://www.nvidia.com/en-us/drivers/unix)"
test -n "$data" || DIE "$FUNCNAME: fetching branch '$string' failed."
if [ -n "$series" ] ; then
echo "$data" | grep "$string" | grep "$series" | head -n 1 | sed 's|^.*">\([0-9\.]*\)<.*$|\1|'
else
echo "$data" | grep "$string" | head -n 1 | sed 's|^.*">\([0-9\.]*\)<.*$|\1|'
fi
}
GetNvidiaIds() {
local branch="$1"
local ids="$(wget -q --timeout=10 -O- https://download.nvidia.com/XFree86/Linux-x86_64/$branch/README/supportedchips.html)"
test -n "$ids" || DIE "$FUNCNAME: fetching ids from branch '$branch' failed."
ids="$(echo "$ids" | sed -n '1,/.*NVIDIA Tesla GPUs.*/p' | grep "</td>" | grep "<td>")"
ids="$(echo "$ids" | grep -P "<td>[0-9A-F][0-9A-F][0-9A-F ]*</td>" | sed -e 's|<td>||' -e 's|</td>||')"
ids="$(echo "$ids" | tr '[:upper:]' '[:lower:]')"
echo "$ids"
}
NvidiaIdsCommon() {
local string="$1"
local series="$2"
local branch="$(GetNvidiaBranch "$string" "$series")"
test -n "$branch" || DIE "$FUNCNAME: GetNvidiaBranch failed."
local ids="$(GetNvidiaIds "$branch")"
test -n "$ids" || DIE "$FUNCNAME: GetNvidiaIds failed."
Info "$string: $branch"
echo "$ids"
}
NvidiaIdsLatest() {
local ids="$(NvidiaIdsCommon "Latest Long Lived Branch")"
if [ -z "$ids" ] ; then
ids="$(NvidiaIdsCommon "Latest Short Lived Branch")"
if [ -z "$ids" ] ; then
DIE "$FUNCNAME: NvidiaIdsCommon failed."
fi
fi
echo "$ids"
}
NvidiaIdsLegacy() {
local series="$legacy_series.xx series"
local ids="$(NvidiaIdsCommon "Latest Legacy GPU version" "$series")"
test -n "$ids" || DIE "$FUNCNAME: NvidiaIdsCommon failed for Legacy $series."
echo "$ids"
}
echo2() { echo "$@" >&2 ; }
DIE() { echo2 "Error: $1" ; exit 1 ; }
Info() { test "$show_additional_info" = "yes" && echo2 "Info: $@" ; }
Options() {
local arg
for arg in "$@" ; do
case "$arg" in
-v | --verbose)
show_additional_info=yes ;;
--tmpdb)
targetdir="/tmp" ;;
-*)
DIE "unsupported option '$arg'" ;;
*)
DIE "unsupported parameter '$arg'" ;;
esac
done
}
ExeEnv() {
case "$(echo "${BASH_SOURCE[@]}")" in
*rogalmic.bash-debug*) exe_env=vscode ;;
*/usr/bin/bashdb*) exe_env=bashdb ;;
*) exe_env=run ;;
esac
}
Logging() {
local newfile="$1"
local currfile="$2"
local branch="$3"
local xx="$(diff $newfile $currfile)"
if [ -n "$xx" ] ; then
printf "\nDiffs for the $branch branch:\n" >> $log
echo "$xx" >> $log
has_logs=yes
fi
}
CreateNvidiaDb()
{
local PROGNAME=nvidia-installer-update-db
local exe_env=""
local show_additional_info=no # "no" is the default, "yes" is TESTING!
local targetdir=/var/lib/pci # db directory
Options "$@"
local legacy_series="390" # this may change some day!
local legacy_available=yes # -"-
local target_latest=$targetdir/nvidia.ids # db (list of ids) for driver type 'nvidia'
local target_legacy=$targetdir/nvidia-${legacy_series}xx.ids # db (list of ids) for legacy driver type 'nvidia-NNNxx'
local bakdir=$targetdir/ORIG.BAK # original db is stored into this directory, if it exists
local nvidia_ids_latest
local nvidia_ids_legacy
local tmpfile1
local tmpfile2
local sudo_commands=":" # collect required root commands here
local readme="NVIDIA db generated by $PROGNAME at $(date +%Y-%m-%d)."
local xx
local log=$(mktemp /tmp/$PROGNAME.log.XXXXX)
local has_logs=no
# check that legacy driver is available either in the repos or AUR
if [ -z "$(yay -S -s nvidia -s dkms -s "$legacy_series"xx -q)" ] ; then
legacy_available=no # not available
fi
echo2 "Fetching NVIDIA graphics card ids from nvidia.com ..."
nvidia_ids_latest="$(NvidiaIdsLatest)"
test -n "$nvidia_ids_latest" || DIE "cannot fetch latest NVIDIA ids"
if [ "$legacy_available" = "yes" ] ; then
nvidia_ids_legacy="$(NvidiaIdsLegacy)"
test -n "$nvidia_ids_legacy" || DIE "cannot fetch legacy NVIDIA Legacy $legacy_series.xx ids"
fi
# Data is fetched from nvidia.com.
# Check if anything has changed.
tmpfile1=$(mktemp)
echo "$nvidia_ids_latest" | awk '{print $1}' | sort | uniq > $tmpfile1
Logging "$tmpfile1" "$target_latest" "Latest"
if [ "$legacy_available" = "yes" ] ; then
tmpfile2=$(mktemp)
echo "$nvidia_ids_legacy" | awk '{print $1}' | sort | uniq > $tmpfile2
Logging "$tmpfile2" "$target_legacy" "Legacy"
fi
if [ "$has_logs" = "no" ] ; then
rm -f $log
echo "Database is already up-to-date." >&2
if [ "$targetdir" != "/tmp" ] ; then
test -r $tmpfile1 && rm -f $tmpfile1
test -r $tmpfile2 && rm -f $tmpfile2
return
fi
fi
# Now save the data into the local database.
sudo_commands+=" ; mkdir -p $targetdir"
sudo_commands+=" ; if [ ! -d $bakdir ] ; then mkdir -p $bakdir ; cp -a $targetdir/*.ids $bakdir >& /dev/null ; fi"
sudo_commands+=" ; mv $tmpfile1 $target_latest"
if [ "$targetdir" != "/tmp" ] ; then
sudo_commands+=" ; chown root:root $target_latest"
fi
sudo_commands+=" ; chmod 0644 $target_latest"
if [ "$legacy_available" = "yes" ] ; then
sudo_commands+=" ; mv $tmpfile2 $target_legacy"
if [ "$targetdir" != "/tmp" ] ; then
sudo_commands+=" ; chown root:root $target_legacy"
fi
sudo_commands+=" ; chmod 0644 $target_legacy"
else
sudo_commands+=" ; if [ -r $target_legacy ] ; then mv -f $target_legacy $target_legacy.unsupported ; fi"
fi
sudo_commands+=" ; echo $readme > $targetdir/README.txt"
echo2 "Adding NVIDIA graphics card ids to the local database."
if [ "$targetdir" = "/tmp" ] ; then
# temporary files used, no sudo needed
bash -c "$sudo_commands"
else
# Now we need elevated privileges.
sudo bash -c "$sudo_commands"
fi
}
CreateNvidiaDb "$@" # program starts here