libsetup.pm 66.8 KB
Newer Older
1
2
#!/usr/bin/perl -wT

Leigh B. Stoller's avatar
Leigh B. Stoller committed
3
4
#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6
7
# All rights reserved.
#
8
# TODO: Signal handlers for protecting db files.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
9

10
11
12
13
14
15
16
#
# Common routines and constants for the client bootime setup stuff.
#
package libsetup;
use Exporter;
@ISA = "Exporter";
@EXPORT =
17
    qw ( libsetup_init libsetup_setvnodeid cleanup_node 
18
	 doifconfig dohostnames domounts dotunnels check_nickname
19
	 doaccounts dorpms dotarballs dostartupcmd install_deltas
20
	 bootsetup nodeupdate startcmdstatus whatsmynickname dosyncserver
21
22
	 TBBackGround TBForkCmd vnodejailsetup plabsetup vnodeplabsetup
	 dorouterconfig jailsetup dojailconfig JailedMounts findiface
Leigh B. Stoller's avatar
Leigh B. Stoller committed
23
	 tmccdie tmcctimeout libsetup_getvnodeid dotrafficconfig
24
	 ixpsetup
25

26
	 OPENTMCC CLOSETMCC RUNTMCC MFS REMOTE JAILED PLAB LOCALROOTFS IXP
27

28
	 CONFDIR TMCC TMIFC TMDELAY TMRPM TMTARBALLS TMHOSTS TMJAILNAME
29
	 TMNICKNAME HOSTSFILE TMSTARTUPCMD FINDIF TMTUNNELCONFIG
30
	 TMTRAFFICCONFIG TMROUTECONFIG TMLINKDELAY TMDELMAP TMMOUNTDB
31
	 TMPROGAGENTS TMPASSDB TMGROUPDB TMGATEDCONFIG
32
33
	 TMCCCMD_REBOOT TMCCCMD_STATUS TMCCCMD_IFC TMCCCMD_ACCT TMCCCMD_DELAY
	 TMCCCMD_HOSTS TMCCCMD_RPM TMCCCMD_TARBALL TMCCCMD_STARTUP
34
	 TMCCCMD_DELTA TMCCCMD_STARTSTAT TMCCCMD_READY TMCCCMD_TRAFFIC
35
	 TMCCCMD_BOSSINFO TMCCCMD_VNODELIST TMCCCMD_ISALIVE TMCCCMD_LINKDELAYS
36
37
	 TMCCCMD_PROGRAMS TMCCCMD_SUBNODELIST TMCCCMD_SUBCONFIG
	 TMCCCMD_STATE
38
39
40
41
42
43
       );

# Must come after package declaration!
use English;

#
44
45
# For virtual (multiplexed nodes). If defined, tack onto tmcc command.
# and use in pathnames. Used in conjunction with jailed virtual nodes.
46
# I am also using this for subnodes; eventually everything will be subnodes.
47
#
48
my $vnodeid;
49
50
51
52
53
54
55
56
sub libsetup_setvnodeid($)
{
    ($vnodeid) = @_;
}
sub libsetup_getvnodeid()
{
    return $vnodeid;
}
57

58
#
59
# True if running inside a jail. Set just below. 
60
61
62
# 
my $injail;

63
64
65
66
67
#
# True if running in a Plab vserver.
#
my $inplab;

68
69
70
71
72
73
#
# Ditto for IXP, although currently there is no "in" IXP setup; it
# is all done from outside.
#
my $inixp;

