rc.mkelab 54.3 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004-2014 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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#
# 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;

# XXX not yet complete
my $domirror = 0;

sub usage()
{
    print "Usage: " .
	scriptname() . " boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist = "ds";
my $action  = "boot";
my $debug   = 0;
my $skipit  = 1;	# Temporary until images updated.

# 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)
    if (REMOTE() || JAILED() || DELAYHOST());

# 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;
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

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

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

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

# Outer FS node name. See below.
my $fsname;

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

# This gets turned on/off below
my $WINSUPPORT = 0;

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

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

# Whether installation of collab tools should be disabled
my $NOCOLLAB   = 0;

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

# Enable elvin compatibility
my $ELVIN_COMPAT = 0;

# This will not change ...
my $PHP4_PKG = "php4-extensions-1.0";

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

#
# 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($);
sub mysystem($;$);
sub SetupOpsNode($);
sub SetupBossNode();
sub CreateDefsFile($);
sub SetupSendMail($$);
sub GetEmulabSource($);
sub MungeMfsRoot($);

#
# 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.
213 214
    print "foo " . keys(%tmccresults) . "\n";
    exit 0;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 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 281 282 283 284 285
    return 0 if (! keys(%tmccresults));

    #
    # This is temporary until images are up to date.
    #
    if (0 && ! $skipit) {
	print "*** Installing current software first ... \n";
	
	mysystem("cd /users/stoller/testbed/obj-real/tmcd/common; ".
		 "gmake local-install");
	exec($PROGRAM_NAME, ("-s"));
	die("*** $0:\n".
	    "    Could not re-exec script!\n");
    }

    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);
    }
    
    #
    # XXX defaults for things that may or may not be passed in.
    # Will be overridden if values are actually passed in.
    #
    $emulabconfig{"JAILIPBASE"} = "172.16.0.0";
    $emulabconfig{"JAILIPMASK"} = "255.240.0.0";
    $emulabconfig{"MFSTARBALL"} = "tftpboot-elabinelab.tar.gz";
    $emulabconfig{"MFSVERSION"} = "62";
    $emulabconfig{"MFSCONSOLE"} = "sio";
    $emulabconfig{"WINSUPPORT"} = 0;
    $emulabconfig{"NOSETUP"}    = 0;
    $emulabconfig{"SCRATCHFS"}  = 0;
    $emulabconfig{"SINGLE_CONTROLNET"} = 0;
    if (-x "/usr/local/libexec/elvind") {
	$emulabconfig{"ELVIN_COMPAT"} = 1;
    } else {
	$emulabconfig{"ELVIN_COMPAT"} = 0;
    }

    #
    # 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);

    #
    # Turn the tmcc results into a hash first. Then call the boss or ops
    # setup function.
    #
    foreach my $key (keys %tmccresults) {
	if ($line =~ /^(.*)="(.+)"$/ ||
	    $line =~ /^(.*)=(.+)$/) {

286
	    $emulabconfig{uc $key} = $tmccresults{uc $key};
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 318 319 320 321 322 323 324 325 326 327 328 329 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 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 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833
	}
    }

    #
    # XXX temporary backward compat til this get implemented
    #
    $emulabconfig{"ROLE"} = "boss"
	if ($emulabconfig{"ROLE"} eq "boss+router");

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

    # Turn on windows support.
    if ($emulabconfig{"WINSUPPORT"}) {
	$WINSUPPORT = 1;
    }
    # Ditto nosetup.
    if ($emulabconfig{"NOSETUP"}) {
	$NOSETUP = 1;
    }
    # and scratch filesystem usage
    if ($emulabconfig{"SCRATCHFS"}) {
	$SCRATCHFS = 1;
    }
    # and single vs dual control network
    if ($emulabconfig{"SINGLE_CONTROLNET"}) {
	$SINGLE_CONTROLNET = 1;
    }
    # and elvin compatibility
    if ($emulabconfig{"ELVIN_COMPAT"}) {
	$ELVIN_COMPAT = 1;
    }

    # XXX Temporary
    if ($FBSD_VERSION == 5.4) {    
	$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";
    }
    elsif ($FBSD_VERSION == 6.0) {
	$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";
    }
    elsif ($FBSD_VERSION == 6.1 ||
	   $FBSD_VERSION == 6.2 ||
	   $FBSD_VERSION == 6.3) {
	if ($ELVIN_COMPAT) {
	    $emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages.elvincompat";
	} else {
	    $emulabconfig{FS_PKG_DIR} = "/share/freebsd/6.1/packages";
	}
	$emulabconfig{OPS_PKG_DIR}  = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{BOSS_PKG_DIR} = $emulabconfig{FS_PKG_DIR};
	$emulabconfig{FS_PKG}       = "emulab-fs-2.0";
	$emulabconfig{OPS_PKG}      = "emulab-ops-2.0";
	$emulabconfig{BOSS_PKG}     = "emulab-boss-2.0";
    }

    # Figure out where /share is coming from (outer fs node). We need
    # that below.
    $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!");
    }

    if ($emulabconfig{"ROLE"} eq "fs") {
	SetupFsNode();
    }
    elsif ($emulabconfig{"ROLE"} eq "ops") {
	SetupOpsNode(0);
    }
    elsif ($emulabconfig{"ROLE"} eq "ops+fs") {
	SetupOpsNode(1);
    }
    elsif ($emulabconfig{"ROLE"} eq "boss") {
	SetupBossNode();
    }
    print "Done!\n";
    return 0;
}

