libsetup.pm 86.2 KB
Newer Older
1
#!/usr/bin/perl -w
2

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
#
Leigh B Stoller's avatar
Leigh B Stoller committed
4
# Copyright (c) 2000-2013 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
#
25
# TODO: Signal handlers for protecting db files.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26

27 28 29 30 31 32 33
#
# Common routines and constants for the client bootime setup stuff.
#
package libsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
Ryan Jackson's avatar
Ryan Jackson committed
34
    qw ( libsetup_init libsetup_setvnodeid libsetup_settimeout cleanup_node
35
	 getifconfig getrouterconfig gettrafgenconfig gettunnelconfig
Ryan Jackson's avatar
Ryan Jackson committed
36
	 check_nickname	bootsetup startcmdstatus whatsmynickname
37
	 TBForkCmd vnodejailsetup plabsetup vnodeplabsetup
Ryan Jackson's avatar
Ryan Jackson committed
38
	 jailsetup dojailconfig findiface libsetup_getvnodeid
Timothy Stack's avatar
 
Timothy Stack committed
39
	 ixpsetup libsetup_refresh gettopomap getfwconfig gettiptunnelconfig
40
	 gettraceconfig genhostsfile getmotelogconfig calcroutes fakejailsetup
41
	 getlocalevserver genvnodesetup getgenvnodeconfig stashgenvnodeconfig
42
         getlinkdelayconfig getloadinfo getbootwhat getnodeattributes
43
	 copyfilefromnfs getnodeuuid getarpinfo getstorageconfig
44
         getmanifest fetchmanifestblobs runbootscript runhooks 
45
         build_fake_macs
46

47 48
	 TBDebugTimeStamp TBDebugTimeStampWithDate
	 TBDebugTimeStampsOn TBDebugTimeStampsOff
49

50 51
	 MFS REMOTE REMOTEDED CONTROL FSNODE WINDOWS JAILED PLAB LOCALROOTFS
	 IXP USESFS SHADOW FSRVTYPE PROJDIR EXPDIR
52

53
	 SIMTRAFGEN SIMHOST ISDELAYNODEPATH JAILHOST DELAYHOST STARGATE
54
	 ISFW FAKEJAILED LINUXJAILED GENVNODE GENVNODETYPE GENVNODEHOST
Ryan Jackson's avatar
Ryan Jackson committed
55
	 SHAREDHOST SUBBOSS
56

57
	 CONFDIR LOGDIR TMDELAY TMBRIDGES TMJAILNAME TMSIMRC TMCC TMCCBIN
58
	 TMNICKNAME TMSTARTUPCMD FINDIF
59
	 TMROUTECONFIG TMLINKDELAY TMDELMAP TMTOPOMAP TMLTMAP TMLTPMAP
60
	 TMGATEDCONFIG TMSYNCSERVER TMKEYHASH TMNODEID TMNODEUUID TMEVENTKEY
Ryan Jackson's avatar
Ryan Jackson committed
61
	 TMCREATOR TMSWAPPER TMFWCONFIG TMGENVNODECONFIG
Leigh B Stoller's avatar
Leigh B Stoller committed
62
	 INXENVM INVZVM
63 64 65 66
       );

# Must come after package declaration!
use English;
67
use Errno;
68

69
my $debug = 0;
70

71 72
# The tmcc library.
use libtmcc;
73
use librc;
74 75 76 77 78 79 80

#
# 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!
#
81 82 83
# IMPORTANT NOTE: if you change the version here, you must also change it
# in clientside/lib/tmcd/tmcd.h!
#
84
sub TMCD_VERSION()	{ 36; };
85 86 87 88 89
libtmcc::configtmcc("version", TMCD_VERSION());

# Control tmcc timeout.
sub libsetup_settimeout($) { libtmcc::configtmcc("timeout", $_[0]); };

90
# Refresh tmcc cache.
91 92
sub libsetup_refresh()	   { libtmcc::tmccgetconfig(); };

93
#
94 95
# For virtual (multiplexed nodes). If defined, tack onto tmcc command.
# and use in pathnames. Used in conjunction with jailed virtual nodes.
96
# I am also using this for subnodes; eventually everything will be subnodes.
97
#
98
my $vnodeid;
99 100
sub libsetup_setvnodeid($)
{
101 102 103 104 105 106 107 108 109 110 111
    my ($vid) = @_;

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

    $vnodeid = $vid;
    libtmcc::configtmcc("subnode", $vnodeid);
112 113 114 115 116
}
sub libsetup_getvnodeid()
{
    return $vnodeid;
}
117

118
#
Ryan Jackson's avatar
Ryan Jackson committed
119 120
# True if running inside a jail. Set just below.
#
121 122
my $injail;

123 124 125 126 127 128
#
# True if $injail == TRUE and running on Linux.
# Right now this means vserves on RHL.
#
my $inlinuxjail;

129 130 131 132 133
#
# True if running inside a vm.
#
my $ingenvnode;

