rc.mkelab 96.2 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
Leigh B Stoller's avatar
Leigh B Stoller committed
4
# Copyright (c) 2004-2012 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
# or this...
my $FSMOUNTDIR = "/q";
129 130 131 132
my $OPSMOUNTDIR = "/ops";

# Hardwired?
my $TBDIR = "/usr/testbed";
133 134 135 136 137 138 139 140 141 142 143

#
# 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
144
my $NOCOLLAB   = 1;
145 146 147 148 149 150

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

152 153 154
# This also gets turned on/off below
my $NOSETUP    = 0;

155 156 157
# Whether we configure a /scratch FS, turned on/off below
my $SCRATCHFS  = 0;

158 159 160
# Single or dual control network.
my $SINGLE_CONTROLNET = 0;

161 162 163
# Support a shared filesystem between fs server and nodes
my $SHAREDFS   = 1;

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

168 169 170
# Is ops a VM (Jail) on boss.
my $OPSVM = 0;

171 172 173
# Are we a XEN VM?
my $XENVM = 0;

174 175 176 177 178 179 180 181
#
# Node to use as ntp server for inner nodes and other inner servers.
# This node will use the outside "ntp1" server as its server.
#
# XXX this has never been tested with anything but "boss".
#
my $NTPSERVER = "boss";

Leigh B Stoller's avatar
Leigh B Stoller committed
182 183 184 185 186
#
# The default password to use.
#
my $DEFAULT_PASSWORD = "ElabInElab";

187 188 189 190 191 192 193 194 195 196 197 198 199
#
# 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",
Leigh B Stoller's avatar
Leigh B Stoller committed
200
    "PASSWORD"    => $DEFAULT_PASSWORD,
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    #
    # 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)
217
    #	CONFIG_SHAREDFS	support shared filesystem	    
218
    #	CONFIG_MROUTED	install/configure multicast router
219 220 221 222 223 224 225 226 227 228 229 230 231
    #
    "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,
232
    "CONFIG_SHAREDFS"	=> $SHAREDFS,
233
    "CONFIG_MROUTED"	=> $NEEDMROUTED,
234
    "CONFIG_OPSVM"	=> $OPSVM,
235
    "CONFIG_NODBINIT"   => 0,
236
    "CONFIG_GENIRACK"   => 0,
237 238 239

    # Default protogeni package if LOAD_PROTOGENI is enabled.
    "PGENI_PKG"		=> "emulab-protogeni-1.0",
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    #
    # 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,
);
269 270 271

# Version of FreeBSD.
my $FBSD_VERSION = 4;
272
if (`uname -r` =~ /^(\d\.\d*)/) {
273 274 275 276 277 278
    $FBSD_VERSION = $1;
}
else {
    die("Could not determine what version of FreeBSD you are running!\n");
}

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
#
# 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($);
318
sub mysystem($;$);
319
sub SetupOpsNode($);
320
sub SetupBossNode($);
321 322
sub CreateDefsFile($);
sub SetupSendMail($$);
323
sub GetEmulabSource($);
324
sub MungeMfsRoot($);
Mike Hibler's avatar
Mike Hibler committed
325 326
sub SetupTBDir($);
sub RecreateDir($$);
327 328
sub CreateOpsJail($);
sub SetupOpsJail();
329
sub FindExtraFSConfig($);
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

#
# 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);
    }
357 358 359 360 361 362

    #
    # Stash us into the BINDIR for later.
    #
    mysystem("cp -fp $0 $BINDIR/rc")
	if ("$0" ne "$BINDIR/rc/rc.mkelab");
363
    
Mike Hibler's avatar
Mike Hibler committed
364
    #
365
    # FreeBSD version specific adjustments for defaults.
Mike Hibler's avatar
Mike Hibler committed
366
    #
Mike Hibler's avatar
Mike Hibler committed
367
    if ($FBSD_VERSION >= 7.2) {
368 369 370 371
	$emulabconfig{"MFSVERSION"} = "72";
    } else {
	$emulabconfig{"MFSVERSION"} = "62";
    }
372