#
# Setup an fs node.
#
sub SetupFsNode()
{
    print "Setting up an Fs node ...\n";
    $TBFS = "/q";
    $TBDIR = "/q";

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

    #
    # Create a ${TBDIR} from the extra slice and put everything there.
    # 
    mysystem("mkdir ${TBFS}")
	if (! -d "${TBFS}");
    mysystem("$BINDIR/mkextrafs.pl -f ${TBFS}");
    mysystem("mkdir ${TBDIR}/testbed");
    mysystem("mkdir ${TBDIR}/testbed/src");
    mysystem("mkdir ${TBDIR}/testbed/obj");

    GetEmulabSource("${TBDIR}/testbed/src");

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

    #
    # 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);
    
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    my $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

    #
    # We also need the hardwired config for the inner control network.
    # Major kludge; should get it from tmcd data.
    #
    my @ifacelist;
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;

    if (! $SINGLE_CONTROLNET) {
	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};
    }

    #
    # 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";
    mysystem("$BINDIR/prepare -N");

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

    #
    # And clear some other stuff.
    # 
    mysystem("rm -rf /usr/testbed/lib");
    mysystem("rm -rf /usr/testbed/bin");
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");

    goto skippkg
	if ($NOSETUP);

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

    #
    # 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!");
    }
    print "Installing the fs metaport.\n";

    # Make sure /usr/ports is not a symlink to RO shared space
    if (-l "/usr/ports") {
	if (!unlink("/usr/ports")) {
	    print "WARNING: /usr/ports is a symlink, port install may fail\n";
	} else {
	    mysystem("mkdir /usr/ports");
	}
    }

    $ENV{"PKG_PATH"} = $emulabconfig{FS_PKG_DIR};
    mysystem("pkg_add $emulabconfig{FS_PKG} >/tmp/perrs 2>&1");
  skippkg:

    #
    # Clean up a few things on the image and create symlinks into ${TBDIR} for
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
    #
    mysystem("umount -A -t nfs");
    # In case umount fails!
    mysystem("cd /; mv -f users users.old");
    mysystem("cd /; mv -f proj proj.old");
    mysystem("cd /; mv -f share share.old");
    # This might not exist
    mysystem("cd /; mv -f groups groups.old")
	if (-d "/groups");
    mysystem("mkdir ${TBDIR}/users ${TBDIR}/proj ${TBDIR}/groups");
    mysystem("ln -s ${TBDIR}/users /users");
    mysystem("ln -s ${TBDIR}/proj /proj");
    mysystem("ln -s ${TBDIR}/groups /groups");

    if ($SCRATCHFS) {
	mysystem("cd /; mv -f scratch scratch.old")
	    if (-d "/scratch");
	mysystem("mkdir ${TBDIR}/scratch");
	mysystem("ln -s ${TBDIR}/scratch /scratch");
    }

    #
    # Setup a stub /share using slice 2 of the image.
    #
    mysystem("mkdir /share");
    mysystem("$BINDIR/mkextrafs.pl -f -s 2 /share");

    #
    # Lets mount the package dir so that we can pass off some stuff to
    # the install scripts;
    #
    if (! -d "/packages") {
	mysystem("mkdir /packages");
    }
    mysystem("mount ${fsname}:" . $emulabconfig{FS_PKG_DIR} . " /packages");

    #
    # Need these for rc.conf.
    # 
    my $bossnode_ip = $emulabconfig{"BOSSIP"};
    my $fsnode_ip  = $emulabconfig{"FSIP"};

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

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

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

    print RC "ntpdate_enable=\"YES\"\n";
    print RC "ntpdate_flags=\"boss\"\n";
    print RC "xntpd_enable=\"YES\"\n";
    print RC "linux_enable=\"YES\"\n";

    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
    print RC "smbd_enable=\"YES\"\n"
	if ($WINSUPPORT);
    print RC "mountd_flags=\"-r -p 900\"\n";

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

    if (! $SINGLE_CONTROLNET) {
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
	    "media 100baseTX mediaopt full-duplex\"\n";
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
    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"} .
	" -iface " . ($SINGLE_CONTROLNET ?
		      $outer_controlif : $inner_controlif) . "\"\n";
    # Leave default route pointing to control network until setup complete.
    if ($NOSETUP || $SINGLE_CONTROLNET) {
	print RC "defaultrouter=\"$outer_routerip\"\n";
    }
    else {
	print RC "defaultrouter=\"$bossnode_ip\"\n";
    }
    print RC "hostname=\"" . $emulabconfig{"FSNODE"} . "." . $domain . "\"\n";
    close(RC);

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

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

    goto skipsetup
	if ($NOSETUP);

    #
    # Configure an object tree. 
    #
    mysystem("mkdir -p ${TBDIR}/testbed/obj/testbed");
    mysystem("cd ${TBDIR}/testbed/obj/testbed; ".
	     "   ../../src/testbed/configure ".
	     "      --with-TBDEFS=../../src/testbed/defs-elabinelab ".
	     ($WINSUPPORT ? "--enable-windows" : "--disable-windows"));

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

    #
    # Create the fs node.
    #
    $ENV{"PKG_PATH"} = "/packages";
    my $pkg = "-P $emulabconfig{FS_PKG} -p /packages";
    mysystem("cd ${TBDIR}/testbed/obj/testbed/install; ".
	     "   perl fs-install $pkg -b");

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

    #
    # Ack! The prepare script above killed the pid file for mountd. This
    # is going to matter later when boss sets up and tries to add accounts.
    # We could reboot fs, but for now its easier if I let both nodes setup
    # before rebooting either one. So, restart mountd so it will create a
    # new pid file in /var/run. Another idea might be that we do not run
    # prepare, or scale it back for inner elab. Needs more thought.
    #
    mysystem("killall mountd");
    #
    # Its worse on FreeBSD5! We lost the rpcbind socket too. Killing
    # that means we have to restart nfsd so it can register.
    #
    if ($FBSD_VERSION >= 5) {
	#
	# The nfsd script will try to kill all nfsd processes, but once
	# the master is killed, it kills all children itself.  Thus the
	# nfsd script may fail to kill the children if they are already
	# dead and return non-zero.  So we ignore the return value of
	# the script by using system rather than mysystem.
	#
	system("/etc/rc.d/nfsd stop");
	mysystem("killall rpcbind");
	mysystem("rpcbind");
	mysystem("/etc/rc.d/nfsd start");
    }
    mysystem("mountd -r");

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

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

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

    return
	if ($NOSETUP);

    mysystem("cp -p ${TBDIR}/testbed/src/testbed/defs-elabinelab ".
	     "      ${TBDIR}/testbed/src");

    #
    # Copy the mirror tree into place. Do not use rsync.
    #
    if (0 && -e "${TBDIR}/mirror") {
	print "Copying mirror tree into place\n";
	mysystem("cp -Rfp ${TBDIR}/mirror/ /");
    }
}

