CSIT-870 Kubernetes/Ligato integration
[csit.git] / resources / libraries / bash / shell / qemu_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 function qemu_utils.qemu_delete {
16     # Deletes the QEMU directory
17     # QEMU install directory
18     qemu_install_dir=$1
19     # QEMU install version
20     qemu_install_ver=$2
21
22     [ -d ${qemu_install_dir}/${qemu_install_ver} ] && \
23         sudo rm -r ${qemu_install_dir}/${qemu_install_ver} && \
24         echo "${qemu_install_dir}/${qemu_install_ver} removed"
25 }
26
27 function qemu_utils.qemu_install {
28     # Downloads and installs QEMU
29     # QEMU install directory
30     qemu_install_dir=$1
31     # QEMU install version
32     qemu_install_ver=$2
33     # QEMU patch
34     qemu_patch=$3
35     # Force install (if true then remove previous installation; default false)
36     force_install=${4:-false}
37     # QEMU repo URL
38     qemu_package_url="http://download.qemu-project.org/${qemu_install_ver}.tar.xz"
39
40     if [ $force_install ]; then
41         # Cleanup QEMU dir
42         qemu_utils.qemu_delete $qemu_install_dir $qemu_install_ver
43     else
44         # Test if QEMU was installed previously
45         test -d $qemu_install_dir && \
46             { echo "Qemu already installed: $qemu_install_dir"; exit 0; }
47     fi
48
49     tmp_dir=$(mktemp -d) || \
50         { echo "Failed to create temporary working dir"; exit 1; }
51     trap "rm -r ${tmp_dir}" EXIT
52
53     # Download QEMU source code if no local copy exists
54     if [ ! -f /opt/${qemu_install_ver}.tar.xz ]; then
55         sudo wget -e use_proxy=yes -P /opt -q ${qemu_package_url} || \
56             { echo "Failed to download ${qemu_install_ver}"; exit 1; }
57     fi
58     tar --strip-components 1 -xvJf ${tmp_dir}/${qemu_install_ver}.tar.xz -C ${tmp_dir} && \
59         { echo "Failed to exctract ${qemu_install_ver}.tar.xz"; exit 1; }
60
61     cd ${tmp_dir}
62     sudo mkdir -p ${qemu_install_dir} || \
63         { echo "Failed to create ${qemu_install_dir}"; exit 1; }
64
65     # Apply additional patches
66     if [ $qemu_patch ]
67     then
68         chmod +x ${SCRIPT_DIR}/qemu_patches/${qemu_install_ver}/*
69         run-parts --verbose --report  ${SCRIPT_DIR}/qemu_patches/${qemu_install_ver}
70     fi
71
72     # Build
73     sudo ./configure --target-list=x86_64-softmmu --prefix=${qemu_install_dir}/${qemu_install_ver} || \
74         { echo "Failed to configure ${qemu_install_ver}"; exit 1; }
75     sudo make -j`nproc` || \
76         { echo "Failed to compile ${qemu_install_ver}"; exit 1; }
77     sudo make install || \
78         { echo "Failed to install ${qemu_install_ver}"; exit 1; }
79
80     echo "QEMU ${qemu_install_ver} ready"
81 }