Optimize Qemu installation to speed up vhost tests
[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     local qemu_install_dir=$1
19
20     [ -d ${qemu_install_dir} ] && \
21         sudo rm -r ${qemu_install_dir} && \
22         echo "${qemu_install_dir} removed"
23 }
24
25 function qemu_utils.qemu_install {
26     # Downloads and installs QEMU
27     # QEMU install directory
28     local qemu_install_dir=$1
29     # QEMU install version
30     local qemu_install_ver=$2
31     # QEMU patch
32     local qemu_patch=${3:-false}
33     # Force install (if true then remove previous installation; default false)
34     local force_install=${4:-false}
35     # QEMU repo URL
36     local qemu_package_url="http://download.qemu-project.org/${qemu_install_ver}.tar.xz"
37     # QEMU target arch
38     local qemu_target_list=${5:-x86_64-softmmu}
39
40     if [ $force_install = true ]; then
41         # Cleanup QEMU dir
42         qemu_utils.qemu_delete $qemu_install_dir
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     local tmp_dir=$(mktemp -d) || \
50         { echo "Failed to create temporary working dir"; exit 1; }
51     trap "sudo 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 -xf /opt/${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 = true ]
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=${qemu_target_list} --prefix=${qemu_install_dir} || \
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 }