rc.mkelab 122 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004-2018 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
# ZFS support
my $ZPOOLCMD = "/sbin/zpool";
my $ZFSCMD =   "/sbin/zfs";
my $ZFSPOOL =  "emulab";

156 157 158 159 160 161
#
# Support for Windows nodes.
# XXX here for compat; replaced by emulabconfig option.
#
my $WINSUPPORT = 1;

162 163 164
# This also gets turned on/off below
my $NOSETUP    = 0;

165 166 167
# Whether we configure a /scratch FS, turned on/off below
my $SCRATCHFS  = 0;

168 169 170
# Single or dual control network.
my $SINGLE_CONTROLNET = 0;

171 172 173
# Support a shared filesystem between fs server and nodes
my $SHAREDFS   = 1;

174 175 176
# When there is a shared FS via NFS, does it have the mountd race?
my $NFSRACY = 1;

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

182 183 184
# Is ops a VM (Jail) on boss.
my $OPSVM = 0;

185 186 187
# Are we a XEN VM?
my $XENVM = 0;

188 189 190 191
#
# Node to use as ntp server for inner nodes and other inner servers.
# This node will use the outside "ntp1" server as its server.
#
192
# XXX this has never been tested with anything but "boss" and "ops".
193
#
194
my $NTPSERVER = "ops";
195

196 197 198 199 200 201 202 203 204 205 206
#
# 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",
207
    "MFSVERSION"  => "8-64",
208 209 210 211 212 213 214 215
    "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.
216
    #	CONFIG_ELVIN	support Elvin compatibility (obsolete)
217
    #	CONFIG_WINDOWS	support Windows OS on nodes
218
    #	CONFIG_SHAREDFS	support shared filesystem
219
    #	CONFIG_QUERIER	configure mfrisbeed to act as IGMP querier
220 221
    #	CONFIG_OPSVM	configure ops node to be jail on boss (obsolete)
    #	CONFIG_NODBINIT	do not load DB state on boss
222
    #	CONFIG_TARGETSYS configure for a target system
223 224
    #	CONFIG_ZFS	configure fs node to use ZFS
    #	CONFIG_AUTOFS	configure autofs instead of amd for boss ZFS mounts
225 226 227 228
    #
    "CONFIG_NOSETUP"    => $NOSETUP,
    "CONFIG_SCRATCHFS"  => $SCRATCHFS,
    "CONFIG_SINGLECNET" => $SINGLE_CONTROLNET,
Mike Hibler's avatar
Mike Hibler committed
229
    "CONFIG_ELVIN"      => 0,
230
    "CONFIG_WINDOWS"    => $WINSUPPORT,
231
    "CONFIG_SHAREDFS"	=> $SHAREDFS,
232
    "CONFIG_NFSRACY"	=> $NFSRACY,
233
    "CONFIG_QUERIER"	=> $NEEDQUERIER,
234
    "CONFIG_OPSVM"	=> $OPSVM,
235
    "CONFIG_NODBINIT"   => 0,
236
    "CONFIG_TARGETSYS"  => 0,
237 238
    "CONFIG_ZFS"	=> 0,
    "CONFIG_AUTOFS"	=> 0,
239
    "CONFIG_PORTAL"	=> 0,
240 241 242

    # Default protogeni package if LOAD_PROTOGENI is enabled.
    "PGENI_PKG"		=> "emulab-protogeni-1.0",
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 269 270 271
    #
    # 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,
);
272 273 274

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

282 283 284 285 286 287 288 289 290
# Architecture
my $FBSD_ARCH = "i386";
if (`uname -m` =~ /^(\S+)$/) {
    $FBSD_ARCH = $1;
}
else {
    die("Could not determine what architecutre FreeBSD is running on!\n");
}

291 292 293 294 295 296
# Extra fetch options on FreeBSD 10.
my $FETCHOPTIONS = "";
if ($FBSD_VERSION >= 10.0) {
    $FETCHOPTIONS = "--no-verify-peer --no-verify-hostname";
}

