libsetup.pm 66.9 KB
Newer Older
1
2
#!/usr/bin/perl -wT

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
# All rights reserved.
#
8
# TODO: Signal handlers for protecting db files.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9

10
11
12
13
14
15
16
#
# Common routines and constants for the client bootime setup stuff.
#
package libsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
17
    qw ( libsetup_init libsetup_setvnodeid cleanup_node 
18
	 doifconfig dohostnames domounts dotunnels check_nickname
19
	 doaccounts dorpms dotarballs dostartupcmd install_deltas
20
	 bootsetup nodeupdate startcmdstatus whatsmynickname dosyncserver
21
22
	 TBBackGround TBForkCmd vnodejailsetup plabsetup vnodeplabsetup
	 dorouterconfig jailsetup dojailconfig JailedMounts findiface
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
	 tmccdie tmcctimeout libsetup_getvnodeid dotrafficconfig
24
	 ixpsetup
25

26
	 OPENTMCC CLOSETMCC RUNTMCC MFS REMOTE JAILED PLAB LOCALROOTFS IXP
27

28
	 CONFDIR TMCC TMIFC TMDELAY TMRPM TMTARBALLS TMHOSTS TMJAILNAME
29
	 TMNICKNAME HOSTSFILE TMSTARTUPCMD FINDIF TMTUNNELCONFIG
30
	 TMTRAFFICCONFIG TMROUTECONFIG TMLINKDELAY TMDELMAP TMMOUNTDB
31
	 TMPROGAGENTS TMPASSDB TMGROUPDB TMGATEDCONFIG
32
33
	 TMCCCMD_REBOOT TMCCCMD_STATUS TMCCCMD_IFC TMCCCMD_ACCT TMCCCMD_DELAY
	 TMCCCMD_HOSTS TMCCCMD_RPM TMCCCMD_TARBALL TMCCCMD_STARTUP
34
	 TMCCCMD_DELTA TMCCCMD_STARTSTAT TMCCCMD_READY TMCCCMD_TRAFFIC
35
	 TMCCCMD_BOSSINFO TMCCCMD_VNODELIST TMCCCMD_ISALIVE TMCCCMD_LINKDELAYS
36
37
	 TMCCCMD_PROGRAMS TMCCCMD_SUBNODELIST TMCCCMD_SUBCONFIG
	 TMCCCMD_STATE
38
39
40
41
42
43
       );

# Must come after package declaration!
use English;

#
44
45
# For virtual (multiplexed nodes). If defined, tack onto tmcc command.
# and use in pathnames. Used in conjunction with jailed virtual nodes.
46
# I am also using this for subnodes; eventually everything will be subnodes.
47
#
48
my $vnodeid;
49
50
51
52
53
54
55
56
sub libsetup_setvnodeid($)
{
    ($vnodeid) = @_;
}
sub libsetup_getvnodeid()
{
    return $vnodeid;
}
57

58
#
59
# True if running inside a jail. Set just below. 
60
61
62
# 
my $injail;

63
64
65
66
67
#
# True if running in a Plab vserver.
#
my $inplab;

68
69
70
71
72
73
#
# Ditto for IXP, although currently there is no "in" IXP setup; it
# is all done from outside.
#
my $inixp;

