rc.mkelab 103 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
Mike Hibler's avatar
Mike Hibler committed
3
# Copyright (c) 2004-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26 27 28 29 30 31 32 33 34
#
# 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;

35 36 37
# XXX not yet complete
my $domirror = 0;

38 39 40 41 42 43
sub usage()
{
    print "Usage: " .
	scriptname() . " boot|shutdown|reconfig|reset\n";
    exit(1);
}
44
my $optlist = "dsj";
45 46
my $action  = "boot";
my $debug   = 0;
47
my $skipit  = 1;	# Temporary until images updated.
48
my $opsjail = 0;	# Only for ops; running inside a jail.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

# 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
79
    if (REMOTE() || JAILED() || DELAYHOST());
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

# 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;
}
97 98 99
if (defined($options{'j'})) {
    $opsjail = 1;
}
100 101 102 103 104 105 106
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

# More stuff we need below.
my $domain;
107
my ($pid,$eid,$vid) = check_nickname();
108 109 110 111 112 113
my $file    = TMCREATOR();
my $creator = `cat $file`;
chomp($creator);

my $hostname = `hostname`;
chomp($hostname);
114
my ($bossname, $outer_bossip) = tmccbossinfo();
115

116 117 118 119
# 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;
120 121
# And the control interface
my $outer_controlif;
122

Leigh B. Stoller's avatar
Leigh B. Stoller committed
123 124 125
# Outer FS node name. See below.
my $fsname;

126 127
# Cert stuff to give the inner emulab
my $RPCCERT = "/usr/testbed/etc/outer_emulab.pem";
128
#my $RPCPORT = 7778;
129

130 131 132 133 134 135 136 137
#
# 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");

138 139 140 141 142 143
#
# Standard system daemons to kill off before we run the Emulab cleanup script.
#
my @SYSDAEMONS = ("cron", "mountd", "ntpd", "sendmail",
		  "syslogd", "nfsd", "rpcbind");

144 145
# or this...
my $FSMOUNTDIR = "/q";
146 147 148 149
my $OPSMOUNTDIR = "/ops";

# Hardwired?
my $TBDIR = "/usr/testbed";
150 151 152 153 154 155 156 157 158 159 160

#
# 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
161
my $NOCOLLAB   = 1;
162 163 164 165 166 167

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

169 170 171
# This also gets turned on/off below
my $NOSETUP    = 0;

172 173 174
# Whether we configure a /scratch FS, turned on/off below
my $SCRATCHFS  = 0;

175 176 177
# Single or dual control network.
my $SINGLE_CONTROLNET = 0;

178 179 180
# Support a shared filesystem between fs server and nodes
my $SHAREDFS   = 1;

181 182
# Start up the frisbee master server to act as an IGMP querier
# so that frisbee works (if there is no other querier running).
183
# -1 means let the system decide.
184
my $NEEDQUERIER = -1;
185

186 187 188
# Is ops a VM (Jail) on boss.
my $OPSVM = 0;

189 190 191
# Are we a XEN VM?
my $XENVM = 0;

192 193 194 195 196 197 198 199
#
# 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";

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
#
# 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)
229
    #	CONFIG_SHAREDFS	support shared filesystem	    
230
    #	CONFIG_QUERIER	configure mfrisbeed to act as IGMP querier
231 232 233 234 235 236 237 238 239 240 241 242 243
    #
    "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,
244
    "CONFIG_SHAREDFS"	=> $SHAREDFS,
245
    "CONFIG_QUERIER"	=> $NEEDQUERIER,
246
    "CONFIG_OPSVM"	=> $OPSVM,
247
    "CONFIG_NODBINIT"   => 0,
248
    "CONFIG_GENIRACK"   => 0,
249 250 251

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

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    #
    # 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,
);
281 282 283

# Version of FreeBSD.
my $FBSD_VERSION = 4;
284
if (`uname -r` =~ /^(\d\.\d*)/) {
285 286 287 288 289 290
    $FBSD_VERSION = $1;
}
else {
    die("Could not determine what version of FreeBSD you are running!\n");
}

