xref: /aosp_15_r20/external/selinux/scripts/ci/travis-kvm-setup.sh (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1#!/usr/bin/env bash
2# SPDX-License-Identifier: MIT
3set -ev
4
5TEST_RUNNER="scripts/ci/fedora-test-runner.sh"
6
7#
8# Variables for controlling the Fedora Image version and download URLs.
9#
10if [ -z "$FEDORA_MAJOR" ] || [ -z "$FEDORA_MINOR" ]; then
11    echo "$0: FEDORA_MAJOR and FEDORA_MINOR must be set!" 1>&2
12    exit 1
13fi
14
15BASE_URL="https://download.fedoraproject.org/pub/fedora/linux/releases"
16IMAGE_BASE_NAME="Fedora-Cloud-Base-$FEDORA_MAJOR-$FEDORA_MINOR.x86_64"
17IMAGE_URL="$BASE_URL/$FEDORA_MAJOR/Cloud/x86_64/images/$IMAGE_BASE_NAME.raw.xz"
18CHECK_URL="$BASE_URL/$FEDORA_MAJOR/Cloud/x86_64/images/Fedora-Cloud-$FEDORA_MAJOR-$FEDORA_MINOR-x86_64-CHECKSUM"
19GPG_URL="https://getfedora.org/static/fedora.gpg"
20
21#
22# Travis gives us 7.5GB of RAM and two cores:
23# https://docs.travis-ci.com/user/reference/overview/
24#
25MEMORY=4096
26VCPUS="$(nproc)"
27
28# Install these here so other builds don't have to wait on these deps to download and install
29sudo apt-get update
30sudo apt-get install qemu-kvm libvirt-bin virtinst bridge-utils cpu-checker libguestfs-tools
31
32sudo usermod -a -G kvm,libvirt,libvirt-qemu "$USER"
33
34# Verify that KVM is working, useful if Travis ever changes anything.
35kvm-ok
36
37sudo systemctl enable libvirtd
38sudo systemctl start libvirtd
39
40# Set up a key so we can ssh into the VM
41ssh-keygen -N "" -f "$HOME/.ssh/id_rsa"
42
43#
44# Get the Fedora Cloud Image, It is a base image that small and ready to go, extract it and modify it with virt-sysprep
45#  - https://alt.fedoraproject.org/en/verify.html
46cd "$HOME"
47wget "$IMAGE_URL"
48
49# Verify the image
50curl "$GPG_URL" | gpg --import
51wget "$CHECK_URL"
52gpg --verify-files ./*-CHECKSUM
53sha256sum --ignore-missing -c ./*-CHECKSUM
54
55# Extract the image
56unxz -T0 "$IMAGE_BASE_NAME.raw.xz"
57
58# Search is needed for $HOME so virt service can access the image file.
59chmod a+x "$HOME"
60
61#
62# Modify the virtual image to:
63#   - Enable a login, we just use root
64#   - Enable passwordless login
65#     - Force a relabel to fix labels on ssh keys
66#
67sudo virt-sysprep -a "$IMAGE_BASE_NAME.raw" \
68  --root-password password:123456 \
69  --hostname fedoravm \
70  --append-line '/etc/ssh/sshd_config:PermitRootLogin yes' \
71  --append-line '/etc/ssh/sshd_config:PubkeyAuthentication yes' \
72  --mkdir /root/.ssh \
73  --upload "$HOME/.ssh/id_rsa.pub:/root/.ssh/authorized_keys" \
74  --chmod '0600:/root/.ssh/authorized_keys' \
75  --run-command 'chown root:root /root/.ssh/authorized_keys' \
76  --copy-in "$TRAVIS_BUILD_DIR:/root" \
77  --network \
78  --selinux-relabel
79
80#
81# Now we create a domain by using virt-install. This not only creates the domain, but runs the VM as well
82# It should be ready to go for ssh, once ssh starts.
83#
84sudo virt-install \
85  --name fedoravm \
86  --memory $MEMORY \
87  --vcpus $VCPUS \
88  --disk "$IMAGE_BASE_NAME.raw" \
89  --import --noautoconsole
90
91#
92# Here comes the tricky part, we have to figure out when the VM comes up AND we need the ip address for ssh. So we
93# can check the net-dhcp leases, for our host. We have to poll, and we will poll for up to 3 minutes in 6 second
94# intervals, so 30 poll attempts (0-29 inclusive).
95#
96# We have a full reboot + relabel, so first sleep gets us close
97#
98sleep 30
99for i in $(seq 0 29); do
100    echo "loop $i"
101    sleep 6s
102    # Get the leases, but tee it so it's easier to debug
103    sudo virsh net-dhcp-leases default | tee dhcp-leases.txt
104
105    # get our ipaddress
106    ipaddy="$(grep fedoravm dhcp-leases.txt | awk '{print $5}' | cut -d'/' -f 1-1)"
107    if [ -n "$ipaddy" ]; then
108        # found it, we're done looking, print it for debug logs
109        echo "ipaddy: $ipaddy"
110        break
111    fi
112    # it's empty/not found, loop back and try again.
113done
114
115# Did we find it? If not die.
116if [ -z "$ipaddy" ]; then
117    echo "ipaddy zero length, exiting with error 1"
118    exit 1
119fi
120
121#
122# Great we have a host running, ssh into it. We specify -o so
123# we don't get blocked on asking to add the servers key to
124# our known_hosts. Also, we need to forward the project directory
125# so forks know where to go.
126#
127project_dir="$(basename "$TRAVIS_BUILD_DIR")"
128ssh -tt -o StrictHostKeyChecking=no -o LogLevel=QUIET "root@$ipaddy" "SELINUX_DIR=/root/$project_dir /root/$project_dir/$TEST_RUNNER"
129
130exit 0
131