-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkhbootapp
executable file
·354 lines (308 loc) · 8.56 KB
/
khbootapp
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/bash
#set -x
#####################################
#
# Boston University
# Project kittyhawk
#
# simple script for booting nodes in various
# configurations of networks. Originally derived from bootramdisk
# but now with signifiant modifications
#
#####################################
if [[ $BASH_VERSION != 3.1.* ]]; then shopt -s compat31; fi
declare num
declare disk
declare addresses
declare port
declare sleep
declare nettype
declare gwapp
declare gwkey
declare kernel
declare cookieprefix
declare workercookie
declare gwcookie
# to support tunnelling you must set to the host
# that you want tunnels made through
#declare tunhost=bgpsn2.us.ibm.com
declare tunhost
function usage
{
echo -e "\`$(basename $0)' boots ramdisks\n" \
"\n" \
"usage: $(basename $0) [-x|-i|-p] [-k gwpublickey] [-u user] [-c cookiefileprefix] -K [kernel] NUM DISK [ADDRESSES] [PORT] [SLEEP]\n" \
"\n" \
"example: $(basename $0) 1 http://kittyhawk.bu.edu/Appliances/worker.cpio\n"
}
defaults()
{
if onBG; then
nettype=internal
else
nettype=external
fi
sleep=45
}
processargs()
{
local origargs="$@"
local -i optcount=0
while getopts "ixpk:K:u:c:h" OPT
do
case $OPT in
("i") nettype=internal; (( optcount=optcount + 1));;
("x") nettype=external; (( optcount=optcount + 1));;
("p") nettype=private; (( optcount=optcount + 1));;
("k") gwkey=$OPTARG; (( optcount=optcount + 2 ));;
("u") USER=$OPTARG; (( optcount=optcount + 2 ));;
("K") kernel=$OPTARG; (( optcount=optcount + 2));;
("c") cookieprefix=$OPTARG; (( optcount=optcount + 2 ));;
("h") usage; exit -1;;
esac
done
shift $optcount
if [ $# -eq 0 -o -f "$1" ]; then
usage
exit 1
fi
num="$1"
disk="$2"
addresses="$3"
port="$4"
sleep="$5"
if [[ -n $kernel && ! -a $kernel ]]; then
echo "ERROR: $kernel does not exist" > /dev/stderr
exit 1
fi
if [[ -n $kernel && $(file -b $kernel) != "u-boot/PPCBoot image" ]]; then
echo "ERROR: $kernel does not seem to be u-boot image" > /dev/stderr
exit 1
fi
}
onBG()
{
[[ -a /proc/device-tree/u-boot-env ]]
return $?
}
setfdtdate()
{
local khalloc="$1"
local now=$(date -u)
local year=$(date -u -d "$now" +"%-Y")
local fdtdate="$(printf 'fdt mknode / rtc && fdt set /rtc second "[00 00 00 %02x]" && fdt set /rtc minute "[00 00 00 %02x]" && fdt set /rtc hour "[00 00 00 %02x]" && fdt set /rtc month "[00 00 00 %02x]" && fdt set /rtc mday "[00 00 00 %02x]" && fdt set /rtc year "[00 00 %02x %02x]"\n' `date -u -d "$now" +"%-S %-M %-H %-m %-d"` $(( $year / 256 )) $(( $year % 256 )))"
echo "$khalloc" | khdo write "$fdtdate"
}
function getdisk
{
echo $disk | grep -q 'http://' && {
cpio=$(echo $disk | sed 's,.*/,,')
rm -f $cpio
echo "fetching $disk"
wget -q $disk || exit 1
disk=$cpio
}
file $disk | grep -q "gzip compressed data" && {
mkramdiskelf $disk || exit 1
disk=${disk}.uimg
}
file $disk | grep -q "cpio archive" && {
mkramdiskelf $disk || exit 1
disk=${disk}.gz.uimg
}
test -f $disk || {
echo "error: cannot find $disk"
exit 1
}
if [[ $(file -b $disk) != "u-boot/PPCBoot image" ]]; then
echo "ERROR: disk does not seem to be u-boot image" > /dev/stderr
fi
}
function recaddrs
{
local khalloc="$1"
addrs="$(echo "$khalloc" | khdo peripcmd "echo %ip%")"
if [[ -n $addrs ]]; then
if [ -n "$addresses" ]; then
echo "$addrs" >> $addresses
sync
fi
echo $addrs
else
echo "Boot failed"
exit 1
fi
}
function configeth
{
local khalloc="$1"
local net="$2"
local ifs="$3"
local con=$(echo "$khalloc" | khdo concmd "echo %con%")
local wpid
local whenout=$(mktemp /tmp/whenout.XXX)
local gwiip
local gwimask
for i in $ifs
do
itype=${i%%:*}
ethif=${i##*:}
if [[ $itype = "External" ]]; then
echo "$khalloc" | khdo write "setenv $ethif \"\$xip \$xmask\""
else
if [[ $itype = "Internal" ]]; then
echo "$khalloc" | khdo write "setenv $ethif \"\$iip \$imask\""
else
echo "$khalloc" | khdo write "setenv $ethif \"\$p0ip \$p0mask\""
fi
fi
done
when "U-Boot" /dev/null ssh $con > $whenout &
wpid=$(jobs -p %%)
# FIXME : and a real handshake here
sleep 0.5
echo "$khalloc" | khdo write "echo ips=\$iip \$p0ip; ver"
wait $wpid
while read line
do
if [[ "$line" =~ "^ips=(.*) (.*)$" ]]; then
gwiip=${BASH_REMATCH[1]}; gwpip=${BASH_REMATCH[2]}
fi
done < $whenout
echo $gwiip $gwpip
rm $whenout
}
function bootnodes
{
local khalloc="$1"
local rdisk="$2"
local kimage="$3"
if [[ -n $kimage ]]; then
echo "loading kernel: $kimage ..." > /dev/stderr
echo "$khalloc" | khdo loadkernel $kimage >/dev/null 2>&1
fi
echo "loading ramdisk: $rdisk ..." > /dev/stderr
echo "$khalloc" | khdo loadramdisk $rdisk >/dev/null 2>&1
setfdtdate "$khalloc"
echo "$khalloc" | khdo write 'imi $ramfsaddr && run kboot'
}
function pingnode
{
local ip="$1"
echo -n "pinging $1 ... "
for ((i=0;i<16;i++)); do
ping -q -c 1 $ip >/dev/null 2>&1 && echo -n "alive" && break
done
echo
}
function mktunnel
{
local ip="$1"
if [[ -n "$tunhost" && -n "$port" ]]; then
ssh -f -g -L $port:$ip:22 -o StrictHostKeyChecking=no $tunhost "sleep $sleep"
echo "ssh root@$tunhost -p $port" > ssh
fi
}
function gatewayedhosts
{
local net="$1"
local numw
local workers
local gateway
local gwoaddrs
local gwxaddr
local gwiaddr
numw=$num
if (( $numw > 0 )); then
if [[ $net = "internal" ]]; then workers=$(khget -i $USER $numw); fi
if [[ $net = "private" ]]; then workers=$(khget $USER $numw); fi
echo "$workers" > $workercookie
recaddrs "$workers"
bootnodes "$workers" "$disk" "$kernel"
gateway=$(khget -x -i $USER 1)
echo "$gateway" > $gwcookie
gwxaddr=$(echo "$gateway" | khdo peripcmd "echo %ip%")
ifs=$(echo "$gateway" | khdo pernetcmd "echo %net%:%if%")
gwoaddrs="$(configeth "$gateway" $nettype "$ifs")"
gwiaddr=${gwoaddrs%% *}
if [[ -n "$addresses" ]]; then
echo "$gwxaddr $gwoaddrs" >> $addresses
fi
echo $gwxaddr $gwoaddrs
bootnodes "$gateway" "$gwapp"
if onBG ; then
pingnode $gwiaddr
else
pingnode $gwxaddr
fi
if [[ -n $gwkey ]]; then
echo "$gateway" | khdo writecon "echo '$gwkey' >> ~root/.ssh/authorized_keys"
fi
if ! onBG; then mktunnel $gwxaddr; fi
fi
return 0
}
function nogwhosts
{
local net="$1"
if (( $num > 0 )); then
if [[ $net = "internal" ]]; then workers=$(khget -i $USER $num); fi
if [[ $net = "external" ]]; then workers=$(khget -x $USER $num); fi
if [[ -z $workers ]];then return -1; fi
echo "$workers" > $workercookie
recaddrs "$workers"
addrs=( $(echo "$workers" | khdo peripcmd "echo %ip%") )
bootnodes "$workers" "$disk" "$kernel"
if onBG && [[ $net = "external" ]]; then
echo "Skipping ping of external hosts from inside BG"
else
pingnode ${addrs[0]}
fi
if ! onBG; then mktunnel $gwxaddr; fi
fi
return 0
}
function privatehosts
{
return 0
}
# main
defaults
processargs "$@"
if ! onBG && [[ -z "$khctlserver" ]]; then
echo "error: khctlserver environment variable must be set"
exit 1
else
export khctlserver
fi
appdir=$(type -p $0)
appdir=${appdir%/*}
appdir=$appdir/../appliances
gwapp=$appdir/khgw/khgw.cpio.gz.uimg
if [[ ! -a $gwapp ]]; then
echo "error: can't find $gwapp your install seems broken"
exit -1
fi
if [[ -n $cookieprefix ]]
then
workercookie=${cookieprefix}.workers
gwcookie=${cookieprefix}.gw
else
workercookie=${USER}.$num.workers
gwcookie=${USER}.$num.gw
fi
getdisk
if [[ -n "$addresses" ]]
then
cat /dev/null > $addresses
fi
case "$nettype" in
"internal")
if onBG; then nogwhosts "internal"; else gatewayedhosts "internal"; fi;;
"external")
nogwhosts "external";;
"private")
gatewayedhosts "private";;
*) echo "ERROR: unknown nettype=$nettype" >&2; exit -1
esac
exit 0