tbbuildkernels 4.06 KB
Newer Older
1
#!/usr/bin/perl -w
2 3
#
# Copyright (c) 2005 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
#
# tbbuildkernels - a little script for building and installing the
#

use strict;

my $SRCDIR = "/share/freebsd/4.7/src";
my $OBJDIR = "/usr/obj";
my $LOGDIR = "/tmp";

my $MAINKERNEL = "TESTBED";
my @MAINKERNEL_ALIASES = ("kernel.100HZ");
my %OTHER_KERNELS = ("TESTBED-DELAY"     => ["kernel.delay", "kernel.10000HZ"],
                     "TESTBED-JAIL"      => ["kernel.jail"],
		     "TESTBED-LINKDELAY" => ["kernel.linkdelay",
			 "kernel.1000HZ"]); 

42 43 44
sub build_kernel($;$);
sub install_kernel($$$);

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
$| = 1;

#
# Some safeguards
#
if (grep {$_ eq `hostname`} ("boss","ops","cash","credit")) {
    die "Run this on a node, not this machine!\n";
}

if ($> != 0) {
    die "Sorry, gotta run this as root\n";
}

print "WARNING: You should only run this on a node, NOT on boss,\n";
print "ops, etc. It will replace kernels, etc. Hit Y to continue..\n";
die "Cancelled\n" unless (<> =~ /y/i);

chdir($SRCDIR) or die "Unable to change to $SRCDIR: $!\n";

#
# Build the main kernel (which we'll need modules for, etc.)
#
build_kernel($MAINKERNEL,1);
install_kernel($MAINKERNEL,1,["kernel", @MAINKERNEL_ALIASES]);

70 71 72 73 74 75
#
# Clean up kernel.old and modules.old
#
system "rm -f /kernel.old" and die "Unable to remove /kernel.old\n";
system "rm -rf /modules.old" and die "Unable to remove /modules.old\n";

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
#
# Now, build the rest!
#
while (my ($name,$install_locations) = each %OTHER_KERNELS) {
    build_kernel($name,0);
    install_kernel($name,0,$install_locations);
}

#
# Finally, clean up after ourselves
#
print "##### Cleaning up\n";
system "rm -rf $OBJDIR/$SRCDIR" or die "Unable to clean up $OBJDIR/$SRCDIR\n";

print "##### Done!\n";
exit 0;

sub build_kernel($;$) {
    my ($name,$with_modules) = @_;

    my $disable_modules;
    if ($with_modules) {
	$disable_modules = "";
    } else {
	$disable_modules = "-DNO_MODULES";
    }

    print "#### Building kernel $name (log in $LOGDIR/$name.build)\n";

    my $rv = system "make $disable_modules buildkernel KERNCONF=$name" .
    	"> $LOGDIR/$name.build 2>&1";
    if ($rv) {
	die "Build died with error $rv\n";
    }
}

sub install_kernel($$$) {
    my ($name, $full_install, $install_locations) = @_;

    my ($realname,@aliases) = @$install_locations;

    #
    # XXX - full_install only works if first location is /kernel
    #
    if ($full_install) {
	print "#### Installing kernel $name (log in $LOGDIR/$name.install)\n";
	my $rv = system "make installkernel KERNCONF=$name " .
	    "> $LOGDIR/$name.install 2>&1";
	if ($rv) {
	    die "Install died with error $rv\n";
	}
	#
	# Have to remove the schg  flag, or we can't hard link to it
	#
	system "chflags noschg /$realname" and die "Unable to chflags\n";

    } else {
	print "#### Installing kernel $name as /$realname\n";
	my $kernfile = "$OBJDIR/$SRCDIR/sys/$name/kernel";
	system "cp $kernfile /$realname" and die "Unable to copy kernel\n";
	system "chown root /$realname"
	    and die "Unable to change ownership to root\n";
    }

    foreach my $alias (@aliases) {
	print "#### Linking /$realname to /$alias\n";
	if (-e "/$alias") {
	    system "rm -f /$alias" and die "Unable to unlink\n";
	}
	system "ln /$realname /$alias" and die "Unable to link\n";
	system "chown root /$alias"
	    and die "Unable to change ownership to root\n";
	system "chmod 755 /$alias" and die "Unable to chmod\n";
    }
}