Populating resolv.conf with DCHP on SmartOS zones


Recently I started to experiment with SmartOS a bit more than my initial ooh, I a working zone...it's nice weather today. So far I cannot help but feel that SmartOS feels Just Right.

At one point I needed to spin up a bunch of OS zones where I didn't care about their IP addresses, thus setting their ip to dhcp. All went well and they got addresses and everything worked, except for DNS. Huh? Wasn't my DHCP server configured to send the domain-name-servers option? Yes it was. So why wasn't resolv.conf populated?

Turns out, people have wondered the same thing back in 2014 on smartos-discuss. However to this day it's still a low priority at Joyent it seems. So while one can manually add a resolvers block to their zone definition, it defeats the purpose of using a Dynamic Host Configuration Protocol.

In the thread linked above someone suggests simply echo'ing the output /usr/sbin/dhcpinfo 6, however that won't work if you have multiple domain-name-servers listed. So here's the user-script I'm using which populates resolv.conf. It does however assume you're also sending the domain-name filled out. Otherwise you'll end up with an empty domain line. It's not perfect, but works good enough for me that it may come in handy for others too.

Note that you'll have to properly escape quotes and backticks if you update an existing vm like vmadm update $UUID <<EOF.

To update an existing zone:

# vmadm update $UUID << EOF
{
  "set_customer_metadata": {
    "user-script": "echo \"domain \`/sbin/dhcpinfo 15\`\" >> /etc/resolv.conf ; for r in \`/sbin/dhcpinfo 6\`; do echo \"nameserver \$r\" >> /etc/resolv.conf; done"
  }
}
EOF

Or the whole definition for use with vmadm create -f:

{
  "brand": "joyent",
  "quota": 5,
  "image_uuid": "96bcddda-beb7-11e5-af20-a3fb54c8ae29",
  "max_physical_memory": 256,
  "alias": "smartos-dhcp1",
  "nics": [
    {
      "nic_tag": "stub0",
      "ip": "dhcp",
      "primary": true
    }
  ],
  "customer_metadata": {
    "user-script": "echo \"domain `/sbin/dhcpinfo 15`\" >> /etc/resolv.conf ; for r in `/sbin/dhcpinfo 6`; do echo \"nameserver $r\" >> /etc/resolv.conf; done"
  }
}

For more information on what the numbers 6 and 15 mean, please refer to RFC 2132