meta-phosphor: add phosphor-static-norootfs-init recipe

There are two existing recipes for early mounting procedures:
  * preinit-mounts - for obmc-ubi-fs
  * phosphor-mmc-init - for phosphor-mmc

Add one for the static-norootfs image type.  This one is similar to
the preinit-mounts, except for it mounts /var as an overlay to avoid
violating FHS expectations (See openbmc/openbmc#3658 as an example of
why this is problematic).  I did not directly change preinit-mounts and
fix it because there are likely field upgrade issues in doing this for
existing users.

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I62ab288c2d139cfecadeb191d576e2753120e123
diff --git a/meta-phosphor/recipes-core/base-files/base-files_%.bbappend b/meta-phosphor/recipes-core/base-files/base-files_%.bbappend
index 8ea8c16..acfbab7 100644
--- a/meta-phosphor/recipes-core/base-files/base-files_%.bbappend
+++ b/meta-phosphor/recipes-core/base-files/base-files_%.bbappend
@@ -15,3 +15,4 @@
 }
 
 RDEPENDS:${PN}:append:df-obmc-ubi-fs = " preinit-mounts"
+RDEPENDS:${PN}:append:df-obmc-static-norootfs = " phosphor-static-norootfs-init"
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init.bb b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init.bb
new file mode 100644
index 0000000..61762a5
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init.bb
@@ -0,0 +1,47 @@
+SUMMARY = "Phosphor OpenBMC pre-init scripts for static-norootfs layout"
+DESCRIPTION = "Phosphor OpenBMC filesystem mount implementation for static-norootfs"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+PR = "r1"
+
+SOURCE_FILES = "\
+    init \
+    10-early-mounts \
+    20-udev \
+    30-ubiattach-or-format \
+    50-mount-persistent \
+    "
+SRC_URI += "\
+    ${@' '.join(\
+        [ 'file://' + x for x in d.getVar('SOURCE_FILES', True).split()])} \
+    "
+
+S = "${WORKDIR}"
+
+inherit allarch
+inherit update-alternatives
+
+PKG_INSTALL_DIR="${libexecdir}/${BPN}"
+FILES:${PN} += "${PKG_INSTALL_DIR}"
+
+do_install() {
+    install -d ${D}${PKG_INSTALL_DIR}
+
+    for f in ${SOURCE_FILES} ; do
+        install -m 0755 ${S}/$f ${D}${PKG_INSTALL_DIR}/$f
+    done
+}
+
+RDEPENDS:${PN} += " \
+    ${VIRTUAL-RUNTIME_base-utils} \
+    mtd-utils-ubifs \
+    udev \
+"
+
+ALTERNATIVE_LINK_NAME[init] = "${base_sbindir}/init"
+# Use a number higher than the systemd init alternative so that
+# ours is enabled instead.
+ALTERNATIVE_PRIORITY[init] ?= "400"
+
+ALTERNATIVE:${PN} = "init"
+ALTERNATIVE_TARGET[init] = "${PKG_INSTALL_DIR}/init"
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/10-early-mounts b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/10-early-mounts
new file mode 100644
index 0000000..f743644
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/10-early-mounts
@@ -0,0 +1,6 @@
+#!/bin/sh
+
+mount dev dev -tdevtmpfs
+mount sys sys -tsysfs
+mount proc proc -tproc
+mount tmpfs run -t tmpfs -o mode=755,nodev
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/20-udev b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/20-udev
new file mode 100644
index 0000000..293b982
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/20-udev
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# We need to run udevd in order to get the /dev/mtd volumes to show up.
+if udevd --daemon; then
+    udevadm trigger --type=devices --action=add && \
+        udevadm settle --timeout=10
+
+    udevadm control --exit
+fi
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/30-ubiattach-or-format b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/30-ubiattach-or-format
new file mode 100644
index 0000000..8a0cbc6
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/30-ubiattach-or-format
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+if [ ! -e /dev/mtd/rwfs ]; then
+    exit 1
+fi
+
+if ! ubiattach -p /dev/mtd/rwfs > /dev/null ; then
+    if ! ubiformat --yes /dev/mtd/rwfs ; then
+        exit 1
+    fi
+
+    if ! ubiattach -p /dev/mtd/rwfs ; then
+        exit 1
+    fi
+
+    if ! ubimkvol /dev/ubi0 -N rwfs -m ; then
+        exit 1
+    fi
+fi
+
+mkdir -p /run/mnt-persist
+mount -t ubifs ubi0:rwfs /run/mnt-persist -o sync,compr=zstd
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/50-mount-persistent b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/50-mount-persistent
new file mode 100644
index 0000000..6b6d9a4
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/50-mount-persistent
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+PERSIST=/run/mnt-persist
+
+if [ ! -d $PERSIST ]; then
+    exit 0
+fi
+
+for m in var etc home/root; do
+    if [ ! -d $PERSIST/$m-data ]; then
+        mkdir -p $PERSIST/$m-data
+    fi
+
+    if [ ! -d $PERSIST/$m-work ]; then
+        mkdir -p $PERSIST/$m-work
+    fi
+
+    mount overlay /$m -t overlay -o \
+        lowerdir=/$m,upperdir=$PERSIST/$m-data,workdir=$PERSIST/$m-work
+done
diff --git a/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/init b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/init
new file mode 100644
index 0000000..01ce019
--- /dev/null
+++ b/meta-phosphor/recipes-phosphor/initrdscripts/phosphor-static-norootfs-init/init
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+cd /
+
+for f in /usr/libexec/phosphor-static-norootfs-init/*; do
+    if [ "$(basename "$f")" = "init" ]; then
+        continue
+    fi
+
+    $f
+done
+
+exec /lib/systemd/systemd