Okay, I've been digging a bit in a systemd manual... and I'd be glad if you could give your 2cents about pro's and con's to this :/.
Here's my preliminary solution:
[hr]
Preceding thoughts:
At first I used the service with Type=forking:
[Unit]
Description=VPN iptables rules for tun-interfaces on boot
[Service]
Type=forking
ExecStart=/bin/sh /storage/.config/iptables.rules
Restart=on-failure
[Install]
WantedBy=kodi.target
Display More
I chose "forking" because I could define Restart=on-failure, as I thought it is good for a security related service like this to be restarted upon failure.
I'm not sure if that does make sense. Perhaps I'm wrong here and this parameter does not have any effect practically. That is because I'm not sure whether forking is some kind of daemon, which is running the whole time in background and notices whenever it gets killed, to be restarted with "Restart=on-failure" in that case. If that is the case, then I don't understand why "ExecStop" in combination with "forking" does not activate the rules. May be I'm missing some other paramter? Or is forking just invoking the rules once and then closes automatically, that would mean that Restart=on-failure is just working for the boot, but not if the service has a failure somewhen later during watching content or so.
Then I made this first Type=oneshot approach:
[Unit]
Description=VPN iptables rules for tun-interfaces on boot
[Service]
Type=oneshot
#Requires=network-online.service
ExecStart=/bin/sh /storage/.config/iptables.rules
ExecStop=/usr/sbin/iptables -F
RemainAfterExit=yes
[Install]
WantedBy=kodi.target
Display More
- The advantage of this was that you could also stop the service on demand with systemctl stop iptables.service and it would then flush the rules thanks to ExecStop=/usr/sbin/iptables -F
- The downside was that you could not use Restart=on-failure in combination with oneshot and,...(see next point)
- If I understand it correctly, oneshot with RemainAfterExit=yes would keep up the service the entire time consuming ressources, wouldn't it? And if it crashes, the service wouldn't restart automatically because of missing Restart=on-failure. So in worst case the rules would vanish until next reboot and you wouldn't notice that your network is exposed again. Unfortunately Restart=on-failure does not work with oneshot.
Finally I sticked to the short "oneshot" version without the ExecStop and RemainAfterExit=yes options to not having the service running the whole time. If I make systemctl stop iptables.service the service would ignore this I think, but the iptables rules will remain active until you invoke iptables -F
Sorry that this post is so long again -_-. However I'd really appreciate if somebody could say which one of the three services would be the best. Or how oneshot or forking handles the situation if something kills the service.