install_NEO_OCL_driver: Updated exit codes, messages. Updated way to remove old drive...
[platform/upstream/dldt.git] / scripts / install_dependencies / install_NEO_OCL_driver.sh
1 #!/bin/bash
2
3 # Copyright (c) 2018 - 2020 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 #
18 # Installs the Graphics Driver for OpenCL on Linux.
19 #
20 # Usage: sudo -E ./install_NEO_OCL_driver.sh
21 #
22 # Supported platforms:
23 #     6th, 7th, 8th or 9th generation Intel® processor with Intel(R)
24 #     Processor Graphics Technology not previously disabled by the BIOS
25 #     or motherboard settings
26 #
27 EXIT_FAILURE=1
28 PKGS=
29 CENTOS_MINOR=
30 UBUNTU_VERSION=
31 DISTRO=
32 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
33 INSTALL_DRIVER_VERSION='19.41.14441'
34
35 params=$@
36
37 _install_prerequisites_centos()
38 {
39     # yum doesn't accept timeout in seconds as parameter
40     echo
41     echo "Note: if yum becomes non-responsive, try aborting the script and run:"
42     echo "      sudo -E $0"
43     echo
44
45     CMDS=("yum -y install tar libpciaccess numactl-libs"
46           "yum -y groupinstall 'Development Tools'"
47           "yum -y install rpmdevtools openssl openssl-devel bc numactl ocl-icd ocl-icd-devel")
48
49     for cmd in "${CMDS[@]}"; do
50         echo $cmd
51         eval $cmd
52         if [[ $? -ne 0 ]]; then
53             echo ERROR: failed to run $cmd >&2
54             echo Problem \(or disk space\)? >&2
55             echo . Verify that you have enough disk space, and run the script again. >&2
56             exit $EXIT_FAILURE
57         fi
58     done
59
60 }
61
62 _install_prerequisites_ubuntu()
63 {
64     CMDS=("apt-get -y update"
65           "apt-get -y install libnuma1 ocl-icd-libopencl1")
66
67     for cmd in "${CMDS[@]}"; do
68         echo $cmd
69         eval $cmd
70         if [[ $? -ne 0 ]]; then
71             echo ERROR: failed to run $cmd >&2
72             echo Problem \(or disk space\)? >&2
73             echo "                sudo -E $0" >&2
74             echo 2. Verify that you have enough disk space, and run the script again. >&2
75             exit $EXIT_FAILURE
76         fi
77     done
78 }
79
80 install_prerequisites()
81 {
82     if [[ $DISTRO == "centos" ]]; then
83         echo Installing prerequisites...
84         _install_prerequisites_centos
85     elif [[ $DISTRO == "ubuntu" ]]; then
86         echo Installing prerequisites...
87         _install_prerequisites_ubuntu
88     else
89         echo Unknown OS
90     fi
91 }
92
93 _deploy_rpm()
94 {
95     # On a CentOS 7.2 machine with Intel Parallel Composer XE 2017
96     # installed we got conflicts when trying to deploy these rpms.
97     # If that happens to you too, try again with:
98     # IGFX_RPM_FLAGS="--force" sudo -E ./install_NEO_OCL_driver.sh install
99     #
100     cmd="rpm $IGFX_RPM_FLAGS -ivh --nodeps --force $1"
101     echo $cmd
102     eval $cmd
103 }
104
105 _deploy_deb()
106 {
107     cmd="dpkg -i $1"
108     echo $cmd
109     eval $cmd
110 }
111
112
113 _install_user_mode_centos()
114 {
115     _deploy_rpm "intel*.rpm"
116     if [[ $? -ne 0 ]]; then
117         echo ERROR: failed to install rpms $cmd error  >&2
118         echo Make sure you have enough disk space or fix the problem manually and try again. >&2
119         exit $EXIT_FAILURE
120     fi
121 }
122
123 _install_user_mode_ubuntu()
124 {
125     _deploy_deb "intel*.deb"
126     if [[ $? -ne 0 ]]; then
127         echo ERROR: failed to install rpms $cmd error  >&2
128         echo Make sure you have enough disk space or fix the problem manually and try again. >&2
129         exit $EXIT_FAILURE
130     fi
131 }
132
133
134 install_user_mode()
135 {
136     echo "Installing user mode driver..."
137     
138     if [[ $DISTRO == "centos" ]]; then
139         _install_user_mode_centos
140     else
141         _install_user_mode_ubuntu
142     fi
143     # exit from $SCRIPT_DIR/neo folder
144     cd -
145     # clean it up
146     rm -rf $SCRIPT_DIR/neo
147 }
148
149 _uninstall_user_mode_centos()
150 {
151     echo Looking for previously installed user-mode driver...
152     PACKAGES=("intel-opencl"
153            "intel-ocloc"
154            "intel-gmmlib"
155            "intel-igc-core"
156            "intel-igc-opencl")
157     for package in "${PACKAGES[@]}"; do      
158         echo "rpm -qa | grep $package"
159         found_package=$(rpm -qa | grep $package)
160         if [[ $? -eq 0 ]]; then
161             echo Found installed user-mode driver, performing uninstall...
162             cmd="rpm -e --nodeps ${found_package}"
163             echo $cmd
164             eval $cmd
165             if [[ $? -ne 0 ]]; then
166                 echo ERROR: failed to uninstall existing user-mode driver. >&2
167                 echo Please try again manually and run the script again. >&2
168                 exit $EXIT_FAILURE
169             fi
170         fi
171     done
172 }
173
174 _uninstall_user_mode_ubuntu()
175 {
176     echo Looking for previously installed user-mode driver...
177
178     PACKAGES=("intel-opencl"
179            "intel-ocloc"
180            "intel-gmmlib"
181            "intel-igc-core"
182            "intel-igc-opencl")
183
184     for package in "${PACKAGES[@]}"; do
185         apt remove -y $package
186         if [[ $? -ne 0 ]]; then
187             echo ERROR: failed to uninstall existing user-mode driver. >&2
188             echo Please try again manually and run the script again. >&2
189             exit $EXIT_FAILURE
190         fi
191     done
192 }
193
194 uninstall_user_mode()
195 {
196     if [[ $DISTRO == "centos" ]]; then
197         _uninstall_user_mode_centos
198     else
199         _uninstall_user_mode_ubuntu
200     fi
201 }
202
203 _is_package_installed()
204 {
205     if [[ $DISTRO == "centos" ]]; then
206         cmd="rpm -qa | grep $1"
207     else
208         cmd="dpkg-query -W -f='${binary:Package}\n' $pkg"
209     fi
210     echo $cmd
211     eval $cmd
212 }
213
214
215 _download_packages_ubuntu()
216 {
217     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/intel-gmmlib_19.3.2_amd64.deb
218     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/intel-igc-core_1.0.2597_amd64.deb
219     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/intel-igc-opencl_1.0.2597_amd64.deb
220     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/intel-opencl_19.41.14441_amd64.deb
221     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/intel-ocloc_19.41.14441_amd64.deb
222 }
223
224 _download_packages_centos()
225 {
226     wget -O intel-igc-core-1.0.2597-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-igc-core-1.0.2597-1.el7.x86_64.rpm/download
227     wget -O intel-opencl-19.41.14441-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-opencl-19.41.14441-1.el7.x86_64.rpm/download
228     wget -O intel-igc-opencl-devel-1.0.2597-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-igc-opencl-devel-1.0.2597-1.el7.x86_64.rpm/download
229     wget -O intel-igc-opencl-1.0.2597-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-igc-opencl-1.0.2597-1.el7.x86_64.rpm/download
230     wget -O intel-gmmlib-19.3.2-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-gmmlib-19.3.2-1.el7.x86_64.rpm/download
231     wget -O intel-gmmlib-devel-19.3.2-1.el7.x86_64.rpm https://sourceforge.net/projects/intel-compute-runtime/files/19.41.14441/centos-7/intel-gmmlib-devel-19.3.2-1.el7.x86_64.rpm/download
232 }
233
234
235 _verify_checksum_ubuntu()
236 {
237     wget https://github.com/intel/compute-runtime/releases/download/19.41.14441/ww41.sum
238     sha256sum -c ww41.sum
239 }
240
241 _verify_checksum_centos()
242 {
243     sha1sum -c $SCRIPT_DIR/neo_centos_ww41.sum
244 }
245
246 verify_checksum()
247 {
248     if [[ $DISTRO == "centos" ]]; then
249         _verify_checksum_centos
250     else
251         _verify_checksum_ubuntu
252     fi
253 }
254
255 download_packages()
256 {
257     mkdir -p $SCRIPT_DIR/neo
258     cd $SCRIPT_DIR/neo
259     
260     if [[ $DISTRO == "centos" ]]; then
261         _download_packages_centos
262     else
263         _download_packages_ubuntu
264     fi
265     verify_checksum
266     if [[ $? -ne 0 ]]; then
267         echo "ERROR: checksum do not match for downloaded packages"
268         echo "       Please verify your Internet connection and make sure you have enough disk space or fix the problem manually and try again "
269         exit $EXIT_FAILURE
270     fi
271 }
272
273
274 version_gt() {
275     # check if first version is greater than second version
276     test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
277 }
278
279 summary()
280 {
281     kernel_version=$(uname -r)
282
283     echo
284     echo Installation completed successfully.
285     echo
286     echo Next steps:
287     echo "Add OpenCL users to the video group: 'sudo usermod -a -G video USERNAME'"
288     echo "   e.g. if the user running OpenCL host applications is foo, run: sudo usermod -a -G video foo"
289     echo "   Current user has been already added to the video group"
290     echo
291
292     # ask to install kernel 4.14 if current kernel version < 4.13 (GPU NEO driver supports only kernels 4.13.x and higher)
293     if version_gt "4.13" "$kernel_version" ; then
294         echo "Install 4.14 kernel using install_4_14_kernel.sh script and reboot into this kernel"
295         echo
296     fi
297
298     echo "If you use 8th Generation Intel® Core™ processor, you will need to add:"
299     echo "   i915.alpha_support=1"
300     echo "   to the 4.14 kernel command line, in order to enable OpenCL functionality for this platform."
301     echo
302  
303 }
304
305 check_root_access()
306 {
307     if [[ $EUID -ne 0 ]]; then
308         echo "ERROR: you must run this script as root." >&2
309         echo "Please try again with "sudo -E $0", or as root." >&2
310         exit $EXIT_FAILURE
311     fi
312 }
313
314 add_user_to_video_group()
315 {
316     local real_user=$(logname 2>/dev/null || echo ${SUDO_USER:-${USER}})
317     echo
318     echo Adding $real_user to the video group...
319     usermod -a -G video $real_user
320     if [[ $? -ne 0 ]]; then
321         echo WARNING: unable to add $real_user to the video group >&2
322     fi
323 }
324
325 _check_distro_version()
326 {
327     if [[ $DISTRO == centos ]]; then
328         CENTOS_MINOR=$(sed 's/CentOS Linux release 7\.\([[:digit:]]\+\).\+/\1/' /etc/centos-release)
329         if [[ $? -ne 0 ]]; then
330             echo ERROR: failed to obtain CentOS version minor. >&2
331             echo This script is supported only on CentOS 7 and above. >&2
332             exit $EXIT_FAILURE
333         fi
334     elif [[ $DISTRO == ubuntu ]]; then
335         UBUNTU_VERSION=$(lsb_release -r -s) 
336         if [[ $UBUNTU_VERSION != '18.04' && $UBUNTU_VERSION != '20.04' ]]; then
337             echo "Warning: This runtime can be installed only on Ubuntu 18.04 or Ubuntu 20.04."
338             echo "More info https://github.com/intel/compute-runtime/releases" >&2
339             echo "Installation of Intel Compute Runtime interrupted"
340             exit $EXIT_FAILURE
341         fi
342     fi
343 }
344
345 distro_init()
346 {
347     if [[ -f /etc/centos-release ]]; then
348         DISTRO="centos"
349     elif [[ -f /etc/lsb-release ]]; then
350         DISTRO="ubuntu"
351     fi
352
353     _check_distro_version
354 }
355
356 check_agreement()
357 {
358     if [ "$params" == "-y" ]; then
359         return 0
360     fi
361
362     echo "This script will download and install Intel(R) Graphics Compute Runtime $INSTALL_DRIVER_VERSION, "
363     echo "that was used to validate this OpenVINO package."
364     echo "In case if you already have the driver - script will try to remove it."
365     while true; do
366         read -p "Want to proceed? (y/n): " yn
367         case $yn in
368             [Yy]*) return 0  ;;
369             [Nn]*) exit 1 ;;
370         esac
371     done
372 }
373
374 check_current_driver()
375 {   
376     echo "Checking current driver version..."
377     if [[ $DISTRO == centos ]]; then
378         gfx_version=$(yum info intel-opencl | grep Version)
379     elif [[ $DISTRO == ubuntu ]]; then
380         gfx_version=$(apt show intel-opencl | grep Version)
381     fi
382     gfx_version="$(echo -e "${gfx_version}" | sed -e 's/^Version\:[[:space:]]*//')"
383     # install NEO OCL driver if current driver version < 19.41.14441
384     if [[ ! -z $gfx_version && "$(printf '%s\n' "$INSTALL_DRIVER_VERSION" "$gfx_version" | sort -V | head -n 1)" = "$INSTALL_DRIVER_VERSION" ]]; then
385         echo "Intel(R) Graphics Compute Runtime installation skipped because current version greater or equal to $INSTALL_DRIVER_VERSION" >&2
386         echo "Installation of Intel Compute Runtime interrupted" >&2
387         exit $EXIT_FAILURE
388     else
389         echo "Starting installation"
390     fi
391 }
392
393 install()
394 {       
395     uninstall_user_mode
396     install_prerequisites
397     download_packages
398     install_user_mode
399     add_user_to_video_group
400 }
401
402 main()
403 {
404     echo "Intel OpenCL graphics driver installer"
405     distro_init
406     check_root_access
407     check_current_driver
408     check_agreement
409     install
410     summary
411 }
412
413 [[ "$0" == "$BASH_SOURCE" ]] && main "$@"