-
Notifications
You must be signed in to change notification settings - Fork 1
/
aboriginal-start
executable file
·233 lines (198 loc) · 5.56 KB
/
aboriginal-start
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
SB_DIRECTORY=
SB_EMULATOR=
SB_INTERACTIVE=false
SB_WORKDIR=
SB_SILENT=false
SB_CPUS=3
#####################################################
# Usage and command line parsing #
#####################################################
function usage () {
echo "Usage: "
echo " aboriginal-start [OPTIONS]"
echo
echo "Starts the emulator so that it's ready to receive build commands"
echo
echo "General Options:"
echo " -h --help Display this help message and exit"
echo " -e --emulator <path> Directory of the aboriginal system image"
echo " -d --directory <directory> Directory to share with the aboriginal emulator"
echo " -w --workdir <directory> Directory to keep some state"
echo " -c --cpus <number> Amount of distcc processes to export for the build slave"
echo " -s --silent Run silently unless an error occurs"
echo " -i --interactive Run an interactive shell instead of launching the interpretor"
echo
}
while : ; do
case "$1" in
-h|--help)
usage;
exit 0;
shift ;;
-d|--directory)
SB_DIRECTORY=${2};
shift 2 ;;
-w|--workdir)
SB_WORKDIR=${2};
shift 2 ;;
-e|--emulator)
SB_EMULATOR=${2};
shift 2 ;;
-c|--cpus)
SB_CPUS=${2};
shift 2 ;;
-s|--silent)
SB_SILENT=true;
shift ;;
-i|--interactive)
SB_INTERACTIVE=true;
shift ;;
*)
break ;;
esac
done
#####################################################
# Sanity Checking #
#####################################################
function isNumber() {
if [[ $1 != *[!0-9]* ]]; then
echo "yes"
else
echo "no"
fi
}
function ensurePipe() {
if [ ! -p ${1} ]; then
mkfifo ${1}
if [ "$?" -ne "0" ]; then
echo "Failed to named pipe at '${1}'"
exit 1;
fi
fi
# Clear pipes at startup
dd if=${1} iflag=nonblock of=/dev/null > /dev/null 2>&1
}
if [ ! -f ${CONTROL_IMAGE} ]; then
echo "Control image could not be found at '${CONTROL_IMAGE}'"
exit 1;
fi
if [ -z "${SB_DIRECTORY}" ]; then
echo "Must specify the directory to share with the emulator"
echo
usage
exit 1;
elif [ ! -d ${SB_DIRECTORY} ]; then
echo "The specified share directory '${SB_DIRECTORY}' is not a directory"
echo
usage
exit 1;
fi
if [ -z "${SB_WORKDIR}" ]; then
echo "Must specify the directory to store sandbox running state"
echo
usage
exit 1;
elif [ ! -d ${SB_WORKDIR} ]; then
mkdir -p ${SB_WORKDIR};
if [ "$?" -ne "0" ]; then
echo "Failed to create workdir at '${SB_WORKDIR}'"
exit 1;
fi
fi
if [ -z "${SB_EMULATOR}" ]; then
echo "Must specify the aboriginal emulator directory"
echo
usage
exit 1;
elif [ ! -d ${SB_EMULATOR} ]; then
echo "The specified emulator '${SB_EMULATOR}' is not a directory"
echo
usage
exit 1;
fi
if [ "$(isNumber ${SB_CPUS})" == "no" ]; then
echo "The --cpus option must be a numeric value"
echo
usage
exit 1;
fi
# Source the common protocol definition and work directory state
#
SB_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. ${SB_PATH}/control/bin/protocol
. ${SB_PATH}/aboriginal-state
# Early abort if we're already running
#
if [ "$(isRunning)" == "1" ]; then
if ! $SB_SILENT; then
echo "Aboriginal sandbox for work directory ${SB_WORKDIR} is already running at pid $(cat ${PID_FILE})";
fi
exit 0;
fi
ensurePipe ${COMM_IN}
ensurePipe ${COMM_OUT}
#####################################################
# Utilities #
#####################################################
function waitReady() {
local tries=0
local ready=false
local readAck="z"
# Here we give it a VERY LONG TIME to come up and be ready,
# this is because when starting a work directory for the first
# time it must create a 1GB swap file which takes some time in
# the emulator to do.
#
while [ "${tries}" -lt 100 ]; do
read -r readAck < ${COMM_IN};
if [ "${readAck}" == "${SB_PROTO_READY}" ]; then
ready=true;
break;
fi
let tries=${tries}+1;
done
if ! $ready; then
echo "Unable to ready ready byte from aboriginal sandbox"
exit 1;
fi
}
#####################################################
# Main #
#####################################################
CONTROL_IMAGE="$SB_PATH/control.sqf"
HDB=${IMAGE_FILE}
HDC=$(realpath ${CONTROL_IMAGE})
EMULATOR_DIR=$(realpath ${SB_EMULATOR})
BUILD_PAUSE=0
# Setup virtfs sharing
#
SHARE_DIR=$(realpath ${SB_DIRECTORY})
QEMU_EXTRA="-virtfs local,path=${SHARE_DIR},security_model=mapped-file,mount_tag=share"
# Setup virtio serial device
#
QEMU_EXTRA=$QEMU_EXTRA" -device virtio-serial "
QEMU_EXTRA=$QEMU_EXTRA" -chardev pipe,path=${COMM_OUT},id=comm0"
QEMU_EXTRA=$QEMU_EXTRA" -device virtserialport,chardev=comm0,name=serial0"
QEMU_EXTRA=$QEMU_EXTRA" -chardev pipe,path=${COMM_IN},id=comm1"
QEMU_EXTRA=$QEMU_EXTRA" -device virtserialport,chardev=comm1,name=serial1"
#
# Run the emulator through aboriginal's dev-environment script
#
export CPUS=${SB_CPUS}
export HDB HDC QEMU_EXTRA BUILD_PAUSE DISTCC_LOG
cd ${EMULATOR_DIR}
if $SB_INTERACTIVE; then
export KERNEL_EXTRA="INTERACTIVE=1"
./dev-environment.sh
else
export NOCONSOLE="1"
./dev-environment.sh > ${QEMU_LOG} 2>&1 &
ABORIGINAL_PID=$!
# Wait for the emulator to come up
waitReady
if ! $SB_SILENT; then
echo "Sandbox is running at PID ${ABORIGINAL_PID}, ready for commands"
fi
echo ${ABORIGINAL_PID} > ${PID_FILE}
fi