mkjail.pl 33.2 KB
Newer Older
1
2
3
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2004 University of Utah and the Flux Group.
5
6
# All rights reserved.
#
7
8
# Kernel, jail, netstat, route, ifconfig, ipfw, header files.
# 
9
10
11
12
13
use English;
use Getopt::Std;
use Fcntl;
use IO::Handle;
use Socket;
14
use Fcntl ':flock';
15

16
17
18
# Drag in path stuff so we can find emulab stuff. Also untaints path.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

19
use libsetup qw(REMOTE LOCALROOTFS TMTOPOMAP TBDebugTimeStamp);
20
use libtmcc;
21

22
23
24
25
26
27
28
29
30
31
32
33
34
#
# Questions:
#
# * Whats the hostname for the jail? Perhaps vnodename.emulab.net
# * What should /etc/resolv.conf look like?
# 

#
# Create a jailed environment. There are some stub files stored in
# /etc/jail that copied into the jail.
#
sub usage()
{
35
    print("Usage: mkjail.pl [-V] [-s] [-i <ipaddr>] [-p <pid>] ".
36
	  "[-h <hostname>] <vnodeid>\n");
37
38
    exit(-1);
}
39
my  $optlist = "Vi:p:e:sh:";
40
41
42
43
44
45
46

#
# Only real root can run this script.
#
if ($UID) {
    die("Must be root to run this script!\n");
}
47
system("sysctl jail.set_hostname_allowed=0 >/dev/null 2>&1");
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

#
# Catch ^C and exit with error. 
#
my $leaveme = 0;
sub handler ($) {
    my ($signame) = @_;
    
    $SIG{INT}  = 'IGNORE';
    $SIG{USR1} = 'IGNORE';
    $SIG{TERM} = 'IGNORE';
    $SIG{HUP}  = 'IGNORE';

    if ($signame eq 'USR1') {
	$leaveme = 1;
    }
    fatal("Caught a SIG${signame}! Killing the jail ...");
}
$SIG{INT}  = \&handler;
$SIG{USR1} = \&handler;
$SIG{HUP}  = \&handler;
$SIG{TERM} = 'IGNORE';

#
# Turn off line buffering on output
#
STDOUT->autoflush(1);
STDERR->autoflush(1);

77
78
79
80
# XXX
my $JAILCNET	   = "172.16.0.0";
my $JAILCNETMASK   = "255.240.0.0";

81
82
83
84
85
86
87
88
#
# XXX hack: use loopback NFS mounts rather than nullfs loopback mounts
# which are still buggy.  Can do either for one or both of local
# filesystems (ROOTMNTDIRS) or remote filesystems (/proj, /users).
#
my $NFSMOUNT_LOCAL  = 1;
my $NFSMOUNT_REMOTE = 0;	# too much load on NFS server

89
90
91
92
#
# Locals
#
my $JAILPATH	= "/var/emulab/jails";
93
my $ETCJAIL     = "/etc/jail";
94
95
my $DEVCACHEMEM = "$JAILPATH/devmem";
my $DEVCACHENMEM= "$JAILPATH/devnomem";
96
my $LOCALFS	= LOCALROOTFS();
97
98
99
my $LOCALMNTPNT = "/local";
my $TMCC	= "$BINDIR/tmcc";
my $JAILCONFIG  = "jailconfig";
100
my @ROOTCPDIRS	= ("etc", "root");
101
102
103
my @ROOTMKDIRS  = ("dev", "tmp", "var", "usr", "proc", "users", 
		   "bin", "sbin", "home", $LOCALMNTPNT);
my @ROOTMNTDIRS = ("bin", "sbin", "usr");
104
my @EMUVARDIRS	= ("logs", "db", "jails", "boot", "lock");
105
my $VNFILESECT  = 512 * ((1024 * 1024) / 512); # 64MB in 512b sectors.
106
my $MAXVNDEVS	= 100;
107
my $IP;
108
109
my $IPALIAS;
my $IPMASK;
110
my $PID;
111
my $idnumber;
112
my $jailhostname;
113
114
115
116
117
118
119
my $debug	= 1;
my $cleaning	= 0;
my $vndevice;
my @mntpoints   = ();
my $jailpid;
my $tmccpid;
my $interactive = 0;
120
my $USEVCNETROUTES = 0;
121
my @controlroutes = ();
122
123
124

# This stuff is passed from tmcd, which we parse into a config string
# and an option set.
125
my %jailconfig  = ();
126
my $jailoptions = "";
127
my $sshdport    = 50000;	# Bogus default, good for testing.
128
my $routetabid;			# Default to main routing table.
129
my $jailflags   = 0;
130
131
132
my @jailips     = ();		# List of jail IPs (for routing table).
my $JAIL_DEVMEM = 0x01;		# We need to know if these options given.
my $JAIL_ROUTING= 0x02;
133
134
135
136
137
138
139
140
141
142
143
144

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (@ARGV != 1) {
    usage();
}
145
my $vnodeid = $ARGV[0];
146
147
148
149

#
# Untaint the arguments.
#
150
151
152
if ($vnodeid =~ /^([-\w\/\.]+)$/) {
    $vnodeid = $1;
    $jailhostname = $1;
153
154
}
else {
155
    die("Tainted argument $vnodeid!\n");
156
157
158
159
160
161
}

if (defined($options{'s'})) {
    $interactive = 1;
}

162
163
164
165
if (defined($options{'V'})) {
    $USEVCNETROUTES = 1;
}

