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
165
166
167
168
169
170
171
172
173
174#!/usr/bin/env bash
set -e
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 [ "${CROSS_RUNNER}" = "" ]; then
if [[ "${arch}" == i?86 ]] || [[ "${arch}" == x86_64 ]]; then
CROSS_RUNNER=native
else
CROSS_RUNNER=qemu-user
fi
fi
# select qemu arch
qarch="${arch}"
case "${arch}" in
armv7)
qarch="arm"
;;
i686)
qarch="i386"
;;
powerpc)
qarch="ppc"
;;
powerpc64)
qarch="ppc64"
;;
powerpc64le)
if [ "${CROSS_RUNNER}" = "qemu-user" ]; then
qarch="ppc64le"
else
qarch="ppc64"
fi
;;
esac
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"
;;
armv7)
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
;;
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
# wait for dropbear
for _ in $(seq 240); do
if grep -q "Not backgrounding" "${LOG}"; then
READY=1
break
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 "${@}"