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
18 local qemu_install_dir=$1
20 [ -d ${qemu_install_dir} ] && \
21 sudo rm -r ${qemu_install_dir} && \
22 echo "${qemu_install_dir} removed"
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
32 local qemu_patch=${3:-false}
33 # Force install (if true then remove previous installation; default false)
34 local force_install=${4:-false}
36 local qemu_package_url="http://download.qemu-project.org/${qemu_install_ver}.tar.xz"
38 local qemu_target_list=${5:-x86_64-softmmu}
40 if [ $force_install = true ]; then
42 qemu_utils.qemu_delete $qemu_install_dir
44 # Test if QEMU was installed previously
45 test -d $qemu_install_dir && \
46 { echo "Qemu already installed: $qemu_install_dir"; exit 0; }
49 local tmp_dir=$(mktemp -d) || \
50 { echo "Failed to create temporary working dir"; exit 1; }
51 trap "sudo 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 -xf /opt/${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
66 if [ $qemu_patch = true ]
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=${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; }
80 echo "QEMU ${qemu_install_ver} ready"