meta-phosphor: initrdscript: Monotonic date setting

This change intends to make the BMC date monotonic with respect to each
powercycle or reboot. It's not a perfect mechanism for maintaining
accurate time, but it's better than resetting to the epoch every single
reboot.

Change-Id: I76ecc49d9a77fc4846b71f1f1ce5398ca3873d72
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/files/obmc-init.sh b/meta-phosphor/recipes-phosphor/initrdscripts/files/obmc-init.sh
index f778f92..c493160 100644
--- a/meta-phosphor/recipes-phosphor/initrdscripts/files/obmc-init.sh
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/files/obmc-init.sh
@@ -420,6 +420,16 @@
 
 mkdir -p $upper $work
 
+# Opportunisticly set a sane BMC date based on a file that gets
+# written right before rebooting or powercycling. If none exists,
+# use the image build date.
+files="$upper/var/lib/systemd/random-seed $rodir/etc/os-release"
+time=$(find $files -exec stat -c %Y {} \; | sort -n | tail -n 1)
+# Allow RTC coordinated time to supersede this setting
+if [ "$(date +%s)" -lt "$time" ]; then
+  date -s @$((time + 5)) || true
+fi
+
 mount -t overlay -o lowerdir=$rodir,upperdir=$upper,workdir=$work cow /root
 
 while ! chroot /root /bin/sh -c "test -x '$init' -a -s '$init'"