74
75
76
# Load up the paths. Its conditionalized to be compatabile with older images.
# Note this file has probably already been loaded by the caller.
BEGIN
77
{
78
79
80
81
82
    if (! -e "/etc/emulab/paths.pm") {
	die("Yikes! Could not require /etc/emulab/paths.pm!\n");
    }
    require "/etc/emulab/paths.pm";
    import emulabpaths;
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    #
    # Determine if running inside a jail. This affects the paths below.
    #
    if (-e "$BOOTDIR/jailname") {
	open(VN, "$BOOTDIR/jailname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$injail = 1;
    }

101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    # Determine if running inside a Plab vserver.
    if (-e "$BOOTDIR/plabname") {
	open(VN, "$BOOTDIR/plabname");
	$vnodeid = <VN>;
	close(VN);

	if ($vnodeid =~ /^([-\w]+)$/) {
	    $vnodeid = $1;
	}
	else {
	    die("Bad data in vnodeid: $vnodeid");
	}
	$inplab = 1;
    }

116
117
118
119
120
121
122
    # Make sure these exist!
    if (! -e "$VARDIR/logs") {
	mkdir("$VARDIR", 0775);
	mkdir("$VARDIR/jails", 0775);
	mkdir("$VARDIR/db", 0755);
	mkdir("$VARDIR/logs", 0775);
	mkdir("$VARDIR/boot", 0775);
123
	mkdir("$VARDIR/lock", 0775);
124
    }
125
126
127
}

#
128
129
130
# The init routine. This is deprecated, but left behind in case an old
# liblocsetup is run against a new libsetup. Whenever a new libsetup
# is installed, better install the path module (see above) too!
131
#
132
133
134
135
136
137
138
139
140
sub libsetup_init($)
{
    my($path) = @_;

    $ETCDIR  = $path;
    $BINDIR  = $path;
    $VARDIR  = $path;
    $BOOTDIR = $path
}
141

142
#
143
# This "local" library provides the OS dependent part. 
144
#
145
use liblocsetup;
146

147
148
149
150
#
# These are the paths of various files and scripts that are part of the
# setup library.
#
151
152
153
sub TMCC()		{ "$BINDIR/tmcc"; }
sub TMHOSTS()		{ "$ETCDIR/hosts"; }
sub FINDIF()		{ "$BINDIR/findif"; }
154
sub HOSTSFILE()		{ "/etc/hosts"; }
155
#
156
# This path is valid only *outside* the jail when its setup.
157
# 
158
159
sub JAILDIR()		{ "$VARDIR/jails/$vnodeid"; }

160
161
162
163
164
#
# Also valid outside the jail, this is where we put local project storage.
#
sub LOCALROOTFS()	{ (REMOTE() ? "/users/local" : "$VARDIR/jails/local");}

165
166
167
#
# Okay, here is the path mess. There are three environments.
# 1. A local node where everything goes in one place ($VARDIR/boot).
168
# 2. A virtual node inside a jail or a Plab vserver ($VARDIR/boot).
169
# 3. A virtual (or sub) node, from the outside. 
170
171
172
173
174
#
# As for #3, whether setting up a old-style virtual node or a new style
# jailed node, the code that sets it up needs a different per-vnode path.
#
sub CONFDIR() {
175
    if ($injail || $inplab) {
176
177
178
179
180
181
	return $BOOTDIR;
    }
    if ($vnodeid) {
	return JAILDIR();
    }
    return $BOOTDIR;
182
}
183

184
185
186
187
188
189
190
191
192
193
194
195
196
#
# These go in /var/emulab. Good for all environments!
# 
sub TMMOUNTDB()		{ $VARDIR . "/db/mountdb"; }
sub TMSFSMOUNTDB()	{ $VARDIR . "/db/sfsmountdb"; }
sub TMPASSDB()		{ $VARDIR . "/db/passdb"; }
sub TMGROUPDB()		{ $VARDIR . "/db/groupdb"; }
#
# The rest of these depend on the environment running in (inside/outside jail).
# 
sub TMNICKNAME()	{ CONFDIR() . "/nickname";}
sub TMJAILNAME()	{ CONFDIR() . "/jailname";}
sub TMJAILCONFIG()	{ CONFDIR() . "/jailconfig";}
197
sub TMPLABCONFIG()	{ CONFDIR() . "/rc.plab";}
198
sub TMSTARTUPCMD()	{ CONFDIR() . "/startupcmd";}
199
sub TMPROGAGENTS()	{ CONFDIR() . "/progagents";}
200
201
202
203
sub TMIFC()		{ CONFDIR() . "/rc.ifc"; }
sub TMRPM()		{ CONFDIR() . "/rc.rpm";}
sub TMTARBALLS()	{ CONFDIR() . "/rc.tarballs";}
sub TMROUTECONFIG()     { CONFDIR() . "/rc.route";}
204
sub TMGATEDCONFIG()     { CONFDIR() . "/gated.conf";}
205
206
207
sub TMTRAFFICCONFIG()	{ CONFDIR() . "/rc.traffic";}
sub TMTUNNELCONFIG()	{ CONFDIR() . "/rc.tunnel";}
sub TMVTUNDCONFIG()	{ CONFDIR() . "/vtund.conf";}
208
209
sub TMDELAY()		{ CONFDIR() . "/rc.delay";}
sub TMLINKDELAY()	{ CONFDIR() . "/rc.linkdelay";}
210
sub TMDELMAP()		{ CONFDIR() . "/delay_mapping";}
211
212
sub TMSYNCSERVER()	{ CONFDIR() . "/syncserver";}
sub TMRCSYNCSERVER()	{ CONFDIR() . "/rc.syncserver";}
213

Austin Clements's avatar
Austin Clements committed
214
215
216
217
218
219
220
221
#
# Whether or not to use SFS (the self-certifying file system).  If this
# is 0, fall back to NFS.  Note that it doesn't hurt to set this to 1
# even if TMCD is not serving out SFS mounts, or if this node is not
# running SFS.  It'll deal and fall back to NFS.
#
my $USESFS		= 1;

222
#
223
224
225
226
227
# This is the VERSION. We send it through to tmcd so it knows what version
# responses this file is expecting.
#
# BE SURE TO BUMP THIS AS INCOMPATIBILE CHANGES TO TMCD ARE MADE!
#
228
sub TMCD_VERSION()	{ 11; };
229
230
231

#
# These are the TMCC commands. 
232
233
234
#
sub TMCCCMD_REBOOT()	{ "reboot"; }
sub TMCCCMD_STATUS()	{ "status"; }
235
sub TMCCCMD_STATE()	{ "state"; }
236
237
238
sub TMCCCMD_IFC()	{ "ifconfig"; }
sub TMCCCMD_ACCT()	{ "accounts"; }
sub TMCCCMD_DELAY()	{ "delay"; }
239
sub TMCCCMD_HOSTS()	{ "hostnames"; }
240
241
242
243
244
245
246
sub TMCCCMD_RPM()	{ "rpms"; }
sub TMCCCMD_TARBALL()	{ "tarballs"; }
sub TMCCCMD_STARTUP()	{ "startupcmd"; }
sub TMCCCMD_DELTA()	{ "deltas"; }
sub TMCCCMD_STARTSTAT()	{ "startstatus"; }
sub TMCCCMD_READY()	{ "ready"; }
sub TMCCCMD_MOUNTS()	{ "mounts"; }
247
sub TMCCCMD_ROUTING()	{ "routing"; }
248
249
sub TMCCCMD_TRAFFIC()	{ "trafgens"; }
sub TMCCCMD_BOSSINFO()	{ "bossinfo"; }
250
sub TMCCCMD_TUNNEL()	{ "tunnels"; }
251
sub TMCCCMD_NSECONFIGS(){ "nseconfigs"; }
252
sub TMCCCMD_VNODELIST() { "vnodelist"; }
253
sub TMCCCMD_SUBNODELIST(){ "subnodelist"; }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
254
sub TMCCCMD_ISALIVE()   { "isalive"; }
Austin Clements's avatar
Austin Clements committed
255
256
sub TMCCCMD_SFSHOSTID()	{ "sfshostid"; }
sub TMCCCMD_SFSMOUNTS() { "sfsmounts"; }
257
sub TMCCCMD_JAILCONFIG(){ "jailconfig"; }
258
sub TMCCCMD_PLABCONFIG(){ "plabconfig"; }
259
sub TMCCCMD_SUBCONFIG() { "subconfig"; }
260
sub TMCCCMD_LINKDELAYS(){ "linkdelays"; }
261
sub TMCCCMD_PROGRAMS()  { "programs"; }
262
sub TMCCCMD_SYNCSERVER(){ "syncserver"; }
263
264
265
266

#
# Some things never change.
# 
267
my $TARINSTALL  = "/usr/local/bin/install-tarfile %s %s %s";
268
my $VTUND       = "/usr/local/sbin/vtund";
269
270
271

#
# This is a debugging thing for my home network.
272
273
274
275
276
277
278
279
280
281
#
my $NODE = "";
if (defined($ENV{'TMCCARGS'})) {
    if ($ENV{'TMCCARGS'} =~ /^([-\w\s]*)$/) {
	$NODE .= " $1";
    }
    else {
	die("Tainted TMCCARGS from environment: $ENV{'TMCCARGS'}!\n");
    }
}
282
283

# Locals
284
285
286
287
my $pid		= "";
my $eid		= "";
my $vname	= "";

Leigh B. Stoller's avatar
Leigh B. Stoller committed
288
289
290
# Control tmcc error condition and timeout. Dynamic, not lexical!
$tmccdie        = 1; 
$tmcctimeout    = 0;
291
my $TMCCTIMEO   = 30;	# Default timeout on remote nodes. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
292

293
294
295
# When on the MFS, we do a much smaller set of stuff.
# Cause of the way the packages are loaded (which I do not understand),
# this is computed on the fly instead of once.
296
sub MFS()	{ if (-e "$ETCDIR/ismfs") { return 1; } else { return 0; } }
297

298
299
300
#
# Same for a remote node.
#
301
302
sub REMOTE()	{ if (-e "$ETCDIR/isrem") { return 1; } else { return 0; } }

303
304
305
306
307
#
# Same for a control node.
#
sub CONTROL()	{ if (-e "$ETCDIR/isctrl") { return 1; } else { return 0; } }

308
309
310
311
#
# Are we jailed? See above.
#
sub JAILED()	{ if ($injail) { return $vnodeid; } else { return 0; } }
312

313
314
315
316
317
#
# Are we on plab?
#
sub PLAB()	{ if ($inplab) { return $vnodeid; } else { return 0; } }

318
319
320
321
322
#
# Are we on an IXP
#
sub IXP()	{ if ($inixp) { return $vnodeid; } else { return 0; } }

323
324
325
326
327
328
329
330
#
# Do not try this on the MFS since it has such a wimpy perl installation.
#
if (!MFS()) {
    require Socket;
    import Socket;
}

331
332
333
334
#
# Open a TMCC connection and return the "stream pointer". Caller is
# responsible for closing the stream and checking return value.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
335
# usage: OPENTMCC(char *command, char *args, char *options)
336
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
337
sub OPENTMCC($;$$)
338
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
339
    my($cmd, $args, $options) = @_;
340
    my $vn = "";
341
342
343
344
345
    local *TM;

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
346
347
348
    if (!defined($options)) {
	$options = "";
    }
349
    if (defined($vnodeid)) {
350
351
	$vn = "-n $vnodeid";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
352
353
354
    if ($tmcctimeout) {
	$options .= " -t $tmcctimeout";
    }
355

Leigh B. Stoller's avatar
Leigh B. Stoller committed
356
357
    my $foo = sprintf("%s -v %d $options $NODE $vn $cmd $args |",
		      TMCC, TMCD_VERSION);
358

Leigh B. Stoller's avatar
Leigh B. Stoller committed
359
360
361
362
363
    if (!open(TM, $foo)) {
	print STDERR "Cannot start TMCC: $!\n";
	die("\n") if $tmccdie;
	return undef;
    }
364
365
366
    return (*TM);
}

367
368
369
370
371
372
#
# Close connection. Die on error.
# 
sub CLOSETMCC($) {
    my($TM) = @_;
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
373
374
375
376
377
378
379
380
381
382
383
    if (! close($TM)) {
	if ($?) {
	    print STDERR "TMCC exited with status $?!\n";
	}
	else {
	    print STDERR "Error closing TMCC pipe: $!\n";
	}
	die("\n") if $tmccdie;
	return 0;
    }
    return 1;
384
385
}

386
387
388
#
# Run a TMCC command with the provided arguments.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
389
# usage: RUNTMCC(char *command, char *args, char *options)
390
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
391
sub RUNTMCC($;$$)
392
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
393
    my($cmd, $args, $options) = @_;
394
395
396
397
398
    my($TM);

    if (!defined($args)) {
	$args = "";
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
399
400
401
    if (!defined($options)) {
	$options = "";
    }
402
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
403
    $TM = OPENTMCC($cmd, $args, $options);
404
405
406
407
408
409
410
411
412
413

    close($TM)
	or die $? ? "TMCC exited with status $?" : "Error closing pipe: $!";
    
    return 0;
}

#
# Reset to a moderately clean state.
#
414
415
416
sub cleanup_node ($) {
    my ($scrub) = @_;
    
417
    print STDOUT "Cleaning node; removing configuration files ...\n";
418
    unlink TMIFC, TMRPM, TMSTARTUPCMD, TMTARBALLS;
419
    unlink TMROUTECONFIG, TMTRAFFICCONFIG, TMTUNNELCONFIG;
420
    unlink TMDELAY, TMLINKDELAY, TMPROGAGENTS, TMSYNCSERVER, TMRCSYNCSERVER;
421
    unlink TMMOUNTDB . ".db";
422
    unlink TMSFSMOUNTDB . ".db";
423
    unlink "$VARDIR/db/rtabid";
424

425
426
427
428
429
    #
    # If scrubbing, remove the password/group file DBs so that we revert
    # to base set.
    # 
    if ($scrub) {
430
	unlink TMNICKNAME;
431
432
433
434
435
436
437
438
439
440
	unlink TMPASSDB . ".db";
	unlink TMGROUPDB . ".db";
    }

    if (! REMOTE()) {
	printf STDOUT "Resetting %s file\n", HOSTSFILE;
	if (system($CP, "-f", TMHOSTS, HOSTSFILE) != 0) {
	    printf "Could not copy default %s into place: $!\n", HOSTSFILE;
	    exit(1);
	}
441
442
    }

443
    return os_cleanup_node($scrub);
444
445
446
}

#
447
448
# Check node allocation. If the nickname file has been created, use
# that to avoid load on tmcd.
449
450
451
452
453
#
# Returns 0 if node is free. Returns list (pid/eid/vname) if allocated.
#
sub check_status ()
{
454
    my $TM = OPENTMCC(TMCCCMD_STATUS);
455
    $_  = <$TM>;
456
    CLOSETMCC($TM);
457
458

    if ($_ =~ /^FREE/) {
459
	unlink TMNICKNAME;
460
461
462
	return 0;
    }
    
463
    if ($_ =~ /ALLOCATED=([-\@\w]*)\/([-\@\w]*) NICKNAME=([-\@\w]*)/) {
464
465
466
467
468
469
470
471
	$pid   = $1;
	$eid   = $2;
	$vname = $3;
    }
    else {
	warn "*** WARNING: Error getting reservation status\n";
	return 0;
    }
472
473
474
    
    #
    # Stick our nickname in a file in case someone wants it.
475
476
    # Do not overwrite; we want to save the original info until later.
    # See bootsetup; indicates project change!
477
    #
478
    if (! -e TMNICKNAME()) {
479
480
	system("echo '$vname.$eid.$pid' > " . TMNICKNAME());
    }
481
    
482
483
484
485
    return ($pid, $eid, $vname);
}

#
486
487
488
489
# Check cached nickname. Its okay if we have been deallocated and the info
# is stale. The node will notice that later.
# 
sub check_nickname()
490
{
491
492
493
    if (-e TMNICKNAME) {
	my $nickfile = TMNICKNAME;
	my $nickinfo = `cat $nickfile`;
494

495
496
497
498
499
500
501
502
503
	if ($nickinfo =~ /([-\@\w]*)\.([-\@\w]*)\.([-\@\w]*)/) {
	    $vname = $1;
	    $eid   = $2;
	    $pid   = $3;

	    return ($pid, $eid, $vname);
	}
    }
    return check_status();
504
505
506
507
}

#
# Process mount directives from TMCD. We keep track of all the mounts we
508
# have added in here so that we delete just the mounts we added, when
509
510
511
512
513
514
515
516
517
# project membership changes. Same goes for project directories on shared
# nodes. We use a simple perl DB for that.
#
sub domounts()
{
    my $TM;
    my %MDB;
    my %mounts;
    my %deletes;
Austin Clements's avatar
Austin Clements committed
518
519
520
    my %sfsmounts;
    my %sfsdeletes;

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    #
    # Update our SFS hostid first. If this fails, dosfshostid will
    # unset USESFS.
    # 
    if ($USESFS) {
	if (! MFS()) {
	    #
	    # Setup SFS hostid.
	    #
	    print STDOUT "Setting up for SFS ... \n";
	    dosfshostid();
	}
	else {
	    # No SFS on the MFS.
	    $USESFS = 0;
	}
    }

Austin Clements's avatar
Austin Clements committed
539
    $TM = OPENTMCC(TMCCCMD_MOUNTS, "USESFS=$USESFS");
540
541

    while (<$TM>) {
Austin Clements's avatar
Austin Clements committed
542
	if ($_ =~ /^REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
543
544
	    $mounts{$1} = $2;
	}
Austin Clements's avatar
Austin Clements committed
545
546
547
548
549
550
	elsif ($_ =~ /^SFS REMOTE=([-:\@\w\.\/]+) LOCAL=([-\@\w\.\/]+)/) {
	    $sfsmounts{$1} = $2;
	}
	else {
	    warn "*** WARNING: Malformed mount information: $_\n";
	}
551
    }
552
553
    CLOSETMCC($TM);
    
554
555
556
557
    #
    # The MFS version does not support (or need) this DB stuff. Just mount
    # them up.
    #
558
    if (MFS()) {
559
560
	while (($remote, $local) = each %mounts) {
	    if (! -e $local) {
561
		if (! os_mkdir($local, "0770")) {
562
563
564
565
566
567
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	
	    print STDOUT "  Mounting $remote on $local\n";
568
569
570
	    if (system("$NFSMOUNT $remote $local")) {
		warn "*** WARNING: Could not $NFSMOUNT ".
		    "$remote on $local: $!\n";
571
572
573
574
575
576
		next;
	    }
	}
	return 0;
    }

577
578
579
580
581
582
583
    dbmopen(%MDB, TMMOUNTDB, 0660);
    
    #
    # First mount all the mounts we are told to. For each one that is not
    # currently mounted, and can be mounted, add it to the DB.
    # 
    while (($remote, $local) = each %mounts) {
584
	if (defined($MDB{$remote})) {
585
586
587
	    next;
	}

588
589
590
591
592
593
	if (! -d $local) {
	    # Leftover SFS link.
	    if (-l $local) {
		unlink($local) or
		    warn "*** WARNING: Could not unlink $local: $!\n";
	    }
594
	    if (! os_mkdir($local, "0770")) {
595
596
597
598
599
600
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
	print STDOUT "  Mounting $remote on $local\n";
601
602
	if (system("$NFSMOUNT $remote $local")) {
	    warn "*** WARNING: Could not $NFSMOUNT $remote on $local: $!\n";
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
	    next;
	}

	$MDB{$remote} = $local;
    }

    #
    # Now unmount the ones that we mounted previously, but are now no longer
    # in the mount set (as told to us by the TMCD). Note, we cannot delete 
    # them directly from MDB since that would mess up the foreach loop, so
    # just stick them in temp and postpass it.
    #
    while (($remote, $local) = each %MDB) {
	if (defined($mounts{$remote})) {
	    next;
	}

	print STDOUT "  Unmounting $local\n";
	if (system("$UMOUNT $local")) {
	    warn "*** WARNING: Could not unmount $local\n";
	    next;
	}
	
	#
	# Only delete from set if we can actually unmount it. This way
	# we can retry it later (or next time).
	# 
	$deletes{$remote} = $local;
    }
    while (($remote, $local) = each %deletes) {
	delete($MDB{$remote});
    }

    # Write the DB back out!
    dbmclose(%MDB);

Austin Clements's avatar
Austin Clements committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    #
    # Now, do basically the same thing over again, but this time for
    # SFS mounted stuff
    #

    if (scalar(%sfsmounts)) {
	dbmopen(%MDB, TMSFSMOUNTDB, 0660);
	
	#
	# First symlink all the mounts we are told to. For each one
	# that is not currently symlinked, and can be, add it to the
	# DB.
	#
	while (($remote, $local) = each %sfsmounts) {
	    if (-l $local) {
		if (readlink($local) eq ("/sfs/" . $remote)) {
		    $MDB{$remote} = $local;
		    next;
		}
		if (readlink($local) ne ("/sfs/" . $remote)) {
		    print STDOUT "  Unlinking incorrect symlink $local\n";
660
		    if (! unlink($local)) {
Austin Clements's avatar
Austin Clements committed
661
662
663
664
665
			warn "*** WARNING: Could not unlink $local: $!\n";
			next;
		    }
		}
	    }
666
667
668
669
670
671
	    elsif (-d $local) {
		if (! rmdir($local)) {
		    warn "*** WARNING: Could not rmdir $local: $!\n";
		    next;
		}
	    }
Austin Clements's avatar
Austin Clements committed
672
673
674
	    
	    $dir = $local;
	    $dir =~ s/(.*)\/[^\/]*$/$1/;
Leigh B. Stoller's avatar
Leigh B. Stoller committed
675
	    if ($dir ne "" && ! -e $dir) {
Austin Clements's avatar
Austin Clements committed
676
		print STDOUT "  Making directory $dir\n";
677
		if (! os_mkdir($dir, "0755")) {
Austin Clements's avatar
Austin Clements committed
678
679
680
681
682
		    warn "*** WARNING: Could not make directory $local: $!\n";
		    next;
		}
	    }
	    print STDOUT "  Symlinking $remote on $local\n";
Leigh B. Stoller's avatar
Leigh B. Stoller committed
683
	    if (! symlink("/sfs/" . $remote, $local)) {
Austin Clements's avatar
Austin Clements committed
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
		warn "*** WARNING: Could not make symlink $local: $!\n";
		next;
	    }
	    
	    $MDB{$remote} = $local;
	}

	#
	# Now delete the ones that we symlinked previously, but are
	# now no longer in the mount set (as told to us by the TMCD).
	# Note, we cannot delete them directly from MDB since that
	# would mess up the foreach loop, so just stick them in temp
	# and postpass it.
	#
	while (($remote, $local) = each %MDB) {
	    if (defined($sfsmounts{$remote})) {
		next;
	    }
	    
	    if (! -e $local) {
		$sfsdeletes{$remote} = $local;
		next;
	    }
	    
	    print STDOUT "  Deleting symlink $local\n";
	    if (! unlink($local)) {
		warn "*** WARNING: Could not delete $local: $!\n";
		next;
	    }
	    
	    #
	    # Only delete from set if we can actually unlink it.  This way
	    # we can retry it later (or next time).
	    #
	    $sfsdeletes{$remote} = $local;
	}
	while (($remote, $local) = each %sfsdeletes) {
	    delete($MDB{$remote});
	}

	# Write the DB back out!
	dbmclose(%MDB);	
    }
    else {
	# There were no SFS mounts reported, so disable SFS
729
	$USESFS = 0;
Austin Clements's avatar
Austin Clements committed
730
731
732
733
734
    }

    return 0;
}

735
#
736
737
738
# Aux function called from the mkjail code to do mounts outside
# of a jail, and return the list of mounts that were created. Can use
# either NFS or local loopback. Maybe SFS someday. Local only, of course.
739
# 
740
sub JailedMounts($$$)
741
{
742
    my ($vid, $rootpath, $usenfs) = @_;
743
    my @mountlist = ();
744
    my $mountstr;
745
746
747
748
749
750
751
752

    #
    # No NFS mounts on remote nodes.
    # 
    if (REMOTE()) {
	return ();
    }

753
754
755
756
757
758
    if ($usenfs) {
	$mountstr = $NFSMOUNT;
    } else {
	$mountstr = $LOOPBACKMOUNT;
    }

759
760
761
762
763
    #
    # Mount same set of existing mounts. A hack, but this whole NFS thing
    # is a serious hack inside jails.
    #
    dbmopen(%MDB, TMMOUNTDB, 0444);
764
    
765
    while (my ($remote, $path) = each %MDB) {
766
	$local = "$rootpath$path";
767
768
769
770
771
772
773
774
	    
	if (! -e $local) {
	    if (! os_mkdir($local, "0770")) {
		warn "*** WARNING: Could not make directory $local: $!\n";
		next;
	    }
	}
	
775
776
777
778
	if (! $usenfs) {
	    $remote = $path;
	}

779
	print STDOUT "  Mounting $remote on $local\n";
780
781
	if (system("$mountstr $remote $local")) {
	    warn "*** WARNING: Could not $mountstr $remote on $local: $!\n";
782
783
784
785
	    next;
	}
	push(@mountlist, $path);
    }
786
    dbmclose(%MDB);	   
787
788
    return @mountlist;
}
Austin Clements's avatar
Austin Clements committed
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#
# Do SFS hostid setup.
# Creates an SFS host key for this node, if it doesn't already exist,
# and sends it to TMCD
#
sub dosfshostid ()
{
    my $TM;
    my $myhostid;

    # Do I already have a host key?
    if (! -e "/etc/sfs/sfs_host_key") {
	warn "*** This node does not have a host key, skipping SFS stuff\n";
	$USESFS = 0;
	return 1;
    }

    # Give hostid to TMCD
807
808
809
810
811
812
    if (-d "/usr/local/lib/sfs-0.6") {
	$myhostid = `sfskey hostid - 2>/dev/null`;
    }
    else {
	$myhostid = `sfskey hostid -s authserv - 2>/dev/null`;
    }
813
    if (! $?) {
Austin Clements's avatar
Austin Clements committed
814
815
816
817
818
	if ( $myhostid =~ /^([-\.\w_]*:[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
819
820
821
822
823
	elsif ( $myhostid =~ /^(@[-\.\w_]*,[a-z0-9]*)$/ ) {
	    $myhostid = $1;
	    print STDOUT "  Hostid: $myhostid\n";
	    RUNTMCC(TMCCCMD_SFSHOSTID, "$myhostid");
	}
Austin Clements's avatar
Austin Clements committed
824
825
826
827
828
	else {
	    warn "*** WARNING: Invalid hostid\n";
	}
    }
    else {
829
	warn "*** WARNING: Could not retrieve this node's SFShostid!\n";
Austin Clements's avatar
Austin Clements committed
830
831
832
833
834
835
	$USESFS = 0;
    }

    return 0;
}

836
837
838
839
#
# Do interface configuration.    
# Write a file of ifconfig lines, which will get executed.
#
840
sub doifconfig (;$)
841
{
842
    my ($rtabid) = @_;
843
    my @ifaces   = ();
844
845
    my $upcmds   = "";
    my $downcmds = "";
846
    my @ifacelist= ();
847

848
849
850
851
852
853
854
    #
    # Kinda ugly, but there is too much perl goo included by Socket to put it
    # on the MFS. 
    # 
    if (MFS()) {
	return 1;
    }
855

856
    my $TM = OPENTMCC(TMCCCMD_IFC);
857
    while (<$TM>) {
858
859
860
	push(@ifaces, $_);
    }
    CLOSETMCC($TM);
861

862
863
864
865
866
867
868
869
870
    #
    # Create the interface list file.
    # Control net is always first.
    #
    open(XIFS, ">$BOOTDIR/tmcc.ifs") or
	die "Cannot open file $BOOTDIR/tmcc.ifs: $!";

    print XIFS `control_interface`;

871
    if (! @ifaces) {
872
	close(XIFS);
873
874
875
876
	return 0;
    }

    my $ethpat  = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) MAC=(\w*) );
877
    $ethpat    .= q(SPEED=(\w*) DUPLEX=(\w*) IPALIASES="(.*)" IFACE=(\w*));
878
879
880

    my $vethpat = q(IFACETYPE=(\w*) INET=([0-9.]*) MASK=([0-9.]*) ID=(\d*) );
    $vethpat   .= q(VMAC=(\w*) PMAC=(\w*));
881

882
883
    foreach my $iface (@ifaces) {
	if ($iface =~ /$ethpat/) {
884
885
886
887
888
	    my $inet     = $2;
	    my $mask     = $3;
	    my $mac      = $4;
	    my $speed    = $5; 
	    my $duplex   = $6;
889
	    my $aliases  = $7;
890
	    my $iface    = $8;
891
	    my $routearg = inet_ntoa(inet_aton($inet) & inet_aton($mask));
892

893
894
	    if (($iface ne "") ||
		($iface = findiface($mac))) {
895
896
897
		if (JAILED()) {
		    next;
		}
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
		print XIFS "$iface\n";

		#
		# Rather than try to wedge the IXP in, I am going with
		# a new approach. Parse the results from tmcd into a
		# simple data structure, and return that for the caller
		# to use. Might want to use a perl module at some point.
		#
		my $ifconfig = {};
		    
		$ifconfig->{"IPADDR"}   = $inet;
		$ifconfig->{"IPMASK"}   = $mask;
		$ifconfig->{"MAC"}      = $mac;
		$ifconfig->{"SPEED"}    = $speed;
		$ifconfig->{"DUPLEX"}   = $duplex;
		$ifconfig->{"ALIASES"}  = $aliases;
		$ifconfig->{"IFACE"}    = $iface;
		push(@ifacelist, $ifconfig);

		if (IXP()) {
		    next;
		}
920

921
		my ($upline, $downline) =
922
923
		    os_ifconfig_line($iface, $inet, $mask,
				     $speed, $duplex, $aliases,$rtabid);
924
		    
925
926
		$upcmds   .= "$upline\n    "
		    if (defined($upline));
927
928
		$upcmds   .= TMROUTECONFIG . " $routearg up\n";
		
929
930
		$downcmds .= TMROUTECONFIG . " $routearg down\n    ";
		$downcmds .= "$downline\n    "
931
		    if (defined($downline));
932
933
934
935
936
937

		# There could be routes for each alias.
		foreach my $alias (split(',', $aliases)) {
		    $routearg = inet_ntoa(inet_aton($alias) &
					  inet_aton($mask));
			
938
939
		    $upcmds   .= TMROUTECONFIG . " $routearg up\n";
		    $downcmds .= TMROUTECONFIG . " $routearg down\n";
940
		}
941
942
	    }
	    else {
943
		warn "*** WARNING: Bad MAC: $mac\n";
944
945
	    }
	}
946
947
948
949
950
951
952
953
954
	elsif ($iface =~ /$vethpat/) {
	    my $iface    = undef;
	    my $inet     = $2;
	    my $mask     = $3;
	    my $id       = $4;
	    my $vmac     = $5;
	    my $pmac     = $6; 
	    my $routearg = inet_ntoa(inet_aton($inet) & inet_aton($mask));

955
956
957
958
959
960
961
	    if (JAILED()) {
		if ($iface = findiface($vmac)) {
		    print XIFS "$iface\n";
		}
		next;
	    }

962
963
	    if ($pmac eq "none" ||
		($iface = findiface($pmac))) {
964
965
		print XIFS "$iface\n"
		    if (defined($iface));
966

967
968
969
		my ($upline, $downline) =
		    os_ifconfig_veth($iface, $inet, $mask, $id, $vmac,$rtabid);
		    
970
		$upcmds   .= "$upline\n    ";
971
972
		$upcmds   .= TMROUTECONFIG . " $routearg up\n";
		
973
974
		$downcmds .= TMROUTECONFIG . " $routearg down\n    ";
		$downcmds .= "$downline\n    "
975
976
977
978
979
980
		    if (defined($downline));
	    }
	    else {
		warn "*** WARNING: Bad PMAC: $pmac\n";
	    }
	}
981
982
983
984
	else {
	    warn "*** WARNING: Bad ifconfig line: $_";
	}
    }
985
    close(XIFS);
986
987
988
    # Done when jailed or an IXP
    return @ifacelist
	if (JAILED() || IXP());
989

990
991
992
993
994
    #
    # Local file into which we write ifconfig commands (as a shell script).
    # 
    open(IFC, ">" . TMIFC)
	or die("Could not open " . TMIFC . ": $!");
995
996
997

    print IFC "# auto-generated by libsetup.pm, DO NOT EDIT\n";
    print IFC "$upcmds\n";
998
999
1000
1001
1002
    print IFC "#!/bin/sh\n";
    print IFC "# auto-generated by libsetup.pm, DO NOT EDIT\n";
    print IFC "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";
    print IFC "case \"\$action\" in\n";
    print IFC "  enable)\n";
1003
1004
    print IFC "    $upcmds\n";
    print IFC "    ;;\n";
1005
    print IFC "  disable)\n";
1006
1007
    print IFC "    $downcmds\n";
    print IFC "    ;;\n";
1008
    print IFC "esac\n";
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
    close(IFC);
    chmod(0755, TMIFC);

    return 0;
}

#
# Convert from MAC to iface name (eth0/fxp0/etc) using little helper program.
# 
sub findiface($)
{
    my($mac) = @_;
    my($iface);

    open(FIF, FINDIF . " $mac |")
	or die "Cannot start " . FINDIF . ": $!";

    $iface = <FIF>;
    
    if (! close(FIF)) {
	return 0;
    }
    
    $iface =~ s/\n//g;
    return $iface;
}

1036
1037
1038
1039
#
# Do router configuration stuff. This just writes a file for someone else
# to deal with.
#
1040
sub dorouterconfig (;$)
1041
{
1042
1043
1044
1045
1046
    my ($rtabid) = @_;
    my @stuff    = ();
    my $routing  = 0;
    my %upmap    = ();
    my %downmap  = ();
1047
    my @routes   = ();
1048
1049
1050
1051
1052
1053
    my $TM;

    $TM = OPENTMCC(TMCCCMD_ROUTING);
    while (<$TM>) {
	push(@stuff, $_);
    }
1054
    CLOSETMCC($TM);
1055
1056
1057
1058
1059
1060

    if (! @stuff) {
	return 0;
    }

    #
1061
1062
    # Look for router type. If none, we still write the file since other
    # scripts expect this to exist.
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
    # 
    foreach my $line (@stuff) {
	if (($line =~ /ROUTERTYPE=(.+)/) && ($1 ne "none")) {
	    $routing = 1;
	    last;
	}
    }
    
    open(RC, ">" . TMROUTECONFIG)
	or die("Could not open " . TMROUTECONFIG . ": $!");

1074
1075
1076
    print RC "#!/bin/sh\n";
    print RC "# auto-generated by libsetup.pm, DO NOT EDIT\n";

1077
1078
1079
1080
1081
1082
1083
    if (! $routing) {
	print RC "true\n";
	close(RC);
	chmod(0755, TMROUTECONFIG);
	return 0;
    }

1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
    #
    # Now convert static route info into OS route commands
    # Also check for use of gated and remember it.
    #
    my $usegated = 0;
    my $pat;

    #
    # ROUTERTYPE=manual
    # ROUTE DEST=192.168.2.3 DESTTYPE=host DESTMASK=255.255.255.0 \
    #	NEXTHOP=192.168.1.3 COST=0
    #
    $pat = q(ROUTE DEST=([0-9\.]*) DESTTYPE=(\w*) DESTMASK=([0-9\.]*) );
    $pat .= q(NEXTHOP=([0-9\.]*) COST=([0-9]*));

    my $usemanual = 0;
1100
    foreach my $line (@stuff) {
1101
1102
1103
1104
1105
	if ($line =~ /ROUTERTYPE=(gated|ospf)/) {
	    $usegated = 1;
	} elsif ($line =~ /ROUTERTYPE=(manual|static)/) {
	    $usemanual = 1;
	} elsif ($usemanual && $line =~ /$pat/) {
1106
	    my $dip   = $1;
1107
1108
	    my $rtype = $2;
	    my $dmask = $3;
1109
1110
	    my $gate  = $4;
	    my $cost  = $5;
1111
	    my $rcline;
1112
	    my $routearg = inet_ntoa(inet_aton($gate) & inet_aton($dmask));
1113

1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
	    #
	    # For IXP.
	    #
	    my $rconfig = {};
		    
	    $rconfig->{"IPADDR"}   = $dip;
	    $rconfig->{"TYPE"}     = $rtype;
	    $rconfig->{"IPMASK"}   = $dmask;
	    $rconfig->{"GATEWAY"}  = $gate;
	    $rconfig->{"COST"}     = $cost;
	    push(@routes, $rconfig);

1126
1127
1128
1129
	    if (! defined($upmap{$routearg})) {
		$upmap{$routearg} = [];
		$downmap{$routearg} = [];
	    }
1130
1131
	    $rcline = os_routing_add_manual($rtype, $dip,
					    $dmask, $gate, $cost, $rtabid);
1132
	    push(@{$upmap{$routearg}}, $rcline);
1133
1134
	    $rcline = os_routing_del_manual($rtype, $dip,
					    $dmask, $gate, $cost, $rtabid);
1135
	    push(@{$downmap{$routearg}}, $rcline);
1136
1137
1138
1139
1140
	} else {
	    warn "*** WARNING: Bad routing line: $line\n";
	}
    }

1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
    print RC "case \"\$1\" in\n";
    foreach my $arg (keys(%upmap)) {
	print RC "  $arg)\n";
	print RC "    case \"\$2\" in\n";
	print RC "      up)\n";
	foreach my $rcline (@{$upmap{$arg}}) {
	    print RC "        $rcline\n";
	}
	print RC "      ;;\n";
	print RC "      down)\n";
	foreach my $rcline (@{$downmap{$arg}}) {
	    print RC "        $rcline\n";
	}
	print RC "      ;;\n";
	print RC "    esac\n";
	print RC "  ;;\n";
    }
    print RC "  enable)\n";

    #
    # Turn on IP forwarding
    #
1163
    print RC "    " . os_routing_enable_forward() . "\n";
1164

1165
1166
1167
1168
1169
1170
1171
    #
    # Finally, enable gated if desired.
    #
    # Note that we allow both manually-specified static routes and gated
    # though more work may be needed on the gated config files to make
    # this work (i.e., to import existing kernel routes).
    #
1172
1173
1174
1175
1176
    # XXX if rtabid is set, we are setting up routing from outside a
    # jail on behalf of a jail.  We don't want to enable gated in this
    # case, it will be run inside the jail.
    #
    if ($usegated && !defined($rtabid)) {
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
	print RC "    " . gatedsetup() . "\n";
    }
    print RC "  ;;\n";

    #
    # For convenience, allup and alldown.
    #
    print RC "  enable-routes)\n";
    foreach my $arg (keys(%upmap)) {
	foreach my $rcline (@{$upmap{$arg}}) {
	    print RC "    $rcline\n";
	}
    }
    print RC "  ;;\n";
    
    print RC "  disable-routes)\n";
    foreach my $arg (keys(%downmap)) {
	foreach my $rcline (@{$downmap{$arg}}) {
	    print RC "    $rcline\n";
	}
1197
    }
1198
1199
1200
    print RC "  ;;\n";
    print RC "esac\n";
    print RC "exit 0\n";
1201

1202
    close(RC);
1203
    chmod(0755, TMROUTECONFIG);
1204

1205
    return @routes;
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
sub gatedsetup ()
{
    my ($cnet, @xifs) = split('\n', `cat $BOOTDIR/tmcc.ifs`);

    open(IFS, ">" . TMGATEDCONFIG)
	or die("Could not open " . TMGATEDCONFIG . ": $!");

    print IFS "# auto-generated by libsetup.pm, DO NOT EDIT\n\n";
    #
    # XXX hack: in a jail, the control net is an IP alias with a host mask.
    # This blows gated out of the water, so we have to make the control
    # interface appear to have a subnet mask.
    #
    if (JAILED() && -e "$BOOTDIR/myip") {
	my $hostip = `cat $BOOTDIR/myip`;
	chomp($hostip);
	print IFS "interfaces {\n".
	    "\tdefine subnet local $hostip netmask 255.240.0.0;\n};\n";
    }
    print IFS "smux off;\nrip off;\nospf on {\n";
    print IFS "\tbackbone {\n\t\tinterface $cnet { passive; };\n\t};\n";
    print IFS "\tarea 0.0.0.2 {\n\t\tauthtype none;\n";

    foreach my $xif (@xifs) {
	print IFS "\t\tinterface $xif { priority 1; };\n";
    }

    print IFS "\t};\n};\n";
    close(IFS);

    return os_routing_enable_gated(TMGATEDCONFIG);
}

1241
1242
1243
1244
1245
1246
#
# Host names configuration (/etc/hosts). 
#
sub dohostnames ()
{
    my $TM;
1247
    my $HTEMP = HOSTSFILE . ".new";
1248
1249

    #
1250
1251
1252
    # Note, we no longer start with the 'prototype' file here, because we have
    # to make up a localhost line that's properly qualified.
    #
1253
1254
    $TM = OPENTMCC(TMCCCMD_HOSTS);

1255
1256
    open(HOSTS, ">$HTEMP")
	or die("Could not open $HTEMP: $!");
1257

1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
    my $localaliases = "loghost";

    #
    # Find out our domain name, so that we can qualify the localhost entry
    #
    my $hostname = `hostname`;
    if ($hostname =~ /[^.]+\.(.+)/) {
	$localaliases .= " localhost.$1";
    }
    
    #
    # First, write a localhost line into the hosts file - we have to know the
    # domain to use here
    #
1272
1273
    print HOSTS os_etchosts_line("localhost", "127.0.0.1",
				 $localaliases), "\n";
1274

1275
1276
    #
    # Now convert each hostname into hosts file representation and write
1277
1278
1279
1280
1281
    # it to the hosts file. Note that ALIASES is for backwards compat.
    # Should go away at some point.
    #
    my $pat  = q(NAME=([-\w\.]+) IP=([0-9\.]*) ALIASES=\'([-\w\. ]*)\');
    
1282
    while (<$TM>) {
1283
1284
1285
1286
1287
1288
	if ($_ =~ /$pat/) {
	    my $name    = $1;
	    my $ip      = $2;
	    my $aliases = $3;
	    
	    my $hostline = os_etchosts_line($name, $ip, $aliases);
1289
1290
1291
1292
1293
1294
1295
	    
	    print HOSTS  "$hostline\n";
	}
	else {
	    warn "*** WARNING: Bad hosts line: $_";
	}
    }
1296
    CLOSETMCC($TM);