Skip to contentSkip to conversation

package/quagga: Add systemd.service file and helper script

PATCH New Nathaniel Roach 2 participants 5 messages Jun 18, 2016 – Jun 23, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 package/quagga: Add systemd.service file and helper script

We use a template service file as all of the daemons use almost identical arguments and generally appear the same to the init system.

We "Wants=" zebra as that's the daemon for interfacing to the kernel, and it's not required for the other daemons to work but it's probably going to be used in nearly all setups.

We need the helper script as systemd doesn't like having the instance (%i) in the path to the executable.

---
 package/quagga/quagga-shim     | 51 ++++++++++++++++++++++++++++++++++++++++++
 package/quagga/quagga.mk       |  6 +++++
 package/quagga/quagga at .service | 19 ++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 package/quagga/quagga-shim
 create mode 100644 package/quagga/quagga at .service
package/quagga/quagga-shim new file +51 -0
new file mode 100644
index 0000000..1687e7c
@@ -0,0 +1,51 @@
+#!/bin/sh
+#Shim for systemd's quagga at .service
+
+export QUAGGA_PATH="/usr/sbin"
+
+# Check if $1 is a valid quagga binary
+case $1 in
+  zebra)
+    export BINARY="zebra"
+    ;;
+
+  bgpd)
+    export BINARY="bgpd"
+    ;;
+
+  ospfd)
+    export BINARY="ospfd"
+    ;;
+
+  ospf6d)
+    export BINARY="bgpd"
+    ;;
+
+  ripd)
+    export BINARY="ripd"
+    ;;
+
+  ripngd)
+    export BINARY="ripngd"
+    ;;
+
+  pimd)
+    export BINARY="pimd"
+    ;;
+
+  *)
+    echo "Must be called with a valid Quagga daemon as the first argument"
+    exit 2
+    ;;
+
+esac
+
+# Check the binary exists
+if [ -e "$QUAGGA_PATH/$BINARY" ]; then
+  shift # Remove the first argument (binary name)
+  "$QUAGGA_PATH/$BINARY" "$@" # Run the daemon with the remaining arguments
+else
+  echo "Binary \"$BINARY\" not found in $QUAGGA_PATH"
+  exit 1
+fi
+
package/quagga/quagga.mk +6 -0
index 22e90ad..5d9c292 100644
@@ -75,6 +75,12 @@ endif
 define QUAGGA_INSTALL_INIT_SYSTEMD
 	$(INSTALL) -D -m 644 package/quagga/quagga_tmpfiles.conf \
 		$(TARGET_DIR)/usr/lib/tmpfiles.d/quagga.conf
+
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service
+
+	$(INSTALL) -D -m 755 package/quagga/quagga-shim \
+		$(TARGET_DIR)/usr/sbin/quagga-shim
 endef
 
 $(eval $(autotools-package))
package/quagga/quagga at .service new file +19 -0
new file mode 100644
index 0000000..3e98318
@@ -0,0 +1,19 @@
+[Unit]
+Description=Quagga %i routing daemon
+PartOf=quagga.service
+ReloadPropagatedFrom=quagga.service
+Wants=quagga at zebra.service
+
+[Service]
+PrivateTmp=true
+KillMode=mixed
+Type=forking
+EnvironmentFile=/etc/default/quagga-%i
+ExecStart=/usr/sbin/quagga-shim %i --daemon $OPTS -f /etc/quagga/%i.conf -i /var/run/quagga/%i.pid
+PIDFile=/var/run/quagga/%i.pid
+KillSignal=SIGINT
+Restart=on-failure
+RestartSec=1
+
+[Install]
+WantedBy=multi-user.target
Signature · 1 line
2.8.1
#1 lore raw

PATCH 1/1 package/quagga: Add systemd.service file and helper script

Hi Nathaniel,

Quoted 7 lines · Nathaniel Roach

We use a template service file as all of the daemons use almost identical arguments and generally appear the same to the init system.

We "Wants=" zebra as that's the daemon for interfacing to the kernel, and it's not required for the other daemons to work but it's probably going to be used in nearly all setups.

... and it can be overridden by fs-overlay.

Quoted 3 lines

We need the helper script as systemd doesn't like having the instance (%i) in the path to the executable.

That last paragraph should IMHO be included as a comment in either the shim
itself or in the service file.

Quoted 16 lines
---
 package/quagga/quagga-shim     | 51 ++++++++++++++++++++++++++++++++++++++++++
 package/quagga/quagga.mk       |  6 +++++
 package/quagga/quagga at .service | 19 ++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 package/quagga/quagga-shim
 create mode 100644 package/quagga/quagga at .service
package/quagga/quagga-shim new file +0 -0
new file mode 100644
index 0000000..1687e7c

package/quagga/quagga-shim line 2

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
@@ -0,0 +1,51 @@
+#!/bin/sh
+#Shim for systemd's quagga at .service

# Shim ...

package/quagga/quagga-shim line 4

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
+
+export QUAGGA_PATH="/usr/sbin"

Why export?

package/quagga/quagga-shim line 9

Quoted 5 lines
package/quagga/quagga-shim new file +5 -0
+
+# Check if $1 is a valid quagga binary
+case $1 in
+  zebra)
+    export BINARY="zebra"

Why export?

I think this check is really redundant. So what if someone calls
"quagga-shim tcpdump -i eth0" and it actually works? Is that a problem? It's not as if this is setuid or anything.

So I'd skip the entire case, and instead go straight to ...

[snip]

package/quagga/quagga-shim line 44

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
+# Check the binary exists
+if [ -e "$QUAGGA_PATH/$BINARY" ]; then

And this check is redundant as well. Just call it, it will error out
automaticaly if the binary doesn't exist.

package/quagga/quagga-shim line 46

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
+  shift # Remove the first argument (binary name)
+  "$QUAGGA_PATH/$BINARY" "$@" # Run the daemon with the remaining arguments

Is there any reason to keep the shim around? I.e., why not exec? Also, the
comment is quite redundant.

In other words, I'd reduce the script to 5 lines:

#! /bin/sh
# Shim for systemd's quagga at .service
BINARY="$1"
shift
exec /usr/sbin/"$BINARY" "$@"

Quoted 10 lines
package/quagga/quagga-shim new file +5 -0
+else
+  echo "Binary \"$BINARY\" not found in $QUAGGA_PATH"
+  exit 1
+fi
+
package/quagga/quagga.mk +0 -0
index 22e90ad..5d9c292 100644

package/quagga/quagga.mk endif · line 80

Quoted 6 lines
package/quagga/quagga.mk +3 -0
@@ -75,6 +75,12 @@ endif
 define QUAGGA_INSTALL_INIT_SYSTEMD
 	$(INSTALL) -D -m 644 package/quagga/quagga_tmpfiles.conf \
 		$(TARGET_DIR)/usr/lib/tmpfiles.d/quagga.conf
+
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service

In the commit log you mention zebra, here it's bandwidthd. /me is confused.

Quoted 14 lines
package/quagga/quagga.mk +3 -0
+
+	$(INSTALL) -D -m 755 package/quagga/quagga-shim \
+		$(TARGET_DIR)/usr/sbin/quagga-shim
 endef
 
 $(eval $(autotools-package))
package/quagga/quagga at .service new file +2 -0
new file mode 100644
index 0000000..3e98318
@@ -0,0 +1,19 @@
+[Unit]
+Description=Quagga %i routing daemon

package/quagga/quagga at .service line 12

Quoted 10 lines
package/quagga/quagga at .service new file +10 -0
+PartOf=quagga.service
+ReloadPropagatedFrom=quagga.service
+Wants=quagga at zebra.service
+
+[Service]
+PrivateTmp=true
+KillMode=mixed
+Type=forking
+EnvironmentFile=/etc/default/quagga-%i
+ExecStart=/usr/sbin/quagga-shim %i --daemon $OPTS -f /etc/quagga/%i.conf -i /var/run/quagga/%i.pid

systemd prefers non-forking daemons because then it has better control over it.
Since you give a --daemon option here, I assume that they are perfectly capable of running as a simple service. Can you give that a try?

Regards,
Arnout

Quoted 8 lines
package/quagga/quagga at .service new file +7 -0
+PIDFile=/var/run/quagga/%i.pid
+KillSignal=SIGINT
+Restart=on-failure
+RestartSec=1
+
+[Install]
+WantedBy=multi-user.target
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#2 lore raw

