You are not logged in.

#1 2014-08-06 11:48:50

DerDave
Member
Registered: 2014-08-06
Posts: 4

[SOLVED] Start service over systemd only when tun0 is ready

Problem:
I wan't to start a service over systemd only after my VPN connection is established.

What I've tried so far:
I already changed the xxx.service file, so it starts after the openvpn@.service by adding the "Requires=" and "After=" arguments.
Unfortunately this is not enough because the openvpn service can run but establishing the connection to the VPN takes longer, so the other service already starts without waiting for the tun0.

I also thought that the service maybe has to wait for the device "tun0" but it seems not to exist in the /dev or /dev/network folders, as it's a virtual device. I saw that in some other unix systems i would find it there but unfortunately not in arch linux.


Question:
Does anyone have an idea how to tell systemd to wait for the tun0 device before the service gets started?
Of course I would prefer a nice and clean systemd solution but if anybody has a working script for that, I would also appreciate the help!

Thank you very much!

Last edited by DerDave (2014-08-07 23:53:01)

Offline

#2 2014-08-06 12:20:07

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: [SOLVED] Start service over systemd only when tun0 is ready

Run "systemctl --all" and see whether there is a tun0 device there.

Offline

#3 2014-08-06 12:55:11

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Start service over systemd only when tun0 is ready

Been a long time since I needed it and don't have it set up, but I thought tun0 would appear under dev (maybe /dev/net ?).

The man does say this about --dev-node:

Explicitly set the device node rather than using /dev/net/tun,  /dev/tun,  /dev/tap,  etc.
              If  OpenVPN  cannot  figure out whether node is a TUN or TAP device based on the name, you
              should also specify --dev-type tun or --dev-type tap.

However, if you really can't get it to work, you could use the --up option (see the man page) to start the service.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#4 2014-08-06 16:55:34

DerDave
Member
Registered: 2014-08-06
Posts: 4

Re: [SOLVED] Start service over systemd only when tun0 is ready

Thanks for your quick answers!

@Lucke: I know how to find out whether my VPN is up or not. I can easily check this with ifconfig. Can't see the tun0 device under "systemctl --all" by the way. My question was rather how can systemd know this and only then start the service (qBittorrent to be precise).

@Skanky: Unfortunately the device can't be seen under /dev/tun respectively /dev/tun is always there, no matter if the VPN connection is established or not.
Your hint with the --up option was interesting. I looked into that but couldn't really solve the problem. I put a script in the argument, that tries to start the service over systemctl. This doesn't work and I don't know why because I don't get any errors. Just the service is not running afterwards.
When i try to run the qBittorrent daemon from the "--up" parameter directly without the detour over systemd it doesn't work either. QBittorrent simply doesn't run.
Again I can't see any errors.

Is there really no way in systemd to set a line in the service file, so the service just starts, when the tun0 connection is there?
Any systemd experts out there?

Thank you!

Offline

#5 2014-08-06 20:01:45

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: [SOLVED] Start service over systemd only when tun0 is ready

If you did see the device in systemctl, then you could ask systemd to run the service after the device appears.

-edit-

I e.g. have "sys-devices-virtual-net-ifb0.device" in systemctl. And adding After/Requires=sys-devices-virtual-net-ifb0.device to the service file should make it start after this device appears.

Last edited by lucke (2014-08-06 20:34:40)

Offline

#6 2014-08-07 08:59:08

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Start service over systemd only when tun0 is ready

You could start the daemon directly and not via systemctl. You'd want to take it down using --down anyway.

I *think* the --dev (or --dev-node) can be used to create a tun0 - but as I can't test, I may be misunderstanding the documentation.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#7 2014-08-07 09:06:20

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED] Start service over systemd only when tun0 is ready


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2014-08-07 13:59:50

DerDave
Member
Registered: 2014-08-06
Posts: 4

Re: [SOLVED] Start service over systemd only when tun0 is ready

Thanks to all of you!
Really great forum and nice helpful people!
Could solve my problem now.

Last edited by DerDave (2014-08-07 14:03:20)

Offline

#9 2014-08-07 15:05:37

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Start service over systemd only when tun0 is ready

When it is solved, can you post here what you did, and mark the thread as solved, please. smile


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#10 2014-08-07 23:52:40

DerDave
Member
Registered: 2014-08-06
Posts: 4

Re: [SOLVED] Start service over systemd only when tun0 is ready

@Skanky of course your're right, that I probably want to start and also end the service over the --up and --down arguments of openvpn.
However I was actually just trying to get around a bug in Qbittorrent.
Qbittorrent already has the option to stop all downloads, when the specified interface is lost (in my case tun0).
Unfortunately if the interface doesn't exist while Qbittorrent starts, it simply ignores the restriction to a specific device and falls back to eth0 for example.

So this bug only appears, when it starts up without the specified interface existing yet, that's why for me it was enough to delay Qbittorrent's start till after the tun0 device is there.

Jasonwryan's link was helpful to better understand what exactly is happening there.
Actually in the end it was even simpler and exactly like Lucke said:

When the VPN connection is established, there is the new folder "/sys/devices/virtual/net/tun0" and accordingly the new device "sys-devices-virtual-net-tun0.device" in systemd.
Using "After= " und "Requires= " works perfectly for that and now I have no problem with this bug anymore.

I simply didn't know, where exactly the tun0 device would appear (looked under /dev before) and had no idea that systemd would recognize the device simple as that, with the path and all. I really start to like systemd :-)

Thanks again for your help!

Last edited by DerDave (2014-08-08 12:03:26)

Offline

#11 2014-08-08 08:59:16

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: [SOLVED] Start service over systemd only when tun0 is ready

DerDave wrote:

@Skanky of course your're right, that I probably want to start and also end the service over the --up and --down arguments of openvpn.
However I was actually just trying to get around a bug in Qbittorrent.
Qbittorrent already has the option to stop all downloads, when the specified interface is lost (in my case tun0).
Unfortunately if the interface doesn't exist while Qbittorrent starts, it simply ignores the restriction to a specific device and falls back to eth0 for example.

So this bug only appears, when it starts up without the specified interface existing yet, that's why for me it was enough to delay Qbittorrent's start till after the tun0 device is there.

Jasonwryan's link was helpful to better understand what exactly is happening there.
Actually in the end it was even simpler and exactly like Lucke said:

When the VPN connection is established, there is the new folder "/sys/devices/net/tun0" and accordingly the new device "sys-devices-net-tun0.device" in systemd.
Using "After= " und "Requires= " works perfectly for that and now I have no problem with this bug anymore.

I simply didn't know, where exactly the tun0 device would appear (looked under /dev before) and had no idea that systemd would recognize the device simple as that, with the path and all. I really start to like systemd :-)

Thanks again for your help!

Thanks for the update. Should help anyone else who has similar issues.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB