systemd: prepare the /etc overlay prior to launching systemd

systemd performs a bunch of critical tasks prior to loading any unit
files from disk, or running generators. Examples of this include the
/etc/machine-id setup (which we worked around by a bind mount some time
ago), or reading )and setting) the hostname configuration.

Rather than playing the whack-a-mole game over and over again by adding
just enough scripts to re-kick a given daemon/subsystem once a
particular /etc file becomes available, this change moves /etc setup
(which includes the overlay and the /cfg filesystem as well) into an
"initramfs" (without actually using a separate initramfs, of course).

Thanks to Mantas Mikulėnas (grawity on IRC) for suggesting this.

Change-Id: I9ec4d7f1243929ea498142d435ae6689c53b6097
diff --git a/package/czechlight-cfg-fs/init-czechlight.sh b/package/czechlight-cfg-fs/init-czechlight.sh
new file mode 100644
index 0000000..a550906
--- /dev/null
+++ b/package/czechlight-cfg-fs/init-czechlight.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+echo "Preparing /etc overlay"
+/bin/mount -t tmpfs tmpfs /.ov -o mode=0700
+/bin/mkdir /.ov/etc-u
+/bin/mkdir /.ov/etc-w
+/bin/mount overlay -t overlay /etc -olowerdir=/etc,upperdir=/.ov/etc-u,workdir=/.ov/etc-w
+
+/bin/mount -t proc proc /proc -o rw,nosuid,nodev,noexec,relatime
+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
+/bin/umount /proc
+echo "RAUC: active slot ${RAUC_SLOT_NAME}"
+
+# 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 = 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
+
+echo "Mounting /cfg"
+/bin/mount ${DEVICE} /cfg -t auto -o relatime,nosuid,nodev
+
+if [ -d /cfg/etc ]; then
+  echo "Restoring /etc from /cfg"
+  /bin/cp -a /cfg/etc/* /etc/
+fi
+
+exec /sbin/init