134 135
#
# True if running as a fake jail (no jail, just processes).
Ryan Jackson's avatar
Ryan Jackson committed
136
#
137 138
my $nojail;

139 140 141 142 143
#
# True if running in a Plab vserver.
#
my $inplab;

144 145 146 147 148 149
#
# Ditto for IXP, although currently there is no "in" IXP setup; it
# is all done from outside.
#
my $inixp;

150 151 152 153 154 155
#
# Shadow mode. Run the client side against a remote tmcd.
#
my $shadow;
my $SHADOWDIR = "$VARDIR/shadow";

156 157 158 159 160 161 162
#
# Fileserver type.
# Default is "racy NFS" (the historical only choice) until proven otherwise
# (via "mounts" tmcc call).
#
my $fsrvtype = "NFS-RACY";

163 164 165
#
# The role of this pnode
#
166
my $role;
167

168 169 170
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
171
{
172 173 174 175 176
    if (! -e "/etc/emulab/paths.pm") {
	die("Yikes! Could not require /etc/emulab/paths.pm!\n");
    }
    require "/etc/emulab/paths.pm";
    import emulabpaths;
177
    $SHADOWDIR = "$VARDIR/shadow";
178

179
    # Make sure these exist! They will not exist on a PLAB vserver initially.
180 181
    mkdir("$VARDIR", 0775);
    mkdir("$VARDIR/jails", 0775);
182
    mkdir("$VARDIR/vms", 0775);
183 184 185 186
    mkdir("$VARDIR/db", 0755);
    mkdir("$VARDIR/logs", 0775);
    mkdir("$VARDIR/boot", 0775);
    mkdir("$VARDIR/lock", 0775);
187 188 189 190 191
    mkdir("$SHADOWDIR", 0775);
    mkdir("$SHADOWDIR/db", 0755);
    mkdir("$SHADOWDIR/logs", 0775);
    mkdir("$SHADOWDIR/boot", 0775);
    mkdir("$SHADOWDIR/lock", 0775);
192

193 194 195 196 197
    #
    # Shadow mode allows the client side to run against remote tmcd.
    #
    if (exists($ENV{'SHADOW'})) {
	$shadow = $ENV{'SHADOW'};
198
	my ($server,$idkey) = split(',', $shadow);
199 200 201 202 203 204 205 206 207
	#
	# Need to taint check these to avoid breakage later.
	#
	if ($server =~ /^([-\w\.]+)$/) {
	    $server = $1;
	}
	else {
	    die("Bad data in server: $server");
	}
208 209
	if ($idkey =~ /^([-\w\+\:\.]*)$/) {
	    $idkey = $1;
210 211
	}
	else {
212
	    die("Bad data in urn: $idkey");
213
	}
Ryan Jackson's avatar
Ryan Jackson committed
214

215 216 217
	# The cache needs to go in a difference location.
	libtmcc::configtmcc("cachedir", $SHADOWDIR);
	libtmcc::configtmcc("server", $server);
218
	libtmcc::configtmcc("idkey", $idkey);
219 220
	# No proxy.
	libtmcc::configtmcc("noproxy", 1);
221
    }
222 223 224
    #
    # Determine if running inside a jail. This affects the paths below.
    #
225
    if (-e "$BOOTDIR/jailname") {
226
	open(VN, "$BOOTDIR/jailname");
227
	my $vid = <VN>;
228 229
	close(VN);

230
	libsetup_setvnodeid($vid);
231
	$injail = 1;
232 233 234
	if ($^O eq "linux") {
	    $inlinuxjail = 1;
	}
235
    }
236 237 238 239 240 241 242
    elsif (exists($ENV{'FAKEJAIL'})) {
	# Fake jail.
	libsetup_setvnodeid($ENV{'FAKEJAIL'});
	$nojail = 1;
    }
    elsif (-e "$BOOTDIR/plabname") {
	# Running inside a Plab vserver.
243
	open(VN, "$BOOTDIR/plabname");
244
	my $vid = <VN>;
245 246
	close(VN);

247
	libsetup_setvnodeid($vid);
248 249
	$inplab = 1;
    }
250 251 252 253 254 255 256 257 258
    elsif (-e "$BOOTDIR/vmname") {
	open(VN, "$BOOTDIR/vmname");
	my $vid = <VN>;
	close(VN);

	libsetup_setvnodeid($vid);
	$ingenvnode = 1;

    }
259

260
    $role = "";
Ryan Jackson's avatar
Ryan Jackson committed
261
    # Get our role.
262 263 264 265
    if (-e "$BOOTDIR/role") {
	open(VN, "$BOOTDIR/role");
	$role = <VN>;
	close(VN);
266
	chomp($role);
267
    }
268 269
}

270
#
Ryan Jackson's avatar
Ryan Jackson committed
271
# This "local" library provides the OS dependent part.
272
#
273
use liblocsetup;
274

