Commit a8d81694 authored by Leigh B. Stoller's avatar Leigh B. Stoller
Browse files

A small set of changes to allow setting the hostname of the jail just

like we set hostnames of local nodes (v0.wa-jail-link.testbed.emulab.net).
Also build a proper /etc/hosts file just like local nodes inside the
jail. Minor change to dohostnames(); do not munge /etc/hosts in place!
Generate a new copy and then atomically move into place. Phew, this
was a close one! Mike has me really worried about race conditions now
that I screwed the pooch in the frisbee client.
parent 040f7156
......@@ -996,16 +996,16 @@ sub dorouterconfig ()
sub dohostnames ()
{
my $TM;
my $HTEMP = HOSTSFILE . ".new";
#
# Note, we no longer start with the 'prototype' file here, because we have
# to make up a localhost line that's properly qualified.
#
$TM = OPENTMCC(TMCCCMD_HOSTS);
open(HOSTS, ">" . HOSTSFILE)
or die("Could not open $HOSTSFILE: $!");
open(HOSTS, ">$HTEMP")
or die("Could not open $HTEMP: $!");
my $localaliases = "loghost";
......@@ -1021,7 +1021,8 @@ sub dohostnames ()
# First, write a localhost line into the hosts file - we have to know the
# domain to use here
#
print HOSTS os_etchosts_line("localhost", "127.0.0.1", $localaliases), "\n";
print HOSTS os_etchosts_line("localhost", "127.0.0.1",
$localaliases), "\n";
#
# Now convert each hostname into hosts file representation and write
......@@ -1046,7 +1047,8 @@ sub dohostnames ()
}
}
CLOSETMCC($TM);
close(HOSTS);
(close(HOSTS) and system("mv -f $HTEMP " . HOSTSFILE)) or
warn("*** Could not mv $HTEMP to ". HOSTSFILE . "!\n");
return 0;
}
......@@ -2037,10 +2039,10 @@ sub bootsetup()
sub jailedsetup()
{
#
# Currently, we rely on the outer environment to set our hostname
# to our vnodeid!
# Currently, we rely on the outer environment to set our vnodeid
# into the environment so we can get it! See mkjail.pl.
#
my $vid = `hostname`;
my $vid = $ENV{'TMCCVNODEID'};
if ($vid =~ /^([-\w]+)$/) {
$vid = $1;
......@@ -2056,10 +2058,10 @@ sub jailedsetup()
$injail = 1;
#
# Create a file inside so the rest of the libsetup code knows its
# inside a jail.
# Create a file inside so that libsetup inside the jail knows its
# inside a jail and what its ID is.
#
system("echo '$vid' > " . TMJAILNAME());
system("echo '$vnodeid' > " . TMJAILNAME());
#
# Do account stuff.
......@@ -2089,6 +2091,9 @@ sub jailedsetup()
print STDOUT "Checking Testbed ifconfig configuration ...\n";
dojailifconfig();
print STDOUT "Checking Testbed hostnames configuration ... \n";
dohostnames();
print STDOUT "Checking Testbed group/user configuration ... \n";
doaccounts();
......
......@@ -10,6 +10,11 @@ use English;
use Errno;
use POSIX qw(setsid);
#
# Configure Variables
#
my $OURDOMAIN = "@OURDOMAIN@";
# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }
......@@ -253,7 +258,7 @@ system("tmcc -n $vnodeid state TBSETUP");
# Invoke remotevnodesetup routine in the setup library. This will talk
# to tmcd and create the rc files.
#
my ($pid) = vnodesetup($vnodeid);
my ($pid, $eid, $vname) = vnodesetup($vnodeid);
if (!defined($pid)) {
#
# Hmm, suddenly got free.
......@@ -316,9 +321,10 @@ if ($dojail) {
fatal("Jail exited unexpectedly!");
}
else {
my $option = ($interactive ? "-s" : "");
my $option = ($interactive ? "-s" : "");
my $jailhostname = "$vname.$eid.$pid.$OURDOMAIN";
exec("mkjail.pl $option -p $pid $vnodeid");
exec("mkjail.pl $option -p $pid -h $jailhostname $vnodeid");
die("*** $0:\n".
" Could not start the jail!\n");
}
......
......@@ -29,10 +29,11 @@ use libsetup qw(JailedNFSMounts REMOTE);
#
sub usage()
{
print("Usage: mkjail.pl [-s] [-i <ipaddr>] [-p <pid>] <hostname>\n");
print("Usage: mkjail.pl [-s] [-i <ipaddr>] [-p <pid>] ".
"[-h <hostname>] <vnodeid>\n");
exit(-1);
}
my $optlist = "i:p:e:s";
my $optlist = "i:p:e:sh:";
#
# Only real root can run this script.
......@@ -88,6 +89,7 @@ my $VNFILEMBS = 64;
my $MAXVNDEVS = 10;
my $IP;
my $PID;
my $jailhostname;
my $debug = 1;
my $cleaning = 0;
my $vndevice;
......@@ -119,16 +121,17 @@ if (! getopts($optlist, \%options)) {
if (@ARGV != 1) {
usage();
}
my $HOST = $ARGV[0];
my $vnodeid = $ARGV[0];
#
# Untaint the arguments.
#
if ($HOST =~ /^([-\w\/]+)$/) {
$HOST = $1;
if ($vnodeid =~ /^([-\w\/\.]+)$/) {
$vnodeid = $1;
$jailhostname = $1;
}
else {
die("Tainted argument $HOST!\n");
die("Tainted argument $vnodeid!\n");
}
if (defined($options{'s'})) {
......@@ -180,23 +183,34 @@ if (defined($options{'p'})) {
}
}
print("Setting up jail for HOST:$HOST using IP:$IP\n")
if (defined($options{'h'})) {
$jailhostname = $options{'h'};
if ($jailhostname =~ /^([-\w\.]+)$/) {
$jailhostname = $1;
}
else {
die("Tainted argument $jailhostname.");
}
}
print("Setting up jail for $vnodeid using $IP\n")
if ($debug);
#
# In most cases, the $HOST directory will have been created by the caller,
# In most cases, the $vnodeid directory will have been created by the caller,
# and a config file possibly dropped in.
# When debugging, we have to create it here.
#
chdir($JAILPATH) or
die("Could not chdir to $JAILPATH: $!\n");
if (! -e $HOST) {
mkdir($HOST, 0770) or
fatal("Could not mkdir $HOST in $JAILPATH: $!");
if (! -e $vnodeid) {
mkdir($vnodeid, 0770) or
fatal("Could not mkdir $vnodeid in $JAILPATH: $!");
}
else {
getjailconfig("$JAILPATH/$HOST");
getjailconfig("$JAILPATH/$vnodeid");
}
#
......@@ -207,24 +221,24 @@ setjailoptions();
#
# Create the "disk";
#
if (-e "$HOST/root") {
if (-e "$vnodeid/root") {
#
# Try to pick up where we left off.
#
restorerootfs("$JAILPATH/$HOST");
restorerootfs("$JAILPATH/$vnodeid");
}
else {
#
# Create the root filesystem.
#
mkrootfs("$JAILPATH/$HOST");
mkrootfs("$JAILPATH/$vnodeid");
}
#
# Start the tmcc proxy. This path will be valid in both the outer
# environment and in the jail!
#
startproxy("$JAILPATH/$HOST");
startproxy("$JAILPATH/$vnodeid");
#
# Start the jail. We do it in a child so we can send a signal to the
......@@ -239,10 +253,10 @@ if ($jailpid) {
}
else {
$SIG{TERM} = 'DEFAULT';
$ENV{'TMCCVNODEID'} = $HOST;
$ENV{'TMCCVNODEID'} = $vnodeid;
my $cmd = "jail $jailoptions ".
"$JAILPATH/$HOST/root $HOST $IP /etc/jail/injail.pl";
"$JAILPATH/$vnodeid/root $jailhostname $IP /etc/jail/injail.pl";
if ($interactive) {
$cmd .= " /bin/csh";
}
......@@ -396,7 +410,6 @@ sub mkrootfs($)
mysystem("cp -p $ETCJAIL/master.passwd $path/root/etc");
mysystem("cp /dev/null $path/root/etc/fstab");
mysystem("pwd_mkdb -p -d $path/root/etc $path/root/etc/master.passwd");
mysystem("echo '$IP $HOST' >> $path/root/etc/hosts");
mysystem("echo 'sshd_flags=\"\$sshd_flags -p $sshdport\"' >> ".
" $path/root/etc/rc.conf");
......@@ -436,7 +449,7 @@ sub mkrootfs($)
# but not sure what to do about that.
#
if (! REMOTE()) {
foreach my $dir ( JailedNFSMounts($HOST, "$path/root") ) {
foreach my $dir ( JailedNFSMounts($vnodeid, "$path/root") ) {
push(@mntpoints, "$path/root/$dir");
}
}
......@@ -508,7 +521,7 @@ sub restorerootfs($)
# but not sure what to do about that.
#
if (! REMOTE()) {
foreach my $dir ( JailedNFSMounts($HOST, "$path/root") ) {
foreach my $dir ( JailedNFSMounts($vnodeid, "$path/root") ) {
push(@mntpoints, "$path/root/$dir");
}
}
......@@ -574,7 +587,7 @@ sub startproxy($)
# The -o option will cause the proxy to detach but not fork!
# Eventually change this to standard pid file kill.
exec("$TMCC -d -x $outsidepath -n $HOST -o $log");
exec("$TMCC -d -x $outsidepath -n $vnodeid -o $log");
die("Exec of $TMCC failed! $!\n");
}
......@@ -625,11 +638,12 @@ sub cleanup()
#
# Ug, with NFS mounts inside the jail, we need to be really careful.
#
if (-d "$JAILPATH/$HOST/root" && !rmdir("$JAILPATH/$HOST/root")) {
if (-d "$JAILPATH/$vnodeid/root" &&
!rmdir("$JAILPATH/$vnodeid/root")) {
die("*** $0:\n".
" $JAILPATH/$HOST/root is not empty! This is very bad!\n");
" $JAILPATH/$vnodeid/root is not empty! This is bad!\n");
}
system("rm -rf $JAILPATH/$HOST");
system("rm -rf $JAILPATH/$vnodeid");
}
}
......
......@@ -996,16 +996,16 @@ sub dorouterconfig ()
sub dohostnames ()
{
my $TM;
my $HTEMP = HOSTSFILE . ".new";
#
# Note, we no longer start with the 'prototype' file here, because we have
# to make up a localhost line that's properly qualified.
#
$TM = OPENTMCC(TMCCCMD_HOSTS);
open(HOSTS, ">" . HOSTSFILE)
or die("Could not open $HOSTSFILE: $!");
open(HOSTS, ">$HTEMP")
or die("Could not open $HTEMP: $!");
my $localaliases = "loghost";
......@@ -1021,7 +1021,8 @@ sub dohostnames ()
# First, write a localhost line into the hosts file - we have to know the
# domain to use here
#
print HOSTS os_etchosts_line("localhost", "127.0.0.1", $localaliases), "\n";
print HOSTS os_etchosts_line("localhost", "127.0.0.1",
$localaliases), "\n";
#
# Now convert each hostname into hosts file representation and write
......@@ -1046,7 +1047,8 @@ sub dohostnames ()
}
}
CLOSETMCC($TM);
close(HOSTS);
(close(HOSTS) and system("mv -f $HTEMP " . HOSTSFILE)) or
warn("*** Could not mv $HTEMP to ". HOSTSFILE . "!\n");
return 0;
}
......@@ -2037,10 +2039,10 @@ sub bootsetup()
sub jailedsetup()
{
#
# Currently, we rely on the outer environment to set our hostname
# to our vnodeid!
# Currently, we rely on the outer environment to set our vnodeid
# into the environment so we can get it! See mkjail.pl.
#
my $vid = `hostname`;
my $vid = $ENV{'TMCCVNODEID'};
if ($vid =~ /^([-\w]+)$/) {
$vid = $1;
......@@ -2056,10 +2058,10 @@ sub jailedsetup()
$injail = 1;
#
# Create a file inside so the rest of the libsetup code knows its
# inside a jail.
# Create a file inside so that libsetup inside the jail knows its
# inside a jail and what its ID is.
#
system("echo '$vid' > " . TMJAILNAME());
system("echo '$vnodeid' > " . TMJAILNAME());
#
# Do account stuff.
......@@ -2089,6 +2091,9 @@ sub jailedsetup()
print STDOUT "Checking Testbed ifconfig configuration ...\n";
dojailifconfig();
print STDOUT "Checking Testbed hostnames configuration ... \n";
dohostnames();
print STDOUT "Checking Testbed group/user configuration ... \n";
doaccounts();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment