Comment 6 for bug 1735331

Revision history for this message
Scott Moser (smoser) wrote :

OK. So dhclient is fun.

Background info
 * we run dhclient like this:
   -1 (try to get an ip address only once exit fail on fail).
   -v (verbose)
   -lf <tmpdir>/dhcp.leases
   -pf <tmpdir>/pid
   <interface>
   -sf /bin/true
 * dhclient.c source [1]

I'm pretty sure this is what is happening here.
1. when daemonizing, dhclient does not write its pidfile immediately (line 971). Instead it waits until it does "go_daemon" (line 4203). This does make some sense as it goes daemon by forking and the new process staying around.
2. on successful acquisition of address, cloud-init is removing the temp dir and looking for the pid before the fork()'ed process writes it.
2a. on failure, i think a pid is probably not written at all. I don't think we're handling the possible ProcessExecutionError that would be raised if dhclient returned non-zero.

Heres a suggestion for a fix
 http://paste.ubuntu.com/26081537/

Another option would be for cloud-init to invoke dhclient with '-d' (do not background) which would avoid the background process entirely. The '-1' would allow it to exit on failure. Then on success we'd have to have the script file signal somehow that it was done or possibly just kill its parent.

The last option of course is to write a dhcp client in python :), which is looking more and more appealing.

[1] https://git.launchpad.net/~usd-import-team/ubuntu/+source/isc-dhcp/tree/client/dhclient.c