-
Notifications
You must be signed in to change notification settings - Fork 1
/
rlaunch
executable file
·211 lines (189 loc) · 4.56 KB
/
rlaunch
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/bin/bash
#
# Usage:
# rlaunch OPTIONS [-- QEMU_ARGS]
# Options:
# -q PORT change qemu monitor port (default is 4444)
# -n VNC_DISPLAY set up VNC and run client
# -h HOST_NAME host name
# -b BUILD_PATH build folder name on host
# -i IMAGE_PATH image folder name on host
# -m QEMU_MEMORY qemu memory
# -t TYPE qemu type:
# -r PORT redirect SSH port to localhost:PORT
# kvm qemu provided by system in KVM mode, will modify base image
# qemu qemu
# qemu-kvm qemu in KVM mode
# s2e s2e-qemu
# s2e-gdb s2e-qemu in GDB
# -v verbose option
# -u unbuffered stream option
# -s use single path s2e
# -x use x86_64 binary
# -A do not use usb device (used to make CGC image)
# Example:
# rlaunch -n 7 -h dslab-vader1.epfl.ch -b '~/workspace/build' -i '~/workspace/image' -m 1G -t s2e -- --s2e-verbose
#
set -e -u
#
#
#
QPORT=4444
VNC=-1
S2E_OPTS=""
SSH_OPTS=""
BUILDTYPE="debug"
BUILDTYPE="release"
SP=""
ARCH="i386"
SSH_REDIR=-1
USB="-usb -device usb-tablet"
OPTIND=1
while getopts "q:n:h:b:i:m:t:r:vusxA" opt; do
case "$opt" in
q)
QPORT=${OPTARG}
;;
n)
VNC=${OPTARG}
VNCPORT=$((5900+${VNC}))
;;
h)
REMOTE_HOST=${OPTARG}
;;
b)
REMOTE_BUILD=${OPTARG}
;;
i)
REMOTE_IMAGE=${OPTARG}
;;
m)
QEMU_MEMORY=${OPTARG}
;;
t)
TYPE=${OPTARG}
;;
r)
SSH_REDIR=${OPTARG}
;;
v)
S2E_OPTS+=" --s2e-verbose"
;;
u)
S2E_OPTS+=" --s2e-unbuffered-stream"
;;
s)
SP='_sp'
;;
x)
ARCH='x86_64'
;;
A)
USB=""
;;
esac
done
shift $((OPTIND-1))
QEMU_ARGS=$*
#
#
#
# set up environment
CMD=
CMD+="export IMAGE=${REMOTE_IMAGE} &&"
CMD+="export BUILD=${REMOTE_BUILD} &&"
CMD+="export S2E_PAYLOAD=\${IMAGE} &&"
CMD+="export WINDOWS_FS=\${IMAGE}/guestfs &&"
CMD+="mkdir -p \${IMAGE}/out &&"
CMD+="cd \${IMAGE}/out &&"
case ${TYPE} in
kvm)
CMD+=" qemu-system-${ARCH}"
CMD+=" -enable-kvm"
CMD+=" -net nic,model=e1000 -net user"
CMD+=" -drive file=\${IMAGE}/image.raw.s2e"
;;
qemu|qemu-kvm)
CMD+=" \${BUILD}/qemu-${BUILDTYPE}/${ARCH}-softmmu/qemu-system-${ARCH}"
CMD+=" -drive file=\${IMAGE}/image.raw.s2e,cache=writeback,format=s2e"
CMD+=" -net none -net nic,model=e1000"
if [ ${TYPE} == 'qemu-kvm' ]; then
CMD+=" -enable-kvm -enable-serial-commands -serial file:/tmp/serial.txt"
fi
;;
s2e|s2e-gdb)
if [ ${TYPE} == 's2e-gdb' ]; then
CMD+="truncate -s0 gdb.cfg &&"
CMD+="echo 'handle SIG38 noprint' >> gdb.cfg &&"
CMD+="echo 'set follow-fork-mode child' >> gdb.cfg &&"
CMD+="echo 'set detach-on-fork off' >> gdb.cfg &&"
CMD+="echo 'run' >> gdb.cfg &&"
CMD+="gdb -x gdb.cfg --args"
fi
CMD+=" \${BUILD}/qemu-${BUILDTYPE}/${ARCH}-s2e${SP}-softmmu/qemu-system-${ARCH}"
CMD+=" -drive file=\${IMAGE}/image.raw.s2e,cache=writeback,format=s2e"
CMD+=" -net none -net nic,model=e1000"
CMD+=" -s2e-config-file \${IMAGE}/s2e-config.lua"
CMD+=" -loadvm ready"
;;
*)
echo 'Invalid target type'; exit 1
;;
esac
# add common qemu options
CMD+=" -m ${QEMU_MEMORY}"
CMD+=" -serial file:serial.txt"
CMD+=" -monitor telnet:localhost:${QPORT},server,nowait"
CMD+=" ${USB} -k en-us"
if [ ${VNC} -ne -1 ]; then
CMD+=" -vnc :${VNC}"
else
CMD+=" -nographic"
fi
if [ ${SSH_REDIR} -ne -1 ]; then
CMD+=" -redir tcp:${SSH_REDIR}::22"
fi
CMD+=" ${S2E_OPTS}"
CMD+=" ${QEMU_ARGS}"
close_qemu()
{
echo 'Closing QEMU'
trap - INT
ssh ${REMOTE_HOST} "
expect <<-EOF
set send_slow {1 .01}
log_user 0
spawn telnet localhost ${QPORT}
expect {
\"(qemu) \"
}
send -s \"termpg\n\n\"
send -s \"q\n\n\"
send_user \"\n\"
EOF"
}
#
#
#
# start VNC connection loop
if [ ${VNC} -ne -1 ]; then
SSH_OPTS+=" -o ExitOnForwardFailure=yes -L ${VNCPORT}:localhost:${VNCPORT}"
VNCLOOP=
VNCLOOP+="for i in {0..19}; do"
VNCLOOP+=" echo 'Connecting to VNC display ${VNC}...';"
VNCLOOP+=" ssh ${REMOTE_HOST} 'netstat -lnt' | grep ${VNCPORT} > /dev/null && break;"
VNCLOOP+=" sleep 0.5;"
VNCLOOP+="done;"
VNCLOOP+="vncviewer localhost:${VNC} >/dev/null 2>&1;"
bash -c "${VNCLOOP}" &
VNCLOOP_PID=$!
fi
# quit qemu on Ctrl+C
trap close_qemu INT
# launch qemu on remote host
ssh ${SSH_OPTS} ${REMOTE_HOST} "${CMD}" || true
# kill VNC connection loop
if [ ${VNC} -ne -1 ]; then
echo 'Closing VNC'
kill ${VNCLOOP_PID} 2>/dev/null || true
fi