You are not logged in.

#1 2014-04-18 08:17:33

popoffka
Member
Registered: 2012-03-29
Posts: 16

[solved] run program before network is available and stop boot on fail

Hi everyone,

Let's say I have an program that is absolutely essential to my system, and I want it to run before netctl even tries initialising my network connection (in fact, I want the network to be unavailable until the program has finished its execution). I _always_ want to see the program's output on the console, and if it exits with a non-zero exit code, I want the whole boot process to be aborted (or at least all of netctl's units to be disabled). Is this possible in systemd?

Last edited by popoffka (2014-04-18 13:07:09)

Offline

#2 2014-04-18 09:54:08

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [solved] run program before network is available and stop boot on fail

popoffka wrote:

Hi everyone,

Hi.

popoffka wrote:

Let's say I have an program that is absolutely essential to my system, ...

Sure.

popoffka wrote:

and I want it to run before netctl even tries initialising my network connection (in fact, I want the network to be unavailable until the program has finished its execution).

Use 'Before=' in your unit file. Make sure it does not fork.

popoffka wrote:

I _always_ want to see the program's output on the console,

Pipe it's output to /dev/console directly. Maybe there is a cleaner why, I'm not sure. SystemD actually supports the redirect directive in it's native language (nice).

popoffka wrote:

and if it exits with a non-zero exit code, I want the whole boot process to be aborted (or at least all of netctl's units to be disabled)

Use 'RequiredBy=' in your unit file. That could be used to prevent netctl from starting. Preventing the whole boot isn't a bit drastic isn't it?

popoffka wrote:

. Is this possible in systemd?

Yes. Consult the systemd.service and systemd.unit manpages.


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#3 2014-04-18 12:01:30

popoffka
Member
Registered: 2012-03-29
Posts: 16

Re: [solved] run program before network is available and stop boot on fail

Thanks for your assistance, Rexilion!

I created a .service file like this:

[Unit]
Before=network.target
Before=netctl-auto@.service
Before=netctl-ifplugd@.service
Before=multi-user.target

[Service]
Type=oneshot
ExecStart=<command>
StandardOutput=journal+console

[Install]
RequiredBy=network.target
RequiredBy=netctl-auto@.service
RequiredBy=netctl-ifplugd@.service
RequiredBy=multi-user.target

However, even if the command fails, the system still continues to boot, and netctl-auto@ still starts (although systemd logs things like "Dependency failed for Network"). Am I missing something here?

Offline

#4 2014-04-18 12:31:22

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [solved] run program before network is available and stop boot on fail

Cool, never knew about systemd.exec . Thank you.

You might want to add StandardError= as well. Otherwise you will not see errormessages on your console.

netctl-auto@ and netctl-ifplugd@ are services that are supposed to be instantiated and hence are not ran directly. You need to add something like netctl-auto@wlan0 for each interface. I'm not aware of any other service that does this for networking as a whole. You could create a service file that polls for a default route and make that fail / succeed. Then other services can depend on that for network connectivity.

Preventing the system from booting would involve issueing a shutdown command the moment this service fails. You could use 'OnFailure=' for this. I'm surprised you did not find that directive.

Last edited by Rexilion (2014-04-18 12:32:00)


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

#5 2014-04-18 13:06:24

popoffka
Member
Registered: 2012-03-29
Posts: 16

Re: [solved] run program before network is available and stop boot on fail

Oh. Yeah, appending the names of particular interfaces to netctl-auto@ and netctl-ifplugd@ solved the issue. Damn, and I'd hoped systemd was smarter.

I decided that halting the boot process is too dramatic indeed and simply disabling the network is enough, so my issue is resolved completely now. Thanks again for your assistance!

Offline

#6 2014-04-18 15:44:30

Rexilion
Member
Registered: 2013-12-23
Posts: 784

Re: [solved] run program before network is available and stop boot on fail

You could create a catch all network-active.service that polls /etc/resolv.conf or for a default route to. This will hook up just one service file and not one for each interface.

Glad I could assist you!


fs/super.c : "Self-destruct in 5 seconds.  Have a nice day...\n",

Offline

Board footer

Powered by FluxBB