Leveraging system hostname as part of a jail's hostname

Joseph Ward jbwlists at hilltopgroup.com
Wed Jun 20 18:14:20 UTC 2018


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


On 06/20/2018 12:52, James Gritton wrote:
> On 2018-06-19 20:41, Joseph Ward wrote:
>> 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? 
>
> 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



More information about the freebsd-jail mailing list