373 374 375 376 377 378 379 380 381
    # Determine if a XEN VM.
    if (system("sysctl -n kern.vm_guest >/dev/null 2>&1") == 0) {
	my $vm_guest = `sysctl -n kern.vm_guest`;
	if ($vm_guest =~ /^xen$/) {
	    $XENVM = 1;
	    print "Running sinde a XEN VM\n";
	}
    }

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    #
    # 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
397

398 399 400 401 402 403 404
    # And the outer interface
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

405 406 407 408 409 410 411
    #
    # Turn the tmcc results into a hash first. Then call the boss or ops
    # setup function.
    #
    foreach my $line (@tmccresults) {
	if ($line =~ /^(.*)="(.+)"$/ ||
	    $line =~ /^(.*)=(.+)$/) {
412 413
	    my $key = $1;
	    my $val = $2;
414 415 416 417 418 419 420 421 422 423 424 425

	    #
	    # 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";
	    }

426 427 428 429 430 431 432 433 434 435 436 437 438 439
	    $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;
	    }
440 441 442
	}
    }

Mike Hibler's avatar
Mike Hibler committed
443
    #
444
    # XXX temporary backward compat til this (router) gets implemented
Mike Hibler's avatar
Mike Hibler committed
445 446 447
    #
    $emulabconfig{"ROLE"} = "boss"
	if ($emulabconfig{"ROLE"} eq "boss+router");
448 449 450 451 452 453
    $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
454

455 456 457 458
    # Override NTPSERVER when singlenet; okay to use inner ops.
    $NTPSERVER = "ops"
	if ($emulabconfig{"CONFIG_SINGLECNET"});

459
    #
460
    # XXX not-so-temporary: adjust package info.
461
    #
462 463 464
    # 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.
465
    #
466 467 468 469 470 471 472 473 474
    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) {    
475 476 477 478 479 480 481
	$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";
    }
482
    elsif ($FBSD_VERSION == 6.0) {
483 484 485 486 487 488 489
	$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";
    }
490
    elsif ($FBSD_VERSION == 6.1 ||
Mike Hibler's avatar
Mike Hibler committed
491
	   $FBSD_VERSION == 6.2) {
492 493 494
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
495 496
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
497 498 499 500
	$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
501
    elsif ($FBSD_VERSION == 6.3) {
502 503 504
	$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
505 506 507 508 509 510
	$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
511
    elsif ($FBSD_VERSION == 7.2) {
512 513 514
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.2/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
515 516 517 518 519 520
	$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
521 522 523 524 525 526 527 528 529 530
    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";
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
531
    elsif ($FBSD_VERSION >= 8.2) {
Leigh B Stoller's avatar
Leigh B Stoller committed
532 533 534 535 536 537 538 539
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/8.2/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-4.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-4.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-4.0";
540
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-8.2-packages.tar.gz";
541
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-2.0";
542 543 544 545 546 547 548 549 550 551 552 553
    }
    if (exists($emulabconfig{"PACKAGE_TARBALL"})) {
	$emulabconfig{FS_PKG_DIR}   = "$TBDIR/packages";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};

	#
	# Grab the package tarball now.
	#
	mysystem("fetch -q -o /usr/packages.tar.gz ".
		 "'http://${bossname}/downloads/" .
		 $emulabconfig{PACKAGE_TARBALL} . "'");
Leigh B Stoller's avatar
Leigh B Stoller committed
554
    }
555

556 557 558 559 560 561 562 563 564 565 566 567
    #
    # 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/");
	}
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    #
    # If we get a password setting, change root/toor.
    #
    if ($emulabconfig{"PASSWORD"} ne $DEFAULT_PASSWORD) {
	#
	# Convert to an encrypted hash.
	#
	my @salt_chars = ('a'..'z','A'..'Z','0'..'9');
	my $salt = $salt_chars[rand(@salt_chars)] .
	           $salt_chars[rand(@salt_chars)];
	my $passhash = crypt($emulabconfig{"PASSWORD"}, "\$1\$${salt}");
	mysystem("echo '$passhash' | /usr/sbin/pw usermod toor -H 0");
	mysystem("echo '$passhash' | /usr/sbin/pw usermod root -H 0");
    }

583 584 585 586 587 588
    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");
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	#
	# 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
605 606
    }

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
    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!
    #
