• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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#
16# Default argument values
17#
18
19QEMU_VERSION='6.2.0'
20DEFAULT_QEMU_PREFIX="/opt/qemu-$QEMU_VERSION"
21QEMU_PREFIX="$DEFAULT_QEMU_PREFIX"
22
23#
24# Aux functions
25#
26
27function print_help
28{
29    HELP_MESSAGE="
30    It is the script for replacing packaged QEMU with vanilla QEMU $QEMU_VERSION
31    downloaded from the official QEMU web site. We need to provide a
32    replacement because:
33
34    1) Packaged QEMU contains bugs that hurt ARK unit tests.
35    2) Versions prior to $QEMU_VERSION do not play well with gdb-multiarch
36       (at least under Ubuntu 18.04).
37
38    The script should be run with superuser privileges.
39
40    EXAMPLE
41
42    $ ./scripts/install-deps-qemu --help
43    $ ./scripts/install-deps-qemu --qemu-prefix=/usr/local
44
45    SYNOPSIS
46
47    $0 [OPTIONS]
48
49    OPTIONS
50
51    --help, -h            Show this message and exit.
52
53    --qemu-prefix=PREFIX  Installation prefix for QEMU [$DEFAULT_QEMU_PREFIX].
54
55    CAVEATS
56
57    After QEMU is installed, the script attempts to symlink all files
58    from PREFIX/bin/qemu-* to /usr/bin. If other qemu binaries (or symlinks)
59    are already present in /usr/bin, they are left intact.
60
61    "
62
63    echo "$HELP_MESSAGE"
64}
65
66function assert_root
67{
68    if [[ $(id -u) -ne 0 ]] ; then
69        echo "FATAL: Please run as root."
70        exit 1
71    fi
72}
73
74function assert_ubuntu
75{
76    if [ ! -f /etc/os-release ]; then
77        echo "FATAL: /etc/os-release not found. Exiting..."
78        exit 1
79    fi
80
81    . /etc/os-release
82    if [[ "$NAME" != "Ubuntu" ]]; then
83        echo "FATAL: Only Ubuntu is supported. This is not. Exiting..."
84        exit 1
85    fi
86}
87
88function install_vanilla_qemu
89{
90    local prefix="$1"
91    local qemu_name="qemu-$QEMU_VERSION"
92    local arch_name="$qemu_name.tar.xz"
93    local src_path="$prefix/src"
94    local arch_full="$src_path/$arch_name"
95
96    apt-get update
97
98    # Packages taken from: https://wiki.qemu.org/Hosts/Linux
99    apt-get install -y --no-install-recommends -o Dpkg::Options::="--force-overwrite" \
100        bzip2 \
101        curl \
102        libfdt-dev \
103        libglib2.0-dev \
104        libpixman-1-dev \
105        ninja-build \
106        tar \
107        zlib1g-dev
108
109    mkdir -p "$src_path"
110    curl --retry 5 --retry-delay 0 -L "https://download.qemu.org/$arch_name" -o "$arch_full"
111
112    tar xfJ "$arch_full" -C "$src_path"
113
114    pushd "$src_path/$qemu_name"
115        ./configure --prefix="$prefix"
116        ninja -C ./build test
117        ninja -C ./build install
118    popd
119
120    # Try to symlink binaries to /usr/bin:
121    for fname in "$prefix"/bin/qemu-*; do
122        local lname="/usr/bin/$(basename "$fname")"
123        if [[ ! -f "$lname" ]]; then
124            ln -s "$fname" "$lname"
125        else
126            echo "WARNING: Unable to symlink $fname to $lname: $lname already exists"
127        fi
128    done
129}
130
131#
132# Main logic
133#
134
135#
136# Parse command-line arguments
137#
138
139for i in "$@"
140do
141    case $i in
142    -h|--help)
143        print_help
144        exit 0
145        ;;
146    --qemu-prefix=*)
147        QEMU_PREFIX=${i//[-a-zA-Z0-9]*=/}
148      ;;
149    *)
150      echo "Error: Unsupported flag $i" >&2
151      exit 1
152      ;;
153  esac
154done
155
156assert_root
157assert_ubuntu
158
159qemu_installed_version=$(qemu-aarch64 --version 2>/dev/null \
160    | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
161    | head -n1)
162if [[ "$qemu_installed_version" == "" ]]; then
163    qemu_installed_version=$($QEMU_PREFIX/bin/qemu-aarch64 --version 2>/dev/null \
164        | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
165        | head -n1)
166fi
167
168qemu_expected_version="version $QEMU_VERSION"
169
170if [[ "$qemu_installed_version" == "$qemu_expected_version" ]]; then
171    echo "Found expected QEMU $QEMU_VERSION, nothing to do"
172    exit 0
173fi
174
175if [[ "$qemu_installed_version" == "" ]]; then
176    echo "No installed QEMU found"
177else
178    echo "Found installed QEMU $qemu_installed_version"
179fi
180
181set -e
182install_vanilla_qemu "$QEMU_PREFIX"
183
184exit 0
185
186