dual-image: Sync files before sync-manager starts

The sync-manager only sync files when it's changed/deleted, but does not
sync when it's untouched.
If the service starts and there is no change, the files will not be
synced.

Mount the rwfs-alt and add a script to sync the files before the
sync-manager starts, so that they are always synced when the
sync-manager is running.

Tested: Verify the files are synced before the sync-manager starts.

Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: I5613738b7b490201c556a175425b48aebae1505b
diff --git a/meson.build b/meson.build
index f1d0792..fffed1a 100644
--- a/meson.build
+++ b/meson.build
@@ -224,7 +224,10 @@
 endif
 
 if get_option('bmc-static-dual-image').enabled()
-    unit_files += 'static/obmc-flash-bmc-static-mount-alt.service.in'
+    unit_files += [
+        'static/obmc-flash-bmc-static-mount-alt.service.in',
+        'static/obmc-flash-bmc-prepare-for-sync.service.in',
+    ]
 endif
 
 if get_option('sync-bmc-files').enabled()
@@ -240,6 +243,11 @@
     install_data('synclist',
         install_dir:  get_option('sysconfdir')
     )
+
+    install_data('sync-once.sh',
+        install_mode: 'rwxr-xr-x',
+        install_dir: get_option('bindir')
+    )
 endif
 
 if (get_option('verify-signature').enabled() or \
diff --git a/static/obmc-flash-bmc-prepare-for-sync.service.in b/static/obmc-flash-bmc-prepare-for-sync.service.in
new file mode 100644
index 0000000..e12a177
--- /dev/null
+++ b/static/obmc-flash-bmc-prepare-for-sync.service.in
@@ -0,0 +1,13 @@
+[Unit]
+Description=Prepare sync for static dual image layout
+Requires=xyz.openbmc_project.Software.Sync.service
+Before=xyz.openbmc_project.Software.Sync.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+ExecStart=-/usr/bin/obmc-flash-bmc static-altfs jffs2 alt-rwfs rwfs-alt
+ExecStart=-/usr/bin/sync-once.sh
+
+[Install]
+WantedBy=xyz.openbmc_project.Software.Sync.service
diff --git a/sync-once.sh b/sync-once.sh
new file mode 100644
index 0000000..e26d5af
--- /dev/null
+++ b/sync-once.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+# Sync the files/dirs specified in synclist once
+# Usually the sync-manager could sync the file once before it starts, so that
+# it makes sure the synclist is always synced when the sync-manager is running.
+
+SYNCLIST=/etc/synclist
+DEST_DIR=/run/media/rwfs-alt/cow
+
+while read l; do
+    echo rsync -a -R "${l}" "${DEST_DIR}"
+    rsync -a -R "${l}" "${DEST_DIR}"
+done < ${SYNCLIST}