622 623 624 625 626 627 628 629
    if ($emulabconfig{"ROLE"} eq "fs") {
	SetupFsNode();
    }
    elsif ($emulabconfig{"ROLE"} eq "ops") {
	SetupOpsNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "ops+fs") {
	SetupOpsNode(1);
630 631
    }
    elsif ($emulabconfig{"ROLE"} eq "boss") {
632 633 634 635 636 637 638
	SetupBossNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "boss+fs") {
	SetupBossNode(1);
    }
    elsif ($emulabconfig{"ROLE"} eq "opsjail") {
	SetupOpsJail();
639
    }
640 641 642
    elsif ($emulabconfig{"ROLE"} eq "node") {
	;
    }
643
    else {
Leigh B Stoller's avatar
Leigh B Stoller committed
644
	fatal("Do not recognize role '". $emulabconfig{"ROLE"} ."'!\n");
645
    }
646 647 648 649 650
    print "Done!\n";
    return 0;
}

#
651
# Setup an fs node.
652
#
653
sub SetupFsNode()
654
{
655
    my $shareslice;
656
    my $FSDIR = $FSMOUNTDIR;
657

658
    my $fromscratch =
659 660
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
661

662 663 664 665 666 667 668 669
    #
    # 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");
    }

670
    #
671 672 673 674 675 676 677 678
    # 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
679
	SetupTBDir($TBDIR);
680
    }
681
    RecreateDir("$FSDIR", 1);
682 683 684 685
    my $fsdev = FindExtraFSConfig($FSDIR);
    if ($fsdev) {
	mysystem("$BINDIR/mkextrafs.pl -s 0 -r $fsdev -f -2 $FSDIR");
	$shareslice = "/dev/$fsdev" . "s1f";
686
    }
687 688
    else {
	mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	#
	# 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";
	} else {
	    SetupFatal("Could not parse mount info to find extra partition");
	}
    }
    # don't mount it yet, just remember
    mysystem("newfs $shareslice");
	
705 706 707
    #
    # Download Emulab source if necessary
    #
708
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
709 710 711 712
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

713 714 715 716 717 718
    # This avoids nfs mounts interruptions
    if (exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("$TBDIR/packages", 1);
	mysystem("tar zxf /usr/packages.tar.gz -C $TBDIR/packages");
    }

719 720 721 722
    #
    # The mirror tree is copied to temp storage, and then copied into
    # place later.
    #
723
    if ($domirror && -e "/proj/$pid/mirror") {
724
	print "Copying over mirror tree from /proj/$pid/mirror\n";
725
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
726 727
    }

728 729 730 731 732
    #
    # 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).
    # 
733
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
734 735
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");

736 737 738 739 740 741 742 743 744 745 746 747 748 749
    #
    # 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;
750 751 752
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;
753
    my $inner_speed;
754

755
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
756 757 758 759 760 761
	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};
762 763 764 765 766 767 768 769 770 771
	$inner_speed     = $ifacelist[0]->{SPEED};
	if ($inner_speed =~ /^(100|1000)Mbps$/) {
	    $inner_speed = $1;
	} else {
	    print STDERR
		"*** Unrecognized inner control net speed '$inner_speed';".
		" defaulting to 100Mbps\n";
		
	    $inner_speed = "100";
	}
772 773
    }

774 775 776 777 778 779 780 781 782 783 784 785
    #
    # 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`");
	}
    }

786 787 788 789 790 791 792 793 794 795 796 797 798 799
    #
    # 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");
	}
    }

800 801 802 803 804
    #
    # 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";
805
    mysystem("$BINDIR/prepare -N");
806

807 808 809 810 811 812 813 814 815 816
    #
    # 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");
    }

817 818 819 820 821
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

822 823 824
    #
    # And clear some other stuff.
    # 
825
    mysystem("rm -rf $TBDIR/bin $TBDIR/lib");
826 827
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
828 829
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");
830

831
    #
832 833
    # Load up packages if necessary
    #
834
    if ($installpkgs) {
835 836 837 838 839
	# 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/;
	}
840

841 842 843 844 845 846 847
	#
	# 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!");
	}
848

849 850
	print "Installing the fs metaport.\n";
	RecreateDir("/usr/ports", 1);
851 852 853
	$ENV{"PKG_PATH"} = $emulabconfig{FS_PKG_DIR};
	mysystem("pkg_add $emulabconfig{FS_PKG} >/tmp/perrs 2>&1");
    }
