5fbbef809e97f914f98cfe0d750770dea6c9bbd7
[csit.git] / resources / libraries / bash / qemu_build.sh
1 #!/bin/bash
2 # Copyright (c) 2016 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 QEMU_VERSION="qemu-2.5.0"
16 QEMU_DOWNLOAD_REPO="http://download.qemu-project.org/"
17 QEMU_DOWNLOAD_PACKAGE="${QEMU_VERSION}.tar.xz"
18 QEMU_PACKAGE_URL="${QEMU_DOWNLOAD_REPO}${QEMU_DOWNLOAD_PACKAGE}"
19 QEMU_INSTALL_DIR="/opt/${QEMU_VERSION}"
20 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21
22 for i in "$@"; do
23     case $i in
24         --version=*)
25             QEMU_VERSION="${i#*=}"
26             shift ;;
27         --directory=*)
28             QEMU_INSTALL_DIR="${i#*=}"
29             shift ;;
30         --patch)
31             PATCH=1
32             shift ;;
33         --force)
34             FORCE=1
35             shift ;;
36         *)
37             ;;
38     esac
39 done
40
41 if test "$(id -u)" -ne 0
42 then
43     echo "Please use root or sudo to be able to install into: ${QEMU_INSTALL_DIR}"
44     exit 1
45 fi
46
47 WORKING_DIR=$(mktemp -d) || \
48     { echo "Failed to create temporary working dir"; exit 1; }
49 trap "rm -r ${WORKING_DIR}" EXIT
50
51 if [ $FORCE ]
52 then
53     rm -rf ${QEMU_INSTALL_DIR}
54 else
55     test -d ${QEMU_INSTALL_DIR} && \
56         { echo "Qemu already installed: ${QEMU_INSTALL_DIR}"; exit 0; }
57 fi
58
59 # Download QEMU source code if no local copy exists
60 if [ ! -f /opt/${QEMU_DOWNLOAD_PACKAGE} ]; then
61     wget -P /opt -q ${QEMU_PACKAGE_URL} || \
62         { echo "Failed to download ${QEMU_VERSION}"; exit 1; }
63 fi
64
65 # Extract archive into temp directory
66 tar --strip-components 1 -xf /opt/${QEMU_DOWNLOAD_PACKAGE} -C ${WORKING_DIR} || \
67     { echo "Failed to extract ${QEMU_VERSION}.tar.xz"; exit 1; }
68
69 cd ${WORKING_DIR}
70 mkdir ${QEMU_INSTALL_DIR} || \
71     { echo "Failed to create ${QEMU_INSTALL_DIR}"; exit 1; }
72
73 # Apply additional patches
74 if [ $PATCH ]
75 then
76     chmod +x ${SCRIPT_DIR}/qemu_patches/${QEMU_VERSION}/*
77     run-parts --verbose --report  ${SCRIPT_DIR}/qemu_patches/${QEMU_VERSION}
78 fi
79
80 # Build
81 ./configure --target-list=x86_64-softmmu --prefix=${QEMU_INSTALL_DIR} || \
82     { echo "Failed to configure ${QEMU_VERSION}"; exit 1; }
83 make -j`nproc` || \
84     { echo "Failed to compile ${QEMU_VERSION}"; exit 1; }
85 make install || \
86     { echo "Failed to install ${QEMU_VERSION}"; exit 1; }
87
88 echo QEMU ${QEMU_VERSION} ready

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.