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