-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkhctl.old
More file actions
executable file
·627 lines (552 loc) · 13.9 KB
/
khctl.old
File metadata and controls
executable file
·627 lines (552 loc) · 13.9 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#!/bin/bash
set -x
#####################################
#
# IBM Coporation
# Project kittyhawk
#
# mkQImg
# Create qemu bootable debian disk
# images.
#
#####################################
# global constants and defaults all can be overridden
# by setting a env variable of the same name with KHCTL
# prevfixed.
# FIXME: May want to change more of these into command line arguments
# and use these as default: eg. kernel and additionals :-)
declare -r KHBASEDIR=${KHCTL_BASEDIR:-/home/kh}
declare -r KHCTLDIR=${KHCTL_CTLDIR:-$KHBASEDIR/Ctl}
declare -r KHCTLLOCK=${KHCTL_CTLLOCK:-$KHCTLDIR/Lock}
declare -r KHNAMESPACEDIR=${KHCTL_NAMESPACEDIR:-$KHBASEDIR/Nodes}
declare -r KHFREEDIR=${KHCTL_FREEDIR:-$KHNAMESPACEDIR/Free}
declare -r KHALLOCDIR=${KHCTL_ALLOCDIR:-$KHNAMESPACEDIR/Allocated}
declare -r KHCONIDFILE=${KHCTL_CONIDFILE:-$KHCTLDIR/ConID}
declare -r KHCONIDSTART=${KHCTL_CONIDSTART:-1}
declare -r KHCONIDEND=${KHCTL_CONIDEND:-$(printf "%d" 0xffffffff)}
declare -r KHPNETIDFILE=${KHCTL_PNETIDFILE:-$KHCTLDIR/PNetID}
declare -r KHPNETIDSTART=${KHCTL_PNETIDSTART:-1}
declare -r KHPNETIDEND=${KHCTL_PNETIDEND:-$(printf "%d" 0xffffffff)}
declare -r KHTTYPROCDIR=${KHCTL_TTYPROCDIR:-/proc/sys/dev/bgtty}
declare -r KHUSERPREFIX=${KHCTL_USERPREFIX:-con}
declare -r KHTOTALSIZEFILE=${KHCTL_TOTALSIZEFILE:-/proc/device-tree/u-boot-env/bgp_totalnodes}
declare -r KHRANKFILE=${KHCTL_RANKFILE:-/proc/device-tree/u-boot-env/bgp_rank}
declare -r KHCTRLTTY=${KHCTLL_CTRLTTY:-/dev/bgtty0}
declare -r KHCTRLID=${KHCTL_CTRLID:-0}
declare -r KHTTYMAJOR=${KHCTL_TTYMAJOR:-230}
# I know this is a little excessive but it is my habit ;-)
declare -r MKNOD=${KHCTL_MKNOD:-mknod}
declare -r ADDUSER=${KHCTL_MKNOD:-adduser}
declare -r IFCONFIG=${KHCTL_IFCONFIG:-/sbin/ifconfig}
declare -r LS=${KHCTL_LS:-/bin/ls}
declare -r WC=${KHCTL_WC:-wc}
declare -r GREP=${KHCTL_GREP:-grep}
declare -r CAT=${KHCTL_CAT:-cat}
declare -r MKDIR=${KHCTL_MKDIR:-mkdir}
declare -r STTY=${KHCTL_STTY:-stty}
declare -r RM=${KHCTL_RM:-rm}
typeset dofunc
usage()
{
local func=$1
if [[ -z $func ]]
then
echo "USAGE: $(basename $0) func args" >&2
$GREP '^function' $0
else
case "$func" in
'acquireNodes')
echo "USAGE: $(basename $0) acquireNodes [-n netid] [-p] [-i] [-x] [-c credentials] user [num of nodes]" >&2
echo " -n netid : add the nodes to the existing Private network identified" >&2
echo " by netid" >&2
echo " -p : create a new private network for the nodes" >&2
echo " -i : add nodes to the internal public network" >&2
echo " -x : add nodes to the external network" >&2
echo " -c cred : associated credentials with user" >&2
echo " user : user to get the nodes for" >&2
echo " num of nodes: number of nodes to get default is 1" >&2
;;
*)
usage
;;
esac
fi
}
processargs()
{
if [[ $# == 0 ]]
then
usage
exit -1
fi
dofunc=$1
}
function myIPAddrs
{
local net
local ip
local rest
local rc
# If no argument all configured ip address are returned
# otherwise only for the speccified interface
$IFCONFIG $1 2> /dev/null | $GREP 'inet addr' | while read net ip rest
do
ip=${ip##addr:}
if [[ $ip != 127.0.0.1 ]]
then
echo $ip
fi
done
}
function myInterfaces
{
local iface
local rest
$IFCONFIG | $GREP Link | while read iface rest
do
echo $iface
done
}
function interfaceAddr
{
local iface
local ip
if [[ -z $1 ]]
then
echo "USAGE: internfaceAddr <ip>" >&2
return -1
fi
for iface in $(myInterfaces)
do
ip=$($IFCONFIG $iface | $GREP 'inet addr')
ip=${ip#*:}
ip=${ip%% *}
if [[ $ip = "$1"* ]]
then
echo $iface
return 0
fi
done
return -1
}
function getTotalNodes
{
local rc
if [[ -a $KHTOTALSIZEFILE ]]
then
rc=$($CAT $KHTOTALSIZEFILE)
fi
echo $rc
}
function getMyRank
{
local rc
if [[ -a $KHRANKFILE ]]
then
rc=$($CAT $KHRANKFILE)
fi
echo $rc
}
configNodes()
{
local khuser=$1
local pnetid=$2
local inet=$3
local xnet=$4
local conid=$5
local num=$6
local nodes="$7"
local n
if [[ ! -c $KHCTRLTTY ]]
then
if [[ -a $KHCTRLTTY ]]; then $RM -rf $KHCTRLTTY; fi
$MKNOD $KHCTRLTTY c $KHTTYMAJOR $KHCTRLID
$STTY raw -clocal cread -echo -echok -echoe < $KHCTRLTTY
fi
#FIXME use isquish to speed this up
for n in $nodes
do
echo "itest \$bgp_rank == $n && setenv bgtty_sendid $conid" > $KHCTRLTTY
for (( i=0; i<10; i++ )); do i=$i; done
echo "itest \$bgp_rank == $n && setenv bgtty_rcvid $conid" > $KHCTRLTTY
for (( i=0; i<10; i++ )); do i=$i; done
done
}
function createTTY
{
local khuser=$1
local conid=$2
local nodes="$3"
local dir=$KHALLOCDIR/$khuser/consoles/$conid
local tty=$dir/tty
local procfile=$KHTTYPROCDIR/$conid
if [[ -d $dir ]]
then
echo "ERROR: $dir already exists?!?" >&2
return -1
fi
mkdir -p $dir
echo "$nodes" > $dir/nodes
$MKNOD $tty c $KHTTYMAJOR $conid
$STTY raw -clocal cread -echo -echok -echoe < $tty
if (( $num == 1 ))
then
echo "FIXME: using broadcast just for one node :-(" >&2
echo "$conid $conid b" > $procfile
else
echo "$conid $conid b" > $procfile
fi
return 0
}
function createConUser
{
local khuser=$1
local pnetid=$2
local conid=$3
local num=$4
local nodes="$5"
local dir=$KHALLOCDIR/$khuser/consoles/$conid
local conuser=$KHUSERPREFIX${conid}
local home=$dir/$conuser
local shell=$dir/conshell
local tty=$dir/tty
if [[ ! -d $dir ]]
then
echo "ERROR: createConUser: $dir does not exist?!?" >&2
return -1
fi
if ! $MKDIR $home
then
echo "ERROR: createConUser: could not make $home?!?" >&2
return -1
fi
if grep "^$conuser:" /etc/passwd
then
echo "ERROR: $conuser already in /etc/passwd" >&2
return -1
fi
cat >$shell <<EOF
#!/bin/bash
$STTY raw -clocal cread -echo -echok -echoe < $tty
if tty > /dev/null
then
echo "$conuser: Console"
$CAT $tty > /dev/tty &
fi
$CAT > $tty
EOF
echo "$conuser::65534:65534:nobody:$home:$shell" >> /etc/passwd
chown $conuser $tty
chmod u+x $shell
chown $conuser $shell
}
assignNodes()
{
local khuser=$1
local pnetid=$2
local inet=$3
local xnet=$4
local conid=$5
local numNodes=$6
local nodes="$7"
local conuser=$KHUSERPREFIX${conid}
local conip
local i
if createTTY $khuser $conid "$nodes"
then
if createConUser $khuser $pnetid $conid $num "$nodes"
then
if configNodes $khuser $pnetid $inet $xnet $conid $num "$nodes"
then
if [[ -n $SSH_CONNECTION ]]
then
conip=${SSH_CONNECTION% *}
conip=${conip##* }
echo $conuser@$conip
else
for i in $(myInterfaces)
do
echo $conuser$(myIPAddrs $i)
done
fi
echo "Private Network ID : $pnetid"
echo "Private Network Interface: eth?"
if (( $inet == 1 ))
then
echo "Internal Public Network Inteface: eth?"
fi
if (( $xnet == 1 ))
then
echo "External Network Interface: eth?"
fi
echo "Nodes:"
echo "$nodes"
return 0
fi
fi
fi
return -1
}
function acquireCtl
{
if [[ ! -d $KHCTLDIR ]]
then
if ! $MKDIR -p $KHCTLDIR
then
echo "ERROR: $KHCTLDIR does not exist and cannot be created" >&2
exit -1
fi
fi
# Acquire lock
lockfile -s1 $KHCTLLOCK
}
function releaseCtl
{
# Release lock
rm -f $KHCTLLOCK
}
locked_initFreeDir()
{
local maxnoderank
local nodes
local myrank
if [[ ! -d $KHFREEDIR ]]
then
$MKDIR -p $KHFREEDIR
# nodes are zero indexed so we start from zero and go to number of nodes - 1
maxnoderank=$(($(getTotalNodes) - 1))
nodes=$(seq 0 $maxnoderank)
if [[ -z $nodes ]]
then
echo "ERROR: initFreeDir failed to create free set of nodes : $nodes" >&2
return -1
fi
(cd $KHFREEDIR; $MKDIR $nodes)
myrank=$(getMyRank)
if (( $myrank <= $maxnoderank ))
then
# remove myself from free set of nodes
if [[ -d $KHFREEDIR/$myrank ]]
then
$RM -rf $KHFREEDIR/$myrank
else
echo "ERROR: initFreeDir failed to remove myself $myrank from node set" >&2
fi
fi
fi
return 0
}
locked_getFreeNodeList()
{
if [[ ! -d $KHFREEDIR ]]
then
if ! locked_initFreeDir
then
return -1
fi
fi
echo "$($LS -1 $KHFREEDIR 2> /dev/null | /usr/bin/sort -n)"
return 0
}
locked_getNewConID()
{
local rc
if [[ ! -e $KHCONIDFILE ]]
then
echo $KHCONIDSTART > $KHCONIDFILE
fi
rc=$($CAT $KHCONIDFILE)
if (( $rc == $KHCONIDEND ))
then
echo "ERROR: locked_getNewConID: last id reached: NYI" >&2
return -1
fi
echo $(( $rc + 1 )) > $KHCONIDFILE
echo $rc
return 0
}
locked_getNewPNetID()
{
local rc
if [[ ! -e $KHPNETIDFILE ]]
then
echo $KHPNETIDSTART > $KHPNETIDFILE
fi
rc=$($CAT $KHPNETIDFILE)
if (( $rc == $KHPNETIDEND ))
then
echo "ERROR: locked_getNewPNetID: last id reached: NYI" >&2
return -1
fi
echo $(( $rc + 1 )) > $KHPNETIDFILE
echo $rc
return 0
}
locked_undoConID()
{
local conid=$1
local rc
local cur
if [[ -e $KHCONIDFILE ]]
then
cur=$($CAT $KHCONIDFILE)
# sanity check
if (( ($conid + 1) == $cur ))
then
echo $conid > $KHCONIDFILE
return 0
fi
fi
return -1
}
function validkhuser
{
local khuser=$1
if [[ "$khuser" =~ "^[a-zA-Z]+[a-zA-Z0-9]*$" ]]
then
return 0
else
return -1
fi
}
function acquireNodes
{
local rc
local num=$1
local freelist
local freecount
local nodes
local khuser
local conid
local createpnet
local pnetid
local inet
local xnet
local cred
local curpnetarray
local curnumpnets
local origargs="$@"
local -i optcount=0
inet=0
xnet=0
createpnet=0
while getopts "n:pixc:h" OPT
do
case $OPT in
("n") pnetid="$OPTARG"; (( optcount=optcount + 2));;
("p") createpnet=1; (( optcount=optcount + 1));;
("i") inet=1; (( optcount=optcount + 1));;
("x") xnet=1; (( optcount=optcount + 1));;
("c") cred="$OPTARG"; (( optcount=optcount + 2));;
("h") usage acquireNodes; exit -1;;
esac
done
shift $optcount
khuser=$1
if [[ -z $khuser ]]
then
echo "ERROR: must specify user" >&2
usage acquireNodes
exit -1
fi
if ! validkhuser $khuser
then
echo "ERROR: invalid user $khuser" >&2
usage acquireNodes
exit -1
fi
num=$2
if [[ -z $num ]]
then
num=1
fi
acquireCtl
# taking the easy way out for the moment and using one big fing lock ;-) FIXME ;-)
# well don't actually since the idea is to throw this away and move to a fully distributed
# mechanism implemented on the control network
if [[ ! -d $KHALLOCDIR ]]
then
$MKDIR -p $KHALLOCDIR
fi
if [[ -n $pnetid && ! -d $KHALLOCDIR/$khuser/pnets/$pnetid ]]
then
echo "ERROR: acquireNodes: netid $pnetid does not belong to you ($khuser/$pnetid)" >&2
releaseCtl
return -1
fi
if [[ ! -d $KHALLOCDIR/$khuser ]]
then
if [[ -n $cred ]]
then
echo "$cred" > $KHALLOCDIR/$khuser/cred
fi
# for each new user we create an private network for them that will be used
# we ignore the any netid that was specified
pnetid=$(locked_getNewPNetID)
if (( $? != 0 ))
then
echo "ERROR: acquireNodes: unable to create a new private network for you :-(" >&2
releaseCtl
return -1
fi
$MKDIR -p $KHALLOCDIR/$khuser/pnets/$pnetid
fi
# this code releys on the fact that pnets dir exits and will never be completely empty
# which the above ensures
curpnetarray=( $KHALLOCDIR/$khuser/pnets/* )
curpnetarray=( ${curpnetarray[*]##*/} )
curnumpnets=${#curpnetarray[*]}
if [[ -z $pnetid ]]
then
if (( $createpnet == 0 ))
then
if (( $curnumpnets == 1 ))
then
pnetid=${curpnetarray[0]}
else
echo "ERROR: acquireNodes: you have more that one private network. You must "\
" specify one of you existing networks with (${curpnetarray[*]}) with -n or "\
" use -p to specify that you want a new private network created." >&2
releaseCtl
return -1
fi
else
pnetid=$(locked_getNewPNetID)
if (( $? != 0 ))
then
echo "ERROR: acquireNodes: unable to create a new private network for you :-(" >&2
releaseCtl
return -1
fi
$MKDIR -p $KHALLOCDIR/$khuser/pnets/$pnetid
fi
fi
conid=$(locked_getNewConID)
if (( $? != 0 ))
then
echo "ERROR: acquireNodes: unable to create a new console channel for you :-(" >&2
releaseCtl
return -1
fi
freelist=$(locked_getFreeNodeList)
if (( $? != 0 )); then exit -1; fi
freecount=$(echo $freelist | $WC -w)
if (( $freecount >= $num ))
then
nodes="$(echo "$freelist" | head -$num)"
$MKDIR -p $KHALLOCDIR/$khuser/pnets/$pnetid/Nodes
(cd $KHFREEDIR;mv $nodes $KHALLOCDIR/$khuser/pnets/$pnetid/Nodes)
fi
releaseCtl
if [[ -z $nodes ]]
then
echo "no nodes available"
return -1;
fi
assignNodes $khuser $pnetid $inet $xnet $conid $num "$nodes"
return 0;
}
processargs "$@"
shift
$dofunc "$@"
exit $?