#
# Setup an ops node.
#
sub SetupOpsNode($)
{
    my ($isfs) = @_;

    print "Setting up an " . ($isfs ? "Ops/Fs" : "Ops") . " node ...\n";

    if ($isfs) {
	$TBFS = "/q";
	$TBDIR = "/q";
    } else {
	$TBFS = "/usr/testbed";
	$TBDIR = "/usr";
    }

    #
    # Create a ${TBFS} from the extra slice and put everything there.
    # 
    mysystem("mkdir ${TBFS}")
	if (! -d "${TBFS}");
    mysystem("$BINDIR/mkextrafs.pl -f ${TBFS}");
    mysystem("mkdir ${TBDIR}/testbed")
	if (! -d "${TBDIR}/testbed");
    mysystem("mkdir ${TBDIR}/testbed/src");
    mysystem("mkdir ${TBDIR}/testbed/obj");

    GetEmulabSource("${TBDIR}/testbed/src");

    #
    # 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";
	mysystem("rsync -a --delete /proj/$pid/mirror ${TBDIR}", 3);
    }

    #
    # 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);
    
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    my $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

    #
    # We also need the hardwired config for the inner control network.
    # Major kludge; should get it from tmcd data.
    #
    my @ifacelist;
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;

    if (! $SINGLE_CONTROLNET) {
	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};
    }

    #
    # 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";
    mysystem("$BINDIR/prepare -N");

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

    #
    # And clear some other stuff.
    # 
    mysystem("rm -rf /usr/testbed/lib");
    mysystem("rm -rf /usr/testbed/bin");
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");

    goto skippkg
	if ($NOSETUP);
    
    #
    # Do this as a separate step because PKG_DIR might be an NFS path,
    # but we must do the NFS unmounts before running ops-install. 
    # 
    if (!$emulabconfig{OPS_PKG_DIR} || !$emulabconfig{OPS_PKG}) {
	SetupFatal("Could not get package info from Emulab!");
    }
    print "Removing conflicting packages.\n";
    mysystem("pkg_delete -r -x mysql-client")
	if (-e "/usr/local/bin/mysql");
    
    print "Installing the ops metaport.\n";

    # Make sure /usr/ports is not a symlink to RO shared space
    if (-l "/usr/ports") {
	if (!unlink("/usr/ports")) {
	    print "WARNING: /usr/ports is a symlink, port install may fail\n";
	} else {
	    mysystem("mkdir /usr/ports");
	}
    }

    $ENV{"PKG_PATH"} = $emulabconfig{OPS_PKG_DIR};
    mysystem("pkg_add $emulabconfig{OPS_PKG} >/tmp/perrs 2>&1");

    if ($isfs) {
	# XXX if not set, derive from the OPS info
	if (!$emulabconfig{FS_PKG_DIR} || !$emulabconfig{FS_PKG}) {
	    $emulabconfig{FS_PKG_DIR} = $emulabconfig{OPS_PKG_DIR};
	    ($emulabconfig{FS_PKG} = $emulabconfig{OPS_PKG}) =~ s/ops/fs/;
	}
    }
  skippkg:

    #
    # Clean up a few things on the image and create symlinks into ${TBDIR} for
    # /proj, /users, /groups and /scratch. Also allows /share to be created/
    #
    mysystem("umount -A -t nfs");
    # In case umount fails!
    mysystem("cd /; mv -f users users.old");
    mysystem("cd /; mv -f proj proj.old");
    mysystem("cd /; mv -f share share.old");
    # This might not exist
    mysystem("cd /; mv -f groups groups.old")
	if (-d "/groups");
    if ($isfs) {
	mysystem("mkdir ${TBDIR}/users ${TBDIR}/proj ${TBDIR}/groups");
	mysystem("ln -s ${TBDIR}/users /users");
	mysystem("ln -s ${TBDIR}/proj /proj");
	mysystem("ln -s ${TBDIR}/groups /groups");

	#
	# Setup a stub /share using slice 2 of the image.
	#
	mysystem("mkdir /share");
	mysystem("$BINDIR/mkextrafs.pl -f -s 2 /share");
    } else {
	mysystem("mkdir /users /proj /groups /share");
    }

    if ($SCRATCHFS) {
	mysystem("cd /; mv -f scratch scratch.old")
	    if (-d "/scratch");
	if ($isfs) {
	    mysystem("mkdir ${TBDIR}/scratch");
	    mysystem("ln -s ${TBDIR}/scratch /scratch");
	} else {
	    mysystem("mkdir /scratch");
	}
    }

    #
    # Lets mount the package dir so that we can pass off some stuff to
    # the install scripts;
    #
    if (! -d "/packages") {
	mysystem("mkdir /packages");
    }
    mysystem("mount ${fsname}:" . $emulabconfig{OPS_PKG_DIR} . " /packages");

    #
    # Need these for rc.conf.
    # 
    my $bossnode_ip = $emulabconfig{"BOSSIP"};
    my $opsnode_ip  = $emulabconfig{"OPSIP"};

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

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

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

    print RC "ntpdate_enable=\"YES\"\n";
    print RC "ntpdate_flags=\"boss\"\n";
    print RC "xntpd_enable=\"YES\"\n";
    print RC "linux_enable=\"YES\"\n";
    print RC "accounting_enable=\"YES\"\n";

    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";
    print RC "smbd_enable=\"YES\"\n"
	if ($WINSUPPORT);
    print RC "mountd_flags=\"-r -p 900\"\n";

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

    print RC "network_interfaces=\"$outer_controlif\"\n";
    print RC "ifconfig_${outer_controlif}=".
	"\"inet $outer_ip netmask $outer_netmask\"\n";
    
    if (! $SINGLE_CONTROLNET) {
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
	    "media 100baseTX mediaopt full-duplex\"\n";
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
    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"} .
	" -iface " . ($SINGLE_CONTROLNET ?
		      $outer_controlif : $inner_controlif) . "\"\n";
    # Leave default route pointing to control network until setup complete.
    if ($NOSETUP || $SINGLE_CONTROLNET) {
	print RC "defaultrouter=\"$outer_routerip\"\n";
    }
    else {
	print RC "defaultrouter=\"$bossnode_ip\"\n";
    }
    print RC "hostname=\"" . $emulabconfig{"OPSNODE"} . "." . $domain . "\"\n";
    close(RC);

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

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

    goto skipsetup
	if ($NOSETUP);

    #
    # Configure an object tree. 
    #
    mysystem("mkdir -p ${TBDIR}/testbed/obj/testbed");
    mysystem("cd ${TBDIR}/testbed/obj/testbed; ".
	     "   ../../src/testbed/configure ".
	     "      --with-TBDEFS=../../src/testbed/defs-elabinelab ".
	     ($WINSUPPORT ? "--enable-windows" : "--disable-windows"));

    #
    # Create the ops node.
    #
    $ENV{"PKG_PATH"} = "/packages";
    my $pkg = "-P $emulabconfig{OPS_PKG} -p /packages " .
	($isfs ? "-F $emulabconfig{FS_PKG}" : "");
    mysystem("cd ${TBDIR}/testbed/obj/testbed/install; ".
	     "   perl ops-install $pkg -b -w ElabInElab ");

    #
    # And install the ops side.
    #
    my $itarget = $isfs ? "opsfs-install" : "ops-install";
    mysystem("cd ${TBDIR}/testbed/obj/testbed; gmake $itarget");

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

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

    #
    # Ack! The prepare script above killed the pid file for mountd. This
    # is going to matter later when boss sets up and tries to add accounts.
    # We could reboot ops, but for now its easier if I let both nodes setup
    # before rebooting either one. So, restart mountd so it will create a
    # new pid file in /var/run. Another idea might be that we do not run
    # prepare, or scale it back for inner elab. Needs more thought.
    #
    mysystem("killall mountd");
    #
    # Its worse on FreeBSD5! We lost the rpcbind socket too. Killing
    # that means we have to restart nfsd so it can register.
    #
    if ($FBSD_VERSION >= 5) {
	#
	# The nfsd script will try to kill all nfsd processes, but once
	# the master is killed, it kills all children itself.  Thus the
	# nfsd script may fail to kill the children if they are already
	# dead and return non-zero.  So we ignore the return value of
	# the script by using system rather than mysystem.
	#
	system("/etc/rc.d/nfsd stop");
	mysystem("killall rpcbind");
	mysystem("rpcbind");
	mysystem("/etc/rc.d/nfsd start");
    }
    mysystem("mountd -r");

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

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

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

    return
	if ($NOSETUP);

    #
    # Remove source code from ops so that mere users do not get access to it.
    # Something to do with licensing ...
    #
    mysystem("cp -p ${TBDIR}/testbed/src/testbed/defs-elabinelab ".
	     "      ${TBDIR}/testbed/src");
    mysystem("rm -rf ${TBDIR}/testbed/src/testbed");
    mysystem("rm -rf ${TBDIR}/testbed/obj/testbed");

    #
    # Copy the mirror tree into place. Do not use rsync.
    #
    if (0 && -e "${TBDIR}/mirror") {
	print "Copying mirror tree into place\n";
	mysystem("cp -Rfp ${TBDIR}/mirror/ /");
    }
}