297 298 299 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
#
# 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($);
336
sub mysystem($;$);
337
sub SetupOpsNode($);
338
sub SetupBossNode($);
339 340
sub CreateDefsFile($);
sub SetupSendMail($$);
341
sub GetEmulabSource($);
342
sub MungeMfsRoot($);
Mike Hibler's avatar
Mike Hibler committed
343 344
sub SetupTBDir($);
sub RecreateDir($$);
345
sub CreateOpsJail($);
346
sub SetupOpsJail();
347
sub FindExtraFSConfig($);
348 349
sub AddPackage($$$);
sub DelPackage($@);
350
sub WriteOPSImage($$);
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
    # NFS race condition was fixed in FreeBSD 9.
Mike Hibler's avatar
Mike Hibler committed
387
    #
388 389 390 391
    if ($FBSD_VERSION >= 9.2) {
	$emulabconfig{"CONFIG_NFSRACY"} = 0;
    }

392 393 394 395 396 397 398
    #
    # Use the 10.x based MFSes for newer installs
    #
    if ($FBSD_VERSION >= 10.3) {
	$emulabconfig{"MFSVERSION"} = "10-64";
    }

399
    # Determine if a XEN VM.
400 401
    if (!$opsjail &&
	system("sysctl -n kern.vm_guest >/dev/null 2>&1") == 0) {
402 403 404
	my $vm_guest = `sysctl -n kern.vm_guest`;
	if ($vm_guest =~ /^xen$/) {
	    $XENVM = 1;
405
	    print "Running inside a XEN VM\n";
406 407 408
	}
    }

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    #
    # 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
424

425 426 427 428 429 430 431
    # And the outer interface
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

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

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

453
	    $emulabconfig{$key} = $val;
454 455 456
	}
    }

Mike Hibler's avatar
Mike Hibler committed
457
    #
458
    # XXX temporary backward compat til this (router) gets implemented
Mike Hibler's avatar
Mike Hibler committed
459 460 461
    #
    $emulabconfig{"ROLE"} = "boss"
	if ($emulabconfig{"ROLE"} eq "boss+router");
462 463 464
    $emulabconfig{"ROLE"} = "boss+fs"
	if ($emulabconfig{"ROLE"} eq "boss+fs+router");

465 466 467 468
    # Override the role when inside an ops jail. 
    $emulabconfig{"ROLE"} = "opsjail"
	if ($opsjail);

469 470 471 472 473 474 475 476 477
    #
    # 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"};
    }

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    #
    # 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);
    }
	
505 506 507 508
    # Override NTPSERVER when singlenet; okay to use inner ops.
    $NTPSERVER = "ops"
	if ($emulabconfig{"CONFIG_SINGLECNET"});

509 510 511 512 513 514 515 516 517 518 519 520 521 522
    #
    # Only support ZFS right now on ZFS-savvy ops+fs node.
    #
    if ($emulabconfig{"CONFIG_ZFS"} != 0) {
	# XXX don't support this yet
	if ($emulabconfig{"ROLE"} eq "fs") {
	    SetupFatal("Do not support ZFS in standalone FS yet.");
	}
	if ($emulabconfig{"ROLE"} eq "ops+fs" &&
	    (! -x $ZPOOLCMD || system("kldload -n zfs.ko"))) {
	    SetupFatal("Image does not support ZFS.");
	}
    }
    
Mike Hibler's avatar
Mike Hibler committed
523 524 525 526 527 528 529
    #
    # XXX obsolete options
    #
    if ($emulabconfig{"CONFIG_ELVIN"} != 0) {
	SetupFatal("Do not support CONFIG_ELVIN anymore.");
    }

530
    #
531
    # XXX not-so-temporary: adjust package info.
532
    #
533 534 535
    # 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.
536
    #
537 538 539 540 541 542 543 544 545
    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) {    
546 547 548 549 550 551 552
	$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";
    }
553
    elsif ($FBSD_VERSION == 6.0) {
554 555 556 557 558 559 560
	$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";
    }
