rc.blobs 3.78 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2004-2010 University of Utah and the Flux Group.
# All rights reserved.
#
use English;
use Getopt::Std;

sub usage()
{
    print "Usage: " .
	scriptname() . " [-j vnodeid] boot|shutdown|reconfig|reset\n";
    exit(1);
}
my $optlist = "j:";
my $action  = "boot";

# 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
my $FRISBEE     = "/usr/local/etc/emulab/frisbee -N ";
my $TARINSTALL  = "/usr/local/bin/install-tarfile";
my $TARDBFILE   = "/var/db/testbed.tarfiles";

#
# Load the OS independent support library. It will load the OS dependent
# library and initialize itself. 
# 
use libsetup;
41
use liblocsetup;
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
use libtmcc;
use librc;

#
# Not all clients support this.
#
exit(0)
    if (MFS() || (REMOTE() && !(REMOTEDED() || JAILED() || PLAB())));

# Protos.
sub doboot();
sub doshutdown();
sub doreconfig();
sub docleanup();

# Parse command line.
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'j'})) {
    my $vnodeid = $options{'j'};
    libsetup_setvnodeid($vnodeid);
}
# Allow default above.
if (@ARGV) {
    $action = $ARGV[0];
}

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

#
# Boot Action.
#
sub doboot()
{
    my @blobs;
    my $errors = 0;
99
    my $need_mkextrafs = 1;
100

101
102
103
    if (-e "$BOOTDIR/rc.blobs-ran") {
	print STDOUT "  Blobs installation already done.\n";
	print STDOUT "  (Remove $BOOTDIR/rc.blobs-ran to run again.)\n";
104
105
106
107
108
109
110
111
112
113
114
	exit 0;
    }

    print STDOUT "Checking Testbed Blobs configuration ... \n";

    if (tmcc(TMCCCMD_BLOBS, undef, \@blobs, nocache => 1) < 0) {
	fatal("Could not get blobs from server!");
    }
    return
	if (! @blobs);

115
    unlink("$LOGDIR/blobs.log");
116
117
118
    foreach my $blob (@blobs) {
	eval {
	    ($mcastaddr, $portnum, undef, $basename, $dest) = 
119
		$blob =~ m|^URL=frisbee.mcast://(.+?):(.+?)/(.+/)?(.+) ACTION=unpack:(\S+)|
120
121
122
123
		or die "Bad Blobs line\n";

	    # FIXME: Make sure $basename doesn't exist...
	    my $local_path = "/var/tmp/$basename";
124
125
	    my $frisbee_cmd = "$FRISBEE -p $portnum -m $mcastaddr $local_path".
		              " >>$LOGDIR/blobs.log 2>&1";
126

127
128
129
130
131
	    if ($dest =~ /^\/local/ && $need_mkextrafs) {
		os_mountextrafs("/local");
		$need_mkextrafs = 0;
	    }

132
	    print STDOUT "  Downloading blob $basename to $local_path\n";
133
134
135
136
137
138
139

	    #print STDOUT "$frisbee_cmd\n";
	    # launch frisbee
	    if (system($frisbee_cmd)) {
		die "Frisbee failed\n";
	    }

140
141
142
	    print STDOUT "  Unpacking to $dest\n";
	    my $unpack_cmd = "$TARINSTALL -f $dest $local_path".
		             " >>$LOGDIR/blobs.log 2>&1";
143
144
145
	    #print STDOUT "$unpack_cmd\n";
	    if (system($unpack_cmd)) {
		if ($? >> 8 == 255) {
146
		    die "Tarinstall failed (see $LOGDIR/blobs.log)\n";
147
148
		}
	    }
149
150

	    system("rm $local_path");
151
152
153
154
155
156
157
158
159
160
	};
	if ($@) {
	    my $err = $@;
	    chomp $err;
	    warning("$err on line: $blob");
	    $errors++;
	}
    }
    
    if (!$errors) {
161
	system("touch $BOOTDIR/rc.blobs-ran");
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
    }

    exit($errors);
}

#
# Shutdown Action.
#
sub doshutdown()
{
    # Nothing to do.
    return;
}

#
# Node Reconfig Action (without rebooting).
#
sub doreconfig()
{
    # Same as booting
    return doboot();
}

#
# Node cleanup action (node is reset to clean state, as if just allocated).
#
sub docleanup()
{
    # Remove DB file. No attempt is made to uninstall though.
    if (-e "$TARDBFILE") {
	unlink "$TARDBFILE";
    }
}