sub SetupBossNode()
{
    print "Setting up a Boss node ...\n";
    $TBDIR = "/usr";

    #
    # Create a ${TBDIR}/testbed from the extra slice and put everything there.
    # 
    mysystem("mkdir ${TBDIR}/testbed")
	if (! -d "${TBDIR}/testbed");
    mysystem("$BINDIR/mkextrafs.pl -f ${TBDIR}/testbed");
    mysystem("mkdir ${TBDIR}/testbed/src");
    mysystem("mkdir ${TBDIR}/testbed/obj");

    GetEmulabSource("${TBDIR}/testbed/src");

    print "Copying over initial dbstate from /proj\n";
    my $expdir = "/proj/$pid/exp/$eid";
    my $stuffdir = "${TBDIR}/testbed/stuff";
    mysystem("mkdir $stuffdir");
    mysystem("cp -fp $expdir/dbstate.tar.gz  $stuffdir");
    mysystem("cp -fp $expdir/outer_db_schema $stuffdir");

    if (!$NOSETUP) {
	print "Check for db schema mismatch before we go any further\n";
	my $testbed_srcdir = "${TBDIR}/testbed/src/testbed";
	my $schemadiff     = "$testbed_srcdir/utils/schemadiff";
	my $master_schema  = "$testbed_srcdir/sql/database-create.sql";
	my $outer_schema   = "$stuffdir/outer_db_schema";
	mysystem("$schemadiff -st $master_schema $outer_schema");
    }

    # Copy over creators ssl certificate for XMLRPC. See below.
    mysystem("cp -fp ~${creator}/.ssl/emulab.pem $stuffdir");

    #
    # 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).
    # 
    mysystem("echo '${outer_bossip}' > $ETCDIR/outer_bossnode");
    mysystem("cp -p $BOOTDIR/routerip $ETCDIR/outer_router");
    mysystem("cp -p $BOOTDIR/myip $ETCDIR/outer_ipaddr");

    #
    # Need outer ip and netmask for hardwired config below.
    #
    if (! -e "$BOOTDIR/myip") {
	SetupFatal("$BOOTDIR/myip does not exist!");
    }
    my $outer_ip = `cat $BOOTDIR/myip`;
    chomp($outer_ip);
    
    #
    # And we need the name of the control interface for natd below.
    #
    if (! -e "$BOOTDIR/controlif") {
	SetupFatal("$BOOTDIR/controlif does not exist!");
    }
    my $outer_controlif = `cat $BOOTDIR/controlif`;
    chomp($outer_controlif);

    #
    # We also need the hardwired config for the inner control network.
    # Major kludge; should get it from tmcd data.
    #
    my @ifacelist;
    my $inner_controlif;
    my $inner_ip;
    my $inner_netmask;

    if (! $SINGLE_CONTROLNET) {
	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};
    }

    #
    # 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";
    mysystem("$BINDIR/prepare -N");
  
    #
    # Remove the outer testbed startup script.
    #
    mysystem("rm -f /usr/local/etc/rc.d/testbed.sh");

    #
    # And clear some other stuff.
    # 
    mysystem("rm -rf /usr/testbed/lib");
    mysystem("rm -rf /usr/testbed/bin");
    unlink("/etc/rc.conf.d/dhclient")
	if (-e "/etc/rc.conf.d/dhclient");
    unlink("/etc/rc.d/netif-emulab")
	if (-e "/etc/rc.d/netif-emulab");

    #
    # Create a bigger /var/db/mysql (before installing mysql!)
    #
    mysystem("mkdir /var/db/mysql")
	if (! -d "/var/db/mysql");
    mysystem("$BINDIR/mkextrafs.pl -f -s 2 /var/db/mysql");

    goto skippkg
	if ($NOSETUP);

    #
    # Do this as a separate step cause we need the NFS mounts, but
    # must do the unmounts before running ops-install. 
    # 
    if (!$emulabconfig{BOSS_PKG_DIR} || !$emulabconfig{BOSS_PKG}) {
	SetupFatal("Could not get package info from Emulab!");
    }
    print "Removing conflicting packages.\n";
    mysystem("pkg_delete -r -x mysql-client")
	if (-e "/usr/local/bin/mysql");
    
    print "Installing the boss metaport.\n";

    # Make sure /usr/ports is not a symlink to RO shared space
    if (-l "/usr/ports") {
	if (!unlink("/usr/ports")) {
	    print "WARNING: /usr/ports is a symlink, port install may fail\n";
	} else {
	    mysystem("mkdir /usr/ports");
	}
    }

    $ENV{"PKG_PATH"} = $emulabconfig{BOSS_PKG_DIR};
    mysystem("pkg_add -f $emulabconfig{BOSS_PKG} >/tmp/perrs 2>&1");

  skippkg:
    #
    # We no longer need anything from NFS, and we need to unmount everything
    # so we can mount new NFS filesystems in their proper places. 
    # 
    mysystem("umount -A -t nfs");

    #
    # Lets mount the package dir so that we can pass off some stuff to
    # the install scripts.
    #
    if (! -d "/packages") {
	mysystem("mkdir /packages");
    }
    mysystem("mount ${fsname}:" . $emulabconfig{BOSS_PKG_DIR} . " /packages");

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

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

    print RC "ntpdate_enable=\"YES\"\n";
    # Points to outer boss
    print RC "ntpdate_flags=\"${outer_bossip}\"\n";
    print RC "linux_enable=\"YES\"\n";
    print RC "accounting_enable=\"YES\"\n";

    print RC "rpcbind_enable=\"YES\"\n";
    print RC "mountd_enable=\"YES\"\n";
    print RC "nfs_server_enable=\"YES\"\n";
    print RC "nfs_server_flags=\"-u -t -n 8\"\n";
    print RC "nfs_client_enable=\"YES\"\n";

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

    if (! $SINGLE_CONTROLNET) {
	print RC "network_interfaces=\"\$network_interfaces $inner_controlif\"\n";
	print RC "ifconfig_${inner_controlif}=".
	    "\"inet $inner_ip netmask $inner_netmask ".
	    "media 100baseTX mediaopt full-duplex\"\n";
    }
    print RC "network_interfaces=\"\$network_interfaces lo0\"\n";
    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"} .
	" -iface " . ($SINGLE_CONTROLNET ?
		      $outer_controlif : $inner_controlif) . "\"\n";

    #
    # Use natd so that internal control network can talk to outside world.
    # Maybe make an option?
    #
    print RC "firewall_enable=\"YES\"\n";
    print RC "firewall_type=\"open\"\n";
    print RC "natd_interface=\"${outer_controlif}\"\n";
    print RC "natd_enable=\"YES\"\n";
    print RC "natd_flags=\"-use_sockets -unregistered_only -same_ports ".
	"-dynamic -log_facility local6\"\n";

    # Points to outer control router.
    print RC "defaultrouter=\"$outer_routerip\"\n";
    print RC "hostname=\"" . $emulabconfig{"BOSSNODE"} . "." . $domain . "\"\n";
    # We act as the router for the inner ops and inner nodes.
    print RC "gateway_enable=\"YES\"\n";

    print RC "check_quotas=\"NO\"\n";
    close(RC);

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

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

    if ($SINGLE_CONTROLNET) {
	#
	# Hack dhcpd.conf.template to ignore the rest of emulab.
	# This precludes dynamic node addition 
	# Might think about allowing back if the experiment is firewalled.
	#
	mysystem("cd ${TBDIR}/testbed/src/testbed/dhcpd ; ".
		 " sed -E -i .orig -e " .
		 "   's;range .DHCPD_DYNRANGE.;ignore unknown-clients;' ".
		 "  dhcpd.conf.template.in");
    }

    goto skipsetup
	if ($NOSETUP);

    #
    # Configure an object tree. 
    #
    mysystem("mkdir -p ${TBDIR}/testbed/obj/testbed");
    mysystem("cd ${TBDIR}/testbed/obj/testbed; ".
	     "   ../../src/testbed/configure ".
	     "      --with-TBDEFS=../../src/testbed/defs-elabinelab ".
	     ($WINSUPPORT ? "--enable-windows" : "--disable-windows"));

    #
    # Create the boss node. This will also install the software.
    #
    $ENV{"PKG_PATH"} = "/packages";
    my $pkg = "-P $emulabconfig{BOSS_PKG} -p /packages";
    mysystem("cd ${TBDIR}/testbed/obj/testbed/install; ".
	     "   perl boss-install $pkg -b -w ElabInElab");

    #
    # The above script wiped out the outer emulab root ssh pub keys from ops.
    # We want to add them back so we can ssh into the node from outer Emulab.
    #
    mysystem("cat /root/.ssh/authorized_keys | ssh " . $emulabconfig{"OPSIP"} .
	     " '(cat >> /root/.ssh/authorized_keys)'");
    mysystem("cat /root/.ssh/authorized_keys | ssh " . $emulabconfig{"FSIP"} .
	     " '(cat >> /root/.ssh/authorized_keys)'")
	if ($emulabconfig{"OPSIP"} ne $emulabconfig{"FSIP"});

    #
    # XXX Remove conflicting package installed by boss-install.
    # This can go away when the emulab-boss port has been updated.
    #
    if ($FBSD_VERSION >= 6) {
	print "Removing conflicting bind package.\n";
	mysystem("pkg_delete -r -x bind9")
	    if (-e "/usr/local/sbin/rndc");
    }

    #
    # Unpack the tftpboot directory. It would be nice if this was part
    # of boss install too. 
    #
    print "Copying over tftpboot tar file from web server and unpacking\n";
    mysystem("wget -q -O $stuffdir/tftpboot.tar.gz ".
	     "http://${bossname}/downloads/".
	     $emulabconfig{"MFSTARBALL"});

    mysystem("tar xzf $stuffdir/tftpboot.tar.gz -C /tftpboot");

    #
    # Its the generic stuff; must localize.
    #
    my $fv = $emulabconfig{"MFSVERSION"};
    if (! -e "/tftpboot/freebsd${fv}") {
	$fv = "47";
    }
    my $pdir = ".";
    if (-d "/tftpboot/pxeboot${fv}") {
	$pdir = "pxeboot${fv}";
    }
    my $cons = $emulabconfig{"MFSCONSOLE"};
    if (! -e "/tftpboot/$pdir/pxeboot.emu-${cons}") {
	$cons = "sio";
    }

    mysystem("cd /tftpboot; mv $pdir/pxeboot.emu-${cons} pxeboot.emu");
    print "Using ${cons} version of pxeboot...\n";

    mysystem("cd /tftpboot; mv freebsd${fv} freebsd");
    print "Using freebsd${fv} version of admin MFS...\n";

    mysystem("cd /tftpboot; mv frisbee${fv} frisbee");
    print "Using frisbee${fv} version of disk load MFS...\n";

    #
    # Have recently added a 6.2 based newnode MFS
    #
    if (! -e "/tftpboot/freebsd.newnode") {
	$fv = $emulabconfig{"MFSVERSION"};
	if (! -e "/tftpboot/freebsd${fv}.newnode") {
	    $fv = "47";
	}
	mysystem("cd /tftpboot; mv freebsd${fv}.newnode freebsd.newnode");
	print "Using freebsd${fv}.newnode version of newnode MFS...\n";
    }

    # Create the compressed versions of the files
    mysystem("cd /tftpboot/frisbee/boot; ./prepare; ".
	     "cd /tftpboot/freebsd/boot; ./prepare; ".
	     "cd /tftpboot/freebsd.newnode/boot; ./prepare");

    #
    # Copy the creators ssl certificate into place. This allows the
    # inner boss to invoke the XMLRPC server on the outer boss for
    # doing things like power control, vlan setup, etc.
    #
    mysystem("cp -p $stuffdir/emulab.pem $RPCCERT");

    #
    # Copy the inner ssl cert and root's public ssh keys to the mfs's
    # so that imported they will talk to the inner boss properly.
    # At Berkeley the frisbee mfs copies these to disk images,
    # needing changes to slicefix , which always needs to be run now.
    #
    MungeMfsRoot("frisbee");
    MungeMfsRoot("freebsd");
    MungeMfsRoot("freebsd.newnode");

    #
    # Set up a bunch of DB stuff.  This part will eventually be optional,
    # resulting in a naked setup that will need to be configured the rest of
    # the way by hand. 
    #
    #
    # Unpack the initial DB contents and load it into the DB.
    #
    mysystem("mkdir /tmp/dbstate.$$");
    mysystem("tar xzf $stuffdir/dbstate.tar.gz -C /tmp/dbstate.$$");
    
    opendir(DIR, "/tmp/dbstate.$$") or
	SetupFatal("Cannot opendir /tmp/dbstate.$$: $!");
    my @tables = grep { $_ ne "." && $_ ne ".." } readdir(DIR);
    closedir(DIR);

    foreach my $table (@tables) {
	mysystem("echo \"load data infile '/tmp/dbstate.$$/$table' ".
		 "replace into table $table\" | mysql tbdb");
    }

    #
    # This script does a bunch of stuff with the above DB state, like
    # create the initial project, create subgroups, users, etc. 
    #
    mysystem("sudo -u elabman /usr/testbed/sbin/withadminprivs ".
	     "     /usr/testbed/sbin/elabinelab_bossinit $pid");

    #
    # Need to regen the dhcpd config file after loading the DB above.
    #
    mysystem("/usr/testbed/sbin/dhcpd_makeconf -i");

    #
    # Ditto for named config.
    #
    mysystem("/usr/testbed/sbin/named_setup");

  skipsetup:    
    #
    # Tack the frisbee mcast addr ipfw rule onto end of /etc/rc.local.
    #
    mysystem("echo 'ipfw add 10 allow udp from any to 224.0.0.0/4' ".
	     "   >> /etc/rc.local");

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