561
    elsif ($FBSD_VERSION == 6.1 ||
Mike Hibler's avatar
Mike Hibler committed
562
	   $FBSD_VERSION == 6.2) {
563
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages";
564 565
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
566 567 568 569
	$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
570
    elsif ($FBSD_VERSION == 6.3) {
571
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.3/packages";
Mike Hibler's avatar
Mike Hibler committed
572 573 574 575 576 577
	$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
578
    elsif ($FBSD_VERSION == 7.2) {
579
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.2/packages";
580 581 582 583 584 585
	$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
586
    elsif ($FBSD_VERSION == 7.3) {
587 588
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/7.3/packages${suf}";
Mike Hibler's avatar
Mike Hibler committed
589 590 591 592 593 594
	$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";
    }
595
    elsif ($FBSD_VERSION == 8.2) {
Leigh B Stoller's avatar
Leigh B Stoller committed
596 597 598 599 600 601
	$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";
602
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-8.2-packages.tar.gz";
603
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-2.0";
604
    }
605 606 607 608 609 610 611 612 613 614 615
    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";
    }
616
    elsif ($FBSD_VERSION >= 9.0 && $FBSD_VERSION < 9.2) {
617 618 619 620 621 622 623
	$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";
624
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-3.0";
625
    } elsif ($FBSD_VERSION >= 9.2 && $FBSD_VERSION < 10.0) {
626 627 628 629 630 631 632 633 634
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/9.2/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-5.1";
	$emulabconfig{OPS_PKG}      = "emulab-ops-5.1";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-5.1";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-9.2-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-3.1";
635
    } elsif ($FBSD_VERSION == 10.0) {
636 637 638 639 640 641 642 643 644
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/10.0/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-6.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-6.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-6.0";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-10.0-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-4.0";
645
    } elsif ($FBSD_VERSION == 10.1) {
646 647 648 649 650 651 652 653 654 655 656
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/10.1/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{EXTRA_PKG_DIR}= $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-6.1";
	$emulabconfig{OPS_PKG}      = "emulab-ops-6.1";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-6.1";
	$emulabconfig{EXTRA_PKG}    = "emulab-extras-6.1";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-10.1-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-6.1";
657
    } elsif ($FBSD_VERSION == 10.2) {
658 659 660 661 662 663 664 665 666 667 668
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/10.2/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{EXTRA_PKG_DIR}= $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-6.2";
	$emulabconfig{OPS_PKG}      = "emulab-ops-6.2";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-6.2";
	$emulabconfig{EXTRA_PKG}    = "emulab-extras-6.2";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-10.2-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-6.2";
669
    } elsif ($FBSD_VERSION >= 10.3 && $FBSD_VERSION < 11.0) {
670 671 672 673 674 675 676 677 678 679 680
	my $suf = ($FBSD_ARCH eq "amd64") ? "-64" : "";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/10.3/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{EXTRA_PKG_DIR}= $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-6.3";
	$emulabconfig{OPS_PKG}      = "emulab-ops-6.3";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-6.3";
	$emulabconfig{EXTRA_PKG}    = "emulab-extras-6.3";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-10.3-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-6.3";
681 682
    } elsif ($FBSD_VERSION <= 11.1) {
	my $suf = "-64";
683 684 685 686 687 688 689 690 691 692
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/11.1/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{EXTRA_PKG_DIR}= $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-7.1";
	$emulabconfig{OPS_PKG}      = "emulab-ops-7.1";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-7.1";
	$emulabconfig{EXTRA_PKG}    = "emulab-extras-7.1";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-11.1-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-7.1";
693 694 695 696 697 698 699 700 701 702 703 704
    } elsif ($FBSD_VERSION >= 11.2) {
	my $suf = "-64";
	$emulabconfig{FS_PKG_DIR} = "/share/freebsd/11.2/packages${suf}";
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{EXTRA_PKG_DIR}= $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-7.2";
	$emulabconfig{OPS_PKG}      = "emulab-ops-7.2";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-7.2";
	$emulabconfig{EXTRA_PKG}    = "emulab-extras-7.2";
	$emulabconfig{PACKAGE_TARBALL} = "FreeBSD-11.2-packages${suf}.tar.gz";
	$emulabconfig{PGENI_PKG}    = "emulab-protogeni-7.2";
705
    }
706 707 708 709
    #
    # If there is a package tarball, prefer that and grab it now.
    # XXX note that we do not download into $TBDIR, it gets moved later.
    #
710 711 712 713
    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};
714
	$emulabconfig{EXTRA_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
715

716
	mysystem("fetch $FETCHOPTIONS -q -o /usr/packages.tar.gz ".
717 718
		 "'http://${bossname}/downloads/" .
		 $emulabconfig{PACKAGE_TARBALL} . "'");
Leigh B Stoller's avatar
Leigh B Stoller committed
719
    }
720 721 722 723 724 725
    #
    # Otherwise, make sure the NFS-mounted package directories exist.
    #
    else {
	if (! -e $emulabconfig{"BOSS_PKG_DIR"} ||
	    ! -e $emulabconfig{"OPS_PKG_DIR"} ||
726 727
	    ! -e $emulabconfig{"FS_PKG_DIR"} ||
	    ! -e $emulabconfig{"EXTRA_PKG_DIR"}) {
728 729 730
	    SetupFatal("Could not locate boss/ops/fs packages");
	}
    }
731

732 733 734
    #
    # XXX Copy in new mkextrafs if necessary
    #
735
    if (-e "/var/run/mkextrafs.pl") {
736 737
	# only copy if newer
	if (! -e "$BINDIR/mkextrafs.pl" ||
738
	    (stat("/var/run/mkextrafs.pl"))[9] >
739
	    (stat("$BINDIR/mkextrafs.pl"))[9]) {
740
	    mysystem("cp /var/run/mkextrafs.pl $BINDIR/");
741 742 743
	}
    }

Leigh B Stoller's avatar
Leigh B Stoller committed
744
    #
745 746
    # 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
747
    #
748 749 750 751 752 753 754 755
    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
756 757
    }

