Merge "systemd: Implement the /cfg mount in a generic manner"
diff --git a/board/czechlight/clearfog/overlay/usr/lib/systemd/system-generators/czehlight-cfg-mount-generator b/board/czechlight/clearfog/overlay/usr/lib/systemd/system-generators/czehlight-cfg-mount-generator
new file mode 100755
index 0000000..3197722
--- /dev/null
+++ b/board/czechlight/clearfog/overlay/usr/lib/systemd/system-generators/czehlight-cfg-mount-generator
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+set -e
+
+if grep -q rauc.slot=A /proc/cmdline; then
+  RAUC_SLOT_NO=0
+  RAUC_SLOT_NAME=A
+elif grep -q rauc.slot=B /proc/cmdline; then
+  RAUC_SLOT_NO=1
+  RAUC_SLOT_NAME=B
+else
+  echo "Cannot determine active RAUC rootfs slot"
+  exit 1
+fi
+
+# sed magic:
+# 1) use `sed -n` so that we only print what's explicitly printed
+# 2) anchor the search between the "[slot.cfg.$RAUC_SLOT_NO]" and any other section
+# 3) look for a line beginning with "device="
+# 4) take stuff which is behind the "="
+# 5) print it
+DEVICE=$(sed -n "/\[slot\.cfg\.${RAUC_SLOT_NO}\]/,/\[.*\]/{/^device=/s/\(.*\)=\(.*\)/\\2/p}" /etc/rauc/system.conf)
+
+if [ x$DEVICE -eq x ]; then
+  echo "Cannot determine device for /cfg from RAUC"
+  exit 1
+fi
+
+if [ ! -b $DEVICE ]; then
+  echo "Device ${DEVICE} is not a block device"
+  exit 1
+fi
+
+cat > $1/cfg.mount <<EOF
+[Unit]
+Description=Persistent config (slot ${RAUC_SLOT_NAME})
+DefaultDependencies=no
+Conflicts=umount.target
+Before=local-fs.target umount.target
+After=swap.target
+
+[Mount]
+What=${DEVICE}
+Where=/cfg
+Type=auto
+Options=relatime,nosuid,nodev
+EOF
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-restore-etc.service b/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-restore-etc.service
index 0540bc9..6e4514e 100644
--- a/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-restore-etc.service
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-restore-etc.service
@@ -3,8 +3,8 @@
 DefaultDependencies=no
 Conflicts=umount.target
 Before=local-fs.target umount.target
-After=cfg-storage.service
-Requires=cfg-storage.service etc-overlay.service
+After=cfg.mount
+Requires=cfg.mount etc-overlay.service
 ConditionDirectoryNotEmpty=/cfg/etc
 
 [Service]
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-storage.service b/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-storage.service
deleted file mode 100644
index 4ec5cde..0000000
--- a/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-storage.service
+++ /dev/null
@@ -1,12 +0,0 @@
-[Unit]
-Description=Mount persistent config at /cfg
-DefaultDependencies=no
-Conflicts=umount.target
-Before=local-fs.target umount.target
-After=etc-overlay.service
-
-[Service]
-Type=oneshot
-# This is ugly, but required due to the limitations of the mount units' names
-# (I need two "duplicates" which differ in the ConditionKernelCommandLine)
-ExecStart=/bin/sh -c '(grep -q rauc.slot=A /proc/cmdline && mount -o relatime,nodev,nosuid,errors=panic /dev/mmcblk0p2 /cfg) || (grep -q rauc.slot=B /proc/cmdline && mount -o relatime,nodev,nosuid,errors=panic /dev/mmcblk0p4 /cfg)'
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-storage.service b/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-storage.service
deleted file mode 120000
index 600c2b9..0000000
--- a/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-storage.service
+++ /dev/null
@@ -1 +0,0 @@
-../cfg-storage.service
\ No newline at end of file