blob: 3cfb5ededc667a77b268c9902d8d8531a4e0e571 [file] [log] [blame]
#! /bin/bash
#
# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
ART_TEST_ON_VM=true . "$(dirname $0)/buildbot-utils.sh"
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
known_actions="create|boot|geniso|install-keys|setup-ssh|connect|quit"
if [[ -z $ANDROID_BUILD_TOP ]]; then
msgfatal "ANDROID_BUILD_TOP is not set"
elif [[ ( $# -ne 1 ) || ! ( "$1" =~ ^($known_actions)$ ) ]]; then
msgfatal "usage: $0 <$known_actions>"
fi
action="$1"
get_stable_binary() {
mkdir tmp && cd tmp
wget "http://security.ubuntu.com/ubuntu/pool/main/$1"
7z x "$(basename $1)" && zstd -d data.tar.zst && tar -xf data.tar
mv "$2" ..
cd .. && rm -rf tmp
}
if [[ $action = create ]]; then
(
rm -rf "$ART_TEST_VM_DIR"
mkdir -p "$ART_TEST_VM_DIR"
cd "$ART_TEST_VM_DIR"
# sudo apt install qemu-system-<arch> qemu-efi cloud-image-utils
# Get the cloud image for Ubunty 22.04 (Jammy)
wget "http://cloud-images.ubuntu.com/releases/22.04/release/$ART_TEST_VM_IMG"
if [[ "$TARGET_ARCH" = "riscv64" ]]; then
# Get U-Boot for Ubuntu 22.04 (Jammy)
get_stable_binary \
u/u-boot/u-boot-qemu_2022.01+dfsg-2ubuntu2.3_all.deb \
usr/lib/u-boot/qemu-riscv64_smode/uboot.elf
# Get OpenSBI for Ubuntu 22.04 (Jammy)
get_stable_binary \
o/opensbi/opensbi_1.3-1ubuntu0.22.04.1_all.deb \
usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf
elif [[ "$TARGET_ARCH" = "arm64" ]]; then
# Get EFI (ARM64) for Ubuntu 22.04 (Jammy)
get_stable_binary \
e/edk2/qemu-efi-aarch64_2022.02-3ubuntu0.22.04.1_all.deb \
usr/share/qemu-efi-aarch64/QEMU_EFI.fd
dd if=/dev/zero of=flash0.img bs=1M count=64
dd if=QEMU_EFI.fd of=flash0.img conv=notrunc
dd if=/dev/zero of=flash1.img bs=1M count=64
fi
qemu-img resize "$ART_TEST_VM_IMG" +128G
)
elif [[ $action = geniso ]]; then
(
#https://help.ubuntu.com/community/CloudInit
cat >user-data <<EOF
#cloud-config
ssh_pwauth: true
chpasswd:
expire: false
users:
- name: $ART_TEST_SSH_USER
password: ubuntu
type: text
users:
- name: $ART_TEST_SSH_USER
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCOYmwd9qoYd7rfYI6Q8zzqoZ3BtLC/SQo0WCvBFoJT6JzwU8F7nkN57KBQPLtvX2OBeDnFbtEY8uLtuNEp1Z19VcDbRd3LhyAMYFz6Ox/vWtPfl0hv0kUMQMAne1Bg0tawlNxawP2HXrLOh/FaXdSBSRUHNqMTQEnkIYw4faArDS/zKjVDs0/+e9mhtjL0akLcK04crlk2KD8Q2csya5givdAD7fVNOx7DtckRR47FLM1bERe0t0FlUESx/x7oLjNEmNUrPXV6GSkCoskmKSZC1vwgAf0VrxFADv1EywQXmlNaa4+rzqS4jMYuwi5QCtQXFFZl5qQ1Sh1rnliTRJvJzjXCeq3QPsPzUJInfVGzrPClfHG7whlJE/Uwv8UOF7WHzUt5OBOsW6nZrplldvfYif/qz6dR+RX2G0zi8tC/2Mzahr6toAqtsqbdp3coYvpi/OjHIV3RhyJxG1FtyGYQRnmGPs8R9ic3pupjLFWM9qIilUCjFrUoiw7QAgfUrUc= ubuntu_user@example.com
EOF
# meta-data is necessary, even if empty.
cat >meta-data <<EOF
EOF
genisoimage -output user-data.img -volid cidata -joliet -rock user-data meta-data
mv user-data.img "$(dirname $0)/user-data.img"
rm user-data meta-data
)
elif [[ $action = boot ]]; then
(
cp "$(dirname $0)/user-data.img" "$ART_TEST_VM_DIR/user-data.img"
cd "$ART_TEST_VM_DIR"
if [[ "$TARGET_ARCH" = "riscv64" ]]; then
(qemu-system-riscv64 \
-m 16G \
-smp 8 \
-M virt \
-nographic \
-bios fw_jump.elf \
-kernel uboot.elf \
-drive file="$ART_TEST_VM_IMG",if=virtio \
-drive file=user-data.img,format=raw,if=virtio \
-device virtio-net-device,netdev=usernet \
-netdev user,id=usernet,hostfwd=tcp::$ART_TEST_SSH_PORT-:22 > $SCRIPT_DIR/boot.out &)
echo "Now listening for successful boot"
finish_str='.*finished at.*'
while IFS= read -d $'\0' -n 1 a ; do
line+="${a}"
if [[ "$line" =~ $finish_str ]] ; then
echo $line
echo "VM Successfully booted!"
exit 0
elif [[ $a = $'\n' ]]
then
echo $line
unset line
fi
done < <(tail -f $SCRIPT_DIR/boot.out)
elif [[ "$TARGET_ARCH" = "arm64" ]]; then
(qemu-system-aarch64 \
-m 16G \
-smp 8 \
-cpu cortex-a57 \
-M virt \
-nographic \
-drive if=none,file="$ART_TEST_VM_IMG",id=hd0 \
-pflash flash0.img \
-pflash flash1.img \
-drive file=user-data.img,format=raw,id=cloud \
-device virtio-blk-device,drive=hd0 \
-device virtio-net-device,netdev=usernet \
-netdev user,id=usernet,hostfwd=tcp::$ART_TEST_SSH_PORT-:22 > $SCRIPT_DIR/boot.out &)
echo "Now listening for successful boot"
while IFS= read -d $'\0' -n 1 a ; do
line+="${a}"
if [[ "$line" =~ '.*finished.*' ]] ; then
echo $line
echo "VM Successfully booted!"
exit 0
elif [[ $a = $'\n' ]]
then
echo $line
unset line
fi
done < <(tail -f $SCRIPT_DIR/boot.out)
fi
)
elif [[ $action = setup-ssh ]]; then
# Clean up mentions of this VM from known_hosts
sed -i -E "/\[$ART_TEST_SSH_HOST.*\]:$ART_TEST_SSH_PORT .*/d" $HOME/.ssh/known_hosts
ssh-copy-id -p "$ART_TEST_SSH_PORT" -o IdentityAgent=none -o StrictHostKeyChecking=no "$ART_TEST_SSH_USER@$ART_TEST_SSH_HOST"
elif [[ $action = install-keys ]]; then
# This key is only used to authorize access to a local test VM and does
# not pose any security risk.
echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCOYmwd9qoYd7rfYI6Q8zzqoZ3BtLC/SQo0WCvBFoJT6JzwU8F7nkN57KBQPLtvX2OBeDnFbtEY8uLtuNEp1Z19VcDbRd3LhyAMYFz6Ox/vWtPfl0hv0kUMQMAne1Bg0tawlNxawP2HXrLOh/FaXdSBSRUHNqMTQEnkIYw4faArDS/zKjVDs0/+e9mhtjL0akLcK04crlk2KD8Q2csya5givdAD7fVNOx7DtckRR47FLM1bERe0t0FlUESx/x7oLjNEmNUrPXV6GSkCoskmKSZC1vwgAf0VrxFADv1EywQXmlNaa4+rzqS4jMYuwi5QCtQXFFZl5qQ1Sh1rnliTRJvJzjXCeq3QPsPzUJInfVGzrPClfHG7whlJE/Uwv8UOF7WHzUt5OBOsW6nZrplldvfYif/qz6dR+RX2G0zi8tC/2Mzahr6toAqtsqbdp3coYvpi/OjHIV3RhyJxG1FtyGYQRnmGPs8R9ic3pupjLFWM9qIilUCjFrUoiw7QAgfUrUc= ubuntu_user@example.com" > ~/.ssh/ubuntu.pub
echo "-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAjmJsHfaqGHe632COkPM86qGdwbSwv0kKNFgrwRaCU+ic8FPBe55D
eeygUDy7b19jgXg5xW7RGPLi7bjRKdWdfVXA20Xdy4cgDGBc+jsf71rT35dIb9JFDEDAJ3
tQYNLWsJTcWsD9h16yzofxWl3UgUkVBzajE0BJ5CGMOH2gKw0v8yo1Q7NP/nvZobYy9Gp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wfEfO7V+VDxNQyrG2EMLT6uTu58XDgOgB9KWej4DTDYm+KNoVblkf
kqu3h4rmEmvk8AAAAjY2F0ZHVuY2FuQGNhdHN0YXRpb24uYy5nb29nbGVycy5jb20=
-----END OPENSSH PRIVATE KEY-----" > ~/.ssh/ubuntu
chmod 600 ~/.ssh/ubuntu
elif [[ $action = connect ]]; then
$ART_SSH_CMD
elif [[ $action = quit ]]; then
$ART_SSH_CMD "sudo poweroff"
fi