275 276 277 278
#
# These are the paths of various files and scripts that are part of the
# setup library.
#
279
sub TMCC()		{ "$BINDIR/tmcc"; }
280
sub TMCCBIN()		{ "$BINDIR/tmcc.bin"; }
281
sub FINDIF()		{ "$BINDIR/findif"; }
282
sub TMUSESFS()		{ "$BOOTDIR/usesfs"; }
283 284
sub ISSIMTRAFGENPATH()	{ "$BOOTDIR/simtrafgen"; }
sub ISDELAYNODEPATH()	{ "$BOOTDIR/isdelaynode"; }
285
sub TMTOPOMAP()		{ "$BOOTDIR/topomap";}
Timothy Stack's avatar
 
Timothy Stack committed
286
sub TMLTMAP()		{ "$BOOTDIR/ltmap";}
287
sub TMLTPMAP()		{ "$BOOTDIR/ltpmap";}
288

289
#
290
# This path is valid only *outside* the jail when its setup.
Ryan Jackson's avatar
Ryan Jackson committed
291
#
292 293
sub JAILDIR()		{ "$VARDIR/jails/$vnodeid"; }

294 295 296 297
#
# This path is valid only *outside* the vm.  Sucks, but this is the best we
# can do.  Probably the only way to make this vm-specific if necessary is to
# have them create their dir and symlink.
Ryan Jackson's avatar
Ryan Jackson committed
298
#
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
sub GENVNODEDIR()	{ "$VARDIR/vms/$vnodeid"; }

#
# XXX: eventually this needs to come from tmcd, but that's not ready yet.
#
sub GENVNODETYPE() {
    if (-e "$ETCDIR/genvmtype") {
	my $vmtype = `cat $ETCDIR/genvmtype`;
	chomp($vmtype);
	return $vmtype;
    }

    return undef;
}

314 315 316
#
# Also valid outside the jail, this is where we put local project storage.
#
317
sub LOCALROOTFS() {
Ryan Jackson's avatar
Ryan Jackson committed
318
    return "/users/local"
319
	if (REMOTE());
Ryan Jackson's avatar
Ryan Jackson committed
320
    return "$VARDIR/jails/local"
321
	if (JAILED());
Ryan Jackson's avatar
Ryan Jackson committed
322
    return "$VARDIR/vms/local"
323
	if (GENVNODE());
324
    return "/local";
325
}
326

327 328 329
#
# Okay, here is the path mess. There are three environments.
# 1. A local node where everything goes in one place ($VARDIR/boot).
330
# 2. A virtual node inside a jail or a Plab vserver ($VARDIR/boot).
Ryan Jackson's avatar
Ryan Jackson committed
331
# 3. A virtual (or sub) node, from the outside.
332
#
333
# As for #3, whether setting up a old-style (fake) virtual node or a new style
334 335 336
# jailed node, the code that sets it up needs a different per-vnode path.
#
sub CONFDIR() {
337 338
    return "$SHADOWDIR/boot"
	if ($shadow);
339
    return $BOOTDIR
340
	if ($injail || $inplab || $ingenvnode);
Ryan Jackson's avatar
Ryan Jackson committed
341
    return GENVNODEDIR()
342
	if ($vnodeid && GENVNODETYPE());
343 344
    return JAILDIR()
	if ($vnodeid);
345
    return $BOOTDIR;
346
}
347 348
# Cause of fakejails, we want log files in the right place.
sub LOGDIR() {
349 350
    return "$SHADOWDIR/logs"
	if ($shadow);
351
    return $LOGDIR
352
	if ($injail || $inplab || $ingenvnode);
Ryan Jackson's avatar
Ryan Jackson committed
353
    return GENVNODEDIR()
354
	if ($vnodeid && GENVNODETYPE());
Ryan Jackson's avatar
Ryan Jackson committed
355
    return JAILDIR()
356 357 358
	if ($vnodeid);
    return $LOGDIR;
}
359

360 361
#
# The rest of these depend on the environment running in (inside/outside jail).
Ryan Jackson's avatar
Ryan Jackson committed
362
#
363 364
sub TMNICKNAME()	{ CONFDIR() . "/nickname";}
sub TMJAILNAME()	{ CONFDIR() . "/jailname";}
365
sub TMFAKEJAILNAME()	{ CONFDIR() . "/fakejail";}
366
sub TMJAILCONFIG()	{ CONFDIR() . "/jailconfig";}
367
sub TMGENVNODECONFIG()  { CONFDIR() . "/genvnodeconfig";}
368 369
sub TMSTARTUPCMD()	{ CONFDIR() . "/startupcmd";}
sub TMROUTECONFIG()     { CONFDIR() . "/rc.route";}
370
sub TMGATEDCONFIG()     { CONFDIR() . "/gated.conf";}
371
sub TMBRIDGES()		{ CONFDIR() . "/rc.bridges";}
372 373
sub TMDELAY()		{ CONFDIR() . "/rc.delay";}
sub TMLINKDELAY()	{ CONFDIR() . "/rc.linkdelay";}
374
sub TMDELMAP()		{ CONFDIR() . "/delay_mapping";}
375
sub TMSYNCSERVER()	{ CONFDIR() . "/syncserver";}
376
sub TMKEYHASH()		{ CONFDIR() . "/keyhash";}
377
sub TMEVENTKEY()	{ CONFDIR() . "/eventkey";}
378
sub TMNODEID()		{ CONFDIR() . "/nodeid";}
379
sub TMNODEUUID()	{ CONFDIR() . "/nodeuuid";}
380 381
sub TMROLE()		{ CONFDIR() . "/role";}
sub TMSIMRC()		{ CONFDIR() . "/rc.simulator";}
382
sub TMCREATOR()		{ CONFDIR() . "/creator";}
383
sub TMSWAPPER()		{ CONFDIR() . "/swapper";}
384
sub TMFWCONFIG()	{ CONFDIR() . "/rc.fw";}
385 386 387