291 292 293 294 295 296 297 298 299
# Architecture
my $FBSD_ARCH = "i386";
if (`uname -m` =~ /^(\S+)$/) {
    $FBSD_ARCH = $1;
}
else {
    die("Could not determine what architecutre FreeBSD is running on!\n");
}

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
#
# 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($);
339
sub mysystem($;$);
340
sub SetupOpsNode($);
341
sub SetupBossNode($);
342 343
sub CreateDefsFile($);
sub SetupSendMail($$);
344
sub GetEmulabSource($);
345
sub MungeMfsRoot($);
Mike Hibler's avatar
Mike Hibler committed
346 347
sub SetupTBDir($);
sub RecreateDir($$);
348 349
sub CreateOpsJail($);
sub SetupOpsJail();
350
sub FindExtraFSConfig($);
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

#
# 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);
    }
378 379 380 381 382 383

    #
    # Stash us into the BINDIR for later.
    #
    mysystem("cp -fp $0 $BINDIR/rc")
	if ("$0" ne "$BINDIR/rc/rc.mkelab");
384
    
Mike Hibler's avatar
Mike Hibler committed
385
    #
386
    # FreeBSD version specific adjustments for defaults.
Mike Hibler's avatar
Mike Hibler committed
387
    #
388 389 390
    if ($FBSD_VERSION >= 8.2) {
	$emulabconfig{"MFSVERSION"} = "82";
    } elsif ($FBSD_VERSION >= 7.2) {
391 392 393 394
	$emulabconfig{"MFSVERSION"} = "72";
    } else {
	$emulabconfig{"MFSVERSION"} = "62";
    }
395

396 397 398 399 400 401 402 403 404
    # 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";
	}
    }

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    #
    # 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
420

421 422 423 424 425 426 427
    # And the outer interface
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

428 429 430 431 432 433 434
    #
    # Turn the tmcc results into a hash first. Then call the boss or ops
    # setup function.
    #
    foreach my $line (@tmccresults) {
	if ($line =~ /^(.*)="(.+)"$/ ||
	    $line =~ /^(.*)=(.+)$/) {
435 436
	    my $key = $1;
	    my $val = $2;
437 438 439 440 441 442 443 444 445 446 447 448

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

449 450 451 452 453 454 455 456 457 458 459 460 461 462
	    $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;
	    }
463 464 465
	}
    }

Mike Hibler's avatar
Mike Hibler committed
466
    #
467
    # XXX temporary backward compat til this (router) gets implemented
Mike Hibler's avatar
Mike Hibler committed
468 469 470
    #
    $emulabconfig{"ROLE"} = "boss"
	if ($emulabconfig{"ROLE"} eq "boss+router");
471 472 473 474 475 476
    $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
477

478 479 480 481 482 483 484 485 486
    #
    # XXX backward compat for short-lived NEEDMROUTED option.
    # mfrisbeed now serves the role that mrouted did briefly.
    #
    if (defined($emulabconfig{"CONFIG_MROUTED"}) &&
	$emulabconfig{"CONFIG_QUERIER"} == -1) {
	$emulabconfig{"CONFIG_QUERIER"} = $emulabconfig{"CONFIG_MROUTED"};
    }

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
    #
    # Look for a private variable cache.
    #
    if ($emulabconfig{"ROLE"} eq "boss" &&
	exists($emulabconfig{"PRIVATE_VARIABLES"}) &&
	$emulabconfig{"PRIVATE_VARIABLES"} ne "") {
	my $filename = $emulabconfig{"PRIVATE_VARIABLES"};
	
	if (! -e $filename) {
	    SetupFatal("$filename does not exist");
	}
	open(CN, $filename)
	    or SetupFatal("Could not open $filename: $!");
	while (<CN>) {
	    if ($_ =~ /^([-\w]*)\s*=\s*(.*)$/) {
		my $key = $1;
		my $val = $2;
		if ($val =~ /^'(.+)'$/ ||
		    $val =~ /^"(.+)"$/) {
		    $val = $1;
		}
		$emulabconfig{$key} = "$val";
	    }
	}
	close(CN);
    }
	
