Skip to content

Improve the dhclient hook for OCI compat#2013

Merged
brauner merged 4 commits intolxc:masterfrom
3XX0:oci-dhcp-improvements
Dec 20, 2017
Merged

Improve the dhclient hook for OCI compat#2013
brauner merged 4 commits intolxc:masterfrom
3XX0:oci-dhcp-improvements

Conversation

@3XX0
Copy link
Copy Markdown
Contributor

@3XX0 3XX0 commented Dec 8, 2017

Now it becomes:

lxc.hook.start-host = /usr/share/lxc/hooks/dhclient
lxc.hook.stop = /usr/share/lxc/hooks/dhclient

@stgraber @brauner Do you have any suggestion to address the LSM problems?
Most distribution protect dhclient, thus it won't have permissions to read or write config/lease/pid files. Should we write Apparmor/SELinux profile overrides or let the users figure it out (e.g. document what to do in /etc/apparmor.d/local/sbin.dhclient)?

@hallyn Seems like the hook namespace fds got fixed so I was able to leverage it.
Right now I use oci.common.conf to setup the hooks for all my OCI containers, should we add a --dhcp option to the OCI template as well? It might be problematic if both are set though, what do you think?

@lxc-jenkins
Copy link
Copy Markdown

This pull request didn't trigger Jenkins as its author isn't in the whitelist.

An organization member must perform one of the following:

  • To have this branch tested by Jenkins, use the "ok to test" command.
  • To have a one time test done, use the "test this please" command.

Those commands are simple Github comments of the format: "jenkins: COMMAND"

rm -f "${pidfile}"
}

if [ "$#" -lt 3 ] || [ "$2" != "lxc" ]; then
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using the same test in my script, I would be interested to know if you think that makes sense and if it's safe to have (e.g.: are we expecting new arguments to be passed to hooks in the near-future?)

fi

if [ -e "${LXC_TEMPLATE_CONFIG}/oci.common.conf" ]; then
echo "lxc.include = ${LXC_TEMPLATE_CONFIG}/oci.common.conf" >> "${LXC_CONF_FILE}"
Copy link
Copy Markdown
Contributor

@flx42 flx42 Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we push oci.common.conf.in in the same commit? Even if it only contains commented hooks for now, e.g.:

# Uncomment if you want to use dhclient...
#lxc.hook.start-host = @LXCHOOKDIR@/dhclient
#lxc.hook.stop = @LXCHOOKDIR@/dhclient

@hallyn
Copy link
Copy Markdown
Member

hallyn commented Dec 9, 2017

Regarding the hook args, quite the opposite - we are hoping to stop sending them as argv and just send them as environment variables.

@hallyn
Copy link
Copy Markdown
Member

hallyn commented Dec 9, 2017

--dhcp option would be nice. Can it just double-check whether the dhcp hook is already set in the config when the template is called to avoid double-setting it?

@3XX0
Copy link
Copy Markdown
Contributor Author

3XX0 commented Dec 11, 2017

I updated the PR, I added the --dhcp option and guarded against multiple inclusion by checking the PID file and I included an oci.common.conf.in commented out.

@hallyn @brauner I will update the hook arguments once we settled on the environment variables

brauner pushed a commit to brauner/lxc that referenced this pull request Dec 12, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 12, 2017
This can be used by scripts to detect what version of the hooks are used.

Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 12, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 12, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 12, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
@brauner
Copy link
Copy Markdown
Member

brauner commented Dec 12, 2017

See #2026 .

@3XX0
Copy link
Copy Markdown
Contributor Author

3XX0 commented Dec 14, 2017

@brauner I updated the hook to work with your PR

brauner pushed a commit to brauner/lxc that referenced this pull request Dec 14, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 14, 2017
This can be used by scripts to detect what version of the hooks are used.

Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 14, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 14, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
brauner pushed a commit to brauner/lxc that referenced this pull request Dec 14, 2017
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
@brauner
Copy link
Copy Markdown
Member

brauner commented Dec 14, 2017

@3XX0, excellent. Thanks! I'll wait for @hallyn to merge my pr and then I'm merging this one.

@brauner brauner added the Blocked Waiting on an external task label Dec 14, 2017
@brauner
Copy link
Copy Markdown
Member

brauner commented Dec 14, 2017

Labeling this as "blocked" until #2026 is merged. :)

@brauner brauner added Incomplete Waiting on more information from reporter and removed Blocked Waiting on an external task labels Dec 14, 2017
Copy link
Copy Markdown
Member

