rc.mkelab 86.8 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2004-2011 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17
# All rights reserved.
#
# XXX I hardwire IPs into generated /etc/rc.conf and /etc/rc.resolv.
#
# TODO:
#  * Put admin people in local homedirs. 
#
#
use English;
use Getopt::Std;
use Socket;
use IO::Handle;

18 19 20
# XXX not yet complete
my $domirror = 0;

21 22 23 24 25 26
sub usage()
{
    print "Usage: " .
	scriptname() . " boot|shutdown|reconfig|reset\n";
    exit(1);
}
27
my $optlist = "dsj";
28 29
my $action  = "boot";
my $debug   = 0;
30
my $skipit  = 1;	# Temporary until images updated.
31
my $opsjail = 0;	# Only for ops; running inside a jail.
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

# Turn off line buffering on output
$| = 1;

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

# Only root.
if ($EUID != 0) {
    die("*** $0:\n".
	"    Must be root to run this script!\n");
}

# Script specific goo. Put it someplace where prepare script will leave it.
my $LOGFILE    = "/usr/mkelab.debug";
my %elabconfig = ();

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
use liblocsetup;
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
David Johnson's avatar
David Johnson committed
62
    if (REMOTE() || JAILED() || DELAYHOST());
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'d'})) {
    $debug = 1;
}
if (defined($options{'s'})) {
    $skipit = 1;
}
80 81 82
if (defined($options{'j'})) {
    $opsjail = 1;
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

# More stuff we need below.
my $domain;
my ($pid,$eid,undef) = check_nickname();
my $file    = TMCREATOR();
my $creator = `cat $file`;
chomp($creator);

my $hostname = `hostname`;
chomp($hostname);
97
my ($bossname, $outer_bossip) = tmccbossinfo();
98

99 100 101 102
# This is the router IP to the outside world. Needed in lots of places.
my $outer_routerip;
# And the netmask on the outer control network.
my $outer_netmask;
103 104
# And the control interface
my $outer_controlif;
105

Leigh B. Stoller's avatar
Leigh B. Stoller committed
106 107 108
# Outer FS node name. See below.
my $fsname;

109 110
# Cert stuff to give the inner emulab
my $RPCCERT = "/usr/testbed/etc/outer_emulab.pem";
111
#my $RPCPORT = 7778;
112

113 114 115 116 117 118 119 120
#
# Elab daemons to kill off before setting up boss/ops/fs.
# Order matters: must kill event agents before evproxy and pubsubd.
# XXX we leave slothd running for idleswap in case we hang.
#
my @DAEMONS = ("emulab-watchdog", "progagent", "linktest",
	       "syncd", "evproxy", "pubsubd");

121 122 123 124 125 126
#
# Standard system daemons to kill off before we run the Emulab cleanup script.
#
my @SYSDAEMONS = ("cron", "mountd", "ntpd", "sendmail",
		  "syslogd", "nfsd", "rpcbind");

127 128 129 130 131
# "Standard" packages
my $PGENI_PKG = "emulab-protogeni-1.0";

# or this...
my $FSMOUNTDIR = "/q";
132 133 134 135
my $OPSMOUNTDIR = "/ops";

# Hardwired?
my $TBDIR = "/usr/testbed";
136 137 138 139 140 141 142 143 144 145 146

#
# Support for Windows nodes.
# XXX here for compat; replaced by emulabconfig option.
#
my $WINSUPPORT = 1;

#
# Whether installation of all collab tools should be disabled.
# XXX here for compat; replaced by emulabconfig option.
#
Mike Hibler's avatar
Mike Hibler committed
147
my $NOCOLLAB   = 1;
148 149 150 151 152 153

#
# Enable elvin compatibility.
# XXX here for compat; replaced by emulabconfig option.
#
my $ELVIN_COMPAT = 0;
154

155 156 157
# This also gets turned on/off below
my $NOSETUP    = 0;

158 159 160
# Whether we configure a /scratch FS, turned on/off below
my $SCRATCHFS  = 0;

161 162 163
# Single or dual control network.
my $SINGLE_CONTROLNET = 0;

164 165 166
# Support a shared filesystem between fs server and nodes
my $SHAREDFS   = 1;

167 168 169 170
# Start up mrouted so frisbee works (if there is no other mcast router)
# -1 means let the system decide.
my $NEEDMROUTED= -1;

171 172 173
# Is ops a VM (Jail) on boss.
my $OPSVM = 0;

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
#
# Defaults for configuration attributes (options).
# These can be overridden if values are passed in via the emulabconfig command.
#
my %emulabconfig = (
    #
    # Random stuff
    #
    "JAILIPBASE"  => "172.16.0.0",
    "JAILIPMASK"  => "255.240.0.0",
    "MFSTARBALL"  => "tftpboot-elabinelab.tar.gz",
    "MFSVERSION"  => "62",
    "MFSCONSOLE"  => "sio",

    #
    # Elabinelab configuration options:
    #
    #   CONFIG_NOSETUP	  debug option: don't do most of rc.mkelab activities.
    #   CONFIG_SCRATCHFS  configure a separate "scratch" filesystem.
    #   CONFIG_SINGLECNET use the real cnet as the inner cnet.
    #	CONFIG_ELVIN	support Elvin compatibility
    #	CONFIG_WINDOWS	support Windows OS on nodes
    #   CONFIG_COLLAB	install/configure for collaboration tools support.
    #	CONFIG_ARCHIVE	support integrated archival via svn (part of COLLAB)
    #   CONFIG_BUGSDB	support integrated bug database (part of COLLAB)
    #   CONFIG_CVS	support integrated CVS SCM (part of COLLAB)
    #   CONFIG_MAILMAN	support integrated mailman lists (part of COLLAB)
    #	CONFIG_USERDB	support integrated MySQL DB (part of COLLAB)
    #   CONFIG_WIKI	support integrated wiki (part of COLLAB)
203
    #	CONFIG_SHAREDFS	support shared filesystem	    
204
    #	CONFIG_MROUTED	install/configure multicast router
205 206 207 208 209 210 211 212 213 214 215 216 217
    #
    "CONFIG_NOSETUP"    => $NOSETUP,
    "CONFIG_SCRATCHFS"  => $SCRATCHFS,
    "CONFIG_SINGLECNET" => $SINGLE_CONTROLNET,
    "CONFIG_ELVIN"      => $ELVIN_COMPAT,
    "CONFIG_WINDOWS"    => $WINSUPPORT,
    "CONFIG_COLLAB"     => !$NOCOLLAB,
    "CONFIG_ARCHIVE"    => !$NOCOLLAB,
    "CONFIG_BUGSDB"     => !$NOCOLLAB,
    "CONFIG_CVS"        => !$NOCOLLAB,
    "CONFIG_MAILMAN"    => !$NOCOLLAB,
    "CONFIG_USERDB"     => !$NOCOLLAB,
    "CONFIG_WIKI"       => !$NOCOLLAB,
218
    "CONFIG_SHAREDFS"	=> $SHAREDFS,
219
    "CONFIG_MROUTED"	=> $NEEDMROUTED,
220
    "CONFIG_OPSVM"	=> $OPSVM,
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
    #
    # Elabinelab build options:
    #
    # 	LOAD_PACKAGES	download and install a standard set of packages,
    #	               	otherwise assume they are already installed.
    #   LOAD_PROTOGENI	load the optional Protogeni package.
    #                   This is different than the other "optional" packages
    #                   such as the collab tools which are installed via
    #                   {boss,ops}-install.  Pgeni still requires manual
    #                   intervention so boss-install doesn't even try.
    #   LOAD_MFS	download and install a tftpboot tarball,
    #	               	otherwise assume it is already installed.
    #   LOAD_ELABSOURCE	download, build and install the Emulab source code,
    #	               	otherwise assume it is already installed.
    #	LOAD_DB		download and install Emulab DB state,
    #			otherwise...this might not even be useful.
    #	LOAD_USERDIRS	download and install "user data" whatever we
    #			decide that might be.
    #
    # Defaults to all *but* LOAD_USERDIRS for backward compat.
    #
    "LOAD_PACKAGES"   => 1,
    "LOAD_PROTOGENI"  => 0,
    "LOAD_MFS"        => 1,
    "LOAD_ELABSOURCE" => 1,
    "LOAD_DB"         => 1,
    "LOAD_USERDIRS"   => 0,
);
250 251 252

# Version of FreeBSD.
my $FBSD_VERSION = 4;
253
if (`uname -r` =~ /^(\d\.\d*)/) {
254 255 256 257 258 259
    $FBSD_VERSION = $1;
}
else {
    die("Could not determine what version of FreeBSD you are running!\n");
}

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
#
# Find out our domain name, so that we can qualify the localhost entry
#
if ($hostname =~ /[^.]+\.(.+)/) {
    $domain = $1;
}

#
# Find the outer domain for sending email to creator.
#
my $outer_domain;
if ($bossname =~ /[^.]+\.(.+)/) {
    $outer_domain = $1;
}

# Execute the action.
SWITCH: for ($action) {
    /^boot$/i && do {
	doboot();
	last SWITCH;
    };
    /^shutdown$/i && do {
	doshutdown();
	last SWITCH;
    };
    /^reconfig$/i && do {
	doreconfig();
	last SWITCH;
    };
    /^reset$/i && do {
	docleanup();
	last SWITCH;
    };
    fatal("Invalid action: $action\n");
}
exit(0);

# More protos
sub SetupFatal($);
299
sub mysystem($;$);
300
sub SetupOpsNode($);
301
sub SetupBossNode($);
302 303
sub CreateDefsFile($);
sub SetupSendMail($$);
304
sub GetEmulabSource($);
305
sub MungeMfsRoot($);
Mike Hibler's avatar
Mike Hibler committed
306 307
sub SetupTBDir($);
sub RecreateDir($$);
308 309
sub CreateOpsJail($);
sub SetupOpsJail();
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

#
# Boot Action.
#
sub doboot()
{
    my @tmccresults;

    if (tmcc(TMCCCMD_EMULABCONFIG, undef, \@tmccresults) < 0) {
	fatal("Could not get Inner Emulab Config info from server!");
    }
    # If no results then do nothing. No inner elab.
    return 0
	if (! @tmccresults);

    if (!$debug) {
	print "Redirecting output to $LOGFILE\n";

	open(STDERR, ">  $LOGFILE") or die("opening $LOGFILE for STDERR: $!");
	open(STDOUT, ">> $LOGFILE") or die("opening $LOGFILE for STDOUT: $!");

	#
	# Turn off line buffering on output
	#
	STDOUT->autoflush(1);
	STDERR->autoflush(1);
    }
337 338 339 340 341 342

    #
    # Stash us into the BINDIR for later.
    #
    mysystem("cp -fp $0 $BINDIR/rc")
	if ("$0" ne "$BINDIR/rc/rc.mkelab");
343
    
Mike Hibler's avatar
Mike Hibler committed
344
    #
345
    # FreeBSD version specific adjustments for defaults.
Mike Hibler's avatar
Mike Hibler committed
346
    #
Mike Hibler's avatar
Mike Hibler committed
347
    if ($FBSD_VERSION >= 7.2) {
348 349 350 351
	$emulabconfig{"MFSVERSION"} = "72";
    } else {
	$emulabconfig{"MFSVERSION"} = "62";
    }
352

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    #
    # Need outer control router IP in lots of places.
    #
    if (! -e "$BOOTDIR/routerip") {
	fatal("$BOOTDIR/routerip does not exist!");
    }
    $outer_routerip = `cat $BOOTDIR/routerip`;
    chomp($outer_routerip);

    # And the outer netmask
    if (! -e "$BOOTDIR/mynetmask") {
	fatal("$BOOTDIR/mynetmask does not exist!");
    }
    $outer_netmask = `cat $BOOTDIR/mynetmask`;
    chomp($outer_netmask);
Mike Hibler's avatar
Mike Hibler committed
368

369 370 371 372 373 374 375
    # And the outer interface
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

376 377 378 379 380 381 382
    #
    # Turn the tmcc results into a hash first. Then call the boss or ops
    # setup function.
    #
    foreach my $line (@tmccresults) {
	if ($line =~ /^(.*)="(.+)"$/ ||
	    $line =~ /^(.*)=(.+)$/) {
383 384
	    my $key = $1;
	    my $val = $2;
385 386 387 388 389 390 391 392 393 394 395 396

	    #
	    # XXX backward compat
	    #
	    if ($key eq "WINSUPPORT") {
		$key = "CONFIG_WINDOWS";
	    } elsif ($key eq "NOSETUP") {
		$key = "CONFIG_NOSETUP";
	    } elsif ($key eq "SINGLE_CONTROLNET") {
		$key = "CONFIG_SINGLECNET";
	    }

397 398 399 400 401 402 403 404 405 406 407 408 409 410
	    $emulabconfig{$key} = $val;

	    #
	    # Handle ordered setting/clearing of aggregate options.
	    # These may be later overridden by individual options.
	    # 
	    if ($key eq "CONFIG_COLLAB") {
		$emulabconfig{"CONFIG_ARCHIVE"} = $val;
		$emulabconfig{"CONFIG_BUGSDB"} = $val;
		$emulabconfig{"CONFIG_CVS"} = $val;
		$emulabconfig{"CONFIG_MAILMAN"} = $val;
		$emulabconfig{"CONFIG_USERDB"} = $val;
		$emulabconfig{"CONFIG_WIKI"} = $val;
	    }
411 412 413
	}
    }

Mike Hibler's avatar
Mike Hibler committed
414
    #
415
    # XXX temporary backward compat til this (router) gets implemented
Mike Hibler's avatar
Mike Hibler committed
416 417 418
    #
    $emulabconfig{"ROLE"} = "boss"
	if ($emulabconfig{"ROLE"} eq "boss+router");
419 420 421 422 423 424
    $emulabconfig{"ROLE"} = "boss+fs"
	if ($emulabconfig{"ROLE"} eq "boss+fs+router");

    # Override the role when inside an ops jail. 
    $emulabconfig{"ROLE"} = "opsjail"
	if ($opsjail);
Mike Hibler's avatar
Mike Hibler committed
425

426
    #
427
    # XXX not-so-temporary: adjust package info.
428
    #
429 430 431
    # There are defaults in sitevars passed via emulabconfig,
    # but they are almost always wrong...  So we ALWAYS override them
    # here; packages are just too intimately tied to the OS version.
432
    #
433 434 435 436 437 438 439 440 441
    if ($FBSD_VERSION == 4.10) {
	$emulabconfig{FS_PKG_DIR}   = "/share/freebsd/4.10/packages";
	$emulabconfig{OPS_PKG_DIR}  = "/share/freebsd/4.10/packages";
	$emulabconfig{BOSS_PKG_DIR} = "/share/freebsd/4.10/packages";
	$emulabconfig{FS_PKG}       = "emulab-fs-1.4";
	$emulabconfig{OPS_PKG}      = "emulab-ops-1.4";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-1.4";
    }
    elsif ($FBSD_VERSION == 5.4) {    
442 443 444 445 446 447 448
	$emulabconfig{FS_PKG_DIR}   = "/share/freebsd/5.4/packages";
	$emulabconfig{OPS_PKG_DIR}  = "/share/freebsd/5.4/packages";
	$emulabconfig{BOSS_PKG_DIR} = "/share/freebsd/5.4/packages";
	$emulabconfig{FS_PKG}       = "emulab-fs-2.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-2.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-2.0";
    }
449
    elsif ($FBSD_VERSION == 6.0) {
450 451 452 453 454 455 456
	$emulabconfig{FS_PKG_DIR}   = "/share/freebsd/6.0/packages";
	$emulabconfig{OPS_PKG_DIR}  = "/share/freebsd/6.0/packages";
	$emulabconfig{BOSS_PKG_DIR} = "/share/freebsd/6.0/packages";
	$emulabconfig{FS_PKG}       = "emulab-fs-2.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-2.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-2.0";
    }
457
    elsif ($FBSD_VERSION == 6.1 ||
Mike Hibler's avatar
Mike Hibler committed
458
	   $FBSD_VERSION == 6.2) {
459 460 461
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
462 463
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
464 465 466 467
	$emulabconfig{FS_PKG}       = "emulab-fs-2.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-2.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-2.0";
    }
Mike Hibler's avatar
Mike Hibler committed
468
    elsif ($FBSD_VERSION == 6.3) {
469 470 471
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.3/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
Mike Hibler's avatar
Mike Hibler committed
472 473 474 475 476 477
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-2.1";
	$emulabconfig{OPS_PKG}      = "emulab-ops-2.1";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-2.1";
    }
Mike Hibler's avatar
Mike Hibler committed
478
    elsif ($FBSD_VERSION == 7.2) {
479 480 481
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.2/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
482 483 484 485 486 487
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-3.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-3.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-3.0";
    }
Mike Hibler's avatar
Mike Hibler committed
488 489 490 491 492 493 494 495 496 497
    elsif ($FBSD_VERSION == 7.3) {
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.3/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-3.1";
	$emulabconfig{OPS_PKG}      = "emulab-ops-3.1";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-3.1";
    }
498

499 500 501 502 503 504 505 506 507 508 509 510
    #
    # XXX Copy in new mkextrafs if necessary
    #
    if (-e "/tmp/mkextrafs.pl") {
	# only copy if newer
	if (! -e "$BINDIR/mkextrafs.pl" ||
	    (stat("/tmp/mkextrafs.pl"))[9] >
	    (stat("$BINDIR/mkextrafs.pl"))[9]) {
	    mysystem("cp /tmp/mkextrafs.pl $BINDIR/");
	}
    }

511 512 513 514 515 516
    if (!$opsjail) {
	#
	# XXX To avoid NFS errors while copying goo from outer boss.
	#
	system("sysctl vfs.nfs.eacces_retry_enable=1 >/dev/null 2>&1");    
	system("sysctl vfs.nfs.eacces_retry_count=20 >/dev/null 2>&1");
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
	#
	# Figure out where /share is coming from (outer fs node).
	# We need this below. Inside the ops jail, /packages is
	# already mounted.
	#
	$fsname = `mount | grep /users | head -1`;
	if ($?) {
	    fatal("Could not get mount information!");
	}
	if ($fsname =~ /^([-\w\.]+):/) {
	    $fsname = $1;
	}
	else {
	    fatal("Could not determine the name of the outer FS node!");
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
533 534
    }

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    my @stdopts = ();
    foreach my $opt (keys %emulabconfig) {
	if ($opt =~ /^(CONFIG|LOAD)_/ && $emulabconfig{$opt} != 0) {
	    push @stdopts, $opt;
	}
    }
    print "Setting up \"", uc($emulabconfig{"ROLE"}), "\" node";
    if (@stdopts > 0) {
	print ", options:\n  ", join(", ", sort @stdopts);
    }
    print "\n";

    #
    # Finally, actually do the setup!
    #
550 551 552 553 554 555 556 557
    if ($emulabconfig{"ROLE"} eq "fs") {
	SetupFsNode();
    }
    elsif ($emulabconfig{"ROLE"} eq "ops") {
	SetupOpsNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "ops+fs") {
	SetupOpsNode(1);
558 559
    }
    elsif ($emulabconfig{"ROLE"} eq "boss") {
560 561 562 563 564 565 566
	SetupBossNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "boss+fs") {
	SetupBossNode(1);
    }
    elsif ($emulabconfig{"ROLE"} eq "opsjail") {
	SetupOpsJail();
567
    }
568 569 570
    elsif ($emulabconfig{"ROLE"} eq "node") {
	;
    }
571
    else {
Leigh B Stoller's avatar
Leigh B Stoller committed
572
	fatal("Do not recognize role '". $emulabconfig{"ROLE"} ."'!\n");
573
    }
574 575 576 577 578
    print "Done!\n";
    return 0;
}

#
579
# Setup an fs node.
580
#
581
sub SetupFsNode()
582
{
583
    my $shareslice;
584
    my $FSDIR = $FSMOUNTDIR;
585

586
    my $fromscratch =
587 588
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
589

590 591 592 593 594 595 596 597
    #
    # XXX (hopefully) tmp hack for dealing with other FS OSes
    #
    my ($os, $rel) = split " ", `uname -sr`;
    if ($os ne "FreeBSD") {
	SetupFatal("FS node must run FreeBSD\n");
    }

598
    #
599 600 601 602 603 604 605 606
    # Create filesystems for fs use.  We need:
    #  * /usr/testbed		 => disk0s2e
    # for a "from scratch" build and:
    #  * /q (proj,users,scratch) => disk0s4e
    #  * /share			 => disk0s4f
    # for ops+fs and fs-only nodes.
    #
    if ($fromscratch) {
Mike Hibler's avatar
Mike Hibler committed
607
	SetupTBDir($TBDIR);
608
    }
609
    RecreateDir("$FSDIR", 1);
610
    mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
611

612 613 614 615 616 617 618 619 620 621 622 623 624
    #
    # XXX mkextrafs does not create the second filesystem, it only creates
    # the BSD partition.  So we need to determine the name of the disk
    # device in use, and create a filesystem on that 'f' partition.
    #
    my $disk = `mount | grep '0s4e on $FSDIR'`;
    if ($disk =~ /(\/dev\/\S+)s4e on/) {
	$shareslice = "$1" . "s4f";
	mysystem("newfs $shareslice");
	# don't mount it yet, just remember
    } else {
	SetupFatal("Could not parse mount info to find extra partition");
    }
625

626 627 628
    #
    # Download Emulab source if necessary
    #
629
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
630 631 632 633
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

634 635 636 637
    #
    # The mirror tree is copied to temp storage, and then copied into
    # place later.
    #
638
    if ($domirror && -e "/proj/$pid/mirror") {
639
	print "Copying over mirror tree from /proj/$pid/mirror\n";
640
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
641 642
    }

643 644 645 646 647
    #
    # Stash the IP of the outer emulab for tmcc (and script above).
    # We use an IP to avoid DNS issues (there will be a DNS running inside).
    # Ditto for the current router. Need that for later (rc.inelab).
    # 
648
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
649 650
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");

651 652 653 654 655 656 657 658 659 660 661 662 663 664
    #
    # Need outer ip and netmask and iface for hardwired config below.
    #
    if (! -e "$BOOTDIR/myip") {
	SetupFatal("$BOOTDIR/myip does not exist!");
    }
    my $outer_ip = `cat $BOOTDIR/myip`;
    chomp($outer_ip);
    
    #
    # We also need the hardwired config for the inner control network.
    # Major kludge; should get it from tmcd data.
    #
    my @ifacelist;
665 666 667 668
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;

669
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
670 671 672 673 674 675
	if (getifconfig(\@ifacelist) != 0 || !@ifacelist) {
	    SetupFatal("Could not get ifconfig from libsetup!");
	}
	$inner_controlif = $ifacelist[0]->{IFACE};
	$inner_ip        = $ifacelist[0]->{IPADDR};
	$inner_netmask   = $ifacelist[0]->{IPMASK};
676 677
    }

678 679 680 681 682 683 684 685 686 687 688 689
    #
    # Kill off any Emulab daemons that might freak once we have removed
    # Emulab accounts.  We have seen this happen with the program agent--
    # it runs amok, filling up its logfile (that has been unlinked) and
    # eventually /var, possibly before we can finish setting up.
    #
    foreach my $daemon (@DAEMONS) {
	if (-e "/var/run/$daemon.pid") {
	    system("kill `cat /var/run/$daemon.pid`");
	}
    }

690 691 692 693 694 695 696 697 698 699 700 701 702 703
    #
    # And make a best effort to kill off system daemons as well.
    # Some of these we will restart below since boss-install needs them.
    #
    foreach my $daemon (@SYSDAEMONS) {
	if (-e "/etc/rc.d/$daemon") {
	    system("/etc/rc.d/$daemon stop");
	} elsif (-e "/var/run/$daemon.pid") {
	    system("kill `cat /var/run/$daemon.pid`");
	} else {
	    system("killall $daemon");
	}
    }

704 705 706 707 708
    #
    # Run the prepare script to clear out the current accounts and such.
    # From this point on will need to log in as root,
    #
    print "Clearing out existing accounts and such\n";
709
    mysystem("$BINDIR/prepare -N");
710

711 712 713 714 715 716 717 718 719 720
    #
    # XXX prepare is a destructive beast.  It will take out the ld hints
    # file so ld.so won't have a search path.  Repair that now if it is gone
    # since ops-install will want to start apps that need libraries from
    # /usr/local/lib.
    #
    if (! -r "/var/run/ld-elf.so.hints") {
	system("/etc/rc.d/ldconfig start");
    }

721 722 723 724 725
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

726 727 728
    #
    # And clear some other stuff.
    # 
729
    mysystem("rm -rf $TBDIR/bin $TBDIR/lib");
730 731
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
732 733
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");
734

735
    #
736 737
    # Load up packages if necessary
    #
738
    if (!$emulabconfig{"CONFIG_NOSETUP"} && $installpkgs) {
739 740 741 742 743
	# XXX fer now: if not set, derive from the OPS info
	if (!$emulabconfig{FS_PKG_DIR} || !$emulabconfig{FS_PKG}) {
	    $emulabconfig{FS_PKG_DIR} = $emulabconfig{OPS_PKG_DIR};
	    ($emulabconfig{FS_PKG} = $emulabconfig{OPS_PKG}) =~ s/ops/fs/;
	}
744

745 746 747 748 749 750 751
	#
	# Do this as a separate step because PKG_DIR might be an NFS path,
	# but we must do the NFS unmounts before running ops-install. 
	# 
	if (!$emulabconfig{FS_PKG_DIR} || !$emulabconfig{FS_PKG}) {
	    SetupFatal("Could not get package info from Emulab!");
	}
752

753 754
	print "Installing the fs metaport.\n";
	RecreateDir("/usr/ports", 1);
755 756 757
	$ENV{"PKG_PATH"} = $emulabconfig{FS_PKG_DIR};
	mysystem("pkg_add $emulabconfig{FS_PKG} >/tmp/perrs 2>&1");
    }
758 759

    #
760
    # Clean up a few things on the image and create symlinks into $FSDIR for
761
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
762
    #
763 764 765 766 767 768 769 770 771 772 773 774
    print "Command: 'umount -A -t nfs'\n";
    print "Started at:  " . libsetup::TBTimeStamp() . "\n";
    system("umount -A -t nfs");
    if ($?) {
	print STDERR "*** umount of NFS filesystems failed\n";
    }
    #
    # Uber-paranoid: even if the umount says it works, don't trust it.
    # Move the old mount points out of the way no matter what.
    # It is not super critical that these be unmounted at this time as
    # we will be rebooting shortly anyway; we just need them out of the way.
    #
775 776 777 778
    RecreateDir("/users", 0);
    RecreateDir("/proj", 0);
    RecreateDir("/groups", 0);
    RecreateDir("/share", 0);
779 780 781 782 783 784 785 786 787 788 789

    mysystem("mkdir $FSDIR/users $FSDIR/proj $FSDIR/groups $FSDIR/share");
    mysystem("ln -s $FSDIR/users /users");
    mysystem("ln -s $FSDIR/proj /proj");
    mysystem("ln -s $FSDIR/groups /groups");

    #
    # Setup /share.  The partition and filesystem were created above.
    #
    mysystem("mount $shareslice $FSDIR/share");
    mysystem("echo \"$shareslice $FSDIR/share ufs rw 0 2\" >> /etc/fstab");
790
    mysystem("ln -s $FSDIR/share /share");
791

792 793 794 795
    #
    # And the optional /scratch which share the same FS as /proj, et.al.
    #
    if ($emulabconfig{"CONFIG_SCRATCHFS"}) {
796
	RecreateDir("/scratch", 0);
797 798
	mysystem("mkdir $FSDIR/scratch");
	mysystem("ln -s $FSDIR/scratch /scratch");
799 800
    }

Mike Hibler's avatar
Mike Hibler committed
801 802
    #
    # Lets mount the package dir so that we can pass off some stuff to
803
    # the install scripts.
Mike Hibler's avatar
Mike Hibler committed
804
    #
805
    RecreateDir("/packages", 1);
Mike Hibler's avatar
Mike Hibler committed
806 807
    mysystem("mount ${fsname}:" . $emulabconfig{FS_PKG_DIR} . " /packages");

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
    #
    # Need these for rc.conf.
    # 
    my $bossnode_ip = $emulabconfig{"BOSSIP"};
    my $fsnode_ip  = $emulabconfig{"FSIP"};

    #
    # Need control network.
    #
    my $control_network = inet_ntoa(inet_aton($fsnode_ip) &
				    inet_aton("255.255.255.0")) . "/24";

    #
    # Need to create an /etc/rc.conf that is more suitable for fs.
    # I took most of this from our real ops node. It will be modified
    # by the fs-install script below.
    #
    print "Creating a new /etc/rc.conf\n";
    open(RC, ">/etc/rc.conf") or
	SetupFatal("Could not open /etc/rc.conf for writing: $!");

    print RC "inetd_enable=\"YES\"\n";
    print RC "sendmail_enable=\"NO\"\n";
    print RC "sshd_enable=\"YES\"\n";

    print RC "ntpdate_enable=\"YES\"\n";
    print RC "ntpdate_flags=\"boss\"\n";
835 836 837 838 839
    if ($FBSD_VERSION >= 5) {
	print RC "ntpd_enable=\"YES\"\n";
    } else {
	print RC "xntpd_enable=\"YES\"\n";
    }
840 841
    print RC "linux_enable=\"YES\"\n";

842 843
    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
844 845 846
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
Mike Hibler's avatar
Mike Hibler committed
847
    print RC "smbd_enable=\"YES\"\n"
848
	if ($emulabconfig{"CONFIG_WINDOWS"});
849 850
    print RC "mountd_flags=\"-r -p 900\"\n";

851
    print RC "network_interfaces=\"$outer_controlif\"\n";
852 853
    print RC "ifconfig_${outer_controlif}=".
	"\"inet $outer_ip netmask $outer_netmask\"\n";
854

855
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
856 857 858 859 860 861
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
	    "media 100baseTX mediaopt full-duplex\"\n";
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
862 863 864 865
    print RC "static_routes=\"outerboss vnodes\"\n";
    print RC "route_outerboss=\"$outer_bossip $outer_routerip\"\n";
    print RC "route_vnodes=\"-net ". $emulabconfig{"JAILIPBASE"} .
	" -netmask " . $emulabconfig{"JAILIPMASK"} .
866
	" -iface " . ($emulabconfig{"CONFIG_SINGLECNET"} ?
867
		      $outer_controlif : $inner_controlif) . "\"\n";
868
    # Leave default route pointing to control network until setup complete.
869 870
    if ($emulabconfig{"CONFIG_NOSETUP"} ||
	$emulabconfig{"CONFIG_SINGLECNET"}) {
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
	print RC "defaultrouter=\"$outer_routerip\"\n";
    }
    else {
	print RC "defaultrouter=\"$bossnode_ip\"\n";
    }
    print RC "hostname=\"" . $emulabconfig{"FSNODE"} . "." . $domain . "\"\n";
    close(RC);

    #
    # Remove some cruft from /etc/syslog.conf
    #
    mysystem("cat /etc/syslog.conf | grep -v '\@users' > /tmp/syslog.conf");
    mysystem("cp -pf /etc/syslog.conf /etc/syslog.conf.old ; ".
	     "cp /tmp/syslog.conf /etc/syslog.conf");

    #
    # Create a defs file. Note that this will move to boss at some point.
    #
889
    CreateDefsFile("$TBDIR/src/testbed/defs-elabinelab");
890 891

    goto skipsetup
892
	if ($emulabconfig{"CONFIG_NOSETUP"});
893 894 895 896

    #
    # Configure an object tree. 
    #
897
    RecreateDir("$TBDIR/obj/testbed", 1);
898 899 900
    mysystem("cd $TBDIR/obj/testbed; ".
	     "   $TBDIR/src/testbed/configure ".
	     "      --with-TBDEFS=$TBDIR/src/testbed/defs-elabinelab ".
901 902
	     ($emulabconfig{"CONFIG_WINDOWS"} ?
	      "--enable-windows" : "--disable-windows"));
903 904 905 906

    #
    # Create the fs node.
    #
Mike Hibler's avatar
Mike Hibler committed
907
    $ENV{"PKG_PATH"} = "/packages";
Mike Hibler's avatar
Mike Hibler committed
908
    my $pkg = "-P $emulabconfig{FS_PKG} -p /packages";
909
    mysystem("cd $TBDIR/obj/testbed/install; ".
910 911 912 913 914
	     "   perl fs-install $pkg -b");

    #
    # And install the fs side.
    #
915
    mysystem("cd $TBDIR/obj/testbed; gmake fs-install");
916 917

    #
918 919
    # We do need to restart nfs services so that boss-install can work
    # (it requires mounting our filesystems).
Mike Hibler's avatar
Mike Hibler committed
920 921
    #
    if ($FBSD_VERSION >= 5) {
922
	mysystem("/etc/rc.d/rpcbind start");
Mike Hibler's avatar
Mike Hibler committed
923
	mysystem("/etc/rc.d/nfsd start");
924 925 926 927 928 929 930 931 932 933 934 935
	#
	# XXX lovely: we normally start mountd bound to port 900, but
	# quite often during an elabinelab setup, that port is already
	# in use causing mountd to silently fail and we don't notice til
	# boss-install when it tries to mount the filesystems.
	#
	# So we don't bind to a specific port here (by directly starting
	# mounted rather than using the rc.d startup script) to avoid this
	# issue.  Note that when elabinelab setup finishes, boss and ops
	# will be rebooted and we will again be bound to port 900.
	#
	mysystem("mountd -r");
936 937 938 939
    } else {
	# XXX FBSD4 is such a pain...
	mysystem("nfsd -u -t -n 8");
	mysystem("mountd -r -p 900");
Mike Hibler's avatar
Mike Hibler committed
940
    }
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963

    #
    # Need to create a resolv.conf that points to inner boss. This is the
    # last thing we do cause after this, stuff is probably going to stop
    # working properly!
    # 
    print "Creating a new /etc/resolv.conf\n";
    open(RC, ">/etc/resolv.conf") or
	SetupFatal("Could not open /etc/resolv.conf for writing: $!");

    print RC "domain $domain\n";
    print RC "search $domain\n";
    print RC "nameserver $bossnode_ip\n";
    close(RC);

  skipsetup:
    #
    # Hmm, need to run this at startup though.
    # 
    mysystem("echo '/usr/local/etc/emulab/rc/rc.inelab' ".
	     "   >> /etc/rc.local");

    return
964
	if ($emulabconfig{"CONFIG_NOSETUP"});
965

966
    mysystem("cp -p $TBDIR/src/testbed/defs-elabinelab $TBDIR/src");
967 968 969 970

    #
    # Copy the mirror tree into place. Do not use rsync.
    #
971
    if (0 && -e "$TBDIR/mirror") {
972
	print "Copying mirror tree into place\n";
973
	mysystem("cp -Rfp $TBDIR/mirror/ /");
974 975 976 977 978 979 980 981 982
    }
}

#
# Setup an ops node.
#
sub SetupOpsNode($)
{
    my ($isfs) = @_;
983 984
    my $shareslice;
    my $FSDIR = "";
985

986
    my $fromscratch =
987 988
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
989 990

    #
991 992 993 994 995 996 997 998
    # Create filesystems for fs use.  We need:
    #  * /usr/testbed		 => disk0s2e
    # for a "from scratch" build and:
    #  * /q (proj,users,scratch) => disk0s4e
    #  * /share			 => disk0s4f
    # for ops+fs and fs-only nodes.
    #
    if ($fromscratch) {
Mike Hibler's avatar
Mike Hibler committed
999
	SetupTBDir($TBDIR);
1000 1001
    }
    if ($isfs) {
1002
	$FSDIR = $FSMOUNTDIR;
1003
	RecreateDir("$FSDIR", 1);
1004
	mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
1005

1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
	#
	# XXX mkextrafs does not create the second filesystem, it only creates
	# the BSD partition.  So we need to determine the name of the disk
	# device in use, and create a filesystem on that 'f' partition.
	#
	my $disk = `mount | grep '0s4e on $FSDIR'`;
	if ($disk =~ /(\/dev\/\S+)s4e on/) {
	    $shareslice = "$1" . "s4f";
	    mysystem("newfs $shareslice");
	    # don't mount it yet, just remember
	} else {
	    SetupFatal("Could not parse mount info to find extra partition");
	}
    }
1020

1021 1022 1023
    #
    # Download Emulab source if necessary
    #
1024
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
1025 1026 1027 1028
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

1029 1030 1031 1032 1033 1034
    #
    # The mirror tree is copied to temp storage, and then copied into
    # place later.
    #
    if ($isfs && $domirror && -e "/proj/$pid/mirror") {
	print "Copying over mirror tree from /proj/$pid/mirror\n";
1035
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
    }

    #
    # Stash the IP of the outer emulab for tmcc (and script above).
    # We use an IP to avoid DNS issues (there will be a DNS running inside).
    # Ditto for the current router. Need that for later (rc.inelab).
    # 
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");

    #
    # Need outer ip and netmask and iface for hardwired config below.
    #
    if (! -e "$BOOTDIR/myip") {
	SetupFatal("$BOOTDIR/myip does not exist!");
    }
    my $outer_ip = `cat $BOOTDIR/myip`;
    chomp($outer_ip);
    
    #
    # We also need the hardwired config for the inner control network.
    # Major kludge; should get it from tmcd data.
    #
    my @ifacelist;
1060 1061 1062 1063
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;

1064
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
1065 1066 1067 1068 1069 1070
	if (getifconfig(\@ifacelist) != 0 || !@ifacelist) {
	    SetupFatal("Could not get ifconfig from libsetup!");
	}
	$inner_controlif = $ifacelist[0]->{IFACE};
	$inner_ip        = $ifacelist[0]->{IPADDR};
	$inner_netmask   = $ifacelist[0]->{IPMASK};
1071 1072
    }

1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    #
    # Kill off any Emulab daemons that might freak once we have removed
    # Emulab accounts.  We have seen this happen with the program agent--
    # it runs amok, filling up its logfile (that has been unlinked) and
    # eventually /var, possibly before we can finish setting up.
    #
    foreach my $daemon (@DAEMONS) {
	if (-e "/var/run/$daemon.pid") {
	    system("kill `cat /var/run/$daemon.pid`");
	}
    }

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    #
    # And make a best effort to kill off system daemons as well.
    # Some of these we will restart below since boss-install needs them.
    #
    foreach my $daemon (@SYSDAEMONS) {
	if (-e "/etc/rc.d/$daemon") {
	    system("/etc/rc.d/$daemon stop");
	} elsif (-e "/var/run/$daemon.pid") {
	    system("kill `cat /var/run/$daemon.pid`");
	} else {
	    system("killall $daemon");
	}
    }

1099 1100 1101 1102 1103
    #
    # Run the prepare script to clear out the current accounts and such.
    # From this point on will need to log in as root,
    #
    print "Clearing out existing accounts and such\n";
1104
    mysystem("$BINDIR/prepare -N");
1105

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    #
    # XXX prepare is a destructive beast.  It will take out the ld hints
    # file so ld.so won't have a search path.  Repair that now if it is gone
    # since fs-install will want to start apps that need libraries from
    # /usr/local/lib.
    #
    if (! -r "/var/run/ld-elf.so.hints") {
	system("/etc/rc.d/ldconfig start");
    }

1116 1117 1118 1119 1120 1121 1122 1123
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

    #
    # And clear some other stuff.
    # 
1124
    mysystem("rm -rf $TBDIR/bin $TBDIR/lib");
1125 1126
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
1127 1128
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");
1129 1130

    #
1131 1132
    # Load up packages if necessary
    #
1133
    if (!$emulabconfig{"CONFIG_NOSETUP"} && $installpkgs) {
1134 1135 1136 1137 1138 1139 1140 1141
	#
	# Do this as a separate step because PKG_DIR might be an NFS path,
	# but we must do the NFS unmounts before running ops-install. 
	# 
	if (!$emulabconfig{OPS_PKG_DIR} || !$emulabconfig{OPS_PKG}) {
	    SetupFatal("Could not get package info from Emulab!");
	}
	print "Removing conflicting packages.\n";
1142
	system("pkg_delete -r -x mysql-client")
1143
	    if (-e "/usr/local/bin/mysql");
Mike Hibler's avatar
Mike Hibler committed
1144 1145
    
	# XXX 7.3 image is out of sync right now
1146
	if ($FBSD_VERSION == 7.3) {
Mike Hibler's avatar
Mike Hibler committed
1147 1148
	    system("pkg_delete -f -x python25 -x py25");
	    system("pkg_delete -f -x sudo -x png -x tiff");
1149
	}
Mike Hibler's avatar
Mike Hibler committed
1150

1151
	print "Installing the ops metaport.\n";
1152
	RecreateDir("/usr/ports", 1);
1153 1154
	$ENV{"PKG_PATH"} = $emulabconfig{OPS_PKG_DIR};
	mysystem("pkg_add $emulabconfig{OPS_PKG} >/tmp/perrs 2>&1");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1155

1156 1157 1158 1159 1160 1161
	if ($isfs) {
	    # XXX if not set, derive from the OPS info
	    if (!$emulabconfig{FS_PKG_DIR} || !$emulabconfig{FS_PKG}) {
		$emulabconfig{FS_PKG_DIR} = $emulabconfig{OPS_PKG_DIR};
		($emulabconfig{FS_PKG} = $emulabconfig{OPS_PKG}) =~ s/ops/fs/;
	    }
1162 1163 1164 1165
	}
    }

    #
1166 1167
    # Clean up a few things on the image and create symlinks for /proj,
    # /users, /groups and /scratch.  Also allows /share to be created.
1168
    #
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
    print "Command: 'umount -A -t nfs'\n";
    print "Started at:  " . libsetup::TBTimeStamp() . "\n";
    system("umount -A -t nfs");
    if ($?) {
	print STDERR "*** umount of NFS filesystems failed\n";
    }
    #
    # Uber-paranoid: even if the umount says it works, don't trust it.
    # Move the old mount points out of the way no matter what.
    # It is not super critical that these be unmounted at this time as
    # we will be rebooting shortly anyway; we just need them out of the way.
    #
1181 1182 1183 1184
    RecreateDir("/users", 0);
    RecreateDir("/proj", 0);
    RecreateDir("/groups", 0);
    RecreateDir("/share", 0);
1185 1186

    mysystem("mkdir $FSDIR/users $FSDIR/proj $FSDIR/groups $FSDIR/share");
1187
    if ($isfs) {
1188 1189 1190
	mysystem("ln -s $FSDIR/users /users");
	mysystem("ln -s $FSDIR/proj /proj");
	mysystem("ln -s $FSDIR/groups /groups");
1191 1192

	#
1193
	# Setup /share.  The partition and filesystem were created above.
1194
	#
1195 1196
	mysystem("mount $shareslice $FSDIR/share");
	mysystem("echo \"$shareslice $FSDIR/share ufs rw 0 2\" >> /etc/fstab");
1197
	mysystem("ln -s $FSDIR/share /share");
1198 1199
    }

1200 1201 1202 1203
    #
    # And the optional /scratch which share the same FS as /proj, et.al.
    #
    if ($emulabconfig{"CONFIG_SCRATCHFS"}) {
1204
	RecreateDir("/scratch", 0);
1205
	mysystem("mkdir $FSDIR/scratch");
1206
	if ($isfs) {
1207
	    mysystem("ln -s $FSDIR/scratch /scratch");
1208 1209 1210
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1211 1212
    #
    # Lets mount the package dir so that we can pass off some stuff to
1213
    # the install scripts.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1214
    #
1215
    RecreateDir("/packages", 1);
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1216 1217
    mysystem("mount ${fsname}:" . $emulabconfig{OPS_PKG_DIR} . " /packages");

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
    #
    # Need these for rc.conf.
    # 
    my $bossnode_ip = $emulabconfig{"BOSSIP"};
    my $opsnode_ip  = $emulabconfig{"OPSIP"};

    #
    # Need control network.
    #
    my $control_network = inet_ntoa(inet_aton($opsnode_ip) &
				    inet_aton("255.255.255.0")) . "/24";

    #
    # Need to create an /etc/rc.conf that is more suitable for ops.
    # I took most of this from our real ops node. It will be modified
    # by the ops-install script below.
    #
    print "Creating a new /etc/rc.conf\n";
    open(RC, ">/etc/rc.conf") or
	SetupFatal("Could not open /etc/rc.conf for writing: $!");

    print RC "inetd_enable=\"YES\"\n";
    print RC "sendmail_enable=\"YES\"\n";
Mike Hibler's avatar
Mike Hibler committed
1241 1242 1243
    if ($emulabconfig{"CONFIG_MAILMAN"}) {
	print RC "mailman_enable=\"YES\"\n";
    }
1244 1245 1246 1247
    print RC "sshd_enable=\"YES\"\n";

    print RC "ntpdate_enable=\"YES\"\n";
    print RC "ntpdate_flags=\"boss\"\n";
1248 1249 1250 1251 1252
    if ($FBSD_VERSION >= 5) {
	print RC "ntpd_enable=\"YES\"\n";
    } else {
	print RC "xntpd_enable=\"YES\"\n";
    }
1253 1254 1255
    print RC "linux_enable=\"YES\"\n";
    print RC "accounting_enable=\"YES\"\n";

1256 1257
    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
1258 1259 1260
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
1261
    print RC "smbd_enable=\"YES\"\n"
1262
	if ($emulabconfig{"CONFIG_WINDOWS"});
1263 1264 1265 1266
    print RC "mountd_flags=\"-r -p 900\"\n";

    print RC "syslogd_flags=\"-a $control_network\"\n";

1267
    print RC "network_interfaces=\"$outer_controlif\"\n";
1268
    print RC "ifconfig_${outer_controlif}=".
1269
	"\"inet $outer_ip netmask $outer_netmask\"\n";
1270

1271
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
1272 1273 1274 1275 1276 1277
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
	    "media 100baseTX mediaopt full-duplex\"\n";
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
Mike Hibler's avatar
Mike Hibler committed
1278
    print RC "static_routes=\"outerboss vnodes\"\n";
1279
    print RC "route_outerboss=\"$outer_bossip $outer_routerip\"\n";
Mike Hibler's avatar
Mike Hibler committed
1280 1281
    print RC "route_vnodes=\"-net ". $emulabconfig{"JAILIPBASE"} .
	" -netmask " . $emulabconfig{"JAILIPMASK"} .
1282
	" -iface " . ($emulabconfig{"CONFIG_SINGLECNET"} ?
1283
		      $outer_controlif : $inner_controlif) . "\"\n";
1284
    # Leave default route pointing to control network until setup complete.
1285 1286
    if ($emulabconfig{"CONFIG_NOSETUP"} ||
	$emulabconfig{"CONFIG_SINGLECNET"}) {
1287 1288 1289 1290 1291
	print RC "defaultrouter=\"$outer_routerip\"\n";
    }
    else {
	print RC "defaultrouter=\"$bossnode_ip\"\n";
    }
1292 1293 1294
    print RC "hostname=\"" . $emulabconfig{"OPSNODE"} . "." . $domain . "\"\n";
    close(RC);

1295 1296 1297 1298 1299 1300 1301
    #
    # Remove some cruft from /etc/syslog.conf
    #
    mysystem("cat /etc/syslog.conf | grep -v '\@users' > /tmp/syslog.conf");
    mysystem("cp -pf /etc/syslog.conf /etc/syslog.conf.old ; ".
	     "cp /tmp/syslog.conf /etc/syslog.conf");

1302 1303 1304
    #
    # Create a defs file. Note that this will move to boss at some point.
    #
1305
    CreateDefsFile("$TBDIR/src/testbed/defs-elabinelab");
1306

1307
    goto skipsetup
1308
	if ($emulabconfig{"CONFIG_NOSETUP"});
1309

1310 1311 1312
    #
    # Configure an object tree. 
    #
1313
    RecreateDir("$TBDIR/obj/testbed", 1);
1314 1315 1316
    mysystem("cd $TBDIR/obj/testbed; ".
	     "   $TBDIR/src/testbed/configure ".
	     "      --with-TBDEFS=$TBDIR/src/testbed/defs-elabinelab ".
1317 1318
	     ($emulabconfig{"CONFIG_WINDOWS"} ?
	      "--enable-windows" : "--disable-windows"));
1319 1320 1321 1322

    #
    # Create the ops node.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1323 1324
    $ENV{"PKG_PATH"} = "/packages";
    my $pkg = "-P $emulabconfig{OPS_PKG} -p /packages " .
1325
	($isfs ? "-F $emulabconfig{FS_PKG}" : "");
1326
    mysystem("cd $TBDIR/obj/testbed/install; ".
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1327
	     "   perl ops-install $pkg -b -w ElabInElab ");
1328 1329 1330 1331

    #
    # And install the ops side.
    #
1332
    my $itarget = $isfs ? "opsfs-install" : "ops-install";
1333
    mysystem("cd $TBDIR/obj/testbed; gmake $itarget");
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348

    #
    # Lets populate the mail lists with the creator of the experiment so
    # that email goes someplace useful.
    # 
    opendir(DIR, "/etc/mail/lists") or
	SetupFatal("Cannot opendir /etc/mail/lists: $!");
    my @lists = grep { $_ ne "." && $_ ne ".." } readdir(DIR);
    closedir(DIR);

    foreach my $list (@lists) {
	mysystem("echo ${creator}\@${outer_domain} > /etc/mail/lists/$list");
    }

    #
1349 1350 1351
    # We do need to restart nfs services so that boss-install can work
    # (it requires mounting our filesystems; and yes this is true even
    # if we are not the FS server).
1352
    #
1353
    # We restart sendmail because boss-install also wants to send email.
1354 1355
    #
    if ($FBSD_VERSION >= 5) {
1356
	mysystem("/etc/rc.d/rpcbind start");
1357
	mysystem("/etc/rc.d/nfsd start");
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
	#
	# XXX lovely: we normally start mountd bound to port 900, but
	# quite often during an elabinelab setup, that port is already
	# in use causing mountd to silently fail and we don't notice til
	# boss-install when it tries to mount the filesystems.
	#
	# So we don't bind to a specific port here (by directly starting
	# mounted rather than using the rc.d startup script) to avoid this
	# issue.  Note that when elabinelab setup finishes, boss and ops
	# will be rebooted and we will again be bound to port 900.
	#
	mysystem("mountd -r");
1370 1371 1372 1373 1374 1375
	mysystem("/etc/rc.d/sendmail start");
    } else {
	# XXX FBSD4 is such a pain...
	mysystem("nfsd -u -t -n 8");
	mysystem("mountd -r -p 900");
	mysystem("/etc/rc.sendmail start");
1376
    }
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390

    #
    # Need to create a resolv.conf that points to inner boss. This is the
    # last thing we do cause after this, stuff is probably going to stop
    # working properly!
    # 
    print "Creating a new /etc/resolv.conf\n";
    open(RC, ">/etc/resolv.conf") or
	SetupFatal("Could not open /etc/resolv.conf for writing: $!");

    print RC "domain $domain\n";
    print RC "search $domain\n";
    print RC "nameserver $bossnode_ip\n";
    close(RC);
1391

1392
  skipsetup:
1393 1394 1395 1396 1397 1398
    #
    # Hmm, need to run this at startup though.
    # 
    mysystem("echo '/usr/local/etc/emulab/rc/rc.inelab' ".
	     "   >> /etc/rc.local");

1399
    return
1400
	if ($emulabconfig{"CONFIG_NOSETUP"});
1401

1402 1403 1404
    #
    # Copy the mirror tree into place. Do not use rsync.
    #
1405
    if (0 && $isfs && -e "$TBDIR/mirror") {
1406
	print "Copying mirror tree into place\n";
1407
	mysystem("cp -Rfp $TBDIR/mirror/ /");
1408
    }
1409 1410
}

Leigh B Stoller's avatar