758 759 760 761 762 763 764 765 766 767
    #
    # 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");

768
    if (!exists($emulabconfig{PACKAGE_TARBALL})) {
769
	#
770
	# XXX To avoid NFS errors while copying goo from outer FS.
771 772 773
	#
	system("sysctl vfs.nfs.eacces_retry_enable=1 >/dev/null 2>&1");    
	system("sysctl vfs.nfs.eacces_retry_count=20 >/dev/null 2>&1");
774

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
	#
	# 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
790 791
    }

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    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!
    #
807 808 809 810 811 812 813 814
    if ($emulabconfig{"ROLE"} eq "fs") {
	SetupFsNode();
    }
    elsif ($emulabconfig{"ROLE"} eq "ops") {
	SetupOpsNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "ops+fs") {
	SetupOpsNode(1);
815 816
    }
    elsif ($emulabconfig{"ROLE"} eq "boss") {
817 818 819 820 821
	SetupBossNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "boss+fs") {
	SetupBossNode(1);
    }
822 823 824
    elsif ($emulabconfig{"ROLE"} eq "opsjail") {
	SetupOpsJail();
    }
825 826 827
    elsif ($emulabconfig{"ROLE"} eq "node") {
	;
    }
828
    else {
829
	fatal("Do not recognize role '". $emulabconfig{"ROLE"}. "'!\n");
830
    }
831 832 833 834 835
    print "Done!\n";
    return 0;
}

#
836
# Setup an fs node.
837
#
838
sub SetupFsNode()
839
{
840
    my $shareslice;
841
    my $FSDIR = $FSMOUNTDIR;
842

843
    my $fromscratch =
844 845
	($emulabconfig{"LOAD_ELABSOURCE"} && $emulabconfig{"LOAD_MFS"});
    my $installpkgs = $emulabconfig{"LOAD_PACKAGES"};
846

847 848 849 850 851 852 853 854
    #
    # 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");
    }

855
    #
856 857 858 859 860 861 862 863
    # 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
864
	SetupTBDir($TBDIR);
865
    }