#
# This is a debugging thing for my home network.
388 389 390 391 392 393 394 395 396 397
#
my $NODE = "";
if (defined($ENV{'TMCCARGS'})) {
    if ($ENV{'TMCCARGS'} =~ /^([-\w\s]*)$/) {
	$NODE .= " $1";
    }
    else {
	die("Tainted TMCCARGS from environment: $ENV{'TMCCARGS'}!\n");
    }
}
398 399

# Locals
400 401 402
my $pid		= "";
my $eid		= "";
my $vname	= "";
403 404 405 406 407 408
my $TIMESTAMPS  = 0;

# Allow override from the environment;
if (defined($ENV{'TIMESTAMPS'})) {
    $TIMESTAMPS = $ENV{'TIMESTAMPS'};
}
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
#
# Any reason NOT to hardwire these?
#
sub PROJDIR() {
    my $p = $pid;
    if (!$p) {
	($p, undef, undef) = check_nickname();
	return ""
	    if (!$p);
    }
    return "/proj/$p";
}

sub EXPDIR() {
    my $p = $pid;
    my $e = $eid;
    if (!$p || !$e) {
	($p, $e, undef) = check_nickname();
	return ""
	    if (!$p || !$e);
    }
    return "/proj/$p/exp/$e";
}

434 435 436
# 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.
437
sub MFS()	{ if (-e "$ETCDIR/ismfs") { return 1; } else { return 0; } }
438

439 440 441
#
# Same for a remote node.
#
442 443
sub REMOTE()	{ if (-e "$ETCDIR/isrem") { return 1; } else { return 0; } }

444 445 446 447 448
#
# Same for a dedicated remote node.
#
sub REMOTEDED()	{ if (-e "$ETCDIR/isremded") { return 1; } else { return 0; } }

449 450 451 452 453
#
# Same for a control node.
#
sub CONTROL()	{ if (-e "$ETCDIR/isctrl") { return 1; } else { return 0; } }

454 455 456 457 458
#
# Same for an FS node.
#
sub FSNODE()	{ if (-e "$ETCDIR/isfs") { return 1; } else { return 0; } }

459 460 461
#
# Same for a Windows (CygWinXP) node.
#
462
# XXX  If you change this, look in libtmcc::tmccgetconfig() as well.
463 464
sub WINDOWS()	{ if (-e "$ETCDIR/iscygwin") { return 1; } else { return 0; } }

Kirk Webb's avatar
 
Kirk Webb committed
465 466 467 468 469
#
# Same for a stargate/garcia node.
#
sub STARGATE()  { if (-e "$ETCDIR/isstargate") { return 1; } else { return 0; } }

470 471 472 473
#
# Are we jailed? See above.
#
sub JAILED()	{ if ($injail) { return $vnodeid; } else { return 0; } }
474
sub FAKEJAILED(){ if ($nojail) { return $vnodeid; } else { return 0; } }
475
sub LINUXJAILED(){ if ($injail && $inlinuxjail) { return $vnodeid; } else { return 0; } }
476

477 478 479 480 481
#
# Are we using the generic vm abstraction for this vnode?  See above.
#
sub GENVNODE()  { if ($ingenvnode) { return $vnodeid; } else { return 0; } }

482 483 484 485 486
#
# Are we on plab?
#
sub PLAB()	{ if ($inplab) { return $vnodeid; } else { return 0; } }

487 488 489 490 491
#
# Are we on an IXP
#
sub IXP()	{ if ($inixp) { return $vnodeid; } else { return 0; } }

492 493 494 495 496
#
# Are we a firewall node
#
sub ISFW()	{ if (-e TMFWCONFIG()) { return 1; } else { return 0; } }

497
#
498
# Are we hosting a simulator or maybe just a NSE based trafgen.
499 500
#
sub SIMHOST()   { if ($role eq "simhost") { return 1; } else { return 0; } }
501
sub SIMTRAFGEN(){ if (-e ISSIMTRAFGENPATH())  { return 1; } else { return 0; } }
502