74
75
76
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
77
{
78
79
80
81
82
    if (! -e "/etc/emulab/paths.pm") {
	die("Yikes! Could not require /etc/emulab/paths.pm!\n");
    }
    require "/etc/emulab/paths.pm";
    import emulabpaths;
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    #
    # Determine if running inside a jail. This affects the paths below.
    #
    if (-e "$BOOTDIR/jailname") {
	open(VN, "$BOOTDIR/jailname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$injail = 1;
    }

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    # Determine if running inside a Plab vserver.
    if (-e "$BOOTDIR/plabname") {
	open(VN, "$BOOTDIR/plabname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$inplab = 1;
    }

116
117
118
119
120
121
122
    # Make sure these exist!
    if (! -e "$VARDIR/logs") {
	mkdir("$VARDIR", 0775);
	mkdir("$VARDIR/jails", 0775);
	mkdir("$VARDIR/db", 0755);
	mkdir("$VARDIR/logs", 0775);
	mkdir("$VARDIR/boot", 0775);
123
	mkdir("$VARDIR/lock", 0775);
124
    }
125
126
127
}

#
128
129
130
# The init routine. This is deprecated, but left behind in case an old
# liblocsetup is run against a new libsetup. Whenever a new libsetup
# is installed, better install the path module (see above) too!
131
#
132
133
134
135
136
137
138
139
140
sub libsetup_init($)
{
    my($path) = @_;

    $ETCDIR  = $path;
    $BINDIR  = $path;
    $VARDIR  = $path;
    $BOOTDIR = $path
}
141

142
#
143
# This "local" library provides the OS dependent part. 
144
#
145
use liblocsetup;
146

147
148
149
150
#
# These are the paths of various files and scripts that are part of the
# setup library.
#
151
152
153
sub TMCC()		{ "$BINDIR/tmcc"; }
sub TMHOSTS()		{ "$ETCDIR/hosts"; }
sub FINDIF()		{ "$BINDIR/findif"; }
154
sub HOSTSFILE()		{ "/etc/hosts"; }
155
#
156
# This path is valid only *outside* the jail when its setup.
157
# 
158
159
sub JAILDIR()		{ "$VARDIR/jails/$vnodeid"; }

160
161
162
163
164
#
# Also valid outside the jail, this is where we put local project storage.
#
sub LOCALROOTFS()	{ (REMOTE() ? "/users/local" : "$VARDIR/jails/local");}

165
166
167
#
# Okay, here is the path mess. There are three environments.
# 1. A local node where everything goes in one place ($VARDIR/boot).
168
# 2. A virtual node inside a jail or a Plab vserver ($VARDIR/boot).
169
# 3. A virtual (or sub) node, from the outside. 
170
171
172
173
174
#
# As for #3, whether setting up a old-style virtual node or a new style
# jailed node, the code that sets it up needs a different per-vnode path.
#
sub CONFDIR() {
175
    if ($injail || $inplab) {
176
177
178
179
180
181
	return $BOOTDIR;
    }
    if ($vnodeid) {
	return JAILDIR();
    }
    return $BOOTDIR;
182
}
183

184
185
186
187
188
189
190
191
192
193
194
195
196
#
# These go in /var/emulab. Good for all environments!
# 
sub TMMOUNTDB()		{ $VARDIR . "/db/mountdb"; }
sub TMSFSMOUNTDB()	{ $VARDIR . "/db/sfsmountdb"; }
sub TMPASSDB()		{ $VARDIR . "/db/passdb"; }
sub TMGROUPDB()		{ $VARDIR . "/db/groupdb"; }
#
# The rest of these depend on the environment running in (inside/outside jail).
# 
sub TMNICKNAME()	{ CONFDIR() . "/nickname";}
sub TMJAILNAME()	{ CONFDIR() . "/jailname";}
sub TMJAILCONFIG()	{ CONFDIR() . "/jailconfig";}
197
sub TMPLABCONFIG()	{ CONFDIR() . "/rc.plab";}
198
sub TMSTARTUPCMD()	{ CONFDIR() . "/startupcmd";}
199
sub TMPROGAGENTS()	{ CONFDIR() . "/progagents";}
200
201
202
203
sub TMIFC()		{ CONFDIR() . "/rc.ifc"; }
sub TMRPM()		{ CONFDIR() . "/rc.rpm";}
sub TMTARBALLS()	{ CONFDIR() . "/rc.tarballs";}
sub TMROUTECONFIG()     { CONFDIR() . "/rc.route";}
204
sub TMGATEDCONFIG()     { CONFDIR() . "/gated.conf";}
205
206
207
sub TMTRAFFICCONFIG()	{ CONFDIR() . "/rc.traffic";}
sub TMTUNNELCONFIG()	{ CONFDIR() . "/rc.tunnel";}
sub TMVTUNDCONFIG()	{ CONFDIR() . "/vtund.conf";}
208
209
sub TMDELAY()		{ CONFDIR() . "/rc.delay";}
sub TMLINKDELAY()	{ CONFDIR() . "/rc.linkdelay";}
210
sub TMDELMAP()		{ CONFDIR() . "/delay_mapping";}
211
212
sub TMSYNCSERVER()	{ CONFDIR() . "/syncserver";}
sub TMRCSYNCSERVER()	{ CONFDIR() . "/rc.syncserver";}
213

Austin Clements's avatar
Austin Clements committed
214
215
216
217
218
219
220
221
#
# Whether or not to use SFS (the self-certifying file system).  If this
# is 0, fall back to NFS.  Note that it doesn't hurt to set this to 1
# even if TMCD is not serving out SFS mounts, or if this node is not
# running SFS.  It'll deal and fall back to NFS.
#
my $USESFS		= 1;

222
#
223
224
225
226
227
# This is the VERSION. We send it through to tmcd so it knows what version
# responses this file is expecting.
#
# BE SURE TO BUMP THIS AS INCOMPATIBILE CHANGES TO TMCD ARE MADE!
#
228
sub TMCD_VERSION()	{ 12; };
229
230
231

#
# These are the TMCC commands. 
232
233
234
#
sub TMCCCMD_REBOOT()	{ "reboot"; }
sub TMCCCMD_STATUS()	{ "status"; }
235
sub TMCCCMD_STATE()	{ "state"; }
236
237
238
sub TMCCCMD_IFC()	{ "ifconfig"; }
sub TMCCCMD_ACCT()	{ "accounts"; }
sub TMCCCMD_DELAY()	{ "delay"; }
239
sub TMCCCMD_HOSTS()	{ "hostnames"; }
240
241
242
243
244
245
246
sub TMCCCMD_RPM()	{ "rpms"; }
sub TMCCCMD_TARBALL()	{ "tarballs"; }
sub TMCCCMD_STARTUP()	{ "startupcmd"; }
sub TMCCCMD_DELTA()	{ "deltas"; }
sub TMCCCMD_STARTSTAT()	{ "startstatus"; }
sub TMCCCMD_READY()	{ "ready"; }
sub TMCCCMD_MOUNTS()	{ "mounts"; }
247
sub TMCCCMD_ROUTING()	{ "routing"; }
248
249
sub TMCCCMD_TRAFFIC()	{ "trafgens"; }
sub TMCCCMD_BOSSINFO()	{ "bossinfo"; }
250
sub TMCCCMD_TUNNEL()	{ "tunnels"; }
251
sub TMCCCMD_NSECONFIGS(){ "nseconfigs"; }
252
sub TMCCCMD_VNODELIST() { "vnodelist"; }
253
sub TMCCCMD_SUBNODELIST(){ "subnodelist"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
254
sub TMCCCMD_ISALIVE()   { "isalive"; }
Austin Clements's avatar
Austin Clements committed
255
256
sub TMCCCMD_SFSHOSTID()	{ "sfshostid"; }
sub TMCCCMD_SFSMOUNTS() { "sfsmounts"; }
257
sub TMCCCMD_JAILCONFIG(){ "jailconfig"; }
258
sub TMCCCMD_PLABCONFIG(){ "plabconfig"; }
259
sub TMCCCMD_SUBCONFIG() { "subconfig"; }
260
sub TMCCCMD_LINKDELAYS(){ "linkdelays"; }
261
sub TMCCCMD_PROGRAMS()  { "programs"; }
262
sub TMCCCMD_SYNCSERVER(){ "syncserver"; }
263
264
265
266

#
# Some things never change.
# 
267
my $TARINSTALL  = "/usr/local/bin/install-tarfile %s %s %s";
268
my $VTUND       = "/usr/local/sbin/vtund";
269
270
271

#
# This is a debugging thing for my home network.
272
273
274
275
276
277
278
279
280
281
#
my $NODE = "";
if (defined($ENV{'TMCCARGS'})) {
    if ($ENV{'TMCCARGS'} =~ /^([-\w\s]*)$/) {
	$NODE .= " $1";
    }
    else {
	die("Tainted TMCCARGS from environment: $ENV{'TMCCARGS'}!\n");
    }
}
282
283

# Locals
284
285
286
287
my $pid		= "";
my $eid		= "";
my $vname	= "";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
288
289
290
# Control tmcc error condition and timeout. Dynamic, not lexical!
$tmccdie        = 1; 
$tmcctimeout    = 0;
291
my $TMCCTIMEO   = 30;	# Default timeout on remote nodes. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292

293
294
295
# When on the MFS, we do a much smaller set of stuff.
# Cause of the way the packages are loaded (which I do not understand),
# this is computed on the fly instead of once.
296
sub MFS()	{ if (-e "$ETCDIR/ismfs") { return 1; } else { return 0; } }
297

298
299
300
#
# Same for a remote node.
#
301
302
sub REMOTE()	{ if (-e "$ETCDIR/isrem") { return 1; } else { return 0; } }

303
304
305
306
307
#
# Same for a control node.
#
sub CONTROL()	{ if (-e "$ETCDIR/isctrl") { return 1; } else { return 0; } }

308
309
310
311
#
# Are we jailed? See above.
#
sub JAILED()	{ if ($injail) { return $vnodeid; } else { return 0; } }
312

313
314
315
316
317
#
# Are we on plab?
#
sub PLAB()	{ if ($inplab) { return $vnodeid; } else { return 0; } }

318
319
320
321
322
#
# Are we on an IXP
#
sub IXP()	{ if ($inixp) { return $vnodeid; } else { return 0; } }

323
324
325
326
327
328
329
330
#
# Do not try this on the MFS since it has such a wimpy perl installation.
#
if (!MFS()) {
    require Socket;
    import Socket;
}

331
332
333
334
#
# Open a TMCC connection and return the "stream pointer". Caller is
# responsible for closing the stream and checking return value.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
# usage: OPENTMCC(char *command, char *args, char *options)
336
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
337
sub OPENTMCC($;$$)
338
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
339
    my($cmd, $args, $options) = @_;
340
    my $vn = "";
341
342
343
344
345
    local *TM;

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
346
347
348
    if (!defined($options)) {
	$options = "";
    }
349
    if (defined($vnodeid)) {
350
351
	$vn = "-n $vnodeid";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
352
353
354
    if ($tmcctimeout) {
	$options .= " -t $tmcctimeout";
    }
355

Leigh B. Stoller's avatar
Leigh B. Stoller committed
356
357
    my $foo = sprintf("%s -v %d $options $NODE $vn $cmd $args |",
		      TMCC, TMCD_VERSION);
358

Leigh B. Stoller's avatar
Leigh B. Stoller committed
359
360
361
362
363
    if (!open(TM, $foo)) {
	print STDERR "Cannot start TMCC: $!\n";
	die("\n") if $tmccdie;
	return undef;
    }
364
365
366
    return (*TM);
}

367
368
369
370
371
372
#
# Close connection. Die on error.
# 
sub CLOSETMCC($) {
    my($TM) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
373
374
375
376
377
378
379
380
381
382
383
    if (! close($TM)) {
	if ($?) {
	    print STDERR "TMCC exited with status $?!\n";
	}
	else {
	    print STDERR "Error closing TMCC pipe: $!\n";
	}
	die("\n") if $tmccdie;
	return 0;
    }
    return 1;
384
385
}

386
387
388
#
# Run a TMCC command with the provided arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
389
# usage: RUNTMCC(char *command, char *args, char *options)
390
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
391
sub RUNTMCC($;$$)
392
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
393
    my($cmd, $args, $options) = @_;
394
395
396
397
398
    my($TM);

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
399
400
401
    if (!defined($options)) {
	$options = "";
    }
402
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
403
    $TM = OPENTMCC($cmd, $args, $options);
404
405
406
407
408
409
410
411
412
413

    close($TM)
	or die $? ? "TMCC exited with status $?" : "Error closing pipe: $!";
    
    return 0;
}

#
# Reset to a moderately clean state.
#
414
415
416
sub cleanup_node ($) {
    my ($scrub) = @_;
    
417
    print STDOUT "Cleaning node; removing configuration files ...\n";
418
    unlink TMIFC, TMRPM, TMSTARTUPCMD, TMTARBALLS;
419
    unlink TMROUTECONFIG, TMTRAFFICCONFIG, TMTUNNELCONFIG;
420
    unlink TMDELAY, TMLINKDELAY, TMPROGAGENTS, TMSYNCSERVER, TMRCSYNCSERVER;
421
    unlink TMMOUNTDB . ".db";
422
    unlink TMSFSMOUNTDB . ".db";
423
    unlink "$VARDIR/db/rtabid";
424

425
426
427
428
429
    #
    # If scrubbing, remove the password/group file DBs so that we revert
    # to base set.
    # 
    if ($scrub) {
430
	unlink TMNICKNAME;
431
432
433
434
435
436
437
438
439
440
	unlink TMPASSDB . ".db";
	unlink TMGROUPDB . ".db";
    }

    if (! REMOTE()) {
	printf STDOUT "Resetting %s file\n", HOSTSFILE;
	if (system($CP, "-f", TMHOSTS, HOSTSFILE) != 0) {
	    printf "Could not copy default %s into place: $!\n", HOSTSFILE;
	    exit(1);
	}
441
442
    }

443
    return os_cleanup_node($scrub);
444
445
446
}

#
447
448
# Check node allocation. If the nickname file has been created, use
# that to avoid load on tmcd.
449
450
451
452
453
#
# Returns 0 if node is free. Returns list (pid/eid/vname) if allocated.
#
sub check_status ()
{
454
    my $TM = OPENTMCC(TMCCCMD_STATUS);
455
    $_  = <$TM>;
456
    CLOSETMCC($TM);
457
458

    if ($_ =~ /^FREE/) {
459
	unlink TMNICKNAME;
460
461
462
	return 0;
    }
    
463
    if ($_ =~ /ALLOCATED=([-\@\w]*)\/([-\@\w]*) NICKNAME=([-\@\w]*)/) {
464
465
466
467
468
469
470
471
	$pid   = $1;
	$eid   = $2;
	$vname = $3;
    }
    else {
	warn "*** WARNING: Error getting reservation status\n";
	return 0;
    }
472
473
474
    
    #
    # Stick our nickname in a file in case someone wants it.
475
476
    # Do not overwrite; we want to save the original info until later.
    # See bootsetup; indicates project change!
477
    #
478
    if (! -e TMNICKNAME()) {
479
480
	system("echo '$vname.$eid.$pid' > " . TMNICKNAME());
    }
481
    
482
483
484
485
    return ($pid, $eid, $vname);
}

#
486
487
488
489
# Check cached nickname. Its okay if we have been deallocated and the info
# is stale. The node will notice that later.
# 
sub check_nickname()
490
{
491
492
493
    if (-e TMNICKNAME) {
	my $nickfile = TMNICKNAME;
	my $nickinfo = `cat $nickfile`;
494

495
496
497
498
499
500
501
502
503
	if ($nickinfo =~ /([-\@\w]*)\.([-\@\w]*)\.([-\@\w]*)/) {
	    $vname = $1;
	    $eid   = $2;
	    $pid   = $3;

	    return ($pid, $eid, $vname);
	}
    }
    return check_status();
504
505
506
507
}

#
# Process mount directives from TMCD. We keep track of all the mounts we
508
# have added in here so that we delete just the mounts we added, when
509
510
511
512
513
514
515
516
517
# project membership changes. Same goes for project directories on shared
# nodes. We use a simple perl DB for that.
#
sub domounts()
{
    my $TM;
    my %MDB;
    my %mounts;
    my %deletes;
Austin Clements's avatar
Austin Clements committed
518
519
520
    my %sfsmounts;
    my %sfsdeletes;

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    #
    # Update our SFS hostid first. If this fails, dosfshostid will
    # unset USESFS.
    # 
    if ($USESFS) {
	if (! MFS()) {
	    #
	    # Setup SFS hostid.
	    #
	    print STDOUT "Setting up for SFS ... \n";
	    dosfshostid();
	}
	else {
	    # No SFS on the MFS.
	    $USESFS = 0;
	}
    }

Austin Clements's avatar
Austin Clements committed
539
    $TM = OPENTMCC(TMCCCMD_MOUNTS, "USESFS=$USESFS");
540
541

    while (<$TM>) {
Austin Clements's avatar
Austin Clements committed
542
	if ($_ =~ /^REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
543
544
	    $mounts{$1} = $2;
	}
Austin Clements's avatar
Austin Clements committed
545
546
547
548
549
550
	elsif ($_ =~ /^SFS REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
	    $sfsmounts{$1} = $2;
	}
	else {
	    warn "*** WARNING: Malformed mount information: $_\n";
	}
551
    }
552
553
    CLOSETMCC($TM);
    
554
555
556
557
    #
    # The MFS version does not support (or need) this DB stuff. Just mount
    # them up.
    #
558
    if (MFS()) {
559
560
	while (($remote, $local) = each %mounts) {
	    if (! -e $local) {
561
		if (! os_mkdir($local, "0770")) {
562
563
564
565
566
567
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	
	    print STDOUT "  Mounting $remote on $local\n";
568
569
570
	    if (system("$NFSMOUNT $remote $local")) {
		warn "*** WARNING: Could not $NFSMOUNT ".
		    "$remote on $local: $!\n";
571
572
573
574
575
576
		next;
	    }
	}
	return 0;
    }

577
578
579
580
581
582
583
    dbmopen(%MDB, TMMOUNTDB, 0660);
    
    #
    # First mount all the mounts we are told to. For each one that is not
    # currently mounted, and can be mounted, add it to the DB.
    # 
    while (($remote, $local) = each %mounts) {
584
	if (defined($MDB{$remote})) {
585
586
587
	    next;
	}

588
589
590
591
592
593
	if (! -d $local) {
	    # Leftover SFS link.
	    if (-l $local) {
		unlink($local) or
		    warn "*** WARNING: Could not unlink $local: $!\n";
	    }
594
	    if (! os_mkdir($local, "0770")) {
595
596
597
598
599
600
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
	print STDOUT "  Mounting $remote on $local\n";
601
602
	if (system("$NFSMOUNT $remote $local")) {
	    warn "*** WARNING: Could not $NFSMOUNT $remote on $local: $!\n";
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
628
629
630
631
632
633
634
635
636
637
638
	    next;
	}

	$MDB{$remote} = $local;
    }

    #
    # Now unmount the ones that we mounted previously, but are now no longer
    # in the mount set (as told to us by the TMCD). Note, we cannot delete 
    # them directly from MDB since that would mess up the foreach loop, so
    # just stick them in temp and postpass it.
    #
    while (($remote, $local) = each %MDB) {
	if (defined($mounts{$remote})) {
	    next;
	}

	print STDOUT "  Unmounting $local\n";
	if (system("$UMOUNT $local")) {
	    warn "*** WARNING: Could not unmount $local\n";
	    next;
	}
	
	#
	# Only delete from set if we can actually unmount it. This way
	# we can retry it later (or next time).
	# 
	$deletes{$remote} = $local;
    }
    while (($remote, $local) = each %deletes) {
	delete($MDB{$remote});
    }

    # Write the DB back out!
    dbmclose(%MDB);

Austin Clements's avatar
Austin Clements committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    #
    # Now, do basically the same thing over again, but this time for
    # SFS mounted stuff
    #

    if (scalar(%sfsmounts)) {
	dbmopen(%MDB, TMSFSMOUNTDB, 0660);
	
	#
	# First symlink all the mounts we are told to. For each one
	# that is not currently symlinked, and can be, add it to the
	# DB.
	#
	while (($remote, $local) = each %sfsmounts) {
	    if (-l $local) {
		if (readlink($local) eq ("/sfs/" . $remote)) {
		    $MDB{$remote} = $local;
		    next;
		}
		if (readlink($local) ne ("/sfs/" . $remote)) {
		    print STDOUT "  Unlinking incorrect symlink $local\n";
660
		    if (! unlink($local)) {
Austin Clements's avatar
Austin Clements committed
661
662
663
664
665
			warn "*** WARNING: Could not unlink $local: $!\n";
			next;
		    }
		}
	    }
666
667
668
669
670
671
	    elsif (-d $local) {
		if (! rmdir($local)) {
		    warn "*** WARNING: Could not rmdir $local: $!\n";
		    next;
		}
	    }
Austin Clements's avatar
Austin Clements committed
672
673
674
	    
	    $dir = $local;
	    $dir =~ s/(.*)\/[^\/]*$/$1/;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
675
	    if ($dir ne "" && ! -e $dir) {
Austin Clements's avatar
Austin Clements committed
676
		print STDOUT "  Making directory $dir\n";
677
		if (! os_mkdir($dir, "0755")) {
Austin Clements's avatar
Austin Clements committed
678
679
680
681
682
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	    print STDOUT "  Symlinking $remote on $local\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
683
	    if (! symlink("/sfs/" . $remote, $local)) {
Austin Clements's avatar
Austin Clements committed
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
		warn "*** WARNING: Could not make symlink $local: $!\n";
		next;
	    }
	    
	    $MDB{$remote} = $local;
	}

	#
	# Now delete the ones that we symlinked previously, but are
	# now no longer in the mount set (as told to us by the TMCD).
	# Note, we cannot delete them directly from MDB since that
	# would mess up the foreach loop, so just stick them in temp
	# and postpass it.
	#
	while (($remote, $local) = each %MDB) {
	    if (defined($sfsmounts{$remote})) {
		next;
	    }
	    
	    if (! -e $local) {
		$sfsdeletes{$remote} = $local;
		next;
	    }
	    
	    print STDOUT "  Deleting symlink $local\n";
	    if (! unlink($local)) {
		warn "*** WARNING: Could not delete $local: $!\n";
		next;
	    }
	    
	    #
	    # Only delete from set if we can actually unlink it.  This way
	    # we can retry it later (or next time).
	    #
	    $sfsdeletes{$remote} = $local;
	}
	while (($remote, $local) = each %sfsdeletes) {
	    delete($MDB{$remote});
	}

	# Write the DB back out!
	dbmclose(%MDB);	
    }
    else {
	# There were no SFS mounts reported, so disable SFS
729
	$USESFS = 0;
Austin Clements's avatar
Austin Clements committed
730
731
732
733
734
    }

    return 0;
}

735
#
736
737
738
# Aux function called from the mkjail code to do mounts outside
# of a jail, and return the list of mounts that were created. Can use
# either NFS or local loopback. Maybe SFS someday. Local only, of course.
739
# 
740
sub JailedMounts($$$)
741
{
742
    my ($vid, $rootpath, $usenfs) = @_;
743
    my @mountlist = ();
744
    my $mountstr;
745
746
747
748
749
750
751
752

    #
    # No NFS mounts on remote nodes.
    # 
    if (REMOTE()) {
	return ();
    }

753
754
755
756
757
758
    if ($usenfs) {
	$mountstr = $NFSMOUNT;
    } else {
	$mountstr = $LOOPBACKMOUNT;
    }

759
760
761
762
763
    #
    # Mount same set of existing mounts. A hack, but this whole NFS thing
    # is a serious hack inside jails.
    #
    dbmopen(%MDB, TMMOUNTDB, 0444);
764
    
765
    while (my ($remote, $path) = each %MDB) {
766
	$local = "$rootpath$path";
767
768
769
770
771
772
773
774
	    
	if (! -e $local) {
	    if (! os_mkdir($local, "0770")) {
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
775
776
777
778
	if (! $usenfs) {
	    $remote = $path;
	}

779
	print STDOUT "  Mounting $remote on $local\n";
780
781
	if (system("$mountstr $remote $local")) {
	    warn "*** WARNING: Could not $mountstr $remote on $local: $!\n";
782
783
784
785
	    next;
	}
	push(@mountlist, $path);
    }
786
    dbmclose(%MDB);	   
787
788
    return @mountlist;
}
Austin Clements's avatar
Austin Clements committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#
# Do SFS hostid setup.
# Creates an SFS host key for this node, if it doesn't already exist,
# and sends it to TMCD
#
sub dosfshostid ()
{
    my $TM;
    my $myhostid;

    # Do I already have a host key?
    if (! -e "/etc/sfs/sfs_host_key") {
	warn "*** This node does not have a host key, skipping SFS stuff\n";
	$USESFS = 0;
	return 1;
    }

    # Give hostid to TMCD
807
808
809
810
811
812
    if (-d "/usr/local/lib/sfs-0.6") {
	$myhostid = `sfskey hostid - 2>/dev/null`;
    }
    else {
	$myhostid = `sfskey hostid -s authserv - 2>/dev/null`;
    }
813
    if (! $?) {
Austin Clements's avatar
Austin Clements committed
814
815
816
817
818
	if ( $myhostid =~ /^([-\.\w_]*:[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
819
820
821
822
823
	elsif ( $myhostid =~ /^(@[-\.\w_]*,[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
Austin Clements's avatar
Austin Clements committed
824
825
826
827
828
	else {
	    warn "*** WARNING: Invalid hostid\n";
	}
    }
    else {
829
	warn "*** WARNING: Could not retrieve this node's SFShostid!\n";
Austin Clements's avatar
Austin Clements committed
830
831
832
833
834
835
	$USESFS = 0;
    }

    return 0;
}

836
837
838
839
#
# Do interface configuration.    
# Write a file of ifconfig lines, which will get executed.
#
840
sub doifconfig (;$)
841
{
842
    my ($rtabid) = @_;
843
    my @ifaces   = ();
844
845
    my $upcmds   = "";
    my $downcmds = "";
846
    my @ifacelist= ();
847

848
849
850
851
852
853
854
    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
855

856
    my $TM = OPENTMCC(TMCCCMD_IFC);
857
    while (<$TM>) {
858
859
860
	push(@ifaces, $_);
    }
    CLOSETMCC($TM);
861

862
863
864
865
866
867
868
869
870
    #
    # Create the interface list file.
    # Control net is always first.
    #
    open(XIFS, ">$BOOTDIR/tmcc.ifs") or
	die "Cannot open file $BOOTDIR/tmcc.ifs: $!";

    print XIFS `control_interface`;

871
    if (! @ifaces) {
872
	close(XIFS);
873
874
875
876
	return 0;
    }

    my $ethpat  = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*) );
877
    $ethpat    .= q(SPEED=(\w*) DUPLEX=(\w*) IPALIASES="(.*)" IFACE=(\w*));
878
879
880

    my $vethpat = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) ID=(\d*) );
    $vethpat   .= q(VMAC=(\w*) PMAC=(\w*));
881

882
883
    foreach my $iface (@ifaces) {
	if ($iface =~ /$ethpat/) {
884
885
886
887
888
	    my $inet     = $2;
	    my $mask     = $3;
	    my $mac      = $4;
	    my $speed    = $5; 
	    my $duplex   = $6;
889
	    my $aliases  = $7;
890
	    my $iface    = $8;
891

892
893
	    if (($iface ne "") ||
		($iface = findiface($mac))) {
894
895
896
		if (JAILED()) {
		    next;
		}
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
		print XIFS "$iface\n";

		#
		# Rather than try to wedge the IXP in, I am going with
		# a new approach. Parse the results from tmcd into a
		# simple data structure, and return that for the caller
		# to use. Might want to use a perl module at some point.
		#
		my $ifconfig = {};
		    
		$ifconfig->{"IPADDR"}   = $inet;
		$ifconfig->{"IPMASK"}   = $mask;
		$ifconfig->{"MAC"}      = $mac;
		$ifconfig->{"SPEED"}    = $speed;
		$ifconfig->{"DUPLEX"}   = $duplex;
		$ifconfig->{"ALIASES"}  = $aliases;
		$ifconfig->{"IFACE"}    = $iface;
		push(@ifacelist, $ifconfig);

		if (IXP()) {
		    next;
		}
919

920
		my ($upline, $downline) =
921
922
		    os_ifconfig_line($iface, $inet, $mask,
				     $speed, $duplex, $aliases,$rtabid);
923
		    
924
925
		$upcmds   .= "$upline\n    "
		    if (defined($upline));
926
		$upcmds   .= TMROUTECONFIG . " $inet up\n";
927
		
928
		$downcmds .= TMROUTECONFIG . " $inet down\n    ";
929
		$downcmds .= "$downline\n    "
930
		    if (defined($downline));
931
932
933

		# There could be routes for each alias.
		foreach my $alias (split(',', $aliases)) {
934
935
		    $upcmds   .= TMROUTECONFIG . " $alias up\n";
		    $downcmds .= TMROUTECONFIG . " $alias down\n";
936
		}
937
938
	    }
	    else {
939
		warn "*** WARNING: Bad MAC: $mac\n";
940
941
	    }
	}
942
943
944
945
946
947
948
949
	elsif ($iface =~ /$vethpat/) {
	    my $iface    = undef;
	    my $inet     = $2;
	    my $mask     = $3;
	    my $id       = $4;
	    my $vmac     = $5;
	    my $pmac     = $6; 

950
951
952
953
954
955
956
	    if (JAILED()) {
		if ($iface = findiface($vmac)) {
		    print XIFS "$iface\n";
		}
		next;
	    }

957
958
	    if ($pmac eq "none" ||
		($iface = findiface($pmac))) {
959
960
		print XIFS "$iface\n"
		    if (defined($iface));
961

962
963
964
		my ($upline, $downline) =
		    os_ifconfig_veth($iface, $inet, $mask, $id, $vmac,$rtabid);
		    
965
		$upcmds   .= "$upline\n    ";
966
		$upcmds   .= TMROUTECONFIG . " $inet up\n";
967
		
968
		$downcmds .= TMROUTECONFIG . " $inet down\n    ";
969
		$downcmds .= "$downline\n    "
970
971
972
973
974
975
		    if (defined($downline));
	    }
	    else {
		warn "*** WARNING: Bad PMAC: $pmac\n";
	    }
	}
976
977
978
979
	else {
	    warn "*** WARNING: Bad ifconfig line: $_";
	}
    }
980
    close(XIFS);
981
982
983
    # Done when jailed or an IXP
    return @ifacelist
	if (JAILED() || IXP());
984

985
986
987
988
989
    #
    # Local file into which we write ifconfig commands (as a shell script).
    # 
    open(IFC, ">" . TMIFC)
	or die("Could not open " . TMIFC . ": $!");
990

991
992
993
994
995
    print IFC "#!/bin/sh\n";
    print IFC "# auto-generated by libsetup.pm, DO NOT EDIT\n";
    print IFC "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";
    print IFC "case \"\$action\" in\n";
    print IFC "  enable)\n";
996
997
    print IFC "    $upcmds\n";
    print IFC "    ;;\n";
998
    print IFC "  disable)\n";
999
1000
    print IFC "    $downcmds\n";
    print IFC "    ;;\n";
For faster browsing, not all history is shown. View entire blame