866
    RecreateDir("$FSDIR", 1);
867 868 869 870
    my $fsdev = FindExtraFSConfig($FSDIR);
    if ($fsdev) {
	mysystem("$BINDIR/mkextrafs.pl -s 0 -r $fsdev -f -2 $FSDIR");
	$shareslice = "/dev/$fsdev" . "s1f";
871
    }
872 873
    else {
	mysystem("$BINDIR/mkextrafs.pl -f -2 $FSDIR");
874

875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	#
	# 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");
	
890 891 892
    #
    # Download Emulab source if necessary
    #
893
    if ($emulabconfig{"LOAD_ELABSOURCE"}) {
894 895 896 897
	RecreateDir("$TBDIR/src", 1);
	GetEmulabSource("$TBDIR/src");
    }

898 899 900
    # This avoids nfs mounts interruptions
    if (exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("$TBDIR/packages", 1);
901
	mysystem("tar zxf /usr/packages.tar.gz -C $TBDIR");
902 903
    }

904 905 906 907
    #
    # The mirror tree is copied to temp storage, and then copied into
    # place later.
    #
908
    if ($domirror && -e "/proj/$pid/mirror") {
909
	print "Copying over mirror tree from /proj/$pid/mirror\n";
910
	mysystem("rsync -a --delete /proj/$pid/mirror $TBDIR", 3);
911 912
    }

913 914 915 916 917
    #
    # 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).
    # 
918
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
919 920
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");

921 922 923 924 925 926 927 928 929 930 931 932 933 934
    #
    # 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;
935 936 937
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;
938
    my $inner_speed;
939

940
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
941 942 943 944 945 946
	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};
947 948 949 950 951 952 953 954 955 956
	$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";
	}
957 958
    }

959 960 961 962 963 964 965 966 967 968 969 970
    #
    # 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`");
	}
    }

971 972 973 974 975 976 977 978 979 980 981 982 983 984
    #
    # 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");
	}
    }

985 986 987 988 989
    my $sshdpid = "";
    if (-r "/var/run/sshd.pid") {
	$sshdpid = `cat /var/run/sshd.pid`;
	chomp($sshdpid);
    }
990
    system("cp -rp /root/.ssh /root/.ssh.backup");
991
    system("cp -p /boot/loader.conf /boot/loader.conf.bak");
992

993 994
    #
    # Run the prepare script to clear out the current accounts and such.
995
    # From this point on will need to log in as root.
996 997
    #
    print "Clearing out existing accounts and such\n";
998
    mysystem("$BINDIR/prepare -N");
999

1000 1001 1002
    #
    # 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
1003
    # since fs-install will want to start apps that need libraries from
1004 1005 1006 1007 1008 1009
    # /usr/local/lib.
    #
    if (! -r "/var/run/ld-elf.so.hints") {
	system("/etc/rc.d/ldconfig start");
    }

1010 1011 1012 1013 1014 1015 1016 1017
    #
    # 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");
    }

1018 1019
    #
    # XXX did I mention what an a**hole prepare can be?
1020 1021
    # Put back the sshd pid file that it removed so that that the install
    # scripts can HUP sshd. Also put back the /boot/loader.conf that it
1022 1023
    # unceremoniously overwrote. Not to mention root's .ssh dir which it
    # pissed all over.
1024 1025 1026 1027
    #
    if ($sshdpid) {
	mysystem("echo '$sshdpid' > /var/run/sshd.pid");
    }
1028 1029
    system("mv /root/.ssh /root/.ssh.old.$$");
    system("mv /root/.ssh.backup /root/.ssh");
1030
    system("cp -p /boot/loader.conf.bak /boot/loader.conf");
1031

1032 1033 1034 1035 1036
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

1037 1038 1039
    #
    # And clear some other stuff.
    # 
1040
    mysystem("rm -rf $TBDIR/bin $TBDIR/lib");
1041 1042
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
1043 1044
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");
1045

1046
    #
1047 1048
    # Load up packages if necessary
    #