Ryan Jackson's avatar
Ryan Jackson committed
503 504 505 506 507
#
# Are we a subboss?
#
sub SUBBOSS()   { if ($role eq "subboss") { return 1; } else { return 0; } }

508
# A jail host?
509 510 511 512
sub JAILHOST()     { return (($role eq "virthost" ||
			      $role eq "sharedhost") ? 1 : 0); }
sub GENVNODEHOST() { if ($role eq "virthost") { return 1; } else { return 0; }}
sub SHAREDHOST()   { return ($role eq "sharedhost" ? 1 : 0); }
513

514 515 516
# A delay host?  Either a delay node or a node using linkdelays
sub DELAYHOST()	{ if (-e ISDELAYNODEPATH()) { return 1; } else { return 0; } }

517 518 519
# A shadow?
sub SHADOW()	   { return (defined($shadow) ? 1 : 0); }

520
#
521
# Is this node using SFS. Several scripts need to know this.
522
#
523
sub USESFS()	{ if (-e TMUSESFS()) { return 1; } else { return 0; } }
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
#
# What type of fileserver is this node using.  Choices are:
#
# NFS-RACY	FreeBSD NFS server with mountd race (the default)
# NFS		NFS server
# LOCAL		No shared filesystems
#
# XXX should come from tmcd
#
sub FSRVTYPE() {
    if (-e "$BOOTDIR/fileserver") {
	open(FD, "$BOOTDIR/fileserver");
	$fsrvtype = <FD>;
	close(FD);
	chomp($fsrvtype);
    }
    return $fsrvtype;
}

# XXX fer now hack: comes from rc.mounts
sub setFSRVTYPE($) {
    $fsrvtype = shift;
    if (open(FD, ">$BOOTDIR/fileserver")) {
	print FD "$fsrvtype\n";
	close(FD);
    }
}

