Service files to remove orphaned UBI volumes

This commit provides supplementary service files for
openbmc/openbmc#2264, which adds a function to delete all versions to
each of the BMC and host updaters.

The service files introduced in this change will locate any "orphaned"
BMC or host volumes - that is to say, volumes which do not correspond to
a current version on the system.

Change-Id: If324d24f66ba08fb0b76ca5873addb6bf2446f97
Signed-off-by: Michael Tritz <mtritz@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager.bb b/common/recipes-phosphor/flash/phosphor-software-manager.bb
index 0dc4dfd..fb24ee9 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -77,6 +77,7 @@
     obmc-flash-bmc-updateubootvars@.service \
     reboot-guard-enable.service \
     reboot-guard-disable.service \
+    obmc-flash-bmc-cleanup.service \
     "
 
 # Name of the mtd device where the ubi volumes should be created
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
index d8eed57..2025a05 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -192,6 +192,26 @@
     fi
 }
 
+ubi_cleanup() {
+    # When ubi_cleanup is run, it expects one or no active version.
+    activeVersion=$(busctl --list --no-pager tree \
+            xyz.openbmc_project.Software.BMC.Updater | \
+            grep /xyz/openbmc_project/software/ | tail -c 9)
+
+    if [[ -z "$activeVersion" ]]; then
+        vols=$(ubinfo -a | grep -e "kernel-" -e "rofs-" | cut -c 14-)
+        vols=(${vols})
+    else
+        vols=$(ubinfo -a | grep -e "kernel-" -e "rofs-" | \
+                grep -v "$activeVersion" | cut -c 14-)
+        vols=(${vols})
+    fi
+
+    for (( index=0; index<${#vols[@]}; index++ )); do
+         ubi_remove ${vols[index]}
+    done
+}
+
 remount_ubi() {
   bmcmtd="$(findmtd "bmc")"
   altbmcmtd="$(findmtd "alt-bmc")"
@@ -346,6 +366,9 @@
     name="$2"
     ubi_remove "${name}"
     ;;
+  ubicleanup)
+    ubi_cleanup
+    ;;
   ubisetenv)
     ubi_setenv "$2"
     ;;
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-cleanup.service b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-cleanup.service
new file mode 100644
index 0000000..41769a4
--- /dev/null
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-cleanup.service
@@ -0,0 +1,8 @@
+[Unit]
+Description=Clean up UBI volumes unattached to a BMC version
+
+[Service]
+Type=oneshot
+RemainAfterExit=no
+ExecStart=/usr/bin/env obmc-flash-bmc ubicleanup
+SyslogIdentifier=obmc-flash-bmc