PATCH 1/1 package/quagga: Add systemd.service file and helper script

In reply to Nathaniel Roach

Two more things...

package/quagga/quagga.mk endif · line 80

Quoted 2 lines · Nathaniel Roach
package/quagga/quagga.mk +2 -0
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service

I believe it should actually be installed as quagga at .service, and then
symlinked from /etc.

I'm not so sure of this, but wouldn't it actually make sense to install all
services that are selected in the config? Like

QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_ZEBRA) += zebra QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_BGPD) += bgpd
...

	$(foreach service,$(QUAGGA_SERVICES_y),\
		ln -sf ../../../../usr/lib/systemd/system/quagga at .service \

$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/$(service).service$(sep))

Regards,
Arnout

Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#3 lore raw

PATCH 1/1 package/quagga: Add systemd.service file and helper script

[snip]

Quoted 9 lines

So I'd skip the entire case, and instead go straight to ...

[snip]

package/quagga/quagga-shim line 44

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
+# Check the binary exists
+if [ -e "$QUAGGA_PATH/$BINARY" ]; then

And this check is redundant as well. Just call it, it will error out
automaticaly if the binary doesn't exist.

Quoted 7 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+  shift # Remove the first argument (binary name)
+  "$QUAGGA_PATH/$BINARY" "$@" # Run the daemon with the remaining

arguments

Is there any reason to keep the shim around? I.e., why not exec?
Also, the comment is quite redundant.

How about in the .service file simply ExecStart=/bin/sh -c "/usr/sbin/%i ..."?

Quoted 22 lines

In other words, I'd reduce the script to 5 lines:

#! /bin/sh
# Shim for systemd's quagga at .service
BINARY="$1"
shift
exec /usr/sbin/"$BINARY" "$@"

Quoted 11 lines
package/quagga/quagga-shim new file +5 -0
+else
+  echo "Binary \"$BINARY\" not found in $QUAGGA_PATH"
+  exit 1
+fi
+
package/quagga/quagga.mk +0 -0
index 22e90ad..5d9c292 100644
@@ -75,6 +75,12 @@ endif
 define QUAGGA_INSTALL_INIT_SYSTEMD
Quoted 8 lines

package/quagga/quagga.mk endif · line 80

Quoted 5 lines
package/quagga/quagga.mk +3 -0
 	$(INSTALL) -D -m 644 package/quagga/quagga_tmpfiles.conf \
 		$(TARGET_DIR)/usr/lib/tmpfiles.d/quagga.conf
+
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service

In the commit log you mention zebra, here it's bandwidthd. /me is
confused.

That was simply my mistake in copying and pasting that section of code from my bandwidthd stuff without properly editing.

Quoted 8 lines

at .service

Quoted 8 lines
package/quagga/quagga.mk +3 -0
+
+	$(INSTALL) -D -m 755 package/quagga/quagga-shim \
+		$(TARGET_DIR)/usr/sbin/quagga-shim
 endef
 
 $(eval $(autotools-package))
at .service renamed from package/quagga/quagga +0 -0

b/package/quagga/quagga at .service

Quoted 9 lines
Quoted 9 lines

new file mode 100644
index 0000000..3e98318

package/quagga/quagga at .service new file +4 -0
@@ -0,0 +1,19 @@
+[Unit]
+Description=Quagga %i routing daemon
+PartOf=quagga.service
+ReloadPropagatedFrom=quagga.service
Quoted 8 lines

package/quagga/quagga at .service

Quoted 8 lines
package/quagga/quagga at .service new file +8 -0
+Wants=quagga at zebra.service
+
+[Service]
+PrivateTmp=true
+KillMode=mixed
+Type=forking
+EnvironmentFile=/etc/default/quagga-%i
+ExecStart=/usr/sbin/quagga-shim %i --daemon $OPTS -f

/etc/quagga/%i.conf -i /var/run/quagga/%i.pid

Quoted 2 lines

systemd prefers non-forking daemons because then it has better

control over it.

Quoted 1 line

Since you give a --daemon option here, I assume that they are

perfectly capable

Quoted 1 line

of running as a simple service. Can you give that a try?

Sure, but would systemd's preference still apply with sh/bash in between them?

Quoted 12 lines

Two more things...

