sysrepo: shoot all sysrepo-using services upon any failure

The new sysrepo is not as robust as I would have wished for; for
example, on sdn-roadm-line, the cla-sysrepo daemon segfaults deep in the
sysrepo library upon the first service start. This is bad because it
leaves behind some of the module_change_cb subscriptions, and as a
result, it is not possible to commit changes to, say,
/czechlight-roadm-device:media-channels. As a result, the whole box is
completely unusable.

While the real fix is for sysrepo upstream to be made, there's a rather
urgent need for a workaround. The hot fix is to throw away the SHM
content, which can be only done when there's no sysrepo-using
application running. Let's solve this by relocating the sysrepo's SHM
data into an extra mountpoint (/run/sysrepo in this case), and by
several layers of systemd magic which attempt to "do the right thing"
whenever *any* sysrepo user bites the dust. Here's the idea:

- the mount is created first,
- then something dies, which triggers a restart of run-sysrepo.mount via
run-sysrepo.mount's `PartOf` statement,
- this kills everything else via that everything elses `BindsTo`
statement.

And restarting a mount service for a tmpfs filesystem indeed remounts,
which is nice because it effectively cleans that filesystem, yay.

Change-Id: I06b8491499255d7883e4fbbacd0bb04b25c2962b
diff --git a/package/reset-sysrepo/reset-sysrepo.mk b/package/reset-sysrepo/reset-sysrepo.mk
new file mode 100644
index 0000000..3b56fd8
--- /dev/null
+++ b/package/reset-sysrepo/reset-sysrepo.mk
@@ -0,0 +1,36 @@
+RESET_SYSREPO_INSTALL_TARGET = YES
+
+define RESET_SYSREPO_PATCH_DEV_SHM
+        sed -i \
+                's|^#define SR_SHM_DIR .*|#define SR_SHM_DIR "/run/sysrepo"|' \
+                $(@D)/src/common.h.in
+endef
+
+SYSREPO_PRE_PATCH_HOOKS += RESET_SYSREPO_PATCH_DEV_SHM
+SYSREPO_POST_RSYNC_HOOKS += RESET_SYSREPO_PATCH_DEV_SHM
+
+define RESET_SYSREPO_INSTALL_TARGET_CMDS
+	$(INSTALL) -D -m 0644 \
+		--target-directory $(TARGET_DIR)/usr/lib/systemd/system/ \
+		$(BR2_EXTERNAL_CZECHLIGHT_PATH)/package/reset-sysrepo/run-sysrepo.mount
+	$(INSTALL) -d -m 0755 $(TARGET_DIR)/usr/lib/systemd/system/run-sysrepo.mount.d/
+	for UNIT in \
+		cla-sdn-inline.service \
+		cla-sdn-roadm-add-drop.service \
+		cla-sdn-roadm-coherent-a-d.service \
+		cla-sdn-roadm-line.service \
+		lldp-systemd-networkd-sysrepo.service \
+		netopeer2.service \
+		sysrepo-persistent-cfg.service \
+		velia.service \
+	; do \
+		echo "Adding systemd drop-ins $${UNIT} <-> /run/sysrepo"; \
+		$(INSTALL) -d -m 0755 $(TARGET_DIR)/usr/lib/systemd/system/$${UNIT}.d/ ; \
+		echo -e "[Unit]\nBindsTo=run-sysrepo.mount\nAfter=run-sysrepo.mount\nPartOf=run-sysrepo.mount" \
+			> $(TARGET_DIR)/usr/lib/systemd/system/$${UNIT}.d/reset-sysrepo.conf ; \
+		echo -e "[Unit]\nPartOf=$${UNIT}" \
+			> $(TARGET_DIR)/usr/lib/systemd/system/run-sysrepo.mount.d/$${UNIT}.conf ; \
+	done
+endef
+
+$(eval $(generic-package))