#
# Create a defs file by starting with the stub file, and turning it into
# a real defs file. We should probably do this on the boss side, but its
# easier to localize here for now. 
# 
sub CreateDefsFile($)
{
    my ($defsfile) = @_;
    
    print "Creating defs file from stub defs file\n";
    
    # XXX compat hack
    if (!defined($emulabconfig{"FSNODE"})) {
	$emulabconfig{"FSNODE"} = $emulabconfig{"OPSNODE"};
	$emulabconfig{"FSIP"} = $emulabconfig{"OPSIP"};
    }

    my $bossnode_ip     = $emulabconfig{"BOSSIP"};
    my $opsnode_ip      = $emulabconfig{"OPSIP"};
    my $fsnode_ip       = $emulabconfig{"FSIP"};
    my $control_netmask = "255.255.255.0";

    #
    # The control network netmask differs if using a single control network. 
    #
    if ($SINGLE_CONTROLNET) {
	$control_netmask = $outer_netmask;
    }
    
    my $control_network = inet_ntoa(inet_aton($opsnode_ip) &
				    inet_aton($control_netmask));
    # Put dynrange at the top.
    my $dynrange_low    = inet_ntoa(inet_aton($control_network) |
				    inet_aton("0.0.0.230"));
    # Note that boss/ops are hardwired to .252 and .253
    my $dynrange_high   = inet_ntoa(inet_aton($control_network) |
				    inet_aton("0.0.0.250"));
    my ($a,$b,$c,$d)    = ($bossnode_ip =~ /(\d+).(\d+).(\d+).(\d+)/);
    my $frismcastaddr   = "235.${d}.${c}";

    open(INDEFS, $defsfile) or
	SetupFatal("Could not open stub defs-elabinelab: $!");
    open(OUTDEFS, "> /tmp/defs-elabinelab") or
	SetupFatal("Could not open new defs-elabinelab: $!");

    while (<INDEFS>) {
	my $key;
	my $val;
	my $line = $_;		# In case the switch doesn't match.
	
	if ($_ =~ /^([-\w]*)="(.+)"$/ ||
	    $_ =~ /^([-\w]*)=(.+)$/) {
	    $key = $1;
	    $val = $2;

	    #
	    # Look for things that include "changeme". Emails are special.
	    #
	    if ($val =~ /^(.*)\@(changeme)$/) {
		my $opsnode = $emulabconfig{"OPSNODE"}; 
		print OUTDEFS "${key}=${1}\@${opsnode}.${domain}\n";
		next;
	    }
	    if (! ($val =~ /changeme/)) {
		print OUTDEFS $_;
		next;
	    }
	
	    SWITCH: for ($key) {
		/^BOSSNODE$/ && do {
		    my $bossnode = $emulabconfig{"BOSSNODE"}; 
		    print OUTDEFS "BOSSNODE=${bossnode}.${domain}\n";
		    last SWITCH;
		};
		/^OUTERBOSS_NODENAME$/ && do {
		    print OUTDEFS "OUTERBOSS_NODENAME=${bossname}\n";
		    print OUTDEFS "OUTERBOSS_SSLCERTNAME=$RPCCERT\n";
		    # Debugging
		    if (defined($RPCPORT)) {
			print OUTDEFS "OUTERBOSS_XMLRPCPORT=$RPCPORT\n";
		    }
		    last SWITCH;
		};
		/^USERNODE$/ && do {
		    my $opsnode = $emulabconfig{"OPSNODE"}; 
		    print OUTDEFS "USERNODE=${opsnode}.${domain}\n";
		    last SWITCH;
		};
		/^FSNODE$/ && do {
		    my $fsnode = $emulabconfig{"FSNODE"}; 
		    print OUTDEFS "FSNODE=${fsnode}.${domain}\n";
		    last SWITCH;
		};
		/^OURDOMAIN$/ && do {
		    print OUTDEFS "OURDOMAIN=${domain}\n";
		    last SWITCH;
		};
		/^WWWHOST$/ && do {
		    my $bossnode = $emulabconfig{"BOSSNODE"}; 
		    print OUTDEFS "WWWHOST=${bossnode}.${domain}\n";
		    last SWITCH;
		};
		/^THISHOMEBASE$/ && do {
		    print OUTDEFS "THISHOMEBASE=MyEmulab.Net\n";
		    last SWITCH;
		};
		/^TESTBED_NETWORK$/ && do {
		    print OUTDEFS "TESTBED_NETWORK=$control_network\n";
		    last SWITCH;
		};
		/^TESTBED_NETMASK$/ && do {
		    print OUTDEFS "TESTBED_NETMASK=$control_netmask\n";
		    last SWITCH;
		};
		/^BOSSNODE_IP$/ && do {
		    print OUTDEFS "BOSSNODE_IP=$bossnode_ip\n";
		    last SWITCH;
		};
		/^USERNODE_IP$/ && do {
		    print OUTDEFS "USERNODE_IP=$opsnode_ip\n";
		    last SWITCH;
		};
		/^FSNODE_IP$/ && do {
		    print OUTDEFS "FSNODE_IP=$fsnode_ip\n";
		    last SWITCH;
		};
		/^CONTROL_ROUTER_IP$/ && do {
		    my $control_router_ip;
		    
		    if ($SINGLE_CONTROLNET) {
			$control_router_ip = $outer_routerip;
		    }
		    else {
			$control_router_ip = $bossnode_ip;
		    }
		    print OUTDEFS "CONTROL_ROUTER_IP=$control_router_ip\n";
		    
		    last SWITCH;
		};
		/^CONTROL_NETWORK$/ && do {
		    print OUTDEFS "CONTROL_NETWORK=$control_network\n";
		    last SWITCH;
		};
		/^CONTROL_NETMASK$/ && do {
		    print OUTDEFS "CONTROL_NETMASK=$control_netmask\n";
		    last SWITCH;
		};
		/^PRIVATE_NETWORK$/ && do {
		    print OUTDEFS "PRIVATE_NETWORK=$control_network\n";
		    last SWITCH;
		};
		/^PRIVATE_ROUTER$/ && do {
		    my $private_router;
		    
		    if ($SINGLE_CONTROLNET) {
			$private_router = $outer_routerip;
		    }
		    else {
			$private_router = $bossnode_ip;
		    }
		    
		    print OUTDEFS "PRIVATE_ROUTER=$private_router\n";
		    last SWITCH;
		};
		/^PRIVATE_NETMASK$/ && do {
		    print OUTDEFS "PRIVATE_NETMASK=255.255.255.0\n";
		    last SWITCH;
		};
		/^PUBLIC_NETWORK$/ && do {
		    print OUTDEFS "PUBLIC_NETWORK=$control_network\n";
		    last SWITCH;
		};
		/^PUBLIC_ROUTER$/ && do {
		    my $public_router;
		    
		    if ($SINGLE_CONTROLNET) {
			$public_router = $outer_routerip;
		    }
		    else {
			$public_router = $bossnode_ip;
		    }
		    print OUTDEFS "PUBLIC_ROUTER=$public_router\n";
		    last SWITCH;
		};
		/^PUBLIC_NETMASK$/ && do {
		    print OUTDEFS "PUBLIC_NETMASK=$control_netmask\n";
		    last SWITCH;
		};
		/^NAMED_FORWARDERS$/ && do {
		    print OUTDEFS "NAMED_FORWARDERS=\"${outer_bossip}\"\n";
		    last SWITCH;
		};
		/^DHCPD_DYNRANGE$/ && do {
		    print OUTDEFS "DHCPD_DYNRANGE=".
			"\"$dynrange_low $dynrange_high\"\n";
		    last SWITCH;
		};
		/^FRISEBEEMCASTADDR$/ && do {
		    print OUTDEFS "FRISEBEEMCASTADDR=\"$frismcastaddr\"\n";
		    print OUTDEFS "FRISEBEEMCASTPORT=\"6000\"\n";
		    last SWITCH;
		};
		/^MAILMANSUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6) {
			print OUTDEFS "MAILMANSUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "MAILMANSUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^CVSSUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6) {
			print OUTDEFS "CVSSUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "CVSSUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^BUGDBSUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6) {
			print OUTDEFS "BUGDBSUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "BUGDBSUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^OPSDBSUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6) {
			print OUTDEFS "OPSDBSUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "OPSDBSUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^WIKISUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6) {
			print OUTDEFS "WIKISUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "WIKISUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^ARCHIVESUPPORT$/ && do {
		    if (!$NOCOLLAB && $FBSD_VERSION >= 6.1) {
			print OUTDEFS "ARCHIVESUPPORT=1\n";
		    }
		    else {
			print OUTDEFS "ARCHIVESUPPORT=0\n";
		    }
		    last SWITCH;
		};
		/^TBCOOKIESUFFIX$/ && do {
		    print OUTDEFS "TBCOOKIESUFFIX=\"$eid\"\n";
		    last SWITCH;
		};
		/^FSDIR_SCRATCH$/ && do {
		    if ($SCRATCHFS) {
			print OUTDEFS "FSDIR_SCRATCH=/q/scratch\n";
		    } else {
			print OUTDEFS "FSDIR_SCRATCH=\n";
		    }
		    last SWITCH;
		};
		/^ELVIN_COMPAT$/ && do {
		    print OUTDEFS "ELVIN_COMPAT=$ELVIN_COMPAT\n";
		    last SWITCH;
		};
		
		print OUTDEFS $line;
	    }
	}
	else {
		print OUTDEFS $_;
	}
    }
    close(INDEFS);
    close(OUTDEFS);
    mysystem("cat /tmp/defs-elabinelab");
    mysystem("mv -f /tmp/defs-elabinelab $defsfile");
}