166
167
168
169
170
171
172
173
174
#
# If the vnodeid looks like what we expect (pcvmXXX-YYY) then pull out
# the YYY part and use that for the rtabid and the vn device number.
# Saves on searching and allocation. Need a better solution at some point.
#
if ($vnodeid =~ /^[\w]*-(\d+)$/) {
    $idnumber = $1;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
175
176
TBDebugTimeStamp("mkjail starting to do real work");

177
178
179
180
181
182
183
184
185
186
187
188
189
190
#
# Get the parent IP.
# 
my $hostname = `hostname`;
my $hostip;

# Untaint and strip newline.
if ($hostname =~ /^([-\w\.]+)$/) {
    $hostname = $1;

    my (undef,undef,undef,undef,@ipaddrs) = gethostbyname($hostname);
    $hostip = inet_ntoa($ipaddrs[0]);
}

191
#
192
193
# If no IP, then it defaults to our hostname's IP, *if* none is provided
# by the jail configuration file. That check is later. 
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# 
if (defined($options{'i'})) {
    $IP = $options{'i'};

    if ($IP =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/) {
	$IP = $1;
    }
    else {
	die("Tainted argument $IP!\n");
    }
}

if (defined($options{'p'})) {
    $PID = $options{'p'};

    if ($PID =~ /^([-\@\w]+)$/) {
	$PID = $1;
    }
    else {
	die("Tainted argument $PID.");
    }
}

217
218
219
220
221
222
223
224
225
226
227
if (defined($options{'h'})) {
    $jailhostname = $options{'h'};

    if ($jailhostname =~ /^([-\w\.]+)$/) {
	$jailhostname = $1;
    }
    else {
	die("Tainted argument $jailhostname.");
    }
}

228
#
229
# In most cases, the $vnodeid directory will have been created by the caller,
230
231
# and a config file possibly dropped in.
# When debugging, we have to create it here. 
232
233
234
235
# 
chdir($JAILPATH) or
    die("Could not chdir to $JAILPATH: $!\n");

236
237
238
if (! -e $vnodeid) {
    mkdir($vnodeid, 0770) or
	fatal("Could not mkdir $vnodeid in $JAILPATH: $!");
239
}
240
else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
241
    TBDebugTimeStamp("mkjail getting jail config");
242
    getjailconfig("$JAILPATH/$vnodeid");
243
244
}

245
246
247
my $phys_cnet_if = `control_interface`;
chomp($phys_cnet_if);

248
249
250
#
# See if special options supported, and if so setup args as directed.
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
251
TBDebugTimeStamp("mkjail setting jail options");
252
setjailoptions();
253

254
255
256
257
258
259
260
261
262
263
#
# If no IP set in the jail config, then use hostip.
#
if (!defined($IP)) {
    $IP = $hostip;
}

print("Setting up jail for $vnodeid using $IP\n")
    if ($debug);

264
265
266
#
# Create the "disk";
#
267
if (-e "$vnodeid/root") {
268
269
    #
    # Try to pick up where we left off.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
270
271
    #
    TBDebugTimeStamp("mkjail restoring root fs");
272
    restorerootfs("$JAILPATH/$vnodeid");
273
274
275
276
277
}
else {
    #
    # Create the root filesystem.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
278
    TBDebugTimeStamp("mkjail creating root fs");
279
    mkrootfs("$JAILPATH/$vnodeid");
280
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
281
TBDebugTimeStamp("mkjail done with root fs");
282

283
284
285
286
287
#
# Lets tell the caller it can proceed. 
#
system("touch $JAILPATH/$vnodeid/proceed");

288
289
#
# If the jail has its own IP, must insert the control network alias.
290
291
# We use a 255.255.255.255 netmask since there might be multiple
# virtual nodes from the same subnet on this node. 
292
293
#
if (defined($IPALIAS)) {
294
    setcnethostalias($IPALIAS);
295
296
}

297
298
299
300
301
302
303
#
# Add control routes if jail gets its own route table.
#
if ($jailflags & $JAIL_ROUTING) {
    addcontrolroutes();
}

304
305
306
307
#
# Set up jail interfaces. We pass it the rtabid as well, and the script
# does all the right stuff. 
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
308
TBDebugTimeStamp("mkjail doing ifsetup for jail");
309
310
311
mysystem("ifsetup -j $vnodeid ".
	 (($jailflags & $JAIL_ROUTING) ? "-r $routetabid " : "") .
	 "boot");
312

313
314
315
316
#
# Start the tmcc proxy. This path will be valid in both the outer
# environment and in the jail!
#
Leigh B. Stoller's avatar
Leigh B. Stoller committed
317
TBDebugTimeStamp("mkjail starting the tmcc proxy");
318
startproxy("$JAILPATH/$vnodeid");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319
TBDebugTimeStamp("mkjail tmcc proxy is running");
320
321
322
323
324
325
326
327

#
# Start the jail. We do it in a child so we can send a signal to the
# jailed process to force it to shutdown. The jail has to shut itself
# down.
#
$jailpid = fork();
if ($jailpid) {
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    #
    # We do not really care about the exit status of the jail. We want to catch
    # any children, which includes the tmcc proxy. If it dies, something has
    # gone wrong, and that appears to happen a lot. Lets restart it until we
    # figure out what is going wrong.
    #
    while (1) {
	my $kidpid = waitpid(-1, 0);

	if ($kidpid == $jailpid) {
	    undef($jailpid);
	    last;
	}
	if ($kidpid == $tmccpid) {
	    print("TMCC proxy exited with status $?. Restarting ...\n");
	    startproxy("$JAILPATH/$vnodeid");
	    next;
	}
	print("Unknown child $kidpid exited with status $?!\n");
    }
348
349
350
}
else {
    $SIG{TERM} = 'DEFAULT';
351
    $ENV{'TMCCVNODEID'} = $vnodeid;
352

Leigh B. Stoller's avatar
Leigh B. Stoller committed
353
354
    TBDebugTimeStamp("mkjail starting the jail");
    
355
356
    my $cmd = "jail $jailoptions $JAILPATH/$vnodeid/root $jailhostname $IP ".
	"/etc/jail/injail -v $vnodeid";
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
    if ($interactive) {
	$cmd .= " /bin/csh";
    }
    exec($cmd);
    die("*** $0:\n".
	"    exec failed to start the jail!\n");
}
	 
#
# Once we exit, cleanup the mess.
#
cleanup();
exit(0);

#
# Create a file for a vnode device, vnconfig it, newfs, and then
# mount it on the "root" directory.
#
sub mkrootfs($)
{
    my ($path) = @_;
378
    my $vnsize = $VNFILESECT;
379
380
381
382
383
384
385
386

    chdir($path) or
	fatal("Could not chdir to $path: $!");

    mkdir("root", 0770) or
	fatal("Could not mkdir 'root' in $path: $!");
    
    #
387
    # Lots of zeros. Note we oseek to create a big hole.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
388
389
    #
    TBDebugTimeStamp("mkjail doing the dd");
390
    mysystem("dd if=/dev/zero of=root.vnode bs=512 oseek=$vnsize count=1");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
391
    TBDebugTimeStamp("mkjail done with the dd");
392
393

    #
394
395
    # Find a free vndevice. We start with the obvious choice, and if that
    # fails, fall back to searching.
396
    #
397
398
399
400
401
402
403
404
405
    if (defined($idnumber) && CheckFreeVN($idnumber, "root.vnode")) {
	$vndevice = $idnumber;
    }
    else {
	for (my $i = 0; $i < $MAXVNDEVS; $i++) {
	    if (CheckFreeVN($i, "root.vnode")) {
		$vndevice = $i;
		last;
	    }
406
407
408
409
	}
    }
    fatal("Could not find a free vn device!") 
	if (!defined($vndevice));
410
411
    print("Using vn${vndevice}\n")
 	if ($debug);
412

Leigh B. Stoller's avatar
Leigh B. Stoller committed
413
    TBDebugTimeStamp("mkjail vnconfig, label, newfs, tunefs, mount");
414
    mysystem("vnconfig -s labels vn${vndevice} root.vnode");
415
    mysystem("disklabel -r -w vn${vndevice} auto");
416
417
    mysystem("newfs -U -b 8192 -f 1024 -i 16384 -c 15 /dev/vn${vndevice}c");
    mysystem("tunefs -m 5 -o time /dev/vn${vndevice}c");
418
    mysystem("mount /dev/vn${vndevice}c root");
Leigh B. Stoller's avatar
Leigh B. Stoller committed
419
    TBDebugTimeStamp("mkjail vnconfig, label, newfs, tunefs, mount done!");
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
    push(@mntpoints, "$path/root");

    #
    # Okay, copy in the top level directories. 
    #
    foreach my $dir (@ROOTCPDIRS) {
	mysystem("hier -f -FN cp /$dir root/$dir");
    }

    #
    # Make some other directories that are nice to have!
    #
    foreach my $dir (@ROOTMKDIRS) {
	mkdir("root/$dir", 0755) or
	    fatal("Could not mkdir '$dir' in $path/root: $!");
    }

    #
    # Okay, mount some other directories to save space.
    #
    foreach my $dir (@ROOTMNTDIRS) {
441
	if ($NFSMOUNT_LOCAL) {
442
443
444
445
	    mysystem("mount -r localhost:/$dir $path/root/$dir");
	} else {
	    mysystem("mount -r -t null /$dir $path/root/$dir");
	}
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
	push(@mntpoints, "$path/root/$dir");
    }

    #
    # The proc FS in the jail is per-jail of course.
    # 
    mysystem("mount -t procfs proc $path/root/proc");
    push(@mntpoints, "$path/root/proc");

    #
    # /tmp is special of course
    #
    mysystem("chmod 1777 root/tmp");

    #
    # /dev is also special. It gets a very restricted set of entries.
462
    # Note that we create some BPF devices since they work in our jails.
463
    #
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
    # Set up the dev directory cache; running MAKEDEV is incredibly slow,
    # but copying a directory device entries is really fast!
    #
    if (! -e $DEVCACHEMEM) {
	TBDebugTimeStamp("mkjail creating /dev cached copy");
	
	mkdir($DEVCACHEMEM, 0755) or
	    fatal("Could not mkdir $DEVCACHEMEM: $!");
	mkdir($DEVCACHENMEM, 0755) or
	    fatal("Could not mkdir $DEVCACHENMEM: $!");

	# Jails with JAIL_DEVMEM get more devices.
	mysystem("cd $DEVCACHEMEM; cp -p /dev/MAKEDEV .; ".
		 " ./MAKEDEV bpf31 std pty0; " .
		 # Remove some extraneous devices.
		 "rm -f pci io klog console; " .
		 #
		 # "Link" /dev/console to /dev/null We actually create a
		 # separate special file rather than just linking in case
		 # anyone mucks with the permissions on /dev/console.
		 #
		 "hier cp null console; chmod 600 console");

	# Otherwise no devmem.
	mysystem("cd $DEVCACHENMEM; cp -p /dev/MAKEDEV .; ".
		 " ./MAKEDEV bpf31 jail; " .
		 # Remove some extraneous devices.
		 "rm -f pci io klog console; " .
		 #
		 # "Link" /dev/console to /dev/null We actually create a
		 # separate special file rather than just linking in case
		 # anyone mucks with the permissions on /dev/console.
		 #
		 "hier cp null console; chmod 600 console");
    }

    #
    # Now copy contents of cached device directory.
    # 
    TBDebugTimeStamp("mkjail copying cached /dev directory");
    
505
    if ($jailflags & $JAIL_DEVMEM) {
506
	mysystem("hier cp $DEVCACHEMEM $path/root/dev");
507
508
    }
    else {
509
	mysystem("hier cp $DEVCACHENMEM $path/root/dev");
510
    }
511
512
513
514
515
516
    
    #
    # Create stub /var and create the necessary log files.
    #
    # NOTE: I stole this little diddy from /etc/rc.diskless2.
    #
Leigh B. Stoller's avatar
Leigh B. Stoller committed
517
518
    TBDebugTimeStamp("mkjail creating /var");
    
519
520
521
522
    mysystem("mtree -nqdeU -f /etc/mtree/BSD.var.dist ".
	     "-p $path/root/var >/dev/null 2>&1");
    mysystem("mkdir -p $path/root/$path");

523
524
525
526
527
528
529
530
531
532
533
534
535
    #
    # Make the emulab directories since they are not in the mtree file.
    #
    if (! -e "$path/root/var/emulab") {
	mkdir("$path/root/var/emulab", 0755) or
	    fatal("Could not mkdir 'emulab' in $path/root/var: $!");
    }
    foreach my $dir (@EMUVARDIRS) {
	if (! -e "$path/root/var/emulab/$dir") {
	    mkdir("$path/root/var/emulab/$dir", 0755) or
		fatal("Could not mkdir 'dir' in $path/root/var/emulab: $!");
	}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
536
537
    TBDebugTimeStamp("mkjail copying the tmcc cache");
    
538
    tmcccopycache($vnodeid, "$path/root");
539
540
541
    if (-e TMTOPOMAP()) {
	mysystem("cp -fp " . TMTOPOMAP() . " $path/root/var/emulab/boot");
    }
542

543
544
545
546
    #
    # Stash the control net IP if not the same as the host IP
    #
    if ($IP ne $hostip) {
547
	mysystem("echo $IP > $path/root/var/emulab/boot/myip");
548
549
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
550
551
    TBDebugTimeStamp("mkjail creating /var/log files");

552
553
554
555
556
557
558
559
560
561
562
563
564
    #
    # Get a list of all the plain files and create zero length versions
    # in the new var.
    #
    opendir(DIR, "/var/log") or
	fatal("Cannot opendir /var/log: $!");
    my @logs = grep { -f "/var/log/$_" } readdir(DIR);
    closedir(DIR);

    foreach my $log (@logs) {
	mysystem("touch $path/root/var/log/$log");
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
565
566
    TBDebugTimeStamp("mkjail configuring etc files");

567
568
569
    #
    # Now a bunch of stuff to set up a nice environment in the jail.
    #
570
571
572
    mysystem("cp -p $ETCJAIL/rc.conf $path/root/etc");
    mysystem("rm -f $path/root/etc/rc.conf.local");
    mysystem("cp -p $ETCJAIL/rc.local $path/root/etc");
573
    mysystem("cp -p $ETCJAIL/crontab $path/root/etc");
574
    mysystem("cp /dev/null $path/root/etc/fstab");
575
    mysystem("ln -s /usr/compat $path/root/compat");
576
577
578
579
580
581

    # No X11 forwarding. 
    mysystem("cat $path/root/etc/ssh/sshd_config | ".
	     "sed -e 's/^X11Forwarding.*yes/X11Forwarding no/' > ".
	     "$path/root/tmp/sshd_foo");
    mysystem("cp -f $path/root/tmp/sshd_foo $path/root/etc/ssh/sshd_config");
582
583
584
585
586
587
588
589
590
591
    
    # Port/Address for sshd.
    if ($IP ne $hostip) {
	mysystem("echo 'ListenAddress $IP' >> ".
		 "$path/root/etc/ssh/sshd_config");
    }
    else {
	mysystem("echo 'sshd_flags=\"\$sshd_flags -p $sshdport\"' >> ".
		 "$path/root/etc/rc.conf");
    }
592
593
594
595
596
597

    # In the jail, 127.0.0.1 refers to the jail, but we want to use the
    # nameserver running *outside* the jail.
    mysystem("cat /etc/resolv.conf | ".
	     "sed -e 's/127\.0\.0\.1/$hostip/' > ".
	     "$path/root/etc/resolv.conf");
598

Leigh B. Stoller's avatar
Leigh B. Stoller committed
599
600
    TBDebugTimeStamp("mkjail setting up password/group files");

601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
    #
    # Password/group file stuff. If remote the jail picks them up. Locally
    # we start with current set of accounts on the physnode, since this is
    # more efficient (less tmcd work), and besides, the physnode starts with
    # exactly the same accounts.
    #
    if (REMOTE()) {
	mysystem("cp -p $ETCJAIL/group $path/root/etc");
	mysystem("cp -p $ETCJAIL/master.passwd $path/root/etc");
	mysystem("pwd_mkdb -p -d $path/root/etc $path/root/etc/master.passwd");
    }
    else {
	mysystem("cp -p /etc/group $path/root/etc");
	mysystem("cp -p /etc/master.passwd $path/root/etc");
	mysystem("pwd_mkdb -p -d $path/root/etc $path/root/etc/master.passwd");
616
617
618
	# XXX
	mysystem("cp -p $DBDIR/passdb.db $path/root/$DBDIR");
	mysystem("cp -p $DBDIR/groupdb.db $path/root/$DBDIR");
619
620
    }
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
621
622
    TBDebugTimeStamp("mkjail mounting NFS filesystems");
    
623
624
625
626
    #
    # Give the jail an NFS mount of the local project directory. This one
    # is read-write.
    #
627
628
    if (defined($PID) && -e $LOCALFS && -e "$LOCALFS/$PID") {
	mysystem("mkdir -p $path/root/$LOCALMNTPNT/$PID");
629
	if ($NFSMOUNT_REMOTE) {
630
631
632
633
634
635
	    mysystem("mount localhost:$LOCALFS/$PID ".
		     "      $path/root/$LOCALMNTPNT/$PID");
	} else {
	    mysystem("mount -t null $LOCALFS/$PID ".
		     "      $path/root/$LOCALMNTPNT/$PID");
	}
636
	push(@mntpoints, "$path/root/$LOCALMNTPNT/$PID");
637

638
639
#	mysystem("ln -s $LOCALMNTPNT/$PID/$vnodeid $path/root/opt");
	mkdir("$path/root/opt", 0775);
640
641
642
    }
    else {
	mkdir("$path/root/opt", 0775);
643
644
    }

645
646
647
648
649
650
    #
    # Ug. Until we have SFS working the way I want it, NFS mount the
    # usual directories inside the jail. This duplicates a lot of mounts,
    # but not sure what to do about that. 
    #
    if (! REMOTE()) {
651
652
	mysystem("$BINDIR/rc/rc.mounts ".
		 "-j $vnodeid $path/root $NFSMOUNT_REMOTE boot");
653
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
654
    TBDebugTimeStamp("mkjail mounting NFS filesystems done");
655

656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
    cleanmess($path);
    return 0;
}

#
# Restore a jail after a crash.
#
sub restorerootfs($)
{
    my ($path) = @_;

    chdir($path) or
	fatal("Could not chdir to $path: $!");

    #
671
672
    # Find a free vndevice. We start with the obvious choice, and if that
    # fails, fall back to searching.
673
    #
674
675
676
677
678
679
680
681
682
    if (defined($idnumber) && CheckFreeVN($idnumber, "root.vnode")) {
	$vndevice = $idnumber;
    }
    else {
	for (my $i = 0; $i < $MAXVNDEVS; $i++) {
	    if (CheckFreeVN($i, "root.vnode")) {
		$vndevice = $i;
		last;
	    }
683
684
685
686
	}
    }
    fatal("Could not find a free vn device!") 
	if (!defined($vndevice));
687
688
    print("Using vn${vndevice}\n")
 	if ($debug);
689

690
    mysystem("vnconfig -s labels vn${vndevice} root.vnode");
691
    mysystem("fsck -p -y /dev/vn${vndevice}c");
692
693
694
695
696
697
698
    mysystem("mount /dev/vn${vndevice}c root");
    push(@mntpoints, "$path/root");

    #
    # Okay, mount some other directories to save space.
    #
    foreach my $dir (@ROOTMNTDIRS) {
699
	if ($NFSMOUNT_LOCAL) {
700
701
702
703
	    mysystem("mount -r localhost:/$dir $path/root/$dir");
	} else {
	    mysystem("mount -r -t null /$dir $path/root/$dir");
	}
704
705
	push(@mntpoints, "$path/root/$dir");
    }
706

707
    tmcccopycache($vnodeid, "$path/root");
708
709
710
    if (-e TMTOPOMAP()) {
	mysystem("cp -fp " . TMTOPOMAP() . " $path/root/var/emulab/boot");
    }
711
712
713
714
715
716
717

    #
    # The proc FS in the jail is per-jail of course.
    # 
    mysystem("mount -t procfs proc $path/root/proc");
    push(@mntpoints, "$path/root/proc");

718
    #
719
    # Stash the control net IP if not the same as the host IP
720
    #
721
    if ($IP ne $hostip) {
722
	mysystem("echo $IP > $path/root/var/emulab/boot/myip");
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
    }

    # No X11 forwarding. 
    mysystem("cat $path/root/etc/ssh/sshd_config | ".
	     "sed -e 's/^X11Forwarding.*yes/X11Forwarding no/' > ".
	     "$path/root/tmp/sshd_foo");
    mysystem("cp -f $path/root/tmp/sshd_foo $path/root/etc/ssh/sshd_config");
    
    # Port/Address for sshd.
    if ($IP ne $hostip) {
	mysystem("echo 'ListenAddress $IP' >> ".
		 "$path/root/etc/ssh/sshd_config");
    }
    else {
	mysystem("echo 'sshd_flags=\"\$sshd_flags -p $sshdport\"' >> ".
		 "$path/root/etc/rc.conf");
739
740
    }

741
742
743
744
    #
    # Give the jail an NFS mount of the local project directory. This one
    # is read-write.
    #
745
746
    if (defined($PID) && -e $LOCALFS && -e "$LOCALFS/$PID") {
	mysystem("mkdir -p $path/root/$LOCALMNTPNT/$PID");
747
	if ($NFSMOUNT_REMOTE) {
748
749
750
751
752
753
	    mysystem("mount localhost:$LOCALFS/$PID ".
		     "      $path/root/$LOCALMNTPNT/$PID");
	} else {
	    mysystem("mount -t null $LOCALFS/$PID ".
		     "      $path/root/$LOCALMNTPNT/$PID");
	}
754
	push(@mntpoints, "$path/root/$LOCALMNTPNT/$PID");
755
    }
756
757
758
759
760
761
762

    #
    # Ug. Until we have SFS working the way I want it, NFS mount the
    # usual directories inside the jail. This duplicates a lot of mounts,
    # but not sure what to do about that. 
    #
    if (! REMOTE()) {
763
764
	mysystem("$BINDIR/rc/rc.mounts ".
		 "-j $vnodeid $path/root $NFSMOUNT_REMOTE boot");
765
    }
766
767
768
769
    return 0;
}

#
770
771
# Okay, we clean up some of what is in /etc and /etc/emulab so that the
# jail cannot see that stuff. 
772
773
774
775
776
777
778
779
780
781
782
#
sub cleanmess($) {
    my ($path) = @_;

    #
    # And, some security stuff. We want to remove bits of stuff from the
    # jail that would enable it to talk to tmcd directly. 
    #
    mysystem("rm -f $path/root/etc/emulab.cdkey");
    mysystem("rm -f $path/root/etc/emulab.pkey");

783
    # This is /etc/emulab inside the jail.
784
785
786
787
    mysystem("rm -f  $path/root/$ETCDIR/*.pem");
    mysystem("rm -f  $path/root/$ETCDIR/cvsup.auth");
    mysystem("rm -rf $path/root/$ETCDIR/.cvsup");
    mysystem("rm -f  $path/root/$ETCDIR/master.passwd");
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820

    #
    # Copy in emulabman if it exists.
    #
    if (my (undef,undef,undef,undef,undef,undef,undef,$dir) =
	getpwnam("emulabman")) {
	mysystem("hier cp $dir $path/root/home/emulabman");
    }
}

#
# Start the tmcc proxy and insert the unix path into the environment
# for the jail to pick up.
#
sub startproxy($)
{
    my ($dir) = @_;
    my $log   = "$dir/tmcc.log";

    #
    # The point of these paths is so that there is a comman path to
    # the socket both outside and inside the jail. Yuck!
    #
    my $insidepath  = "$dir/tmcc";
    my $outsidepath = "$dir/root/$insidepath";

    $tmccpid = fork();
    if ($tmccpid) {
	#
	# So tmcc will work nicely inside the jail without needing the
	# -l option specified all over.
	#
	$ENV{'TMCCUNIXPATH'} = $insidepath;
821
822
823
824
825
826
827

	#
	# The above is good only for direct decendents of the init process.
	# So, the above will be phased out in favor of a file in the usual
        # place to clue the client in. 
	#
	mysystem("echo $insidepath > $dir/root/${BOOTDIR}/proxypath");
828
829
830
831
832
833
834
835
	
	select(undef, undef, undef, 0.2);
	return 0;
    }
    $SIG{TERM} = 'DEFAULT';

    # The -o option will cause the proxy to detach but not fork!
    # Eventually change this to standard pid file kill.
Mike Hibler's avatar
Mike Hibler committed
836
    exec("$TMCC -d -x $outsidepath -n $vnodeid -o $log");
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
    die("Exec of $TMCC failed! $!\n");
}

#
# Cleanup at exit.
#
sub cleanup()
{
    if ($cleaning) {
	die("*** $0:\n".
	    "    Oops, already cleaning!\n");
    }
    $cleaning = 1;
    
    #
    # Note that the unmounts will fail unless all the processes inside
    # the jail exit!
    # 
    if (defined($tmccpid)) {
	kill('TERM', $tmccpid);
	waitpid($tmccpid, 0);
    }

    if (defined($jailpid)) {
861
	kill('USR1', $jailpid);
862
863
864
	waitpid($jailpid, 0);
    }

865
    # Clean up interfaces we might have setup
866
    system("ifsetup -j $vnodeid shutdown");
867
868
869

    # If the jail has its own IP, clean the alias.
    if (defined($IPALIAS)) {
870
	clearcnethostalias($IPALIAS);
871
872
    }

873
874
875
876
877
878
879
880
881
882
    # Clean control routes
    foreach my $route (reverse(@controlroutes)) {
	print("deleting route: '$route'\n");
	system("route delete -rtabid $routetabid $route");
    }

    # Unmounts.
    if (!REMOTE()) {
	# If this fails, fail, we do want to continue. Dangerous!
	system("$BINDIR/rc/rc.mounts ".
883
	       "-j $vnodeid foo $NFSMOUNT_REMOTE shutdown");
884
885
886
887
888
889
890
891

	if ($?) {
	    # Avoid recursive calls to cleanup; do not use fatal.
	    die("*** $0:\n".
		"    rc.mounts failed!\n");
	}
    }

892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
    while (@mntpoints) {
	my $mntpoint = pop(@mntpoints);

	# If the umounts fail, we do want to continue. Dangerous!
	system("umount $mntpoint");
	if ($?) {
	    # Avoid recursive calls to cleanup; do not use fatal.
	    die("*** $0:\n".
		"    umount '$mntpoint' failed: $!\n");
	}
    }
    if (defined($vndevice)) {
	system("vnconfig -u vn${vndevice}");
    }
    if (!$leaveme) {
907
908
909
        #
        # Ug, with NFS mounts inside the jail, we need to be really careful.
        #
910
911
	if (-d "$JAILPATH/$vnodeid/root" &&
	    !rmdir("$JAILPATH/$vnodeid/root")) {
912
	    die("*** $0:\n".
913
		"    $JAILPATH/$vnodeid/root is not empty! This is bad!\n");
914
	}
915
	system("rm -rf $JAILPATH/$vnodeid");
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
    }
}

#
# Print error and exit.
#
sub fatal($)
{
    my ($msg) = @_;

    cleanup();
    die("*** $0:\n".
	"    $msg\n");
}

#
# Run a command string, redirecting output to a logfile.
#
sub mysystem($)
{
    my ($command) = @_;

    system($command);
    if ($?) {
	fatal("Command failed: $? - $command");
    }
}
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974

#
# Read in the jail config file. 
#
sub getjailconfig($)
{
    my ($path) = @_;

    $path .= "/$JAILCONFIG";

    if (! -e $path) {
	return 0;
    }
    
    if (! open(CONFIG, $path)) {
	print("$path could not be opened for reading: $!\n");
	return -1;
    }
    while (<CONFIG>) {
	if ($_ =~ /^(.*)="(.+)"$/ ||
	    $_ =~ /^(.*)=(.+)$/) {
	    $jailconfig{$1} = $2;
	}
    }
    close(CONFIG);
    return 0;
}

#
# See if special jail opts supported.
#
sub setjailoptions() {
975
976
    my $portrange;
    
977
978
979
980
981
982
983
    #
    # Do this all the time, so that we can figure out the sshd port.
    # 
    foreach my $key (keys(%jailconfig)) {
	my $val = $jailconfig{$key};

        SWITCH: for ($key) {
984
985
986
987
988
989
990
991
992
993
994
995
	    /^JAILIP$/ && do {
		if ($val =~ /([\d\.]+),([\d\.]+)/) {
		    # Set the jail IP, but do not override one on the 
		    # command line.
		    if (!defined($IP)) {
			$IP      = $1;
			$IPALIAS = $1;
			$IPMASK  = $2;
		    }
		}
		last SWITCH;
	    };
996
997
	    /^PORTRANGE$/ && do {
		if ($val =~ /(\d+),(\d+)/) {
998
		    $portrange = "$1:$2";
999
1000
1001
1002
1003
		}
		last SWITCH;
	    };
	    /^SSHDPORT$/ && do {
		if ($val =~ /(\d+)/) {
1004
1005
1006
1007
1008
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
		    $sshdport     = $1;
		}
		last SWITCH;
	    };
	    /^SYSVIPC$/ && do {
		if ($val) {
		    $jailoptions .= " -o sysvipc";
		}
		else {
		    $jailoptions .= " -o nosysvipc";
		}
		last SWITCH;
	    };
	    /^INETRAW$/ && do {
		if ($val) {
		    $jailoptions .= " -o inetraw";
		}
		else {
		    $jailoptions .= " -o noinetraw";
		}
		last SWITCH;
	    };
	    /^BPFRO$/ && do {
		if ($val) {
		    $jailoptions .= " -o bpfro";
		}
		else {
		    $jailoptions .= " -o nobpfro";
		}
		last SWITCH;
	    };
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
	    /^INADDRANY$/ && do {
		if ($val) {
		    $jailoptions .= " -o inaddrany";
		}
		else {
		    $jailoptions .= " -o noinaddrany";
		}
		last SWITCH;
	    };
	    /^ROUTING$/ && do {
		if ($val) {
1046
		    $jailoptions .= " -o routing -i 127.0.0.1 ";
1047
1048
1049
1050

		    $jailflags |= $JAIL_ROUTING;
		    #
		    # If the jail gets routing privs, then it must get
1051
		    # its own routing table. 
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
		    #
		    $routetabid   = getnextrtabid();
		    $jailoptions .= " -r $routetabid";
		}
		last SWITCH;
	    };
	    /^DEVMEM$/ && do {
		$jailflags |= $JAIL_DEVMEM;
		last SWITCH;
	    };
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1062
 	    /^IPADDRS$/ && do {
1063
1064
1065
1066
1067
1068
		# Comma separated list of IPs
		my @iplist = split(",", $val);

		foreach my $ip (@iplist) {
		    if ($ip =~ /(\d+\.\d+\.\d+\.\d+)/) {
			$jailoptions .= " -i $1";
1069
			push(@jailips, $1);
1070
1071
1072
1073
		    }
		}
 		last SWITCH;
 	    };
1074
1075
	}
    }
1076
1077
1078
1079
    #
    # If there is no IP for the jail, must restrict the port range of
    # it. Otherwise it has it own IP, and there is no need to.
    # 
1080
1081
1082
1083
1084
1085
1086
1087
1088
    if (! defined($IPALIAS)) {
	if (defined($portrange)) {
	    $jailoptions .= " -p $portrange";
	}
	else {
	    fatal("Must have a port range if jail ip equals host ip!");
	}
	print("SSHD port is $sshdport\n");
    }
1089
1090
1091

    system("sysctl jail.inetraw_allowed=1 >/dev/null 2>&1");
    system("sysctl jail.bpf_allowed=1 >/dev/null 2>&1");
1092
1093
1094
1095
    system("sysctl jail.inaddrany_allowed=1 >/dev/null 2>&1");
    system("sysctl jail.multiip_allowed=1 >/dev/null 2>&1");
    system("sysctl net.link.ether.inet.useloopback=0 >/dev/null 2>&1");

1096
1097
1098
    if ($?) {
	print("Special jail options are NOT supported!\n");
	$jailoptions = "";
1099
	$jailflags   = 0;
1100
1101
1102
	return 0;
    }
    print("Special jail options are supported: '$jailoptions'\n");
1103
1104
1105
1106
1107

    return 0;
}

#
1108
1109
# Append a list of static routes to the rc.conf file. This basically
# augments the list of static routes that tmcd tells us to install. 
1110
#
1111
sub addcontrolroutes()
1112
1113
1114
1115
{
    my ($rc)   = @_;
    my $count  = 0;

1116
1117
1118
1119
1120
    #
    # XXX use address for localhost.  For some reason the route command is
    # going to the DNS before using the hosts file, despite the host.conf
    # file.
    # 
1121
    my $routerip  = getcnetrouter($USEVCNETROUTES);
1122
    my $localhost = "127.0.0.1";
1123
    my $hostip    = `cat $BOOTDIR/myip`;
1124
1125
    chomp($hostip);

1126
1127
1128
    push(@controlroutes, "default $routerip");
    push(@controlroutes, "$localhost -interface lo0");
    push(@controlroutes, "$hostip $localhost");
1129

1130
    if ($IP ne $hostip) {
1131
1132
	my $net;

1133
	#
1134
	# Setup a route for all jails on this node, to the loopback.
1135
	#
1136
1137
	$net = inet_ntoa(inet_aton($IP) & inet_aton("255.255.255.0"));
	push(@controlroutes, "-net $net -interface lo0 255.255.255.0");
1138

1139
1140
1141
	#
	# All other jails are reachable via the control net interface.
	#
1142
1143
1144
1145
1146
1147
1148
1149
	# N.B. privnet is setup first even before the default route.
	# This is because the gateway for the default route is on the
	# privnet, but our control net interface is not (because it has
	# a 255.255.255.255 netmask by virtue of being an alias on the
	# real control interface).  Thus there is no way to use the
	# default route until the privnet route is up (ARP complains
	# about "host is not on local network").
	#
1150
1151
1152
	$net = inet_ntoa(inet_aton($IP) & inet_aton($IPMASK));
	push(@controlroutes, "-net $net -interface $phys_cnet_if $IPMASK");
	
1153
1154
1155
1156
1157
	#
	# If using the virtual control net for routes, also make sure that
	# nodes are reachable with their real control net addresses directly.
	#
	if ($USEVCNETROUTES) {
1158
1159
1160
1161
1162
	    my $mask = getcnetmask(0);
	    
	    $net = inet_ntoa(inet_aton($hostip) & inet_aton($mask));
	    push(@controlroutes,
		 "-net $net -netmask $mask -interface $phys_cnet_if");
1163
	}
1164
    }
1165
1166

    #
1167
1168
1169
1170
1171
1172
1173
    # Now a list of routes for each of the IPs the jail has access
    # to. The idea here is to override the interface route such that
    # traffic to the local interface goes through lo0 instead. This
    # avoids going through traffic shaping when, say, pinging your own
    # interface!
    # 
    foreach my $ip (@jailips) {
1174
1175
1176
1177
1178
1179
1180
1181
1182
	push(@controlroutes, "$ip -interface lo0");
    }

    #
    # Now add them:
    #
    foreach my $route (@controlroutes) {
	print("Adding route: '$route'\n");
	mysystem("route add -rtabid $routetabid $route");
1183
    }
1184
1185
    return 0;
}
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208

#
# Ug, with NFS mounts inside the jail, we need to be really careful.
#
sub removevnodedir($)
{
    my ($dir) = @_;

    if (-d "$dir/root" && !rmdir("$dir/root")) {
	die("*** $0:\n".
	    "    $dir/root is not empty! This is very bad!\n");
    }
    system("rm -rf $dir");
}

#
# Get a free routing table ID. This should eventually come from the
# kernel, but for now just use a file with a number in it. 
#
sub getnextrtabid()
{
    my $nextrtabid = 1;

1209
1210
1211
1212
1213
1214
1215
1216
1217
    #
    # If the vnodeid is what we think (pcvmXXX-YYY) then just use the YYY
    # part. At some point we need better control of this from Emulab Central.
    #
    if (defined($idnumber)) {
	$nextrtabid = $idnumber;
	goto gotit;
    }

1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
    #
    # The chances of a race are low, but need to deal with it anyway.
    #
    my $lockfile  = "/var/emulab/lock/rtabid";
    my $rtabidfile= "/var/emulab/db/rtabid";

    open(LOCK, ">>$lockfile") ||
	fatal("Could not open $lockfile\n");
    
    while (flock(LOCK, LOCK_EX|LOCK_NB) == 0) {
	print "Waiting to get lock for $lockfile\n";
	sleep(1);
    }
    if (-e $rtabidfile) {
	my $rtabid = `cat $rtabidfile`;
	if ($rtabid =~ /^(\d*)$/) {
	    $nextrtabid = $1 + 1;
	}
	else {
	    close(LOCK);
	    fatal("Bad data in $rtabidfile!");
	}
    }
    system("echo $nextrtabid > $rtabidfile");
    close(LOCK);
1243
1244

  gotit:
1245
1246
1247
1248
1249
1250
    #
    # Flush that routing table so its pristine. Again, this should be
    # handled in the kernel.
    #
    system("route flush -rtabid $nextrtabid");
    
1251
1252
    return $nextrtabid;
}
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317

#
# Return the number of alias on the jailhost control interface
#
sub jailcnetaliases()
{
    my $count = 0;

    if (open(IFC, "ifconfig $phys_cnet_if |")) {
	while (<IFC>) {
	    if ($_ =~ /inet ([0-9\.]*) netmask 0xffffffff/) {
		my $host = $1;
		if (inet_ntoa(inet_aton($host) & inet_aton($JAILCNETMASK)) eq
		    $JAILCNET) {
		    $count++;
		}
	    }
	}
	close(IFC);
    }
    return $count;
}

sub setcnethostalias($)
{
    my ($vnodeip) = @_;

    mysystem("ifconfig $phys_cnet_if alias $vnodeip netmask 255.255.255.255");

    if (!$USEVCNETROUTES) {
	return;
    }

    #
    # If the jail's IP is part of the local virtual control net,
    # make sure the physical host has an alias on it as well.
    #
    # The convention is that .0 is the physical host alias.
    #
    my $cnalias = inet_aton($vnodeip);
    if ((inet_ntoa($cnalias & inet_aton($JAILCNETMASK)) eq $JAILCNET) &&
	jailcnetaliases() == 1) {
	my $palias = inet_ntoa($cnalias & inet_aton("255.255.255.0"));
	mysystem("ifconfig $phys_cnet_if alias $palias netmask $JAILCNETMASK");
    }
}

sub clearcnethostalias($)
{
    my ($vnodeip) = @_;

    system("ifconfig $phys_cnet_if -alias $vnodeip");

    if (!$USEVCNETROUTES) {
	return;
    }

    #
    # If the jail's IP is part of the local virtual control net,
    # and we were the last jail, remove the physical host's alias.
    #
    my $cnalias = inet_aton($vnodeip);
    if ((inet_ntoa($cnalias & inet_aton($JAILCNETMASK)) eq $JAILCNET) &&
	jailcnetaliases() == 0) {
	my $palias = inet_ntoa($cnalias & inet_aton("255.255.255.0"));
1318
	system("ifconfig $phys_cnet_if -alias $palias");
1319
1320
1321
    }
}

1322
sub getcnetrouter($)
1323
{
1324
1325
    my ($usevcnet) = @_;

1326
1327
    my $routerip;

1328
    if (!$usevcnet) {
1329
1330
1331
1332
1333
1334
1335
1336
1337
	$routerip = `cat $BOOTDIR/routerip`;
	chomp($routerip);
    } else {
	$routerip = inet_ntoa(inet_aton($JAILCNET) |
			      inet_aton("0.0.0.1"));
    }

    return $routerip;
}
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355

sub getcnetmask($)
{
    my ($usevcnet) = @_;

    my $cnetmask = "255.255.255.0";

    if (!$usevcnet) {
	if (-e "$BOOTDIR/mynetmask") {
	    $cnetmask = `cat $BOOTDIR/mynetmask`;
	    chomp($cnetmask);
	}
    } else {
	$cnetmask = $JAILCNETMASK;
    }

    return $cnetmask;
}
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371

#
# Check for free vn device.
#
sub CheckFreeVN($$)
{
    my ($vn, $file) = @_;
    
    # Make sure the dev entries exist!
    if (! -e "/dev/vn${vn}") {
	mysystem("(cd /dev; ./MAKEDEV vn${vn})");
    }
	
    system("vnconfig -c vn${vn} $file");
    return ($? ? 0 : 1);
}