liblocsetup.pm 6.64 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
4
5
6
7
#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2002 University of Utah and the Flux Group.
# All rights reserved.
#

8
9
10
11
12
13
14
#
# Linux specific routines and constants for the client bootime setup stuff.
#
package liblocsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
15
    qw ( $CP $EGREP $MOUNT $UMOUNT $TMPASSWD $SFSSD $SFSCD
16
	 os_cleanup_node os_ifconfig_line os_etchosts_line
17
	 os_setup os_groupadd os_useradd os_userdel os_usermod os_mkdir
18
	 os_rpminstall_line
19
20
	 os_routing_enable_forward os_routing_enable_gated
	 os_routing_add_manual os_routing_del_manual os_homedirdel
21
	 os_groupdel
22
23
24
25
26
       );

# Must come after package declaration!
use English;

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
{
    if (-e "/etc/emulab/paths.pm") {
	require "/etc/emulab/paths.pm";
	import emulabpaths;
    }
    else {
	my $ETCDIR  = "/etc/rc.d/testbed";
	my $BINDIR  = "/etc/rc.d/testbed";
	my $VARDIR  = "/etc/rc.d/testbed";
	my $BOOTDIR = "/etc/rc.d/testbed";
    }
}
42
43
44
45
46
47
48
49

#
# Various programs and things specific to Linux and that we want to export.
# 
$CP		= "/bin/cp";
$EGREP		= "/bin/egrep -q";
$MOUNT		= "/bin/mount";
$UMOUNT		= "/bin/umount";
50
$TMPASSWD	= "$ETCDIR/passwd";
51
52
$SFSSD		= "/usr/local/sbin/sfssd";
$SFSCD		= "/usr/local/sbin/sfscd";
53
54
55
56

#
# These are not exported
#
57
58
59
my $TMGROUP	= "$ETCDIR/group";
my $TMSHADOW    = "$ETCDIR/shadow";
my $TMGSHADOW   = "$ETCDIR/gshadow";
60
61
62
63
my $USERADD     = "/usr/sbin/useradd";
my $USERDEL     = "/usr/sbin/userdel";
my $USERMOD     = "/usr/sbin/usermod";
my $GROUPADD	= "/usr/sbin/groupadd";
64
my $GROUPDEL	= "/usr/sbin/groupdel";
65
my $IFCONFIG    = "/sbin/ifconfig %s inet %s netmask %s";
66
67
68
69
my $IFC_100MBS  = "100baseTx";
my $IFC_10MBS   = "10baseT";
my $IFC_FDUPLEX = "FD";
my $IFC_HDUPLEX = "HD";
70
71
my $RPMINSTALL  = "/bin/rpm -i %s";
my @LOCKFILES   = ("/etc/group.lock", "/etc/gshadow.lock");
72
my $MKDIR	= "/bin/mkdir";
73
74
my $GATED	= "/usr/sbin/gated";
my $ROUTE	= "/sbin/route";
75
76
77
78

#
# OS dependent part of cleanup node state.
# 
79
80
81
sub os_cleanup_node ($) {
    my ($scrub) = @_;

82
83
    unlink @LOCKFILES;

84
85
86
87
    if (! $scrub) {
	return 0;
    }
    
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    printf STDOUT "Resetting passwd and group files\n";
    if (system("$CP -f $TMGROUP $TMPASSWD /etc") != 0) {
	print STDERR "Could not copy default group file into place: $!\n";
	exit(1);
    }
    
    if (system("$CP -f $TMSHADOW $TMGSHADOW /etc") != 0) {
	print STDERR "Could not copy default passwd file into place: $!\n";
	exit(1);
    }

    return 0;
}

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

    #
    # 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") {
	$media = "$media-$IFC_FDUPLEX";
    }
    elsif ($duplex eq "half") {
	$media = "$media-$IFC_HDUPLEX";
    }
    else {
	warn("*** Bad duplex in ifconfig!\n");
	$media = "$media-$IFC_FDUPLEX";
    }

147
148
    $ifc = "/sbin/mii-tool --force=$media $iface\n" .
	   sprintf($IFCONFIG, $iface, $inet, $mask);
149
    
150
    return "$ifc";
151
152
153
154
155
156
}

#
# Generate and return an string that is approriate for putting
# into /etc/hosts.
#
157
sub os_etchosts_line($$$)
158
{
159
    my ($name, $ip, $aliases) = @_;
160
    
161
    return sprintf("%s\t%s %s", $ip, $name, $aliases);
162
163
164
165
166
167
168
169
170
171
172
173
}

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

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

174
175
176
177
178
179
180
181
182
183
#
# Delete an old group
# 
sub os_groupdel($)
{
    my($group) = @_;

    return system("$GROUPDEL $group");
}

184
185
186
187
188
189
190
191
192
193
194
195
196
#
# Remove a user account.
# 
sub os_userdel($)
{
    my($login) = @_;

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

#
# Modify user group membership.
# 
197
sub os_usermod($$$$$)
198
{
199
    my($login, $gid, $glist, $pswd, $root) = @_;
200
201
202
203
204
205
206
207

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

208
    return system("$USERMOD -g $gid $glist -p '$pswd' $login");
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
}

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

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

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

233
234
235
236
237
238
239
240
#
# Remove a homedir. Might someday archive and ship back.
#
sub os_homedirdel($$)
{
    return 0;
}

241
242
243
244
245
246
247
248
249
250
251
#
# 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);
}

252
253
254
255
256
257
258
259
260
261
262
263
264
#
# Create a directory including all intermediate directories.
#
sub os_mkdir($$)
{
    my ($dir, $mode) = @_;

    if (system("$MKDIR -p -m $mode $dir")) {
	return 0;
    }
    return 1;
}

265
266
267
268
269
270
271
272
#
# OS Dependent configuration. 
# 
sub os_setup()
{
    return 0;
}
    
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#
# OS dependent, routing-related commands
#
sub os_routing_enable_forward()
{
    my $cmd;

    $cmd = "sysctl -w net.ipv4.conf.all.forwarding=1";
    return $cmd;
}

sub os_routing_enable_gated()
{
    my $cmd;

288
289
290
    # XXX hack to avoid gated dying mysteriously with TCP/611 already in use
    $cmd = "sleep 3\n    ";
    $cmd .= "(ps alxww ; netstat -na) > /tmp/gated.state\n    ";
291
    $cmd .= "$GATED -f $BINDIR/gated_`$BINDIR/control_interface`.conf";
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    return $cmd;
}

sub os_routing_add_manual($$$$$)
{
    my ($routetype, $destip, $destmask, $gate, $cost) = @_;
    my $cmd;

    if ($routetype eq "host") {
	$cmd = "$ROUTE add -host $destip gw $gate";
    } elsif ($routetype eq "net") {
	$cmd = "$ROUTE add -net $destip netmask $destmask gw $gate";
    } else {
	warn "*** WARNING: bad routing entry type: $routetype\n";
	$cmd = "";
    }

    return $cmd;
}

312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
sub os_routing_del_manual($$$$$)
{
    my ($routetype, $destip, $destmask, $gate, $cost) = @_;
    my $cmd;

    if ($routetype eq "host") {
	$cmd = "$ROUTE delete -host $destip";
    } elsif ($routetype eq "net") {
	$cmd = "$ROUTE delete -net $destip netmask $destmask gw $gate";
    } else {
	warn "*** WARNING: bad routing entry type: $routetype\n";
	$cmd = "";
    }

    return $cmd;
}

329
1;