854 855

    #
856
    # Clean up a few things on the image and create symlinks into $FSDIR for
857
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
858
    #
859 860 861 862 863 864 865 866 867 868 869 870
    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.
    #
871 872 873 874
    RecreateDir("/users", 0);
    RecreateDir("/proj", 0);
    RecreateDir("/groups", 0);
    RecreateDir("/share", 0);
875 876 877 878 879 880 881 882 883 884 885

    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");
886
    mysystem("ln -s $FSDIR/share /share");
887

888 889 890 891
    #
    # And the optional /scratch which share the same FS as /proj, et.al.
    #
    if ($emulabconfig{"CONFIG_SCRATCHFS"}) {
892
	RecreateDir("/scratch", 0);
893 894
	mysystem("mkdir $FSDIR/scratch");
	mysystem("ln -s $FSDIR/scratch /scratch");
895 896
    }

Mike Hibler's avatar
Mike Hibler committed
897 898
    #
    # Lets mount the package dir so that we can pass off some stuff to
899
    # the install scripts.
Mike Hibler's avatar
Mike Hibler committed
900
    #
901 902 903 904
    if (! exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("/packages", 1);
	mysystem("mount ${fsname}:" . $emulabconfig{FS_PKG_DIR} . " /packages");
    }
Mike Hibler's avatar
Mike Hibler committed
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
    #
    # 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";
932 933 934 935 936
    if ($NTPSERVER eq "fs") {
	print RC "ntpdate_flags=\"ntp1.${outer_domain}\"\n";
    } else {
	print RC "ntpdate_flags=\"$NTPSERVER\"\n";
    }
937 938 939 940 941
    if ($FBSD_VERSION >= 5) {
	print RC "ntpd_enable=\"YES\"\n";
    } else {
	print RC "xntpd_enable=\"YES\"\n";
    }
942

943 944
    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
945 946 947
    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
948
    print RC "smbd_enable=\"YES\"\n"
949
	if ($emulabconfig{"CONFIG_WINDOWS"});
950 951
    print RC "mountd_flags=\"-r -p 900\"\n";

952
    print RC "network_interfaces=\"$outer_controlif\"\n";
953 954
    print RC "ifconfig_${outer_controlif}=".
	"\"inet $outer_ip netmask $outer_netmask\"\n";
955

956
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
957 958 959
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
960
	    "media ${inner_speed}baseTX mediaopt full-duplex\"\n";
961 962
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
963 964 965 966
    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"} .
967
	" -iface " . ($emulabconfig{"CONFIG_SINGLECNET"} ?
968
		      $outer_controlif : $inner_controlif) . "\"\n";
969
    # Leave default route pointing to control network until setup complete.
970 971
    if ($emulabconfig{"CONFIG_NOSETUP"} ||
	$emulabconfig{"CONFIG_SINGLECNET"}) {
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
	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");

987 988 989 990 991 992 993
    #
    # If not us, fixup our ntp.conf file to talk to the inner ntp server.
    #
    if ($NTPSERVER ne "fs") {
	mysystem("sed -i '.orig' -E -e 's/^server .*/server $NTPSERVER/' /etc/ntp.conf");
    }

994 995 996
    #
    # Create a defs file. Note that this will move to boss at some point.
    #
997
    CreateDefsFile("$TBDIR/src/testbed/defs-elabinelab");
998 999

    goto skipsetup
1000
	if ($emulabconfig{"CONFIG_NOSETUP"});
1001 1002 1003 1004

    #
    # Configure an object tree. 
    #
1005
    RecreateDir("$TBDIR/obj/testbed", 1);
1006 1007 1008
    mysystem("cd $TBDIR/obj/testbed; ".
	     "   $TBDIR/src/testbed/configure ".
	     "      --with-TBDEFS=$TBDIR/src/testbed/defs-elabinelab ".
1009 1010
	     ($emulabconfig{"CONFIG_WINDOWS"} ?
	      "--enable-windows" : "--disable-windows"));
1011 1012 1013 1014

    #
    # Create the fs node.
    #
1015 1016 1017 1018 1019 1020 1021
    if (exists($emulabconfig{PACKAGE_TARBALL})) {
	$ENV{"PKG_PATH"} = "$TBDIR/packages";
    }
    else {
	$ENV{"PKG_PATH"} = "/packages";
    }
    my $pkg = "-P $emulabconfig{FS_PKG} -p " . $ENV{"PKG_PATH"};
1022
    mysystem("cd $TBDIR/obj/testbed/install; ".
1023
	     "   perl emulab-install $pkg -l -b fs");
1024 1025 1026 1027

    #
    # And install the fs side.
    #
1028
    mysystem("cd $TBDIR/obj/testbed; gmake fs-install");
1029 1030

    #
1031 1032
    # 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
1033 1034
    #
    if ($FBSD_VERSION >= 5) {
1035
	mysystem("/etc/rc.d/rpcbind start");
Mike Hibler's avatar
Mike Hibler committed
1036
	mysystem("/etc/rc.d/nfsd start");
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	#
	# 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");
1049 1050 1051 1052
    } 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
1053
    }
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069

    #
    # 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:
