-
Notifications
You must be signed in to change notification settings - Fork 2
/
Install_Cuda.sh
executable file
·224 lines (199 loc) · 7.02 KB
/
Install_Cuda.sh
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env -S -P /usr/local/bin:/usr/bin:${PATH} bash
cur_path=$(cd "$(dirname "$0")"; pwd)
cur_workdir=${cur_path}/Nvidia_CUDA
cur_sys=`cat /etc/*-release | sed -r "s/^ID=(.*)$/\\1/;tA;d;:A;s/^\"(.*)\"$/\\1/" | tr -d '\n'`
# Stop the script when any Error occur
set -e
# Functions
Color_Error='\E[1;31m'
Color_Success='\E[1;32m'
Color_Res='\E[0m'
function echo_error(){
echo -e "${Color_Error}${1}${Color_Res}"
}
function echo_success(){
echo -e "${Color_Success}${1}${Color_Res}"
}
function service_detect(){
tmp_status=`systemctl list-unit-files ${1} | sed -r "s/^${1}\s+(enabled|disabled|masked|static)\$/true/;tA;d;:A;" | tr -d '\n'`
printf ${tmp_status:-false}
}
function modules_detect(){
tmp_status=`lsmod | sed -r "s/^${1}\s+[0-9]+\s+[0-9]+.*\$/true/;tA;d;:A;" | tr -d '\n'`
printf ${tmp_status:-false}
}
function runlevel_detect(){
tmp_status=`runlevel | sed -r "s/^[N0-6]\s+${1}\$/true/;tA;d;:A;" | tr -d '\n'`
printf ${tmp_status:-false}
}
function default_detect(){
tmp_status=`systemctl get-default | sed -r "s/^(graphical.target)\$/true/;tA;d;:A;" | tr -d '\n'`
printf ${tmp_status:-false}
}
# Vshell Detect
VShell=${WINDOWID:+true}
if ${VShell:=false}; then
echo_error "VShell can't complete the installtion"
echo_success 'Switch the terminal by `Ctrl + Alt + F1~F6`'
exit 1
fi
CUDA_Kit_list=(
# CUDA
cuda_*_*_linux*
# CUDNN
cudnn-*.tgz
)
# Detect CUDA & CUDNN
echo_error "Detecting CUDA runtime"
for v in ${CUDA_Kit_list[@]}; do
if [ -e ${cur_workdir}/${v} ]; then
echo_success "Found -> [ $(basename ${cur_workdir}/${v}) ]"
else
if [ -e ${cur_workdir}/Download_Nvidia_Kit.sh ]; then
echo_error 'Try to Download CUDA & CUDNN Kit'
sudo bash ${cur_workdir}/Download_Nvidia_Kit.sh
else
echo_error 'Please make sure u had download CUDA & CUDNN Kit'
echo_success "Problem could be solved by reading ${cur_workdir}/Readme.md"
echo_error "${cur_workdir}/${v} Needed"
exit 1
fi
fi
done
echo_success "Detecting CUDA runtime : [ Done ]"
# Detect Desktop Service
echo_error "Detecting Graphical Service"
Desktop_Service=false
case ${cur_sys} in
"ubuntu")
Desktop_Service=`service_detect lightdm.service`
if ${Desktop_Service}; then
sudo systemctl stop lightdm.service
fi
;;
"centos")
Desktop_Service=`service_detect gdm.service`
if ${Desktop_Service}; then
sudo systemctl stop gdm.service
fi
;;
esac
echo_success "Detecting Graphical Service : [ ${Desktop_Service} ]"
# Disable the Nouveau
echo_error "Detecting Nouveau module"
Nouveau_Blacklist_Root=/etc/modprobe.d/nvidia-installer-disable-nouveau.conf
var_auto_reboot=`modules_detect "nouveau"`
echo_success "Detecting Nouveau module : [ ${var_auto_reboot} ]"
if ${var_auto_reboot}; then
echo '# generated by nvidia-installer' > ${Nouveau_Blacklist_Root}
echo 'blacklist nouveau' >> ${Nouveau_Blacklist_Root}
echo 'options nouveau modeset=0' >> ${Nouveau_Blacklist_Root}
case ${cur_sys} in
"ubuntu")
# Update Source
sudo apt-get update
# Fixed missing kernel 4.13+ firmware issue
sudo apt-get install --reinstall linux-firmware
sudo update-initramfs -u
;;
"centos")
sudo sed -ri "s/^\s*#?(\s*blacklist\s+nvidiafb)\$/#\\1/g" /usr/lib/modprobe.d/dist-blacklist.conf
sudo dracut --force
;;
esac
fi
# Install Basic Env
echo_error "Installing OS Env"
case ${cur_sys} in
"ubuntu")
sudo apt-get install -y dkms linux-headers-$(uname -r) build-essential cmake
;;
"centos")
sudo yum install -y epel-release
sudo yum install -y dkms kernel-headers-$(uname -r) kernel-devel-$(uname -r) cmake
;;
esac
echo_success "Installing OS Env : [ Done ]"
# Nvidia Driver Detect
echo_error "Detecting Nvidia module"
var_nvidia_exist=`modules_detect nvidia`
echo_success "Detecting Nvidia module : [ ${var_nvidia_exist} ]"
# CUDA Install
if ${var_nvidia_exist}; then
sudo bash ${cur_workdir}/cuda_*_*_linux* --toolkit --override --silent
else
Nvidia_Driver=${cur_workdir}/NVIDIA-Linux-x86_64-*.*.run
if [ -e ${Nvidia_Driver} ]; then
if ! ${var_auto_reboot}; then
sudo bash ${Nvidia_Driver} --dkms --disable-nouveau --run-nvidia-xconfig --silent
echo_error "ReDetecting Nvidia module"
var_nvidia_exist=`modules_detect nvidia`
echo_success "ReDetecting Nvidia module : [ ${var_nvidia_exist} ]"
if ${var_nvidia_exist}; then
sudo bash ${cur_workdir}/cuda_*_*_linux* --toolkit --override --silent
fi
fi
else
sudo bash ${cur_workdir}/cuda_*_*_linux* --driver --no-opengl-libs --run-nvidia-xconfig --toolkit --override --silent
fi
fi
# Reboot was needed for disable nouveau
if ${var_auto_reboot}; then
echo_error "Please Rerun this script after reboot ! ! !"
read -n1 -sp "Press any key except 'ESC' to reboot: " var_ikey
case ${var_ikey:=*} in
$'\e')
echo_success "Reboot has been canceled"
;;
*)
echo_success "Rebooting"
sudo reboot now
;;
esac
else
# CUDA Env
CUDA_Profile_Root=/etc/profile.d/cuda.sh
CUDA_HOME=/usr/local/cuda
echo "export CUDA_HOME=${CUDA_HOME}" > ${CUDA_Profile_Root}
echo 'export PATH=$CUDA_HOME/bin:$PATH' >> ${CUDA_Profile_Root}
echo 'export LD_LIBRARY_PATH=$CUDA_HOME/lib64:$LD_LIBRARY_PATH' >> ${CUDA_Profile_Root}
echo 'export INCLUDE=$CUDA_HOME/include:$INCLUDE' >> ${CUDA_Profile_Root}
# Include PATH for gcc
echo 'export C_INCLUDE_PATH=$CUDA_HOME/include:$C_INCLUDE_PATH' >> ${CUDA_Profile_Root}
# Include PATH for g++
echo 'export CPLUS_INCLUDE_PATH=$CUDA_HOME/include:$CPLUS_INCLUDE_PATH' >> ${CUDA_Profile_Root}
source ${CUDA_Profile_Root}
# CUDA Conf Env
CUDA_Conf_Root=/etc/ld.so.conf.d/cuda.conf
echo '/usr/local/cuda/lib64' > ${CUDA_Conf_Root}
sudo ldconfig
# CUDNN Install
sudo tar -zxvf ${cur_workdir}/cudnn-*.tgz -C /usr/local
read -n1 -sp "Press any key except 'ESC' to launch desktop: " var_ikey
case ${var_ikey:=*} in
$'\e')
# Switch Default mode
if `default_detect`; then
sudo systemctl set-default multi-user.target
fi
echo_success "Desktop launch has been canceled"
;;
*)
case ${cur_sys} in
"ubuntu")
if ${Desktop_Service}; then
sudo systemctl set-default graphical.target
sudo systemctl start lightdm.service
fi
;;
"centos")
if ${Desktop_Service}; then
sudo systemctl set-default graphical.target
sudo systemctl start gdm.service
fi
;;
esac
;;
esac
echo_success "CUDA Ready"
fi