sub MungeMfsRoot($)
{
    my $tftpdir = shift;
    my ($mount, $release);
    
    if ($FBSD_VERSION >= 5) {
	$mount = "mdconfig -a -t vnode -f mfsroot -u 2; mount /dev/md2 /mnt;";
	$release = "mdconfig -d -u 2;";
    }
    else {
	$mount = "vnconfig -c vn1 mfsroot; mount /dev/vn1 /mnt;";
	$release = "vnconfig -u vn1;";
    }
    mysystem("cd /tftpboot/$tftpdir/boot; " . $mount . "cd /usr/testbed/etc;" .
	     "cat /root/.ssh/*.pub >> /mnt/root/.ssh/authorized_keys2; " .
	     "cp -p emulab.pem client.pem /mnt/etc/emulab ; umount /mnt ; " .
	     $release . " cd /tftpboot/$tftpdir/boot; " .
	     "gzip < mfsroot > mfsroot.gz ");
}

#
# Print error and exit.
#
sub SetupFatal($)
{
    my ($msg) = @_;

    die("*** $0:\n".
	"    $msg\n");
}

#
# Send email. This should come from a library. 
#
sub SetupSendMail($$)
{
    my ($isfatal, $msg) = @_;

1834
    if (! open(MAIL, "|/usr/sbin/sendmail -i -t")) {
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
	die("*** $0:\n".
	    "    SENDMAIL: Could not start sendmail: $!\n".
	    "    $msg\n");
    }
    print MAIL "From: ${creator}\@${hostname}\n";
    print MAIL "To: ${creator}\@${outer_domain}\n";
    if ($isfatal) {
	print MAIL "Subject: ElabInElab setup failure on $hostname\n";
    }
    else {
	print MAIL "Subject: ElabInElab setup completed on $hostname\n";
    }
    print MAIL "\n";
    print MAIL "$msg\n";
    print MAIL "\n";

    if (open(IN, "$LOGFILE")) {
	print MAIL "\n--------- $LOGFILE --------\n";

	while (<IN>) {
	    print MAIL "$_";
	}
	close(IN);
    }
    
    print MAIL "\n";
    if (! close(MAIL)) {
	print "SENDMAIL: Could not finish sendmail: $!\n";
    }
}

