meta-facebook: santabarbara: back up USB UART logs before BMC reboot

USB UART console logs were being lost on chassis AC cycle and BMC reboot
since they only lived in volatile storage. Back them up first so they
are still available for debugging afterward.

Change-Id: I2f9661cf1ac686e892b3aa75ed37c47c28b4c07e
Signed-off-by: Fred Chen <Fred.Chen@quantatw.com>
diff --git a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
index f929b75..22b6fac 100644
--- a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
+++ b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/chassis-powercycle
@@ -6,6 +6,9 @@
 # shellcheck source=meta-facebook/recipes-fb/obmc_functions/files/fb-common-functions
 source /usr/libexec/fb-common-functions
 
+# shellcheck source=meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log
+source /usr/libexec/phosphor-state-manager/move-uart-log
+
 #Sled cycle
 echo "Starting Chassis Power Cycle"
 
@@ -95,6 +98,7 @@
 if [ "$ChassisNumber" == 0 ]
 then
   echo "Starting Chassis Power Cycle"
+  move-uart-log
   chassis-power-cycle
 elif [ "$ChassisNumber" == 1 ] || [ "$ChassisNumber" == 2 ] || 
     [ "$ChassisNumber" == 3 ] || [ "$ChassisNumber" == 4 ]
diff --git a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log
new file mode 100644
index 0000000..a01cbd7
--- /dev/null
+++ b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log
@@ -0,0 +1,28 @@
+#!/bin/bash
+
+# /run is tmpfs and gets wiped on chassis AC cycle or BMC reboot, so move
+# the UART console logs to persistent storage beforehand.
+move-uart-log() {
+    local log_dir="/run/log"
+    local dest_dir="/var/log"
+
+    shopt -s nullglob
+    local files=("$log_dir"/*.log "$log_dir"/*.log.1)
+    shopt -u nullglob
+
+    if [ "${#files[@]}" -eq 0 ]; then
+        return 0
+    fi
+
+    if ! mv -f "${files[@]}" "$dest_dir"/; then
+        echo "Failed to move uart log from $log_dir to $dest_dir" >&2
+        return 1
+    fi
+
+    sync "$dest_dir"
+    return 0
+}
+
+if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
+    move-uart-log
+fi
diff --git a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log.service b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log.service
new file mode 100644
index 0000000..1b95638
--- /dev/null
+++ b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager/move-uart-log.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Move UART console logs to persistent storage before BMC reboot
+DefaultDependencies=no
+Before=shutdown.target
+
+[Service]
+Type=oneshot
+RemainAfterExit=true
+ExecStart=/usr/libexec/phosphor-state-manager/move-uart-log
+
+[Install]
+WantedBy=shutdown.target
diff --git a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager_%.bbappend b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager_%.bbappend
index 02f0ba0..39c7247 100644
--- a/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager_%.bbappend
+++ b/meta-facebook/meta-santabarbara/recipes-phosphor/state/phosphor-state-manager_%.bbappend
@@ -95,14 +95,21 @@
     file://discover-sys-init.conf \
     file://phosphor-state-manager-init \
     file://phosphor-state-manager-init.conf \
+    file://move-uart-log \
+    file://move-uart-log.service \
     "
 
+FILES:${PN}-bmc += "${systemd_system_unitdir}/shutdown.target.wants/move-uart-log.service"
+
 RDEPENDS:${PN}:append = " bash"
 
 do_install:append() {
     install -d ${D}${systemd_system_unitdir}
     install -m 0644 ${UNPACKDIR}/*.service ${D}${systemd_system_unitdir}/
 
+    install -d ${D}${systemd_system_unitdir}/shutdown.target.wants
+    ln -sf ${systemd_system_unitdir}/move-uart-log.service ${D}${systemd_system_unitdir}/shutdown.target.wants/move-uart-log.service
+
     install -d ${D}${libexecdir}/${PN}
     install -m 0755 ${UNPACKDIR}/chassis-poweroff ${D}${libexecdir}/${PN}/
     install -m 0755 ${UNPACKDIR}/chassis-poweron ${D}${libexecdir}/${PN}/
@@ -113,6 +120,7 @@
     install -m 0755 ${UNPACKDIR}/host-powerreset ${D}${libexecdir}/${PN}/
     install -m 0755 ${UNPACKDIR}/power-cmd ${D}${libexecdir}/${PN}/
     install -m 0755 ${UNPACKDIR}/phosphor-state-manager-init ${D}${libexecdir}/${PN}/
+    install -m 0755 ${UNPACKDIR}/move-uart-log ${D}${libexecdir}/${PN}/
 }
 SYSTEMD_OVERRIDE:${PN}-discover += "discover-sys-init.conf:phosphor-discover-system-state@0.service.d/discover-sys-init.conf"
 SYSTEMD_OVERRIDE:${PN}-systemd-target-monitor += "phosphor-state-manager-init.conf:phosphor-systemd-target-monitor.service.d/phosphor-state-manager-init.conf"