liblocsetup.pm 8.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/perl -wT

#
# FreeBSD specific routines and constants for the client bootime setup stuff.
#
package liblocsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
    qw ( $CP $EGREP $MOUNT $UMOUNT $TMPASSWD
	 os_cleanup_node os_ifconfig_line os_etchosts_line
	 os_setup os_groupadd os_useradd os_userdel os_usermod
13
	 os_rpminstall_line update_delays
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
41
42
43
       );

# Must come after package declaration!
use English;

#
# This is the FreeBSD dependent part of the setup library. 
# 
my $SETUPDIR = "/etc/testbed";
libsetup::libsetup_init($SETUPDIR);

#
# Various programs and things specific to FreeBSD and that we want to export.
# 
$CP		= "/bin/cp";
$EGREP		= "/usr/bin/egrep -s -q";
$MOUNT		= "/sbin/mount";
$UMOUNT		= "/sbin/umount";
$TMGROUP	= "$SETUPDIR/group";
$TMPASSWD	= "$SETUPDIR/master.passwd";

#
# These are not exported
#
my $USERADD     = "/usr/sbin/pw useradd";
my $USERDEL     = "/usr/sbin/pw userdel";
my $USERMOD     = "/usr/sbin/pw usermod";
my $GROUPADD	= "/usr/sbin/pw groupadd";
my $CHPASS	= "/usr/bin/chpass -p";
my $MKDB	= "/usr/sbin/pwd_mkdb -p";
44
45
46
47
my $IFCONFIG    = "/sbin/ifconfig %s inet %s netmask %s %s %s";
my $IFC_100MBS  = "media 100baseTX";
my $IFC_10MBS   = "media 10baseT/UTP";
my $IFC_FDUPLEX = "mediaopt full-duplex";
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
my $RPMINSTALL  = "/usr/local/bin/rpm -i %s";

#
# Delay node configuration goop.
#
my $KERNEL100   = "/kernel.100HZ";
my $KERNEL1000  = "/kernel.1000HZ";
my $KERNEL10000 = "/kernel.10000HZ";
my @KERNELS     = ($KERNEL100, $KERNEL1000, $KERNEL10000); 
my $kernel      = $KERNEL100;
my $IFACE	= "fxp";
my $CTLIFACENUM = "4";
my $CTLIFACE    = "${IFACE}${CTLIFACENUM}";
my $TMDELAY	= "$SETUPDIR/rc.delay";
my $TMCCCMD_DELAY = "delay";

#
# OS dependent part of cleanup node state.
# 
sub os_cleanup_node () {
    unlink $TMDELAY;

    printf STDOUT "Resetting passwd and group files\n";
    if (system("$CP -f $TMGROUP /etc/group") != 0) {
	print STDERR "Could not copy default group file into place: $!\n";
	exit(1);
    }
    
    if (system("$CP -f $TMPASSWD /etc/master.passwd_testbed") != 0) {
	print STDERR "Could not copy default passwd file into place: $!\n";
	exit(1);
    }
    
    if (system("$MKDB /etc/master.passwd_testbed") != 0) {
	print STDERR "Failure running $MKDB on default password file: $!\n";
	exit(1);
    }

    return 0;
}

#
# Generate and return an ifconfig line that is approriate for putting
# into a shell script (invoked at bootup).
#
93
sub os_ifconfig_line($$$$$)
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
    my ($iface, $inet, $mask, $speed, $duplex) = @_;
    my $media    = "";
    my $mediaopt = "";

    #
    # Need to check units on the speed. Just in case.
    #
    if ($speed =~ /(\d*)([A-Za-z]*)/) {
	if ($2 eq "Mbps") {
	    $speed = $1;
	}
	elsif ($2 eq "Kbps") {
	    $speed = $1 / 1000;
	}
	else {
	    warn("*** Bad speed units in ifconfig!\n");
	    $speed = 100;
	}
	if ($speed == 100) {
	    $media = $IFC_100MBS;
	}
	elsif ($speed == 10) {
	    $media = $IFC_10MBS;
	}
	else {
	    warn("*** Bad Speed in ifconfig!\n");
	    $media = $IFC_100MBS;
	}
    }

    if ($duplex eq "full") {
	$mediaopt = $IFC_FDUPLEX;
    }
   
    return sprintf($IFCONFIG, $iface, $inet, $mask, $media, $mediaopt);