514 515 516 517
    # Override NTPSERVER when singlenet; okay to use inner ops.
    $NTPSERVER = "ops"
	if ($emulabconfig{"CONFIG_SINGLECNET"});

518
    #
519
    # XXX not-so-temporary: adjust package info.
520
    #
521 522 523
    # 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.
524
    #
525 526 527 528 529 530 531 532 533
    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) {    
534 535 536 537 538 539 540
	$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";
    }
541
    elsif ($FBSD_VERSION == 6.0) {
542 543 544 545 546 547 548
	$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";
    }
549
    elsif ($FBSD_VERSION == 6.1 ||
Mike Hibler's avatar
Mike Hibler committed
550
	   $FBSD_VERSION == 6.2) {
551 552 553
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
554 555
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
556 557 558 559
	$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
560
    elsif ($FBSD_VERSION == 6.3) {
561 562 563
	$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
564 565 566 567 568 569
	$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
570
    elsif ($FBSD_VERSION == 7.2) {
571 572 573
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.2/packages";
	$emulabconfig{FS_PKG_DIR} .= ".elvincompat"
	    if ($emulabconfig{"CONFIG_ELVIN"});
574 575 576 577 578 579
	$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
580
    elsif ($FBSD_VERSION == 7.3) {
581 582
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.3/packages${suf}";
Mike Hibler's avatar
Mike Hibler committed
583 584 585 586 587 588
	$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";
    }
589
    elsif ($FBSD_VERSION == 8.2) {
Leigh B Stoller's avatar
Leigh B Stoller committed
590 591 592 593 594 595
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/8.2/packages";
	$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";
596
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-8.2-packages.tar.gz";
597
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-2.0";
598
    }
599 600 601 602 603 604 605 606 607 608 609
    elsif ($FBSD_VERSION >= 8.3 && $FBSD_VERSION < 9.0) {
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/8.3/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-5.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-5.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-5.0";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-8.3-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-3.0";
    }
610 611 612 613 614 615 616 617
    elsif ($FBSD_VERSION >= 9.0) {
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/9.0/packages";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-5.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-5.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-5.0";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-9.0-packages.tar.gz";
618
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-3.0";
619
    }
620 621 622 623 624 625 626
    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.
627
	# XXX note that we do not download into $TBDIR, it gets moved later.
628 629 630 631
	#
	mysystem("fetch -q -o /usr/packages.tar.gz ".
		 "'http://${bossname}/downloads/" .
		 $emulabconfig{PACKAGE_TARBALL} . "'");
Leigh B Stoller's avatar
Leigh B Stoller committed
632
    }
633

634 635 636 637 638 639 640 641 642 643 644 645
    #
    # 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
646
    #
647 648
    # We no longer use a static password. Generate a random password and
    # stash it in /usr/testbed/etc.
Leigh B Stoller's avatar
Leigh B Stoller committed
649
    #
650 651 652 653 654 655 656 657
    if (! exists($emulabconfig{"PASSWORD"}) ||
	$emulabconfig{"PASSWORD"} eq "") {
	my $rand= `/bin/dd if=/dev/urandom count=128 bs=1 2>/dev/null | /sbin/md5`;
	if ($?) {
	    SetupFatal("Could not generate a root password");
	}
	chomp($rand);
	$emulabconfig{"PASSWORD"} = substr($rand, 0, 10);
Leigh B Stoller's avatar
Leigh B Stoller committed
658 659
    }

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

670 671 672 673 674 675
    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");
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
	#
	# 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
692 693
    }

694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
    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!
    #
709 710 711 712 713 714 715 716
    if ($emulabconfig{"ROLE"} eq "fs") {
	SetupFsNode();
    }
    elsif ($emulabconfig{"ROLE"} eq "ops") {
	SetupOpsNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "ops+fs") {
	SetupOpsNode(1);
717 718
    }
    elsif ($emulabconfig{"ROLE"} eq "boss") {
719 720 721 722 723 724 725
	SetupBossNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "boss+fs") {
	SetupBossNode(1);
    }
    elsif ($emulabconfig{"ROLE"} eq "opsjail") {
	SetupOpsJail();
726
    }
727 728 729
    elsif ($emulabconfig{"ROLE"} eq "node") {
	;
    }
730
    else {
731
	fatal("Do not recognize role '". $emulabconfig{"ROLE"}. "'!\n");
732
    }
733 734 735 736 737
    print "Done!\n";
    return 0;
}

