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/openpower-software-manager/obmc-flash-bios b/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
index 948c6ec..6e4aa78 100644
--- a/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
+++ b/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
@@ -270,6 +270,27 @@
   fi
 }
 
+ubi_cleanup() {
+    # When ubi_cleanup is run, it expects one or no active version.
+    activeVersion=$(busctl --list --no-pager tree \
+            org.open_power.Software.Host.Updater | \
+            grep /xyz/openbmc_project/software/ | tail -c 9)
+
+    if [[ -z "$activeVersion" ]]; then
+        vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | cut -c 14-)
+        vols=(${vols})
+    else
+        vols=$(ubinfo -a | grep -e "pnor-ro-" -e "pnor-rw-" | \
+                grep -v "$activeVersion" | cut -c 14-)
+        vols=(${vols})
+    fi
+
+    for (( index=0; index<${#vols[@]}; index++ )); do
+         name=${vols[index]}
+         umount_ubi
+    done
+}
+
 case "$1" in
   ubiattach)
     attach_ubi
@@ -297,6 +318,9 @@
     name="$2"
     clear_ubi
     ;;
+  ubicleanup)
+    ubi_cleanup
+    ;;
   *)
     echo "Invalid argument"
     exit 1