130
131
132
133
134
135
}

#
# Generate and return an string that is approriate for putting
# into /etc/hosts.
#
136
sub os_etchosts_line($$$)
137
{
138
    my ($name, $ip, $aliases) = @_;
139
    
140
    return sprintf("%s\t%s %s", $ip, $name, $aliases);
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
213
214
215
216
217
218
219
220
221
222
223
224
225
}

#
# Add a new group
# 
sub os_groupadd($$)
{
    my($group, $gid) = @_;

    return system("$GROUPADD $group -g $gid");
}

#
# Remove a user account.
# 
sub os_userdel($)
{
    my($login) = @_;

    return system("$USERDEL $login");
}

#
# Modify user group membership.
# 
sub os_usermod($$$$)
{
    my($login, $gid, $glist, $root) = @_;

    if ($root) {
	$glist = join(',', split(/,/, $glist), "wheel");
    }
    if ($glist ne "") {
	$glist = "-G $glist";
    }

    return system("$USERMOD $login -g $gid $glist");
}

#
# Add a user.
# 
sub os_useradd($$$$$$$$)
{
    my($login, $uid, $gid, $pswd, $glist, $homedir, $gcos, $root) = @_;

    if ($root) {
	$glist = join(',', split(/,/, $glist), "wheel");
    }
    if ($glist ne "") {
	$glist = "-G $glist";
    }

    if (system("$USERADD $login -u $uid -g $gid $glist ".
	       "-d $homedir -s /bin/tcsh -c \"$gcos\"") != 0) {
	warn "*** WARNING: $USERADD $login error.\n";
	return -1;
    }

    if (system("$CHPASS $pswd $login") != 0) {
	warn "*** WARNING: $CHPASS $login error.\n";
	return -1;
    }
    return 0;
}

#
# Generate and return an RPM install line that is approriate for putting
# into a shell script (invoked at bootup).
#
sub os_rpminstall_line($)
{
    my ($rpm) = @_;
    
    return sprintf($RPMINSTALL, $rpm);
}

#
# OS Dependent configuration. 
# 
sub os_setup()
{
    print STDOUT "Checking Testbed delay configuration ... \n";
    dodelays();
}
226
227
228
229
230
231
232
233
234

