clearfog: Introduce a /cfg partition for persistent changes

The idea is to store whatever important bits that need real persistence
in there.

Note that I am deliberately *not* using this as the upper directory for
the /etc overlay. I inherited that company-wide worrying about flash
lifetime, and hence I would like to limit the amount of writes to some
reasonable minimum. That doesn't play terribly well with SW such as
sysrepo which happily stores both persistent and run-time data on disk,
in the same directory -- hence these ugly detours...

I've tried to do the right thing with the alignment:

- we need a 1MB of nothing at the very beginning for U-Boot and its
environment
- the eMMC version of the microSOM has 8GB of storage
- it probably makes sense to split that into two halves for
future-proofness, so that we don't have to deal with overlapping
partitions later
- finally, the 1MB gap between the end of cfg-A and the beginning of
rootfs-B is there to be symmetric with regards to the U-Boot's reserved
space in the first 1MB

Hopefully, we should be able to just (more or less) `dd` stuff in-place.

There's one missing piece: nothing explicitly puts stuff to /cfg/etc/,
yet.

Change-Id: Idc540ecfedf1b17615b01f28cdce5dca9b976260
diff --git a/board/czechlight/clearfog/genimage.cfg b/board/czechlight/clearfog/genimage.cfg
index a0e0471..7633c8f 100644
--- a/board/czechlight/clearfog/genimage.cfg
+++ b/board/czechlight/clearfog/genimage.cfg
@@ -12,10 +12,27 @@
 		partition-type = 0x83
 		image = "rootfs.ext4"
 		offset = 1M
+		size = 1999M
+	}
+
+	partition cfg-A {
+		partition-type = 0x83
+		image = "cfg.ext4"
+		offset = 2000M
+		size = 2000M
 	}
 
 	partition rootfs-B {
 		partition-type = 0x83
 		image = "rootfs.ext4"
+		offset = 4001M
+		size = 1999M
+	}
+
+	partition cfg-B {
+		partition-type = 0x83
+		image = "cfg.ext4"
+		offset = 6000M
+		size = 2000M
 	}
 }
diff --git a/board/czechlight/clearfog/overlay/etc/rauc/system.conf b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
index 8de0da8..11cf7c5 100644
--- a/board/czechlight/clearfog/overlay/etc/rauc/system.conf
+++ b/board/czechlight/clearfog/overlay/etc/rauc/system.conf
@@ -4,8 +4,19 @@
 
 [slot.rootfs.0]
 device=/dev/mmcblk0p1
+type=ext4
 bootname=A
 
-[slot.rootfs.1]
+[slot.cfg.0]
 device=/dev/mmcblk0p2
+type=ext4
+parent=rootfs.0
+
+[slot.rootfs.1]
+device=/dev/mmcblk0p3
 bootname=B
+
+[slot.cfg.1]
+device=/dev/mmcblk0p4
+type=ext4
+parent=rootfs.1
diff --git a/board/czechlight/clearfog/patches/u-boot/boot.patch b/board/czechlight/clearfog/patches/u-boot/boot.patch
index 1d4520b..f826cb3 100644
--- a/board/czechlight/clearfog/patches/u-boot/boot.patch
+++ b/board/czechlight/clearfog/patches/u-boot/boot.patch
@@ -88,7 +88,7 @@
 +CONFIG_WDT=y
 +CONFIG_WDT_ORION=y
 diff --git a/include/configs/clearfog.h b/include/configs/clearfog.h
-index 5061f6c6fd..2465b3d933 100644
+index 5061f6c6fd..592c7cc499 100644
 --- a/include/configs/clearfog.h
 +++ b/include/configs/clearfog.h
 @@ -32,6 +32,11 @@
@@ -143,7 +143,7 @@
 +	"    if test ${BOOT_B_LEFT} -gt 0; then" \
 +	"      setexpr BOOT_B_LEFT ${BOOT_B_LEFT} - 1;" \
 +	"      echo \"Found valid slot B, ${BOOT_B_LEFT} attempts remaining\";" \
-+	"      setenv rauc_part 2;" \
++	"      setenv rauc_part 3;" \
 +	"      setenv rauc_slot B;" \
 +	"    fi;" \
 +	"  fi;" \
diff --git a/board/czechlight/common/mk-empty-cfg-fs.sh b/board/czechlight/common/mk-empty-cfg-fs.sh
new file mode 100755
index 0000000..90966c7
--- /dev/null
+++ b/board/czechlight/common/mk-empty-cfg-fs.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+CFG_FS=${BINARIES_DIR}/cfg.ext4
+rm -f ${CFG_FS}
+${HOST_DIR}/sbin/mkfs.ext4 -L cfg -O ^64bit -e panic ${CFG_FS} 256M
diff --git a/board/czechlight/common/overlay/cfg/.keep b/board/czechlight/common/overlay/cfg/.keep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/board/czechlight/common/overlay/cfg/.keep
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
new file mode 100644
index 0000000..0540bc9
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-restore-etc.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Restore /etc from the Persistent config
+DefaultDependencies=no
+Conflicts=umount.target
+Before=local-fs.target umount.target
+After=cfg-storage.service
+Requires=cfg-storage.service etc-overlay.service
+ConditionDirectoryNotEmpty=/cfg/etc
+
+[Service]
+Type=oneshot
+ExecStart=/bin/sh -c 'cp -a /cfg/etc/* /etc/'
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
new file mode 100644
index 0000000..e1256a8
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/cfg-storage.service
@@ -0,0 +1,12 @@
+[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 /dev/mmcblk0p2 /cfg) || (grep -q rauc.slot=B /proc/cmdline && mount -o relatime,nodev,nosuid /dev/mmcblk0p4 /cfg)'
diff --git a/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-restore-etc.service b/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-restore-etc.service
new file mode 120000
index 0000000..7ee72f5
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-restore-etc.service
@@ -0,0 +1 @@
+../cfg-restore-etc.service
\ No newline at end of file
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
new file mode 120000
index 0000000..600c2b9
--- /dev/null
+++ b/board/czechlight/common/overlay/usr/lib/systemd/system/local-fs.target.wants/cfg-storage.service
@@ -0,0 +1 @@
+../cfg-storage.service
\ No newline at end of file
diff --git a/configs/czechlight_clearfog_defconfig b/configs/czechlight_clearfog_defconfig
index 863bed3..7bebbd4 100644
--- a/configs/czechlight_clearfog_defconfig
+++ b/configs/czechlight_clearfog_defconfig
@@ -12,6 +12,7 @@
 BR2_TARGET_GENERIC_GETTY_BAUDRATE_115200=y
 # BR2_TARGET_GENERIC_REMOUNT_ROOTFS_RW is not set
 BR2_ROOTFS_OVERLAY="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/overlay/ $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/overlay/"
+BR2_ROOTFS_POST_BUILD_SCRIPT="$(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/common/mk-empty-cfg-fs.sh"
 BR2_ROOTFS_POST_IMAGE_SCRIPT="support/scripts/genimage.sh"
 BR2_ROOTFS_POST_SCRIPT_ARGS="-c $(BR2_EXTERNAL_CZECHLIGHT_PATH)/board/czechlight/clearfog/genimage.cfg"
 BR2_LINUX_KERNEL=y