• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env bash
2# Copyright (c) 2021-2024 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 install
117    popd
118
119    # Try to symlink binaries to /usr/bin:
120    for fname in "$prefix"/bin/qemu-*; do
121        local lname="/usr/bin/$(basename "$fname")"
122        if [[ ! -f "$lname" ]]; then
123            ln -s "$fname" "$lname"
124        else
125            echo "WARNING: Unable to symlink $fname to $lname: $lname already exists"
126        fi
127    done
128}
129
130#
131# Main logic
132#
133
134#
135# Parse command-line arguments
136#
137
138for i in "$@"
139do
140    case $i in
141    -h|--help)
142        print_help
143        exit 0
144        ;;
145    --qemu-prefix=*)
146        QEMU_PREFIX=${i//[-a-zA-Z0-9]*=/}
147      ;;
148    *)
149      echo "Error: Unsupported flag $i" >&2
150      exit 1
151      ;;
152  esac
153done
154
155assert_root
156assert_ubuntu
157
158qemu_installed_version=$(qemu-aarch64 --version 2>/dev/null \
159    | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
160    | head -n1)
161if [[ "$qemu_installed_version" == "" ]]; then
162    qemu_installed_version=$($QEMU_PREFIX/bin/qemu-aarch64 --version 2>/dev/null \
163        | grep -Eo 'version [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+' \
164        | head -n1)
165fi
166
167qemu_expected_version="version $QEMU_VERSION"
168
169if [[ "$qemu_installed_version" == "$qemu_expected_version" ]]; then
170    echo "Found expected QEMU $QEMU_VERSION, nothing to do"
171    exit 0
172fi
173
174if [[ "$qemu_installed_version" == "" ]]; then
175    echo "No installed QEMU found"
176else
177    echo "Found installed QEMU $qemu_installed_version"
178fi
179
180set -e
181install_vanilla_qemu "$QEMU_PREFIX"
182
183exit 0
184
185