Mike Hibler's avatar
Mike Hibler committed
553
#
Leigh B Stoller's avatar
Leigh B Stoller committed
554
# XXX fernow hack so I can readily identify code that is special to VMs
Mike Hibler's avatar
Mike Hibler committed
555 556
#
sub INXENVM()	{ return ($ingenvnode && GENVNODETYPE() eq "xen"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
557
sub INVZVM()	{ return ($ingenvnode && GENVNODETYPE() eq "openvz"); }
Mike Hibler's avatar
Mike Hibler committed
558

559 560 561
#
# Reset to a moderately clean state.
#
562 563
sub cleanup_node ($) {
    my ($scrub) = @_;
Ryan Jackson's avatar
Ryan Jackson committed
564

565
    print STDOUT "Cleaning node; removing configuration files\n";
566
    unlink TMUSESFS, TMROLE, ISSIMTRAFGENPATH, ISDELAYNODEPATH;
567

568
    #
569 570
    # If scrubbing, also remove the password/group files and DBs so
    # that we revert to base set.
Ryan Jackson's avatar
Ryan Jackson committed
571
    #
572
    if ($scrub) {
573
	unlink TMNICKNAME;
574 575 576 577
    }
}

#
578 579
# Check node allocation. If the nickname file has been created, use
# that to avoid load on tmcd.
580 581 582 583 584
#
# Returns 0 if node is free. Returns list (pid/eid/vname) if allocated.
#
sub check_status ()
{
585 586 587
    my @tmccresults;

    if (tmcc(TMCCCMD_STATUS, undef, \@tmccresults) < 0) {
588 589
	warn("*** WARNING: Could not get status from server!\n");
	return -1;
590
    }
591 592 593 594
    #
    # This is possible if the boss node does not now about us yet.
    # We want to appear free. Specifically, it could happen on the
    # MFS when trying to bring in brand new nodes. tmcd will not know
Ryan Jackson's avatar
Ryan Jackson committed
595
    # anything about us, and return no info.
596 597 598 599 600
    #
    return 0
	if (! @tmccresults);

    my $status = $tmccresults[0];
601

602
    if ($status =~ /^FREE/) {
603
	unlink TMNICKNAME;
604 605
	return 0;
    }
Ryan Jackson's avatar
Ryan Jackson committed
606

607
    if ($status =~ /ALLOCATED=([-\@\w]*)\/([-\@\w]*) NICKNAME=([-\@\w]*)/) {
608 609 610 611 612 613
	$pid   = $1;
	$eid   = $2;
	$vname = $3;
    }
    else {
	warn "*** WARNING: Error getting reservation status\n";
614
	return -1;
615
    }
Ryan Jackson's avatar
Ryan Jackson committed
616

617 618
    #
    # Stick our nickname in a file in case someone wants it.
619 620
    # Do not overwrite; we want to save the original info until later.
    # See bootsetup; indicates project change!
621
    #
622
    if (! -e TMNICKNAME()) {
623 624
	system("echo '$vname.$eid.$pid' > " . TMNICKNAME());
    }
Ryan Jackson's avatar
Ryan Jackson committed
625

626 627 628 629
    return ($pid, $eid, $vname);
}

#
630 631
# Check cached nickname. Its okay if we have been deallocated and the info
# is stale. The node will notice that later.
Ryan Jackson's avatar
Ryan Jackson committed
632
#
633
sub check_nickname()
634
{
635 636 637
    if (-e TMNICKNAME) {
	my $nickfile = TMNICKNAME;
	my $nickinfo = `cat $nickfile`;
638

639 640 641 642 643 644 645 646 647
	if ($nickinfo =~ /([-\@\w]*)\.([-\@\w]*)\.([-\@\w]*)/) {
	    $vname = $1;
	    $eid   = $2;
	    $pid   = $3;

	    return ($pid, $eid, $vname);
	}
    }
    return check_status();
648 649 650
}

#
651 652
# Do SFS hostid setup. If we have an SFS host key and we can get a hostid
# from the SFS daemon, then send it to TMCD.
Austin Clements's avatar
Austin Clements committed
653
#
654
sub initsfs()
Austin Clements's avatar
Austin Clements committed
655 656 657
{
    my $myhostid;

658 659 660
    # Default to no SFS unless we can determine we have it running.
    unlink TMUSESFS()
	if (-e TMUSESFS());
Ryan Jackson's avatar
Ryan Jackson committed
661

662
    # Do I have a host key?
Austin Clements's avatar
Austin Clements committed
663
    if (! -e "/etc/sfs/sfs_host_key") {
664
	return;
Austin Clements's avatar
Austin Clements committed
665 666 667
    }

    # Give hostid to TMCD
668 669 670 671 672 673
    if (-d "/usr/local/lib/sfs-0.6") {
	$myhostid = `sfskey hostid - 2>/dev/null`;
    }
    else {
	$myhostid = `sfskey hostid -s authserv - 2>/dev/null`;
    }
674
    if (! $?) {
Austin Clements's avatar
Austin Clements committed
675 676 677
	if ( $myhostid =~ /^([-\.\w_]*:[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
678
	    tmcc(TMCCCMD_SFSHOSTID, "$myhostid");
Austin Clements's avatar
Austin Clements committed
679
	}
680 681 682
	elsif ( $myhostid =~ /^(@[-\.\w_]*,[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
683
	    tmcc(TMCCCMD_SFSHOSTID, "$myhostid");
684
	}
Austin Clements's avatar
Austin Clements committed
685 686
	else {
	    warn "*** WARNING: Invalid hostid\n";
687
	    return;
Austin Clements's avatar
Austin Clements committed
688
	}
689
	system("touch " . TMUSESFS());
Austin Clements's avatar
Austin Clements committed
690 691
    }
    else {
692
	warn "*** WARNING: Could not retrieve this node's SFShostid!\n";
Austin Clements's avatar
Austin Clements committed
693
    }
694 695 696
}

#
Ryan Jackson's avatar
Ryan Jackson committed
697 698
# Get the role of the node and stash it for future libsetup load.
#
699 700 701
sub dorole()
{
    my @tmccresults;
Austin Clements's avatar
Austin Clements committed
702

703 704 705 706 707 708
    if (tmcc(TMCCCMD_ROLE, undef, \@tmccresults) < 0) {
	warn("*** WARNING: Could not get role from server!\n");
	return -1;
    }
    return 0
	if (! @tmccresults);
Ryan Jackson's avatar
Ryan Jackson committed
709

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
    #
    # There should be just one string. Ignore anything else.
    #
    if ($tmccresults[0] =~ /([\w]*)/) {
	# Storing the value into the global variable
	$role = $1;
    }
    else {
	warn "*** WARNING: Bad role line: $tmccresults[0]";
	return -1;
    }
    system("echo '$role' > " . TMROLE());
    if ($?) {
	warn "*** WARNING: Could not write role to " . TMROLE() . "\n";
    }
Austin Clements's avatar
Austin Clements committed
725 726 727
    return 0;
}

728 729
#
# Get the nodeid
Ryan Jackson's avatar
Ryan Jackson committed
730
#
731 732 733 734 735
sub donodeid()
{
    my $nodeid;
    my @tmccresults;

736 737 738
    # Do this too.
    donodeuuid();

739 740 741 742 743 744
    if (tmcc(TMCCCMD_NODEID, undef, \@tmccresults) < 0) {
	warn("*** WARNING: Could not get nodeid from server!\n");
	return -1;
    }
    return 0
	if (! @tmccresults);
Ryan Jackson's avatar
Ryan Jackson committed
745

746 747 748 749 750 751 752 753 754 755
    #
    # There should be just one string. Ignore anything else.
    #
    if ($tmccresults[0] =~ /([-\w]*)/) {
	$nodeid = $1;
    }
    else {
	warn "*** WARNING: Bad nodeid line: $tmccresults[0]";
	return -1;
    }
Ryan Jackson's avatar
Ryan Jackson committed
756

757 758 759 760 761 762 763
    system("echo '$nodeid' > ". TMNODEID);
    if ($?) {
	warn "*** WARNING: Could not write nodeid to " . TMNODEID() . "\n";
    }
    return 0;
}

764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
#
# Get the nodeuuid
#
sub donodeuuid()
{
    my $nodeuuid;
    my @tmccresults;

    if (tmcc(TMCCCMD_NODEUUID, undef, \@tmccresults) < 0) {
	warn("*** WARNING: Could not get nodeuuid from server!\n");
	return -1;
    }
    return 0
	if (! @tmccresults);

    #
    # There should be just one string. Ignore anything else.
    #
    if ($tmccresults[0] =~ /([-\w]*)/) {
	$nodeuuid = $1;
    }
    else {
	warn "*** WARNING: Bad nodeuuid line: $tmccresults[0]";
	return -1;
    }

    system("echo '$nodeuuid' > ". TMNODEUUID);
    if ($?) {
	warn "*** WARNING: Could not write nodeuuid to " . TMNODEUUID() . "\n";
    }
    return 0;
}

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
#
# Get the boot script manifest -- whether scripts are enabled, or hooked, and 
# how and when they or their hooks run!
#
sub getmanifest($;$)
{
    my ($rptr,$nofetch) = @_;
    my @tmccresults;
    my %manifest = ();

    print "Checking manifest...\n";

    if (tmcc(TMCCCMD_MANIFEST, undef, \@tmccresults) < 0) {
	warn("*** WARNING: Could not get manifest from server!\n");
	%$rptr = ();
	return -1;
    }
    if (@tmccresults == 0) {
	%$rptr = ();
	return 0;
    }

    my $servicepat = q(SERVICE NAME=([\w\.\-]+) ENV=(\w+) WHENCE=(\w+));
    $servicepat   .= q( ENABLED=(0|1) HOOKS_ENABLED=(0|1));
    $servicepat   .= q( FATAL=(0|1) BLOBID=([\w\-]*));

    my $hookpat = q(HOOK SERVICE=([\w\.\-]+) ENV=(\w+) WHENCE=(\w+));
    $hookpat   .= q( OP=(\w+) POINT=(\w+));
    $hookpat   .= q( FATAL=(0|1) BLOBID=([\w\-]+));
    $hookpat   .= q( ARGV="([^"]*)");

    my @loadinforesults = ();
    if (tmcc(TMCCCMD_LOADINFO, undef, \@loadinforesults) < 0) {
	warn("*** WARNING: getmanifest could not get loadinfo from server,\n".
	     "             unsure if node is in MFS and reloading, continuing!\n");
    }

    #
    # Are we in a loading environment?  If yes, filter the manifest
    # so that only the service and hook settings that apply to the 
    # loading MFS apply.
    #
    if (@loadinforesults && MFS()) {
	$manifest{'_ENV'} = 'load';
    }
    #
    # Otherwise, if we're not in an MFS, we must be booting!
    # NOTE: we don't do any configuration of the image in the 
    # admin MFS!
    #
    elsif (!MFS()) {
	$manifest{'_ENV'} = 'boot';
    }
    #
    # Otherwise, don't return *anything* -- the admin mfs doesn't do any
    # config of the node.
    #
    else {
	%$rptr = ();
	return 0;
    }

    #
    # Process our results.
    #
    for (my $i = 0; $i < @tmccresults; ++$i) {
	my $line = $tmccresults[$i];
	my %service;

	if ($line =~ /^$servicepat/) {
	    my %service = ( 'ENABLED' => $4,
			    'HOOKS_ENABLED' => $5,
			    'BLOBID' => $7,
			    'WHENCE' => $3,
			    'FATAL' => $6 );
	    #
	    # Filter the service part of the manifest so that only the 
	    # settings that apply here are passed to scripts.
	    #
	    if ($2 eq $manifest{'_ENV'}) {
		# assume that there might be a hook line that applied to this
		# service ahead of the service line!  Other possibility
		# is that there was already a service line for this env...
		# which is a bug -- so just silently stomp it in that case.
		#
		# anyway, just update the results pointer for this service
		foreach my $k (keys(%service)) {
		    $manifest{$1}{$k} = $service{$k};
		}

		if (!exists($manifest{$1}{'_PREHOOKS'})) {
		    $manifest{$1}{'_PREHOOKS'} = [];
		}
		if (!exists($manifest{$1}{'_POSTHOOKS'})) {
		    $manifest{$1}{'_POSTHOOKS'} = [];
		}
	    }
	    # Otherwise just skip this entry -- it doesn't apply to us.
	    else {
		next;
	    }
	}
	elsif ($line =~ /^$hookpat/) {
	    #
	    # Filter the service part of the manifest so that only the 
	    # settings that apply here are passed to scripts.
	    #
	    if ($2 eq $manifest{'_ENV'}) {
		my $hookstr = "_" . uc($5) . "HOOKS";
		if (!exists($manifest{$1}) || !exists($manifest{$1}{$hookstr})) {
		    $manifest{$1}{$hookstr} = [];
		}

		my $hook = { 'BLOBID' => $7,
			     'OP' => $4, 
			     'WHENCE' => $3, 
			     'FATAL' => $6, 
			     'ARGV' => $8 };

		$manifest{$1}{$hookstr}->[@{$manifest{$1}{$hookstr}}] = $hook;
	    }
	    # Otherwise just skip this entry -- it doesn't apply to us.
	    else {
		next;
	    }
	}
	else {
	    warn("*** WARNING: did not recognize manifest line '$line'," . 
		 " continuing!\n");
	}
    }

    my $retval = 0;

    if (!defined($nofetch) || $nofetch != 1) {
	print "Downloading any manifest blobs...\n";
	%$rptr = %manifest;
	$retval = fetchmanifestblobs($rptr,undef,'manifest');
    }

    %$rptr = %manifest;
    return $retval;
}

sub fetchmanifestblobs($;$$)
{
    my ($manifest,$savedir,$basename) = @_;
    if (!defined($savedir)) {
	$savedir = $BLOBDIR;
    }
    if (!defined($basename)) {
	$basename = '';
    }
    my $blobpath = "$savedir/$basename";
    my $retval;
    my $failed = 0;

    foreach my $script (keys(%$manifest)) {
	# first grab the script replacement...
	if (exists($manifest->{$script}{'BLOBID'})
	    && $manifest->{$script}{'BLOBID'} ne '') {
	    my $bpath = $blobpath . "." . $manifest->{$script}{'BLOBID'};
	    $retval = libtmcc::blob::getblob($manifest->{$script}{'BLOBID'},
					     $bpath);
	    if ($retval == -1) {
		print STDERR "ERROR(fetchmanifestblobs): could not fetch " . 
		    $manifest->{$script}{'BLOBID'} . "!\n";
		++$failed;
	    }
	    else {
		$manifest->{$script}{'BLOBPATH'} = $bpath;
		chmod(0755,$bpath);
	    }
	}

	# now do hooks...
	my @hooktypes = ('_PREHOOKS','_POSTHOOKS');
	foreach my $hooktype (@hooktypes) {
	    next 
		if (!exists($manifest->{$script}{$hooktype}));

	    foreach my $hook (@{$manifest->{$script}{$hooktype}}) {
		my $bpath = $blobpath . "." . $hook->{'BLOBID'};
		$retval = libtmcc::blob::getblob($hook->{'BLOBID'},$bpath);
		if ($retval == -1) {
		    print STDERR "ERROR(fetchmanifestblobs): could not fetch " . 
			$hook->{'BLOBID'} . "!\n";
		    ++$failed;
		}
		else {
		    $hook->{'BLOBPATH'} = $bpath;
		    chmod(0755,$bpath);
		}
	    }
	}
		
    }

    return $failed;
}

sub runhooks($$$$)
{
    my ($manifest,$which,$script,$what) = @_;
    my $hookstr = "_".uc($which)."HOOKS";
    my $failed = 0;

    if (exists($manifest->{$script}) 
	# if hooks are enabled because of a service line
	&& ((exists($manifest->{$script}{'HOOKS_ENABLED'}) 
	     && $manifest->{$script}{'HOOKS_ENABLED'} == 1)
	    # or if there was no service line, in which case hooks are
	    # enabled by default
	    || !exists($manifest->{$script}{'HOOKS_ENABLED'}))
	&& exists($manifest->{$script}{$hookstr})) {
	print "  Running $script $which hooks\n"
	    if ($debug);

	for (my $i = 0; $i < @{$manifest->{$script}{$hookstr}}; ++$i) {
	    my $hook = $manifest->{$script}{$hookstr}->[$i];
	    my $blobid = $hook->{'BLOBID'};
	    my $argv = $hook->{'ARGV'};
	    my $hookrunfile = "$VARDIR/db/$script.${which}hook.$blobid.run";

	    # if the path doesn't exist, probably we failed to fetch the blob
	    if (!exists($hook->{'BLOBPATH'})) {
		++$failed;
		if ($hook->{'FATAL'}) {
		    fatal("Failed running $script $which hook $blobid (no blobpath!)");
		}
		else {
		    warn("  $script $which hook $blobid failed! (no blobpath!)");
		}
		next;
	    }

	    my $blobpath = $hook->{'BLOBPATH'};

	    # Only run the hook if its operation matches the operation we're
	    # doing (boot,shutdown,reconfig,reset)
	    if ($hook->{'OP'} ne $what) {
		next;
	    }

	    # If this is a first-only hook, skip if we've already done it!
	    if ($hook->{'WHENCE'} eq 'first' && -e $hookrunfile) {
1043 1044
		print "  Not running $which hook $blobid (first config only)\n" 
		    if ($debug);
1045 1046 1047
		next;
	    }

1048
	    print "  Running $script $which hook $blobid\n";