#
# Special update.
#
sub update_delays()
{
    dodelays();
    system($TMDELAY);
}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    
sub dodelays ()
{
    my @delays;
    my $checkreplace;

    my $TM = libsetup::OPENTMCC($TMCCCMD_DELAY);
    while (<$TM>) {
	push(@delays, $_);
    }
    close($TM);

    if (@delays) {
	$count    = 69;
	$mindelay = 10000;
    
	open(DEL, ">$TMDELAY")
	    or die("Could not open $TMDELAY");
	print DEL "#!/bin/sh\n";
	print DEL "sysctl -w net.link.ether.bridge=0\n";
	print DEL "sysctl -w net.link.ether.bridge_ipfw=0\n";
256
	print DEL "sysctl -w net.link.ether.bridge_cfg=";
257
258

	foreach $delay (@delays) {
259
	    $delay =~ /DELAY INT0=([\d\w]+) INT1=([\d\w]+) /;
260
261
	    my $iface1 = libsetup::findiface($1);
	    my $iface2 = libsetup::findiface($2);
262

263
	    print DEL "$iface1:$count,$iface2:$count,";
264
265
266
267
268
269
270
271
272
	    $count++;
	}
	print DEL "\n";
	print DEL "sysctl -w net.link.ether.bridge=1\n";
	print DEL "sysctl -w net.link.ether.bridge_ipfw=1\n";
	print DEL "ipfw -f flush\n";

	$count = 69;
	foreach $delay (@delays) {
273
	    $pat  = q(DELAY INT0=([\d\w]+) INT1=([\d\w]+) );
274
275
	    $pat .= q(PIPE0=(\d+) DELAY0=([\d\.]+) BW0=(\d+) PLR0=([\d\.]+) );
	    $pat .= q(PIPE1=(\d+) DELAY1=([\d\.]+) BW1=(\d+) PLR1=([\d\.]+));
276
277
	    
	    $delay =~ /$pat/;
278
279

	    #
280
	    # tmcd returns the interfaces as MAC addrs.
281
	    # 
282
283
	    my $iface1 = libsetup::findiface($1);
	    my $iface2 = libsetup::findiface($2);
284
285
286
287
288
289
290
291
	    $p1        = $3;
	    $delay1    = $4;
	    $bandw1    = $5;
	    $plr1      = $6;
	    $p2        = $7;
	    $delay2    = $8;
	    $bandw2    = $9;
	    $plr2      = $10;
292

293
294
295
296
297
298
299
300
301
	    #
	    # Delays are floating point numbers (unit is ms). ipfw does not
	    # support floats, so apply a cheesy rounding function to convert
            # to an integer (since perl does not have a builtin way to
	    # properly round a floating point number to an integer).
	    #
	    $delay1 = int($delay1 + 0.5);
	    $delay2 = int($delay2 + 0.5);

302
303
304
305
306
307
	    print DEL "ifconfig $iface1 media 100baseTX mediaopt full-duplex";
	    print DEL "\n";
	    print DEL "ifconfig $iface2 media 100baseTX mediaopt full-duplex";
	    print DEL "\n";
	    print DEL "ipfw add pipe $p1 ip from any to any in recv $iface1\n";
	    print DEL "ipfw add pipe $p2 ip from any to any in recv $iface2\n";
308
	    print DEL "ipfw pipe $p1 config delay ${delay1}ms ";
309
	    print DEL "bw ${bandw1}Kbit/s plr $plr1\n";
310
	    print DEL "ipfw pipe $p2 config delay ${delay2}ms ";
311
	    print DEL "bw ${bandw2}Kbit/s plr $plr2\n";
312
313

	    print STDOUT "  $iface1/$iface2 pipe $p1 config delay ";
314
	    print STDOUT "${delay1}ms bw ${bandw1}Kbit/s plr $plr1\n";
315
	    print STDOUT "  $iface1/$iface2 pipe $p2 config delay ";
316
	    print STDOUT "${delay2}ms bw ${bandw2}Kbit/s plr $plr2\n";
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
    
	    $count++;
	}
	#
	# If a delay node, then lets report status and ready in so that batch
	# experiments do not become stuck.
	#
	printf DEL "%s %s\n", libsetup::TMCC(), libsetup::TMCCCMD_READY();
	printf DEL "%s %s 0\n", libsetup::TMCC(),libsetup::TMCCCMD_STARTSTAT();

	print DEL "echo \"Delay Configuration Complete\"\n";
	close(DEL);
	chmod(0755, "$TMDELAY");
    
	#
	# Now do kernel configuration. All of the above work is wasted, but
	# we needed to know the minimum delay. Eventually we will boot the
	# correct kernel to start with via PXE. 
	#
	$kernel = $KERNEL1000;
	$checkreplace = 1;
    }
    else {
	#
	# Make sure we are running the correct non delay kernel. This
	# is really only necessary in the absence of disk reloading.
	# Further, we don't want to replace a user supplied kernel.
	#
	$kernel = $KERNEL100;
	$checkreplace = 0;
	foreach $kern (@KERNELS) {
	    if (system("cmp -s /kernel $kern") == 0) {
		$checkreplace = 1;
	    }
	}
    }

    print STDOUT "Checking kernel configuration ... \n";
    if ($checkreplace) {
	#
	# Make sure we are running the correct kernel. 
	#
	if (-e $kernel) {
	    if (system("cmp -s /kernel $kernel") != 0) {
		if (system("cp -f /kernel /kernel.save")) {
		    print STDOUT
			"Could not backup /kernel! Aborting kernel change\n";
		}
		else {
		    if (system("cp -f $kernel /kernel")) {
			print STDOUT "Could not cp $kernel to /kernel! ".
			    "Aborting kernel change\n";
		    }
		    else {
			system("sync");
			system("reboot");
		    }
		}
	    }
	}
	else {
	    print STDOUT "Kernel $kernel does not exist!\n";
	}
    }

    return 0;
}

1;