#
# Run a command string.
#
sub mysystem($;$)
{
    my ($command, $retrycount) = @_;

    $retrycount = 1
	if (!defined($retrycount));

    while ($retrycount--) {
	print "Command: '$command\'\n";
	print "Started at:  " . libsetup::TBTimeStamp() . "\n";
	
	system($command);
	last
	    if ($? == 0 || $retrycount == 0);
	
	sleep(1);
    }
    if ($?) {
	SetupFatal("Command failed: $? - $command");
    }
    print "Finished at: " . libsetup::TBTimeStamp() . "\n";
}

#
# Deal with the source code!
#
sub GetEmulabSource($)
{
    my ($destdir) = @_;

    mysystem("mkdir $destdir/testbed");
    
    #
    # Look to see if the source code is already here (say, cause the user
    # specified a tarfile). If so, copy it into place.
    #
    if (-e "/usr/src/defs-elabinelab") {
	print "Copying over current testbed software from /usr/src\n";
	mysystem("rsync -a --delete /usr/src/ $destdir/testbed");
    }
    else {
	#
	# Get the tarball from the server.
	#
	my $file   = TMNODEID();
	my $nodeid = `cat $file`;
	chomp($nodeid);

	my $keyfile = TMKEYHASH();
	my $keyhash = `cat $keyfile`;
	chomp($keyhash);

	my $cvstag = (! defined($emulabconfig{"CVSSRCTAG"}) ? "" :
		      "&cvstag=" . $emulabconfig{"CVSSRCTAG"});

	mysystem("fetch -q -o /tmp/foo.tar.gz ".
		 "'https://${bossname}/spewrpmtar.php3?nodeid=${nodeid}&".
		 "key=${keyhash}&elabinelab_source=1${cvstag}'");

	mysystem("tar xzf /tmp/foo.tar.gz -C $destdir/testbed");
    }
}