1070 1071 1072
    return
	if ($emulabconfig{"CONFIG_GENIRACK"});
    
1073 1074 1075 1076 1077 1078
    #
    # Hmm, need to run this at startup though.
    # 
    mysystem("echo '/usr/local/etc/emulab/rc/rc.inelab' ".
	     "   >> /etc/rc.local");

1079 1080
    mysystem("cp -p $TBDIR/src/testbed/defs-elabinelab $TBDIR/src");

1081
    return
1082
	if ($emulabconfig{"CONFIG_NOSETUP"});
1083 1084 1085 1086

    #
    # Copy the mirror tree into place. Do not use rsync.
    #
1087
    if (0 && -e "$TBDIR/mirror") {
1088
	print "Copying mirror tree into place\n";
1089
	mysystem("cp -Rfp $TBDIR/mirror/ /");
1090 1091 1092 1093 1094 1095 1096 1097 1098
    }
}

#
# Setup an ops node.
#
sub SetupOpsNode($)
{
    my ($isfs) = @_;
1099 1100
    my $shareslice;
    my $FSDIR = "";
1101

1102
    my $fromscratch =
1103 1104
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
1105 1106

    #
1107 1108 1109 1110 1111 1112 1113 1114
    # 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
1115
	SetupTBDir($TBDIR);
1116 1117
    }
    if ($isfs) {
1118
	$FSDIR = $FSMOUNTDIR;
1119
	RecreateDir("$FSDIR", 1);
1120 1121 1122 1123 1124 1125 1126
	my $fsdev = FindExtraFSConfig($FSDIR);
	if ($fsdev) {
	    mysystem("$BINDIR/mkextrafs.pl -s 0 -r $fsdev -f -2 $FSDIR");
	    $shareslice = "/dev/$fsdev" . "s1f";
	}
	else {
	    mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
1127

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
	    #
	    # 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";
	    } else {
		SetupFatal("Could not parse mount info to find extra partition");
	    }
1140
	}
1141 1142
	# don't mount it yet, just remember
	mysystem("newfs $shareslice");
1143
    }
1144

1145 1146 1147
    #
    # Download Emulab source if necessary
    #
1148
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
1149 1150 1151 1152
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

1153 1154 1155 1156 1157 1158
    # This avoids nfs mounts interruptions
    if (exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("$TBDIR/packages", 1);
	mysystem("tar zxf /usr/packages.tar.gz -C $TBDIR/packages");
    }

1159 1160 1161 1162 1163 1164
    #
    # 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";
1165
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
    }

    #
    # 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;
1190 1191 1192
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;
1193
    my $inner_speed;
1194

1195
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
1196 1197 1198 1199 1200 1201
	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};
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
	$inner_speed     = $ifacelist[0]->{SPEED};
	if ($inner_speed =~ /^(100|1000)Mbps$/) {
	    $inner_speed = $1;
	} else {
	    print STDERR
		"*** Unrecognized inner control net speed '$inner_speed';".
		" defaulting to 100Mbps\n";
		
	    $inner_speed = "100";
	}
1212 1213
    }

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
    #
    # 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`");
	}
    }

1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    #
    # 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");
	}
    }

1240 1241 1242 1243 1244
    #
    # 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";
1245
    mysystem("$BINDIR/prepare -N");