• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /bin/bash
2#
3# Copyright (C) 2023 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17set -e
18
19ART_TEST_ON_VM=true . "$(dirname $0)/buildbot-utils.sh"
20
21known_actions="create|boot|setup-ssh|connect|quit"
22
23if [[ -z $ANDROID_BUILD_TOP ]]; then
24    msgfatal "ANDROID_BUILD_TOP is not set"
25elif [[ ( $# -ne 1 ) || ! ( "$1" =~ ^($known_actions)$ ) ]]; then
26    msgfatal "usage: $0 <$known_actions>"
27fi
28
29action="$1"
30
31get_stable_binary() {
32    mkdir tmp && cd tmp
33    wget "http://security.ubuntu.com/ubuntu/pool/main/$1"
34    7z x "$(basename $1)" && zstd -d data.tar.zst && tar -xf data.tar
35    mv "$2" ..
36    cd .. && rm -rf tmp
37}
38
39if [[ $action = create ]]; then
40(
41    rm -rf "$ART_TEST_VM_DIR"
42    mkdir -p "$ART_TEST_VM_DIR"
43    cd "$ART_TEST_VM_DIR"
44
45    # sudo apt install qemu-system-<arch> qemu-efi cloud-image-utils
46
47    # Get the cloud image for Ubunty 22.04 (Jammy)
48    wget "http://cloud-images.ubuntu.com/releases/22.04/release/$ART_TEST_VM_IMG"
49
50    if [[ "$TARGET_ARCH" = "riscv64" ]]; then
51        # Get U-Boot for Ubuntu 22.04 (Jammy)
52        get_stable_binary \
53            u/u-boot/u-boot-qemu_2022.01+dfsg-2ubuntu2.3_all.deb \
54            usr/lib/u-boot/qemu-riscv64_smode/uboot.elf
55
56        # Get OpenSBI for Ubuntu 22.04 (Jammy)
57        get_stable_binary \
58            o/opensbi/opensbi_1.1-0ubuntu0.22.04.1_all.deb \
59            usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf
60
61    elif [[ "$TARGET_ARCH" = "arm64" ]]; then
62        # Get EFI (ARM64) for Ubuntu 22.04 (Jammy)
63        get_stable_binary \
64            e/edk2/qemu-efi-aarch64_2022.02-3ubuntu0.22.04.1_all.deb \
65            usr/share/qemu-efi-aarch64/QEMU_EFI.fd
66
67        dd if=/dev/zero of=flash0.img bs=1M count=64
68        dd if=QEMU_EFI.fd of=flash0.img conv=notrunc
69        dd if=/dev/zero of=flash1.img bs=1M count=64
70    fi
71
72    qemu-img resize "$ART_TEST_VM_IMG" +128G
73
74    # https://help.ubuntu.com/community/CloudInit
75    cat >user-data <<EOF
76#cloud-config
77ssh_pwauth: true
78chpasswd:
79  expire: false
80  list:
81    - $ART_TEST_SSH_USER:ubuntu
82EOF
83    cloud-localds user-data.img user-data
84)
85elif [[ $action = boot ]]; then
86(
87    cd "$ART_TEST_VM_DIR"
88    if [[ "$TARGET_ARCH" = "riscv64" ]]; then
89        qemu-system-riscv64 \
90            -m 16G \
91            -smp 8 \
92            -M virt \
93            -nographic \
94            -bios fw_jump.elf \
95            -kernel uboot.elf \
96            -drive file="$ART_TEST_VM_IMG",if=virtio \
97            -drive file=user-data.img,format=raw,if=virtio \
98            -device virtio-net-device,netdev=usernet \
99            -netdev user,id=usernet,hostfwd=tcp::$ART_TEST_SSH_PORT-:22
100    elif [[ "$TARGET_ARCH" = "arm64" ]]; then
101        qemu-system-aarch64 \
102            -m 16G \
103            -smp 8 \
104            -cpu cortex-a57 \
105            -M virt \
106            -nographic \
107            -drive if=none,file="$ART_TEST_VM_IMG",id=hd0 \
108            -pflash flash0.img \
109            -pflash flash1.img \
110            -drive file=user-data.img,format=raw,id=cloud \
111            -device virtio-blk-device,drive=hd0 \
112            -device virtio-net-device,netdev=usernet \
113            -netdev user,id=usernet,hostfwd=tcp::$ART_TEST_SSH_PORT-:22
114    fi
115
116)
117elif [[ $action = setup-ssh ]]; then
118    # Clean up mentions of this VM from known_hosts
119    sed -i -E "/\[$ART_TEST_SSH_HOST.*\]:$ART_TEST_SSH_PORT .*/d" $HOME/.ssh/known_hosts
120    ssh-copy-id -p "$ART_TEST_SSH_PORT" -o IdentityAgent=none "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST"
121
122elif [[ $action = connect ]]; then
123    $ART_SSH_CMD
124
125elif [[ $action = quit ]]; then
126    $ART_SSH_CMD "sudo poweroff"
127
128fi
129