Ubuntu: How to start a task before networking?



Question:

I've written an upstart task that modifies /etc/network/interfaces. (Actually a file sourced into it.) Which start on condition do I need to declare to let my task run before any networking jobs?

I've tried start on starting networking, but that's apparently too late. When I log in after booting I can see that the changes were written, but obviously they are not used: the new config states a static IP, but the boot process waits for a non-existing DHCP server (old config) to time out.

I've also tried start on starting network-interface INTERFACE=eth0, which didn't work either. IIRC there was an error in the log that the change couldn't be written.

Background: I need a VM template that can be cloned and the clones configured through a script. Among other settings, I need to give them a static IP address to access them from the host. I use guestfish to write a config file to one of the virtual disks and let a script apply these settings to the system. I don't want that disk to contain an actual system settings file. I can't modify /etc directly, because that disk is shared (copy-on-write/diff) among the clones and guestfish apparently doesn't support that type of image. I could also let them use DHCP and setup a server that assigns IP by MAC, but I'm afraid of the complexity. I could also add just another virtual disk for configuration files, but if possible I'd prefer to store settings directly on the system disk image.

Used software: Ubuntu Server 12.04, VirtualBox. The configuration modifier is a self written ruby script.


Solution:1

Found a workaround for myself.

I removed any configuration for eth0 from the VM template. Thus it is not brought up automatically. My config job now starts on starting networking, modifies the config file and explicitly calls ifup eth0.

While this solves my problem, this doesn't fit the general case of how to start a service/task in upstart before networking. Thus I'm going to leave the question open.


Solution:2

start on starting network-interface  instance $INTERFACE  task  exec mything  

and

start on starting networking  task  exec mything  

ifup is run by one of two methods, either by the network-interface job or by the networking job, which is used to configure any interfaces not covered by udev.

The instance stanza above makes sure that your job is started for each of the two cases, because the way upstart works, one or condition will not block the other.

(that is this bug btw: https://bugs.launchpad.net/upstart/+bug/568860)

If your job cannot run concurrently or twice, you will need to split it into three jobs. One called 'mything-waiter':

start on starting network-interface  instance $INTERFACE  task  exec start wait-for-state WAITER=mything-waiter-$UPSTART_JOB-$INTERFACE WAIT_FOR=mything  

And another with a similar exec line for starting networking:

start on starting networking  task  exec start wait-for-state WAITER=mything-waiter-networking WAIT_FOR=mything  

And the other called 'mything' which is just

task  exec mything  

I think an argument can be made to have 'start on starting networking' do all of this, but that will require some redesign of the networking bring-up in Ubuntu.


Solution:3

Could you simply start your job at start on startup? Is there any reason to tie it to the start of the networks, if you just need the configuration be changed before networking starts.

Edit: ok, that didn't work.

I'm not able to mess up with my init system right now, so I gotta give some advise untested:

First option: add task stanza to your job. It should make Upstart wait until it exits before launching pending networking/network-interface job. I'm fairly sure this isn't fixing the timing issue as there has been plenty of questions in AskUbuntu how to inject stuff into normal boot process.

Second option: use override files. I think network-interface.conf is the one you need to spoof. Sadly it uses pre-start section already, so instead of just making one up we need to copy it (and only it) to our network-interface.override file.

Once you've copied pre-start section, just modify the content to suit your needs. Original file stays as is, but pre-start section from your override literally overrides the one in the original job.

Hope either helps.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »