openpower-software-manager: Move squashfsmount.service logic to script

The service files that manage the pnor ubi volumes and mount points are
starting to get too much logic in them. Move the logic to a shell script.

Change-Id: I4396bcd9fcfd253eeaa3a5ce00621d97386ff399
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
index acc550b..cab5f86 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios
@@ -8,6 +8,20 @@
   echo "${m}"
 }
 
+# Get the ubi device number (ubiX_Y)
+findubi() {
+  u="$(grep -xl "$1" /sys/class/ubi/ubi?/subsystem/ubi*/name)"
+  u="${u%/name}"
+  u="${u##*/}"
+  echo "${u}"
+}
+
+# Get the mount information
+is_mounted() {
+  grep -q "$1" /proc/mounts
+  return $?
+}
+
 # Attach the pnor mtd device to ubi
 attach_ubi() {
   pnormtd="$(findmtd pnor)"
@@ -33,10 +47,48 @@
   fi
 }
 
+mount_squashfs() {
+  pnormtd="$(findmtd pnor)"
+  pnor="${pnormtd#mtd}"
+  ubidev="/dev/ubi${pnor}"
+  mountdir="/media/${name}"
+
+  if [ ! -d "${mountdir}" ]; then
+    mkdir "${mountdir}"
+  fi
+
+  # Create a static ubi volume of arbitrary size 32MB,
+  # the volume will shrink to fit the pnor image if smaller
+  vol="$(findubi "${name}")"
+  if [ -z "${vol}" ]; then
+    ubimkvol "${ubidev}" -N "${name}" -s 32MiB --type=static
+    vol="$(findubi "${name}")"
+  fi
+
+  # Create a ubi block needed for read-only volumes,
+  # and update the volume with the pnor squashfs image
+  ubidevid="${vol#ubi}"
+  block="/dev/ubiblock${ubidevid}"
+  if [ ! -e "$block" ]; then
+    img="/tmp/images/${version}/pnor.xz.squashfs"
+    ubiblock --create "/dev/ubi${ubidevid}"
+    ubiupdatevol "/dev/ubi${ubidevid}" "${img}"
+  fi
+
+  if ! is_mounted "${name}"; then
+    mount -t squashfs -o ro "${block}" "${mountdir}"
+  fi
+}
+
 case "$1" in
   ubiattach)
     attach_ubi
     ;;
+  squashfsmount)
+    name="$2"
+    version="$3"
+    mount_squashfs
+    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-squashfsmount@.service b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-squashfsmount@.service
index 5cca347..9109532 100644
--- a/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-squashfsmount@.service
+++ b/meta-openbmc-machines/meta-openpower/common/recipes-phosphor/flash/openpower-software-manager/obmc-flash-bios-squashfsmount@.service
@@ -6,9 +6,4 @@
 [Service]
 Type=oneshot
 RemainAfterExit=no
-ExecStart=/bin/sh -c '{sbindir}/ubimkvol /dev/ubi0 -N pnor-ro-%i -s 32MiB --type=static; \
-                      mkdir /media/pnor-ro-%i; \
-                      volumeid%i=`ubinfo -d 0 -N pnor-ro-%i | grep "Volume ID" | sed -e "s|Volume ID:||" -e "s/^ *//" | grep -o "^\S*"`; \
-                      ubiblock --create /dev/ubi0_$volumeid%i; \
-                      ubiupdatevol /dev/ubi0_$volumeid%i /tmp/images/%i/pnor.xz.squashfs; \
-                      mount -t squashfs -o ro /dev/ubiblock0_$volumeid%i /media/pnor-ro-%i'
+ExecStart={sbindir}/obmc-flash-bios squashfsmount pnor-ro-%i %i