linux-runner 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env bash
  2. set -e
  3. LOG=/tmp/qemu.log
  4. LOCK=/tmp/qemu.lock
  5. if [ -n "${CROSS_DEBUG}" ]; then
  6. set -x
  7. fi
  8. # arch in the rust target
  9. arch="${1}"
  10. shift
  11. if [ "${CROSS_RUNNER}" = "" ]; then
  12. if [[ "${arch}" == i?86 ]] || [[ "${arch}" == x86_64 ]]; then
  13. CROSS_RUNNER=native
  14. else
  15. CROSS_RUNNER=qemu-user
  16. fi
  17. fi
  18. # select qemu arch
  19. qarch="${arch}"
  20. case "${arch}" in
  21. armv7)
  22. qarch="arm"
  23. ;;
  24. i686)
  25. qarch="i386"
  26. ;;
  27. powerpc)
  28. qarch="ppc"
  29. ;;
  30. powerpc64)
  31. qarch="ppc64"
  32. ;;
  33. powerpc64le)
  34. if [ "${CROSS_RUNNER}" = "qemu-user" ]; then
  35. qarch="ppc64le"
  36. else
  37. qarch="ppc64"
  38. fi
  39. ;;
  40. esac
  41. case "${CROSS_RUNNER}" in
  42. native)
  43. exec "${@}"
  44. ;;
  45. qemu-user)
  46. exec "qemu-${qarch}" "${@}"
  47. ;;
  48. qemu-system)
  49. true
  50. ;;
  51. *)
  52. echo "Invalid runner: \"${CROSS_RUNNER}\"";
  53. echo "Valid runners are: native, qemu-user and qemu-system"
  54. exit 1
  55. ;;
  56. esac
  57. n="$(nproc)"
  58. memory=1G
  59. driver9p="virtio-9p-pci"
  60. drivernet="virtio-net-pci"
  61. # select qemu parameters
  62. case "${arch}" in
  63. aarch64)
  64. # 8 is the max number of cpu supported by qemu-aarch64
  65. n=$(( n > 8 ? 8 : n ))
  66. opt="-machine virt -cpu cortex-a57"
  67. ;;
  68. armv7)
  69. opt="-machine virt"
  70. driver9p="virtio-9p-device"
  71. drivernet="virtio-net-device"
  72. ;;
  73. i686)
  74. opt="-append console=ttyS0"
  75. ;;
  76. mips|mipsel)
  77. # avoid kernel error
  78. # https://blahcat.github.io/2017/07/14/building-a-debian-stretch-qemu-image-for-mipsel/
  79. opt="-append nokaslr"
  80. n=1
  81. ;;
  82. mips64el)
  83. # avoid kernel error
  84. # https://blahcat.github.io/2017/07/14/building-a-debian-stretch-qemu-image-for-mipsel/
  85. opt="-append nokaslr -cpu MIPS64R2-generic"
  86. n=1
  87. ;;
  88. powerpc)
  89. opt="-append console=ttyPZ0"
  90. n=1
  91. ;;
  92. powerpc64|powerpc64le)
  93. opt="-append console=hvc0 --nodefaults -serial stdio"
  94. ;;
  95. s390x)
  96. n=1
  97. driver9p="virtio-9p-ccw"
  98. drivernet="virtio-net-ccw"
  99. ;;
  100. sparc64)
  101. n=1
  102. driver9p+=",bus=pciB"
  103. drivernet+=",bus=pciB"
  104. ;;
  105. x86_64)
  106. opt="-append console=ttyS0"
  107. ;;
  108. esac
  109. (
  110. flock -n 200 || exit 0
  111. echo Booting QEMU virtual machine with $n cpus...
  112. QEMU_CMD="qemu-system-${qarch} \
  113. -m ${memory} \
  114. -smp ${n} \
  115. -nographic \
  116. -monitor none \
  117. -netdev user,id=net0,hostfwd=tcp::10022-:22 \
  118. -device ${drivernet},netdev=net0 \
  119. -kernel /qemu/kernel \
  120. -initrd /qemu/initrd.gz \
  121. ${opt} \
  122. -fsdev local,id=fs0,path=/target,security_model=mapped \
  123. -device ${driver9p},fsdev=fs0,mount_tag=target"
  124. touch "${LOG}"
  125. if [[ -n "${CROSS_DEBUG}" ]]; then
  126. (${QEMU_CMD} 2>&1 | tee -a "${LOG}") &
  127. else
  128. ${QEMU_CMD} >> "${LOG}" 2>&1 &
  129. fi
  130. # wait for dropbear
  131. for _ in $(seq 240); do
  132. if grep -q "Not backgrounding" "${LOG}"; then
  133. READY=1
  134. break
  135. fi
  136. sleep 0.5s
  137. done
  138. if [ -z "${READY}" ]; then
  139. if [ -n "${CROSS_DEBUG}" ]; then
  140. echo "Not ready but continuing because CROSS_DEBUG is set"
  141. else
  142. echo "Qemu is not ready after ${SECONDS} seconds..."
  143. echo "Set the environment variable CROSS_DEBUG=1 to debug"
  144. echo "Last 100 lines of qemu output:"
  145. tail -n 100 "${LOG}"
  146. exit 1
  147. fi
  148. fi
  149. echo "Booted in ${SECONDS} seconds"
  150. ) 200>"${LOCK}"
  151. if [[ -t 1 ]] && [[ -t 2 ]]; then
  152. tty_flag='-t'
  153. fi
  154. exec dbclient ${tty_flag} -p 10022 -y -y root@localhost "${@}"