Discussion:
Leveraging system hostname as part of a jail's hostname
Joseph Ward
2018-06-20 02:41:15 UTC
Permalink
Hi everyone,

I have several jails, configured via jail.conf, whose hostname I want to
make: $name.$system_hostname.

Is there a way to do this in jail.conf?   If I use:

host = inherit;

the hostnames of the jails all match the hostname of the system.  I
tried using:

host.hostname = $name;
host.domainname = inherit;

but the hostname ends up just being $name (expanded, of course).

Trying:

host = inherit;
host.hostname = $name;

ended up with simply $name as well, with the "inherit" ignored.

So, am I missing something? 


Thanks,

Joseph
James Gritton
2018-06-20 16:52:20 UTC
Permalink
Post by Joseph Ward
Hi everyone,
I have several jails, configured via jail.conf, whose hostname I want to
make: $name.$system_hostname.
host = inherit;
the hostnames of the jails all match the hostname of the system.  I
host.hostname = $name;
host.domainname = inherit;
but the hostname ends up just being $name (expanded, of course).
host = inherit;
host.hostname = $name;
ended up with simply $name as well, with the "inherit" ignored.
So, am I missing something? 
You can't do it with a simple substitution in a parameter setting, since
there's no way in the config file to read the current hostname. You
don't want "host = inherit", because that will cause all jails to use
the same hostname - if you change one, it changes all of them (and
changes the system hostname).

But you can do it in two steps, involving some fairly ugly hackery
around exec.poststart. Something like:

foo
{
exec.poststart = "jail -m name=foo host.hostname=$name.`hostname`";
exec.poststart += "jexec foo sleep 600";
}

Unfortunately the second exec.poststart line is required as a
replacement for the typical "exec.start='sh /etc/rc'" because
exec.poststart runs after exec.start and that's to late to set the
hostname. This is one of these cases that suggests the need for a
parameter run by the main host between jail creation and the running of
exec.start.

Another possible hack would be to do something with setting a file
inside the jail that's peeked at by the jail's rc.conf or rc.d/hostname.

Nothing pretty, I'm afraid.

- Jamie
Joseph Ward
2018-06-20 18:12:21 UTC
Permalink
Thank you for getting back to me!

I tested your solution, and using exec.poststart works perfectly.  I
didn't even need the sleep, however, and was able to leave the

exec.start = "/bin/sh /etc/rc";

alone, and it worked perfectly.  As the jail is running, the

jail -m name=$name host.hostname=$name.`hostname`"

did work, so I tried the following jail.conf and everything works
perfectly.


exec.start = "/bin/sh /etc/rc";
exec.poststart = "jail -m name=$name host.hostname=$name.`hostname`";
exec.stop = "/bin/sh /etc/rc.shutdown";
exec.clean;
mount.devfs;
db {
  host.hostname = "db";
  path = "/usr/local/jails/db";
  interface = "lo1";
  ip4.addr = 127.0.1.2;
  mount.fstab = "/usr/local/jails/db.fstab";
}

ldap {
  host.hostname = "ldap";
  path = "/usr/local/jails/ldap";
  interface = "lo1";
  ip4.addr = 127.0.1.3;
  mount.fstab = "/usr/local/jails/ldap.fstab";
}


Thank you!

-Joseph
Post by James Gritton
Post by Joseph Ward
Hi everyone,
I have several jails, configured via jail.conf, whose hostname I want to
make: $name.$system_hostname.
host = inherit;
the hostnames of the jails all match the hostname of the system.  I
host.hostname = $name;
host.domainname = inherit;
but the hostname ends up just being $name (expanded, of course).
host = inherit;
host.hostname = $name;
ended up with simply $name as well, with the "inherit" ignored.
So, am I missing something? 
You can't do it with a simple substitution in a parameter setting,
since there's no way in the config file to read the current hostname. 
You don't want "host = inherit", because that will cause all jails to
use the same hostname - if you change one, it changes all of them (and
changes the system hostname).
But you can do it in two steps, involving some fairly ugly hackery
foo
{
  exec.poststart = "jail -m name=foo host.hostname=$name.`hostname`";
  exec.poststart += "jexec foo sleep 600";
}
Unfortunately the second exec.poststart line is required as a
replacement for the typical "exec.start='sh /etc/rc'" because
exec.poststart runs after exec.start and that's to late to set the
hostname.  This is one of these cases that suggests the need for a
parameter run by the main host between jail creation and the running
of exec.start.
Another possible hack would be to do something with setting a file
inside the jail that's peeked at by the jail's rc.conf or rc.d/hostname.
Nothing pretty, I'm afraid.
- Jamie
Erich Dollansky
2018-06-21 00:57:27 UTC
Permalink
Hi,

I use a script that generates the configuration file when the jail is
created.

This should work for you too.

Erich


On Tue, 19 Jun 2018 22:41:15 -0400
Post by Joseph Ward
Hi everyone,
I have several jails, configured via jail.conf, whose hostname I want
to make: $name.$system_hostname.
host = inherit;
the hostnames of the jails all match the hostname of the system.  I
host.hostname = $name;
host.domainname = inherit;
but the hostname ends up just being $name (expanded, of course).
host = inherit;
host.hostname = $name;
ended up with simply $name as well, with the "inherit" ignored.
So, am I missing something? 
Thanks,
Joseph
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-jail
To unsubscribe, send any mail to
Joseph Ward
2018-06-21 15:16:48 UTC
Permalink
Thanks for the suggestion, but my case is a bit wacky.  I'm actually
creating the jails on a build box, packaging them, and deploying them to
a bunch of different host machines, so ideally the change would be
handled across all machines without any changes.  Fortunately, the
suggestion in the other sub-thread worked well.  (In a previous
iteration I'd had the deploy script make the change on each machine, but
that's a lot less clean than just having a single jail.conf that worked
for all).


-Joseph
Post by Erich Dollansky
Hi,
I use a script that generates the configuration file when the jail is
created.
This should work for you too.
Erich
On Tue, 19 Jun 2018 22:41:15 -0400
Post by Joseph Ward
Hi everyone,
I have several jails, configured via jail.conf, whose hostname I want
to make: $name.$system_hostname.
host = inherit;
the hostnames of the jails all match the hostname of the system.  I
host.hostname = $name;
host.domainname = inherit;
but the hostname ends up just being $name (expanded, of course).
host = inherit;
host.hostname = $name;
ended up with simply $name as well, with the "inherit" ignored.
So, am I missing something? 
Thanks,
Joseph
_______________________________________________
https://lists.freebsd.org/mailman/listinfo/freebsd-jail
To unsubscribe, send any mail to
Loading...