#
738
# Setup an fs node.
739
#
740
sub SetupFsNode()
741
{
742
    my $shareslice;
743
    my $FSDIR = $FSMOUNTDIR;
744

745
    my $fromscratch =
746 747
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
748

749 750 751 752 753 754 755 756
    #
    # 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");
    }

757
    #
758 759 760 761 762 763 764 765
    # 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
766
	SetupTBDir($TBDIR);
767
    }
768
    RecreateDir("$FSDIR", 1);
769 770 771 772
    my $fsdev = FindExtraFSConfig($FSDIR);
    if ($fsdev) {
	mysystem("$BINDIR/mkextrafs.pl -s 0 -r $fsdev -f -2 $FSDIR");
	$shareslice = "/dev/$fsdev" . "s1f";
773
    }
774 775
    else {
	mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
776

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	#
	# 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");
	
792 793 794
    #
    # Download Emulab source if necessary
    #
795
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
796 797 798 799
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

800 801 802
    # This avoids nfs mounts interruptions
    if (exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("$TBDIR/packages", 1);
803
	mysystem("tar zxf /usr/packages.tar.gz -C $TBDIR");
804 805
    }

806 807 808 809
    #
    # The mirror tree is copied to temp storage, and then copied into
    # place later.
    #
810
    if ($domirror && -e "/proj/$pid/mirror") {
811
	print "Copying over mirror tree from /proj/$pid/mirror\n";
812
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
813 814
    }

815 816 817 818 819
    #
    # 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).
    # 
820
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
821 822
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");

823 824 825 826 827 828 829 830 831 832 833 834 835 836
    #
    # 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;
837 838 839
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;
840
    my $inner_speed;
841

842
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
843 844 845 846 847 848
	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};
849 850 851 852 853 854 855 856 857 858
	$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";
	}
859 860
    }

861 862 863 864 865 866 867 868 869 870 871 872
    #
    # 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`");
	}
    }

873 874 875 876 877 878 879 880 881 882 883 884 885 886
    #
    # 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");
	}
    }

887 888 889 890 891
    #
    # 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";
892
    mysystem("$BINDIR/prepare -N");
893

894 895 896 897 898 899 900 901 902 903
    #
    # 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");
    }

904 905 906 907 908 909 910 911
    #
    # This is a fixup, cause prepare removes the vmname, which is needed
    # during the reboot to tell outer boss we are alive (rc.inelab).
    #
    if ($XENVM) {
	mysystem("echo '$vid' > $BOOTDIR/vmname");
    }

912 913 914 915 916
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

917 918 919
    #
    # And clear some other stuff.
    # 
920
    mysystem("rm -rf $TBDIR/bin $TBDIR/lib");
921 922
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
923 924
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");
925

926
    #
927 928
    # Load up packages if necessary
    #
929
    if ($installpkgs) {
930 931 932 933 934
	# 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/;
	}
935

936 937 938 939 940 941 942
	#
	# 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!");
	}
943

944 945
	print "Installing the fs metaport.\n";
	RecreateDir("/usr/ports", 1);
946 947 948
	$ENV{"PKG_PATH"} = $emulabconfig{FS_PKG_DIR};
	mysystem("pkg_add $emulabconfig{FS_PKG} >/tmp/perrs 2>&1");
    }
949 950

    #
951
    # Clean up a few things on the image and create symlinks into $FSDIR for
952
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
953
    #
954 955 956 957 958 959 960 961 962 963 964 965
    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.
    #
966 967 968 969
    RecreateDir("/users", 0);
    RecreateDir("/proj", 0);
    RecreateDir("/groups", 0);
    RecreateDir("/share", 0);
970 971 972 973 974 975 976 977 978 979 980

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