@brauner brauner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. I just left a comment about attaching to the user namespace. I think the logic needs to change there. Otherwise this lgtm.

echo "WARN: DHCP client is already running, skipping start hook." >> "${debugfile}"
else
echo "INFO: Starting DHCP client and acquiring a lease..." >> "${debugfile}"
nsenter --uts --user --net --target "${LXC_PID}" -- \
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I think this won't work unconditionally. The problem is that with --user the host and container need to be in different user namespaces. If they are not this will fail with EINVAL. I think what you want to do is look whether LXC_USER_NS is set if LXC_HOOK_VERSION is set to 1 or if user:/proc/<pid>/fd/<nr> is passed as argument when LXC_HOOK_VERSION is set to 0 or not set at all.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good catch. Shouldn't we do that for every namespace though since the user could have specified a --share-<ns>?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, then only user namespaces have this restriction. The other ones should be fine. Was that your concern or did I misunderstand?
(Fwiw, since I introduced lxc.namespace.[namespace identifier] users can actually even specify this in their config.)

Copy link
Copy Markdown
Contributor Author

@3XX0 3XX0 Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for the stop hook but what about the start-host hook? Do I have to check the userns proc inodes or there is a better way to know whether the userns is shared or not?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to do it would be to compare:

readlink /proc/${LXD_PID}/ns/user == readlink /proc/self/ns/user

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since by the time the start-hook runs we already know the init pid of the container and we make it available in LXC_PID.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and the LXC_PID is guaranteed to be valid until after the start-host hook is run since we place a barrier right after it. Only after the start-hooks are run is the the child allowed to exec*().

done
else
ns_args+=("--uts=${LXC_UTS_NS}")
ns_args+=("--user=${LXC_USER_NS}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment applies here: So I think this won't work unconditionally. The problem is that with --user the host and container need to be in different user namespaces. If they are not this will fail with EINVAL. I think what you want to do is look whether LXC_USER_NS is set if LXC_HOOK_VERSION is set to 1 or if user:/proc/<pid>/fd/<nr> is passed as argument when LXC_HOOK_VERSION is set to 0 or not set at all.

3XX0 added 4 commits December 19, 2017 15:18
- Merge dhclient-start and dhclient-stop into a single hook.
- Wait for a lease before returning from the hook.
- Generate a logfile when LXC log level is either DEBUG or TRACE.
- Rely on namespace file descriptors for the stop hook.
- Use settings from /<sysconf>/lxc/dhclient.conf if available.
- Attempt to cleanup if dhclient fails to shutdown properly.

Signed-off-by: Jonathan Calmels <jcalmels@nvidia.com>
Excerpt from dnsmasq(8):
By default, the DHCP server will attempt to ensure that an address in not
in use before allocating it to a host. It does this by sending an ICMP echo
request (aka "ping") to the address in question. If it gets a reply, then the
address must already be in use, and another is tried. This flag disables this check.

This is useful if one expects all the containers to get an IP address
from the LXC authoritative DHCP server and wants to speed up the process
of getting a lease.

Signed-off-by: Jonathan Calmels <jcalmels@nvidia.com>
Signed-off-by: Jonathan Calmels <jcalmels@nvidia.com>
Signed-off-by: Jonathan Calmels <jcalmels@nvidia.com>
@3XX0
Copy link
Copy Markdown
Contributor Author

3XX0 commented Dec 19, 2017

  • Rebased on master
  • Fixed the issue w.r.t the userns
  • Ran shellcheck
  • Tested unprivileged/privileged containers with hook.version 0 and 1

@brauner
Copy link
Copy Markdown
Member

brauner commented Dec 20, 2017

jenkins: test this please

@brauner brauner merged commit d1de8dd into lxc:master Dec 20, 2017
geaaru pushed a commit to geaaru/lxc that referenced this pull request Jul 4, 2018
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
geaaru pushed a commit to geaaru/lxc that referenced this pull request Jul 4, 2018
This can be used by scripts to detect what version of the hooks are used.

Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
geaaru pushed a commit to geaaru/lxc that referenced this pull request Jul 4, 2018
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
geaaru pushed a commit to geaaru/lxc that referenced this pull request Jul 4, 2018
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
geaaru pushed a commit to geaaru/lxc that referenced this pull request Jul 4, 2018
Unblocks lxc#2013.
Unblocks lxc#2015.
Closes lxc#1766.

Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Incomplete Waiting on more information from reporter

Development

Successfully merging this pull request may close these issues.

5 participants