Skip to contentSkip to conversation

make the systemd unit example more general

PATCH In review Christian Hesse 2 participants 5 messages Dec 8, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 make the systemd unit example more general

From: Christian Hesse <[email protected]>

---
 contrib/examples/systemd/README            |  5 +++--
 contrib/examples/systemd/example.address   |  1 +
 contrib/examples/systemd/example.conf      |  8 ++++++++
 contrib/examples/systemd/wgserver.service  | 15 ---------------
 contrib/examples/systemd/[email protected] | 15 +++++++++++++++
 5 files changed, 27 insertions(+), 17 deletions(-)
 create mode 100644 contrib/examples/systemd/example.address
 create mode 100644 contrib/examples/systemd/example.conf
 delete mode 100644 contrib/examples/systemd/wgserver.service
 create mode 100644 contrib/examples/systemd/[email protected]
contrib/examples/systemd/README +3 -2
index 1ef51aa..b8f4455 100644
@@ -1,5 +1,6 @@
 Until WireGuard receives full integration to the various network
 management utilities, there are a number of ways of setting up
 a WireGuard tunnel at boot time. This systemd unit file is one
-such way of doing things. Probably it should be tweaked before
-using.
+such way of doing things.
+Configuration files example.address and example.conf are expected
+in /etc/wireguard/, then start service [email protected].
contrib/examples/systemd/example.address new file +1 -0
new file mode 100644
index 0000000..983bb76
@@ -0,0 +1 @@
+ADDRESS=10.10.10.1/24
contrib/examples/systemd/example.conf new file +8 -0
new file mode 100644
index 0000000..1cfc5ee
@@ -0,0 +1,8 @@
+[Interface]
+PrivateKey = gI6EdUSYvn8ugXOt8QQD6Yc+JyiZxIhp3GInSWRfWGE=
+ListenPort = 21841
+
+[Peer]
+PublicKey = HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=
+Endpoint = 192.95.5.69:41414
+AllowedIPs = 10.10.10.0/24
contrib/examples/systemd/wgserver.service deleted +0 -15
deleted file mode 100644
index dfce1e9..0000000
@@ -1,15 +0,0 @@
-[Unit]
-Description=WireGuard Server
-
-[Service]
-Type=oneshot
-RemainAfterExit=yes
-ExecStart=/bin/ip link add dev wgserver type wireguard
-ExecStart=/bin/ip address add 192.168.177.1/24 dev wgserver
-ExecStart=/usr/bin/wg setconf wgserver /etc/wireguard-server.conf
-ExecStart=/bin/ip link set up dev wgserver
-ExecStop=/bin/sh -c 'umask 077; /usr/bin/wg showconf wgserver > /etc/wireguard-server.conf.tmp && mv /etc/wireguard-server.conf.tmp /etc/wireguard-server.conf'
-ExecStop=/bin/ip link del dev wgserver
-
-[Install]
-WantedBy=multi-user.target
contrib/examples/systemd/[email protected] new file +15 -0
new file mode 100644
index 0000000..721ce88
@@ -0,0 +1,15 @@
+[Unit]
+Description=WireGuard Server %I
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+EnvironmentFile=/etc/wireguard/%i.address
+ExecStart=/bin/ip link add dev %i type wireguard
+ExecStart=/bin/ip address add $ADDRESS dev %i
+ExecStart=/usr/bin/wg setconf %i /etc/wireguard/%i.conf
+ExecStart=/bin/ip link set up dev %i
+ExecStop=/bin/ip link del dev %i
+
+[Install]
+WantedBy=multi-user.target
Signature · 1 line
2.10.2
#1 lore raw

I like this, but two questions.

1. Why did you get rid of the ExecStop config saving line?
2. You're not planning to actually package this unit file, are you?

#2 lore raw

"Jason A. Donenfeld" <[email protected]> on Thu, 2016/12/08 12:18:

Quoted 3 lines

I like this, but two questions.

1. Why did you get rid of the ExecStop config saving line?

I do not like units altering configuration. If you want to save configuration on stop create a second unit with correct dependencies an ordering that writes the configuration.

Quoted 1 line

2. You're not planning to actually package this unit file, are you?

Not sure about that. :-p At least I left it in examples directory. ;)

In first place this is a RFC. Nevertheless we should polish things a bit and provide a usable systemd unit.

I still do not like to have two files that hold the settings... But no idea how to get rid of that in a clean way. Having shell code in a systemd unit is even worse...

Signature · 3 lines
main(a){char*c=/*    Schoene Gruesse                         */"B?IJj;MEH"
"CX:;",b;for(a/*    Best regards             my address:    */=0;b=c[a++];)
putchar(b-1/(/*    Chris            cc -ox -xc - && ./x    */b/42*2-3)*42);}
#3 lore raw
Quoted 8 lines · Christian Hesse

"Jason A. Donenfeld" <[email protected]> on Thu, 2016/12/08 12:18:

Quoted 3 lines

I like this, but two questions.

1. Why did you get rid of the ExecStop config saving line?

I do not like units altering configuration. If you want to save configuration on stop create a second unit with correct dependencies an ordering that writes the configuration.

That's fair. Want to submit a v2, then, with a shutdown unit too? (By the way, please prefix the commit message with "contrib: ".)

Quoted 4 lines

Not sure about that. :-p At least I left it in examples directory. ;)

In first place this is a RFC. Nevertheless we should polish things a bit and provide a usable systemd unit.

Alright then. By the way, there's some work upstream with systemd to include this in systemd-networkd, which would probably be the "more correct" way of doing it. But I guess a unit would nonetheless be useful for folks who don't use systemd-networkd.

Quoted 4 lines

I still do not like to have two files that hold the settings... But no idea how to get rid of that in a clean way. Having shell code in a systemd unit is even worse...

Yea, it's not nice. There's some additional logic: adding allowed-ips to routes for the device, if the allowed-ips aren't already included in the device's address prefix route.

I've thought about just adding this to wg(8) itself, and letting it be specified in the config file. This would indeed be _nice_, but it still seems like the wrong place to be doing these things. Maybe it'd be useful to have an extra shell-script in contrib called wg-config.sh that takes a device name, a wg(8), config file, a list of IPs given by --address=, a list of additional routes given by --route=, and the ability to disable auto-route-from-allowed-ips-adding via --no-auto-routes. Maybe I'll code this up myself -- it should only take a few minutes to do...

#4 lore raw
Quoted 7 lines · Jason A. Donenfeld

still seems like the wrong place to be doing these things. Maybe it'd be useful to have an extra shell-script in contrib called wg-config.sh that takes a device name, a wg(8), config file, a list of IPs given by --address=, a list of additional routes given by --route=, and the ability to disable auto-route-from-allowed-ips-adding via --no-auto-routes. Maybe I'll code this up myself -- it should only take a few minutes to do...

https://git.zx2c4.com/WireGuard/tree/contrib/examples/wg-config/README

That was pretty easy.

Is this useful somehow?