983 984 985 986
    #
    # And the optional /scratch which share the same FS as /proj, et.al.
    #
    if ($emulabconfig{"CONFIG_SCRATCHFS"}) {
987
	RecreateDir("/scratch", 0);
988 989
	mysystem("mkdir $FSDIR/scratch");
	mysystem("ln -s $FSDIR/scratch /scratch");
990 991
    }

Mike Hibler's avatar
Mike Hibler committed
992 993
    #
    # Lets mount the package dir so that we can pass off some stuff to
994
    # the install scripts.
Mike Hibler's avatar
Mike Hibler committed
995
    #
996 997 998 999
    if (! exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("/packages", 1);
	mysystem("mount ${fsname}:" . $emulabconfig{FS_PKG_DIR} . " /packages");
    }
Mike Hibler's avatar
Mike Hibler committed
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    #
    # 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";
1027 1028 1029 1030 1031
    if ($NTPSERVER eq "fs") {
	print RC "ntpdate_flags=\"ntp1.${outer_domain}\"\n";
    } else {
	print RC "ntpdate_flags=\"$NTPSERVER\"\n";
    }
1032 1033 1034 1035 1036
    if ($FBSD_VERSION >= 5) {
	print RC "ntpd_enable=\"YES\"\n";
    } else {
	print RC "xntpd_enable=\"YES\"\n";
    }
1037

1038 1039
    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
1040 1041 1042
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
    if ($emulabconfig{"CONFIG_WINDOWS"}) {
	#
	# As of 10/2012, the startup for samba changed.
	# XXX not directly related to the FreeBSD version, but it is
	# the best we can do--the port version was not bumped when this
	# change was made.
	#
	if ($FBSD_VERSION >= 8.3) {
	    print RC "samba_enable=\"YES\"\n";
	    print RC "nmbd_enable=\"NO\"\n";
	} else {
	    print RC "smbd_enable=\"YES\"\n";
	}
    }
1057 1058
    print RC "mountd_flags=\"-r -p 900\"\n";

1059
    print RC "network_interfaces=\"$outer_controlif\"\n";
1060 1061
    print RC "ifconfig_${outer_controlif}=".
	"\"inet $outer_ip netmask $outer_netmask\"\n";
1062

1063
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
1064 1065 1066
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
1067
	    "media ${inner_speed}baseTX mediaopt full-duplex\"\n";
1068 1069
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
1070 1071 1072 1073
    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"} .
1074
	" -iface " . ($emulabconfig{"CONFIG_SINGLECNET"} ?
1075
		      $outer_controlif : $inner_controlif) . "\"\n";
1076
    # Leave default route pointing to control network until setup complete.
1077 1078
    if ($emulabconfig{"CONFIG_NOSETUP"} ||
	$emulabconfig{"CONFIG_SINGLECNET"}) {
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	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");

1094 1095 1096 1097 1098 1099 1100
    #
    # 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");
    }

1101 1102 1103
    #
    # Create a defs file. Note that this will move to boss at some point.
    #
1104
    CreateDefsFile("$TBDIR/src/testbed/defs-elabinelab");
1105 1106

    goto skipsetup
1107
	if ($emulabconfig{"CONFIG_NOSETUP"});
1108 1109 1110 1111

    #
    # Configure an object tree. 
    #
1112
    RecreateDir("$TBDIR/obj/testbed", 1);
1113 1114 1115
    mysystem("cd $TBDIR/obj/testbed; ".
	     "   $TBDIR/src/testbed/configure ".
	     "      --with-TBDEFS=$TBDIR/src/testbed/defs-elabinelab ".
1116 1117
	     ($emulabconfig{"CONFIG_WINDOWS"} ?
	      "--enable-windows" : "--disable-windows"));
1118 1119 1120 1121

    #
    # Create the fs node.
    #
1122 1123 1124 1125 1126 1127 1128
    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"};
1129
    mysystem("cd $TBDIR/obj/testbed/install; ".
1130
	     "   perl emulab-install $pkg -l -b fs");
1131 1132 1133 1134

    #
    # And install the fs side.
    #
1135
    mysystem("cd $TBDIR/obj/testbed; gmake fs-install");
1136 1137

    #
1138 1139
    # 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
1140 1141
    #
    if ($FBSD_VERSION >= 5) {
1142
	mysystem("/etc/rc.d/rpcbind start");
Mike Hibler's avatar
Mike Hibler committed
1143
	mysystem("/etc/rc.d/nfsd start");
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	#
	# 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.
	#
1155 1156 1157 1158
	# XXX: nfsd start above may have already started mountd, so don't
	# fail if this mountd restart fails!
	#
	system("mountd -r");
1159 1160 1161 1162
    } 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
1163
    }
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179

    #
    # 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:
1180 1181 1182
    return
	if ($emulabconfig{"CONFIG_GENIRACK"});
    
1183 1184 1185 1186 1187 1188
    #
    # Hmm, need to run this at startup though.
    # 
    mysystem("echo '/usr/local/etc/emulab/rc/rc.inelab' ".
	     "   >> /etc/rc.local");

1189 1190
    mysystem("cp -p $TBDIR/src/testbed/defs-elabinelab $TBDIR/src");

1191
    return
1192
	if ($emulabconfig{"CONFIG_NOSETUP"});
1193 1194 1195 1196

    #
    # Copy the mirror tree into place. Do not use rsync.
    #
1197
    if (0 && -e "$TBDIR/mirror") {
1198
	print "Copying mirror tree into place\n";
1199
	mysystem("cp -Rfp $TBDIR/mirror/ /");
1200 1201 1202 1203 1204 1205 1206 1207 1208
    }
}

#
# Setup an ops node.
#
sub SetupOpsNode($)
{
    my ($isfs) = @_;
1209
    my $shareslice;
1210
    my $sharefsdev;
1211
    my $FSDIR = "";
1212

1213
    my $fromscratch =
1214 1215
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
1216 1217

    #
1218 1219 1220 1221 1222 1223 1224 1225
    # 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
1226
	SetupTBDir($TBDIR);
1227 1228
    }
    if ($isfs) {
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	#
	# Look for a specified /share, otherwise do the -2 trick to
	# split $FSDIR
	#
	$sharefsdev = FindExtraFSConfig("/share");
	if ($sharefsdev) {
	    #
	    # Use the nomount option, since we might already have /share
	    # NFS mounted. See below.
	    #
	    mysystem("$BINDIR/mkextrafs.pl -m -s 0 -r $sharefsdev -f /share");
	    $shareslice = "/dev/${sharefsdev}s1e";
	}
1242
	$FSDIR = $FSMOUNTDIR;
1243
	RecreateDir("$FSDIR", 1);
1244 1245
	my $fsdev = FindExtraFSConfig($FSDIR);
	if ($fsdev) {
1246 1247 1248 1249 1250
	    my $opt = (!defined($sharefsdev) ? "-2" : "");
	    
	    mysystem("$BINDIR/mkextrafs.pl -s 0 -r $fsdev -f $opt $FSDIR");
	    $shareslice = "/dev/$fsdev" . "s1f"
		if (!defined($sharefsdev));
1251 1252
	}
	else {
1253 1254
	    my $opt = (!defined($sharefsdev) ? "-2" : "");

Leigh B Stoller's avatar
Leigh B Stoller committed
1255
	    mysystem("$BINDIR/mkextrafs.pl -f $opt $FSDIR");
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

	    if (!defined($sharefsdev)) {
		#
		# 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");
		}
1271
	    }
1272
	}
1273 1274 1275 1276 1277 1278 1279 1280 1281
	#
	# If we used -2 above, then need to add the fstab entry and
	# run newfs cause mkextrafs does not do that. But do not mount
	# yet since we probably have a /share nfs mounted. See below.
	#
	if (!defined($sharefsdev)) {
	    mysystem("newfs $shareslice");
	    mysystem("echo '$shareslice $FSDIR/share ufs rw 0 2' >>/etc/fstab");
	}
1282
    }
1283

1284 1285 1286
    #
    # Download Emulab source if necessary
    #