• 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        libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev \
101        curl tar ninja-build
102
103    mkdir -p "$src_path"
104    curl --retry 5 --retry-delay 0 -L "https://download.qemu.org/$arch_name" -o "$arch_full"
105
106    tar xfJ "$arch_full" -C "$src_path"
107
108    pushd "$src_path/$qemu_name"
109        ./configure --prefix="$prefix"
110        ninja -C ./build test
111        ninja -C ./build install
112    popd
113
114    # Try to symlink binaries to /usr/bin:
115    for fname in "$prefix"/bin/qemu-*; do
116        local lname="/usr/bin/$(basename "$fname")"
117        if [[ ! -f "$lname" ]]; then
118            ln -s "$fname" "$lname"
119        else
120            echo "WARNING: Unable to symlink $fname to $lname: $lname already exists"
121        fi
122    done
123}
124
125#
126# Main logic
127#
128
129#
130# Parse command-line arguments
131#
132
133for i in "$@"
134do
135    case $i in
136    -h|--help)
137        print_help
138        exit 0
139        ;;
140    --qemu-prefix=*)
141        QEMU_PREFIX=${i//[-a-zA-Z0-9]*=/}
142      ;;
143    *)
144      echo "Error: Unsupported flag $i" >&2
145      exit 1
146      ;;
147  esac
148done
149
150assert_root
151assert_ubuntu
152
153qemu_installed_version=$(qemu-aarch64 --version 2>/dev/null \
154    | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
155    | head -n1)
156if [[ "$qemu_installed_version" == "" ]]; then
157    qemu_installed_version=$($QEMU_PREFIX/bin/qemu-aarch64 --version 2>/dev/null \
158        | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
159        | head -n1)
160fi
161
162qemu_expected_version="version $QEMU_VERSION"
163
164if [[ "$qemu_installed_version" == "$qemu_expected_version" ]]; then
165    echo "Found expected QEMU $QEMU_VERSION, nothing to do"
166    exit 0
167fi
168
169if [[ "$qemu_installed_version" == "" ]]; then
170    echo "No installed QEMU found"
171else
172    echo "Found installed QEMU $qemu_installed_version"
173fi
174
175set -e
176install_vanilla_qemu "$QEMU_PREFIX"
177
178exit 0
179
180