meta-phosphor: mmc-init: Abide by init= from kernel commandline

Use of e.g. systemd-bootchart[1] requires that we override the path to
`init` on the kernel commandline. Currently `mmc-init.sh` hard-codes
`/sbin/init` as the init path and this prevents such boot analysis.

Generalise option processing for the kernel commandline so we can
extract the value for arbitrary key-value pairs and build the required
functionality on top of this new abstraction.

[1]: https://manpages.debian.org/testing/systemd-bootchart/systemd-bootchart.1.en.html

Change-Id: I3d3d441f4861e1ce37954a0fcd49eb15906006a1
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
index c55977f..295bcc1 100644
--- a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-mmc-init/mmc-init.sh
@@ -1,20 +1,21 @@
 #!/bin/sh
 
-# Get the value of the root env variable found in /proc/cmdline
-get_root() {
+kgetopt ()
+{
     _cmdline="$(cat /proc/cmdline)"
-    root=
-    for opt in $_cmdline
+    _optname="$1"
+    _optval="$2"
+    for _opt in $_cmdline
     do
-        case $opt in
-            root=PARTLABEL=*)
-                root=${opt##root=PARTLABEL=}
+        case "$_opt" in
+            "${_optname}"=*)
+                _optval="${_opt##"${_optname}"=}"
                 ;;
             *)
                 ;;
         esac
     done
-    [ -n "$root" ] && echo "$root"
+    [ -n "$_optval" ] && echo "$_optval"
 }
 
 fslist="proc sys dev run"
@@ -61,7 +62,7 @@
 udevadm settle --timeout=10
 
 mkdir -p $rodir
-if ! mount /dev/disk/by-partlabel/"$(get_root)" $rodir -t ext4 -o ro; then
+if ! mount /dev/disk/by-partlabel/"$(kgetopt root=PARTLABEL)" $rodir -t ext4 -o ro; then
     /bin/sh
 fi
 
@@ -102,8 +103,10 @@
 mkdir -p $rodir/var/persist/etc $rodir/var/persist/etc-work $rodir/var/persist/home/root
 mount overlay $rodir/etc -t overlay -o lowerdir=$rodir/etc,upperdir=$rodir/var/persist/etc,workdir=$rodir/var/persist/etc-work
 
+init="$(kgetopt init /sbin/init)"
+
 for f in $fslist; do
     mount --move "$f" "$rodir/$f"
 done
 
-exec switch_root $rodir /sbin/init
+exec switch_root $rodir "$init"