1049
    if ($installpkgs) {
1050 1051 1052 1053 1054
	# 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/;
	}
1055

1056 1057 1058 1059 1060 1061 1062
	#
	# 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!");
	}
1063

1064 1065
	if ($FBSD_VERSION >= 10.1) {
	    print "Removing ALL packages.\n";
1066
	    DelAllPackages($emulabconfig{FS_PKG_DIR});
1067 1068
	}

1069 1070
	print "Installing the fs metaport.\n";
	RecreateDir("/usr/ports", 1);
1071
	AddPackage($emulabconfig{FS_PKG}, $emulabconfig{FS_PKG_DIR}, 0);
1072 1073 1074 1075 1076 1077 1078

	# Add extras (i.e., emacs) to make life worth living
	if ($FBSD_VERSION >= 10.1) {
	    AddPackage($emulabconfig{EXTRA_PKG}, $emulabconfig{EXTRA_PKG_DIR}, 1);
	}

	if ($FBSD_VERSION >= 10.1) {
1079
	    # XXX FreeBSD got rid of the /usr/bin/perl symlink; we need it.
1080 1081 1082 1083 1084 1085 1086
	    if (! -x "/usr/bin/perl") {
		system("ln -sf /usr/local/bin/perl5 /usr/bin/perl");
	    }
	    # hmm...python too?
	    if (! -x "/usr/local/bin/python") {
		system("ln -sf /usr/local/bin/python2 /usr/local/bin/python");
	    }
1087 1088 1089 1090 1091 1092

	    #
	    # Create a pkg config file for the Emulab repository and synch
	    # up with it.
	    #
	    SyncPackages();
1093
	}
1094
    }
1095 1096

    #
1097
    # Clean up a few things on the image and create symlinks into $FSDIR for
1098
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
1099
    #
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
    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.
    #
1112 1113 1114 1115
    RecreateDir("/users", 0);
    RecreateDir("/proj", 0);
    RecreateDir("/groups", 0);
    RecreateDir("/share", 0);
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

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

1129 1130 1131 1132
    #
    # And the optional /scratch which share the same FS as /proj, et.al.
    #
    if ($emulabconfig{"CONFIG_SCRATCHFS"}) {
1133
	RecreateDir("/scratch", 0);
1134 1135
	mysystem("mkdir $FSDIR/scratch");
	mysystem("ln -s $FSDIR/scratch /scratch");
1136 1137
    }

Mike Hibler's avatar
Mike Hibler committed
1138 1139
    #
    # Lets mount the package dir so that we can pass off some stuff to
1140
    # the install scripts.
Mike Hibler's avatar
Mike Hibler committed
1141
    #
1142 1143 1144 1145
    if (! exists($emulabconfig{PACKAGE_TARBALL})) {
	RecreateDir("/packages", 1);
	mysystem("mount ${fsname}:" . $emulabconfig{FS_PKG_DIR} . " /packages");
    }
Mike Hibler's avatar
Mike Hibler committed
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
    #
    # 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";
1173 1174 1175 1176 1177
    if ($NTPSERVER eq "fs") {
	print RC "ntpdate_flags=\"ntp1.${outer_domain}\"\n";
    } else {
	print RC "ntpdate_flags=\"$NTPSERVER\"\n";
    }
1178 1179 1180 1181 1182
    if ($FBSD_VERSION >= 5) {
	print RC "ntpd_enable=\"YES\"\n";
    } else {
	print RC "xntpd_enable=\"YES\"\n";
    }
1183

1184 1185
    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
1186 1187 1188
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
    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";
	}
    }
1203 1204
    print RC "mountd_flags=\"-r -p 900\"\n";

1205
    print RC "network_interfaces=\"$outer_controlif\"\n";
1206 1207
    print RC "ifconfig_${outer_controlif}=".
	"\"inet $outer_ip netmask $outer_netmask\"\n";
1208

1209
    if (! $emulabconfig{"CONFIG_SINGLECNET"}) {
1210 1211 1212
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
1213
	    "media ${inner_speed}baseTX mediaopt full-duplex\"\n";
1214