package/quagga/quagga at .service line 14

Quoted 2 lines · Nathaniel Roach
package/quagga/quagga at .service new file +2 -0
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service

I believe it should actually be installed as quagga at .service, and then
symlinked from /etc.

I'm not so sure of this, but wouldn't it actually make sense to install all
services that are selected in the config? Like

Quoted 8 lines

QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_ZEBRA) += zebra QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_BGPD) += bgpd
...

	$(foreach service,$(QUAGGA_SERVICES_y),\
		ln -sf ../../../../usr/lib/systemd/system/quagga at .service \

$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/$(service).service$(sep))

You're right on how the service is used, except that the symlink would be quagga at zebra.service.
I deliberately did not "install" the service file. If we are to do that, we'll also need to create environment files and empty configuration files for all of the selected daemons.

Quoted 4 lines

Regards,
Arnout

Thanks,

Nathaniel

#4 lore raw

PATCH 1/1 package/quagga: Add systemd.service file and helper script

Quoted 12 lines · Nathaniel Roach

[snip]

Quoted 11 lines

So I'd skip the entire case, and instead go straight to ...

[snip]

package/quagga/quagga-shim line 44

Quoted 2 lines
package/quagga/quagga-shim new file +2 -0
+# Check the binary exists
+if [ -e "$QUAGGA_PATH/$BINARY" ]; then

And this check is redundant as well. Just call it, it will error out
automaticaly if the binary doesn't exist.

Quoted 2 lines
(quoted diff) +2 -0
+  shift # Remove the first argument (binary name)
+  "$QUAGGA_PATH/$BINARY" "$@" # Run the daemon with the remaining
Quoted 7 lines
Quoted 5 lines

arguments

Is there any reason to keep the shim around? I.e., why not exec?
Also, the comment is quite redundant.

How about in the .service file simply ExecStart=/bin/sh -c "/usr/sbin/%i ..."?

Yeah, even better. But add an exec, so

ExecStart=/bin/sh -c "exec /usr/sbin/%i ..."

Alternatively, you may be able to use

ExecStart=/usr/bin/env /usr/sbin/%i ...

BTW I would add a comment above it saying that systemd doesn't allow % to be used in the command name, only in the arguments.

[snip]

Quoted 11 lines
Quoted 1 line
Quoted 1 line

+ExecStart=/usr/sbin/quagga-shim %i --daemon $OPTS -f

/etc/quagga/%i.conf -i /var/run/quagga/%i.pid

Quoted 2 lines

systemd prefers non-forking daemons because then it has better

control over it.

Quoted 1 line

Since you give a --daemon option here, I assume that they are

perfectly capable

Quoted 1 line

of running as a simple service. Can you give that a try?

Sure, but would systemd's preference still apply with sh/bash in between them?

With exec or env, there is no intermediate (only temporarily).

Quoted 17 lines
Quoted 17 lines

Two more things...

package/quagga/quagga.mk endif · line 80

Quoted 2 lines · Nathaniel Roach
package/quagga/quagga.mk +2 -0
+	$(INSTALL) -D -m 644 package/quagga/quagga at .service \
+		$(TARGET_DIR)/usr/lib/systemd/system/bandwidthd.service

I believe it should actually be installed as quagga at .service, and then
symlinked from /etc.

I'm not so sure of this, but wouldn't it actually make sense to install all
services that are selected in the config? Like

QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_ZEBRA) += zebra QUAGGA_SERVICES_$(BR2_PACKAGE_QUAGGA_BGPD) += bgpd
...

Quoted 6 lines
Quoted 4 lines
	$(foreach service,$(QUAGGA_SERVICES_y),\
		ln -sf ../../../../usr/lib/systemd/system/quagga at .service \

$(TARGET_DIR)/etc/systemd/system/multi-user.target.wants/$(service).service$(sep))

You're right on how the service is used, except that the symlink would be quagga at zebra.service.

Yes of course.

Quoted 3 lines

I deliberately did not "install" the service file. If we are to do that, we'll also need to create environment files and empty configuration files for all of the selected daemons.

OK. Perhaps mention that in the commit message.

Regards,
Arnout

Quoted 9 lines
Quoted 4 lines

Regards,
Arnout

Thanks,

Nathaniel

Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF