run.sh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/bin/sh
  2. set -e
  3. if [ "$(uname -s)" = "Darwin" ]; then
  4. export PATH="$(dirname $(brew list gnu-getopt | grep "bin/getopt$")):$PATH"
  5. fi
  6. AYA_SOURCE_DIR="$(realpath $(dirname $0)/..)"
  7. LIBBPF_DIR=$1
  8. # Temporary directory for tests to use.
  9. AYA_TMPDIR="${AYA_SOURCE_DIR}/.tmp"
  10. # Directory for VM images
  11. AYA_IMGDIR=${AYA_TMPDIR}
  12. if [ -z "${AYA_BUILD_TARGET}" ]; then
  13. AYA_BUILD_TARGET=$(rustc -vV | sed -n 's|host: ||p')
  14. fi
  15. AYA_HOST_ARCH=$(uname -m)
  16. if [ "${AYA_HOST_ARCH}" = "arm64" ]; then
  17. AYA_HOST_ARCH="aarch64"
  18. fi
  19. if [ -z "${AYA_GUEST_ARCH}" ]; then
  20. AYA_GUEST_ARCH="${AYA_HOST_ARCH}"
  21. fi
  22. if [ "${AYA_GUEST_ARCH}" = "aarch64" ]; then
  23. if [ -z "${AARCH64_UEFI}" ]; then
  24. AARCH64_UEFI="$(brew list qemu -1 -v | grep edk2-aarch64-code.fd)"
  25. fi
  26. fi
  27. if [ -z "$AYA_MUSL_TARGET" ]; then
  28. AYA_MUSL_TARGET=${AYA_GUEST_ARCH}-unknown-linux-musl
  29. fi
  30. # Test Image
  31. if [ -z "${AYA_TEST_IMAGE}" ]; then
  32. AYA_TEST_IMAGE="fedora37"
  33. fi
  34. case "${AYA_TEST_IMAGE}" in
  35. fedora*) AYA_SSH_USER="fedora";;
  36. centos*) AYA_SSH_USER="centos";;
  37. esac
  38. download_images() {
  39. mkdir -p "${AYA_IMGDIR}"
  40. case $1 in
  41. fedora37)
  42. if [ ! -f "${AYA_IMGDIR}/fedora37.${AYA_GUEST_ARCH}.qcow2" ]; then
  43. IMAGE="Fedora-Cloud-Base-37-1.7.${AYA_GUEST_ARCH}.qcow2"
  44. IMAGE_URL="https://download.fedoraproject.org/pub/fedora/linux/releases/37/Cloud/${AYA_GUEST_ARCH}/images"
  45. echo "Downloading: ${IMAGE}, this may take a while..."
  46. curl -o "${AYA_IMGDIR}/fedora37.${AYA_GUEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  47. fi
  48. ;;
  49. centos8)
  50. if [ ! -f "${AYA_IMGDIR}/centos8.${AYA_GUEST_ARCH}.qcow2" ]; then
  51. IMAGE="CentOS-8-GenericCloud-8.4.2105-20210603.0.${AYA_GUEST_ARCH}.qcow2"
  52. IMAGE_URL="https://cloud.centos.org/centos/8/${AYA_GUEST_ARCH}/images"
  53. echo "Downloading: ${IMAGE}, this may take a while..."
  54. curl -o "${AYA_IMGDIR}/centos8.${AYA_GUEST_ARCH}.qcow2" -sSL "${IMAGE_URL}/${IMAGE}"
  55. fi
  56. ;;
  57. *)
  58. echo "$1 is not a recognized image name"
  59. return 1
  60. ;;
  61. esac
  62. }
  63. start_vm() {
  64. download_images "${AYA_TEST_IMAGE}"
  65. # prepare config
  66. cat > "${AYA_TMPDIR}/metadata.yaml" <<EOF
  67. instance-id: iid-local01
  68. local-hostname: test
  69. EOF
  70. if [ ! -f "${AYA_TMPDIR}/test_rsa" ]; then
  71. ssh-keygen -t rsa -b 4096 -f "${AYA_TMPDIR}/test_rsa" -N "" -C "" -q
  72. pub_key=$(cat "${AYA_TMPDIR}/test_rsa.pub")
  73. fi
  74. if [ ! -f "${AYA_TMPDIR}/ssh_config" ]; then
  75. cat > "${AYA_TMPDIR}/ssh_config" <<EOF
  76. StrictHostKeyChecking=no
  77. UserKnownHostsFile=/dev/null
  78. GlobalKnownHostsFile=/dev/null
  79. EOF
  80. fi
  81. cat > "${AYA_TMPDIR}/user-data.yaml" <<EOF
  82. #cloud-config
  83. ssh_authorized_keys:
  84. - ${pub_key}
  85. EOF
  86. $AYA_SOURCE_DIR/test/cloud-localds "${AYA_TMPDIR}/seed.img" "${AYA_TMPDIR}/user-data.yaml" "${AYA_TMPDIR}/metadata.yaml"
  87. case "${AYA_GUEST_ARCH}" in
  88. x86_64)
  89. QEMU=qemu-system-x86_64
  90. machine="q35"
  91. cpu="qemu64"
  92. nr_cpus="$(nproc --all)"
  93. if [ "${AYA_HOST_ARCH}" = "${AYA_GUEST_ARCH}" ]; then
  94. if [ -c /dev/kvm ]; then
  95. machine="${machine},accel=kvm"
  96. cpu="host"
  97. elif [ "$(uname -s)" = "Darwin" ]; then
  98. machine="${machine},accel=hvf"
  99. cpu="host"
  100. fi
  101. fi
  102. ;;
  103. aarch64)
  104. QEMU=qemu-system-aarch64
  105. machine="virt"
  106. cpu="cortex-a57"
  107. uefi="-drive file=${AARCH64_UEFI},if=pflash,format=raw,readonly=on"
  108. if [ "${AYA_HOST_ARCH}" = "${AYA_GUEST_ARCH}" ]; then
  109. if [ -c /dev/kvm ]; then
  110. machine="${machine},accel=kvm"
  111. cpu="host"
  112. nr_cpus="$(nproc --all)"
  113. elif [ "$(uname -s)" = "Darwin" ]; then
  114. machine="${machine},accel=hvf,highmem=off"
  115. cpu="cortex-a72"
  116. # nrpoc --all on apple silicon returns the two extra fancy
  117. # cores and then qemu complains that nr_cpus > actual_cores
  118. nr_cpus=8
  119. fi
  120. fi
  121. ;;
  122. *)
  123. echo "${AYA_GUEST_ARCH} is not supported"
  124. return 1
  125. ;;
  126. esac
  127. if [ ! -f "${AYA_IMGDIR}/vm.qcow2" ]; then
  128. echo "Creating VM image"
  129. qemu-img create -F qcow2 -f qcow2 -o backing_file="${AYA_IMGDIR}/${AYA_TEST_IMAGE}.${AYA_GUEST_ARCH}.qcow2" "${AYA_IMGDIR}/vm.qcow2" || return 1
  130. CACHED_VM=0
  131. else
  132. echo "Reusing existing VM image"
  133. CACHED_VM=1
  134. fi
  135. $QEMU \
  136. -machine "${machine}" \
  137. -cpu "${cpu}" \
  138. -m 3G \
  139. -smp "${nr_cpus}" \
  140. -display none \
  141. -monitor none \
  142. -daemonize \
  143. -pidfile "${AYA_TMPDIR}/vm.pid" \
  144. -device virtio-net-pci,netdev=net0 \
  145. -netdev user,id=net0,hostfwd=tcp::2222-:22 \
  146. $uefi \
  147. -drive if=virtio,format=qcow2,file="${AYA_IMGDIR}/vm.qcow2" \
  148. -drive if=virtio,format=raw,file="${AYA_TMPDIR}/seed.img" || return 1
  149. trap cleanup_vm EXIT
  150. echo "Waiting for SSH on port 2222..."
  151. retry=0
  152. max_retries=300
  153. while ! ssh -q -F "${AYA_TMPDIR}/ssh_config" -o ConnectTimeout=1 -i "${AYA_TMPDIR}/test_rsa" ${AYA_SSH_USER}@localhost -p 2222 echo "Hello VM"; do
  154. retry=$((retry+1))
  155. if [ ${retry} -gt ${max_retries} ]; then
  156. echo "Unable to connect to VM"
  157. return 1
  158. fi
  159. sleep 1
  160. done
  161. echo "VM launched"
  162. exec_vm uname -a
  163. echo "Installing dependencies"
  164. exec_vm sudo dnf install -qy bpftool llvm llvm-devel clang clang-devel zlib-devel
  165. exec_vm 'curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- \
  166. -y --profile minimal --default-toolchain nightly --component rust-src --component clippy'
  167. exec_vm 'echo source ~/.cargo/env >> ~/.bashrc'
  168. exec_vm cargo install bpf-linker --no-default-features --features system-llvm
  169. }
  170. scp_vm() {
  171. local=$1
  172. remote=$(basename "$1")
  173. scp -q -F "${AYA_TMPDIR}/ssh_config" \
  174. -i "${AYA_TMPDIR}/test_rsa" \
  175. -P 2222 "${local}" \
  176. "${AYA_SSH_USER}@localhost:${remote}"
  177. }
  178. rsync_vm() {
  179. rsync -a -e "ssh -p 2222 -F ${AYA_TMPDIR}/ssh_config -i ${AYA_TMPDIR}/test_rsa" $1 $AYA_SSH_USER@localhost:
  180. }
  181. exec_vm() {
  182. ssh -q -F "${AYA_TMPDIR}/ssh_config" \
  183. -i "${AYA_TMPDIR}/test_rsa" \
  184. -p 2222 \
  185. ${AYA_SSH_USER}@localhost \
  186. "$@"
  187. }
  188. stop_vm() {
  189. if [ -f "${AYA_TMPDIR}/vm.pid" ]; then
  190. echo "Stopping VM forcefully"
  191. kill -9 "$(cat "${AYA_TMPDIR}/vm.pid")"
  192. rm "${AYA_TMPDIR}/vm.pid"
  193. fi
  194. }
  195. cleanup_vm() {
  196. stop_vm
  197. if [ "$?" != "0" ]; then
  198. rm -f "${AYA_IMGDIR}/vm.qcow2"
  199. fi
  200. }
  201. if [ -z "$LIBBPF_DIR" ]; then
  202. echo "path to libbpf required"
  203. exit 1
  204. fi
  205. start_vm
  206. trap cleanup_vm EXIT
  207. # make sure we always use fresh aya and libbpf (also see comment at the end)
  208. exec_vm "rm -rf aya/* libbpf"
  209. rsync_vm "--exclude=target --exclude=.tmp $AYA_SOURCE_DIR"
  210. rsync_vm "$LIBBPF_DIR"
  211. # need to build or linting will fail trying to include object files
  212. exec_vm "cd aya; cargo xtask build-integration-test --libbpf-dir ~/libbpf"
  213. exec_vm "cd aya; cargo clippy -p integration-test -- --deny warnings"
  214. exec_vm "cd aya; cargo xtask integration-test --libbpf-dir ~/libbpf"
  215. # we rm and sync but it doesn't seem to work reliably - I guess we could sleep a
  216. # few seconds after but ain't nobody got time for that. Instead we also rm
  217. # before rsyncing.
  218. exec_vm "rm -rf aya/* libbpf; sync"