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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 function qemu_utils.qemu_delete {
16 # Deletes the QEMU directory
17 # QEMU install directory
19 # QEMU install version
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"
27 function qemu_utils.qemu_install {
28 # Downloads and installs QEMU
29 # QEMU install directory
31 # QEMU install version
35 # Force install (if true then remove previous installation; default false)
36 force_install=${4:-false}
38 qemu_package_url="http://download.qemu-project.org/${qemu_install_ver}.tar.xz"
40 if [ $force_install ]; then
42 qemu_utils.qemu_delete $qemu_install_dir $qemu_install_ver
44 # Test if QEMU was installed previously
45 test -d $qemu_install_dir && \
46 { echo "Qemu already installed: $qemu_install_dir"; exit 0; }
49 tmp_dir=$(mktemp -d) || \
50 { echo "Failed to create temporary working dir"; exit 1; }
51 trap "rm -r ${tmp_dir}" EXIT
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; }
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; }
62 sudo mkdir -p ${qemu_install_dir} || \
63 { echo "Failed to create ${qemu_install_dir}"; exit 1; }
65 # Apply additional patches
68 chmod +x ${SCRIPT_DIR}/qemu_patches/${qemu_install_ver}/*
69 run-parts --verbose --report ${SCRIPT_DIR}/qemu_patches/${qemu_install_ver}
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; }
80 echo "QEMU ${qemu_install_ver} ready"