RAUC: Copy old /cfg to the new /cfg during updates

It seems that RAUC doesn't really have a concept of hooks that are
already located on the system being updated. One could completely
replace some steps, but that's not what I want to do because I do not
really want to duplicate the logic for rootfs.

Instead, let's go with hooks stored in the individual bundles. In the
end, I do not think that this is a huge drawback -- the updates will
have to contain this hook "somewhere", either in the rootfs, or in the
bundle metadata. Having it just in the bundle metadata is no worse. In
fact, it's better because changes take effect immediately.

How this works:

- an empty tarball is needed because RAUC otherwise attempts to mmap() a
directory when no image file name is given
- because there's an empty tarball, the cfg FS gets wiped at first
- this empty tarball is then extracted over the just-made FS
- finally, our hooks runs and copies stuff from the existing /cfg into
the new /cfg

This all means that any configuration changed after this update won't
survive a reboot into the new software. This should probably be
documented somewhere.

Change-Id: I12b11ab93f53f99f1d8875655fa9ec9575830586
diff --git a/board/czechlight/clearfog/mk-rauc-bundle.sh b/board/czechlight/clearfog/mk-rauc-bundle.sh
index 8d82432..12241fb 100755
--- a/board/czechlight/clearfog/mk-rauc-bundle.sh
+++ b/board/czechlight/clearfog/mk-rauc-bundle.sh
@@ -3,19 +3,30 @@
 RAUC_BUILD_DIR=${BUILD_DIR}/rauc-work-tmp
 RAUC_IMAGE=${BINARIES_DIR}/update.raucb
 ROOTFS_NAME=rootfs.tar.xz
+EMPTY_TAR_NAME=cfg.tar.xz
+RAUC_HOOK=${RAUC_BUILD_DIR}/hook.sh
 
 rm -rf ${RAUC_BUILD_DIR}
 mkdir ${RAUC_BUILD_DIR}
 rm -f ${RAUC_IMAGE}
 
 ln ${BINARIES_DIR}/${ROOTFS_NAME} ${RAUC_BUILD_DIR}/
+tar -cJf ${RAUC_BUILD_DIR}/${EMPTY_TAR_NAME} -T /dev/null
+cp ${BR2_EXTERNAL_CZECHLIGHT_PATH}/board/czechlight/common/rauc-hook.sh ${RAUC_HOOK}
 cat > ${RAUC_BUILD_DIR}/manifest.raucm << EOF
 [update]
 compatible=czechlight-clearfog
 version=dev
 
+[hooks]
+filename=hook.sh
+
 [image.rootfs]
 filename=${ROOTFS_NAME}
+
+[image.cfg]
+filename=${EMPTY_TAR_NAME}
+hooks=post-install
 EOF
 
 rauc \
diff --git a/board/czechlight/common/rauc-hook.sh b/board/czechlight/common/rauc-hook.sh
new file mode 100755
index 0000000..65401d6
--- /dev/null
+++ b/board/czechlight/common/rauc-hook.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+case "$1" in
+  slot-post-install)
+    case "$RAUC_SLOT_CLASS" in
+      cfg)
+        if [[ -d /cfg/etc ]]; then
+          cp -a /cfg/etc ${RAUC_SLOT_MOUNT_POINT}/
+        fi
+        ;;
+      *)
+        echo "Internal error: hook mismatched"
+        exit 11
+    esac
+    ;;
+  *)
+    echo "Internal error: unrecognized hook"
+    exit 11
+    ;;
+esac
+
+exit 0