init: Add init script to setup /etc mount before starting systemd

Systemd requires /etc mounted and ready at init time. Previously, we
were mounting /etc as an overlay from the persistant filesystem after
systemd started. This caused a variety of issues with systemd and
associated applications (journal, etc).
This change also adds factory reset functionality.

Resolves openbmc/openbmc#2542

Change-Id: Ib8a38e99df39f833026fe4475670a7ec2b9c8d4d
Signed-off-by: Edward A. James <eajames@us.ibm.com>
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager.bb b/common/recipes-phosphor/flash/phosphor-software-manager.bb
index b180022..ed8de0b 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager.bb
+++ b/common/recipes-phosphor/flash/phosphor-software-manager.bb
@@ -79,7 +79,6 @@
     reboot-guard-enable.service \
     reboot-guard-disable.service \
     obmc-flash-bmc-cleanup.service \
-    obmc-flash-bmc-reset.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 1a20660..40ec068 100644
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
+++ b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc
@@ -59,6 +59,14 @@
   echo "${n}"
 }
 
+# Set the u-boot env command that performs the factory reset if requested
+set_do_rwreset() {
+  if ! fw_printenv do_rwreset; then
+    fw_setenv do_rwreset "if test \"\${rwreset}\" = \"true\"; then ubi remove rwfs; ubi create rwfs 0x400000; fi"
+    fw_setenv obmc_bootcmd "ubi part obmc-ubi; run do_rwreset; ubi read \${loadaddr} \${kernelname}; bootm \${loadaddr}"
+  fi
+}
+
 # Make space on flash before creating new volumes. This can be enhanced
 # determine current flash usage. For now only keep a "keepmax" number of them
 ubi_remove_volumes()
@@ -239,6 +247,8 @@
       fi
     done
   done
+
+  set_do_rwreset
 }
 
 # Read the current env variable and set it on the alternate boot env
@@ -323,19 +333,6 @@
   fi
 }
 
-factory_reset() {
-    # A lock file for printenv exists on /var, which isn't mounted when this
-    # function is called. We have to read the rwreset variable out of file.
-    ubootmtd="$(findmtd "u-boot-env")"
-    grep -q -x "rwreset=true" /dev/$ubootmtd
-    if [ "$?" = "0" ]; then
-      ubi_remove "rwfs"
-      reqmtd="bmc"
-      name="rwfs"
-      ubi_rw
-    fi
-}
-
 case "$1" in
   mtduboot)
     reqmtd="$2"
@@ -396,9 +393,6 @@
   rebootguarddisable)
     rebootguarddisable
     ;;
-  reset)
-    factory_reset
-    ;;
   *)
     echo "Invalid argument"
     exit 1
diff --git a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service b/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service
deleted file mode 100644
index 38dc60e..0000000
--- a/common/recipes-phosphor/flash/phosphor-software-manager/obmc-flash-bmc-reset.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Factory reset BMC if reset enabled
-DefaultDependencies=no
-
-[Service]
-Type=oneshot
-RemainAfterExit=no
-ExecStart=/usr/bin/env obmc-flash-bmc reset
-SyslogIdentifier=obmc-flash-bmc
diff --git a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help.bb b/common/recipes-phosphor/mount-overlay-help/mount-overlay-help.bb
deleted file mode 100644
index 4125fd0..0000000
--- a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help.bb
+++ /dev/null
@@ -1,5 +0,0 @@
-inherit obmc-phosphor-license
-
-SYSTEMD_SERVICE_${PN} = "prepare-overlay.service mount-machine-id.service"
-
-inherit obmc-phosphor-systemd
diff --git a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/mount-machine-id.service b/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/mount-machine-id.service
deleted file mode 100644
index 0d72f0a..0000000
--- a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/mount-machine-id.service
+++ /dev/null
@@ -1,14 +0,0 @@
-[Unit]
-Description=Mount machine-id
-Requires=etc.mount
-After=etc.mount
-DefaultDependencies=false
-ConditionPathExists=!/var/persist/etc/machine-id
-
-[Install]
-RequiredBy=local-fs.target
-
-[Service]
-ExecStart=/bin/sh -c "mount --bind /run/machine-id /etc/machine-id"
-Type=oneshot
-RemainAfterExit=no
diff --git a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/prepare-overlay.service b/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/prepare-overlay.service
deleted file mode 100644
index 66843b3..0000000
--- a/common/recipes-phosphor/mount-overlay-help/mount-overlay-help/prepare-overlay.service
+++ /dev/null
@@ -1,9 +0,0 @@
-[Unit]
-Description=Prepare for overlay fs
-RequiresMountsFor=/var
-DefaultDependencies=false
-
-[Service]
-ExecStart=/bin/sh -c "if ! mountpoint /etc; then rm -rf /var/persist/etc-work && mkdir -p /var/persist/etc-work; fi; mkdir -p /var/persist/etc /var/persist/home/root"
-Type=oneshot
-RemainAfterExit=no
diff --git a/common/recipes-phosphor/preinit-mounts/preinit-mounts.bb b/common/recipes-phosphor/preinit-mounts/preinit-mounts.bb
new file mode 100644
index 0000000..e3aaf54
--- /dev/null
+++ b/common/recipes-phosphor/preinit-mounts/preinit-mounts.bb
@@ -0,0 +1,10 @@
+inherit obmc-phosphor-license
+
+SRC_URI += "file://init"
+
+FILES_${PN} += "/sbin/init"
+
+do_install() {
+	install -d ${D}/sbin
+	install -m 0755 ${WORKDIR}/init ${D}/sbin/init
+}
diff --git a/common/recipes-phosphor/preinit-mounts/preinit-mounts/init b/common/recipes-phosphor/preinit-mounts/preinit-mounts/init
new file mode 100644
index 0000000..91f8038
--- /dev/null
+++ b/common/recipes-phosphor/preinit-mounts/preinit-mounts/init
@@ -0,0 +1,19 @@
+#!/bin/sh
+
+if ! mount ubi0:rwfs /var -t ubifs -o defaults; then
+  if ! mount ubi0:rwfs /var -t ubifs -o defaults,ro; then
+    mount tmpfs /var -t tmpfs -o defaults
+  fi
+fi
+
+mkdir -p /var/persist/etc /var/persist/etc-work /var/persist/home/root
+
+rm -rf /var/persist/etc-work/*
+# rm -rf specifically skips . and .. directories; pipe all output to null to avoid the error message
+rm -rf /var/persist/etc-work/.* > /dev/null 2>&1
+
+if ! mount overlay /etc -t overlay -o defaults,lowerdir=/etc,upperdir=/var/persist/etc,workdir=/var/persist/etc-work; then
+  mount overlay /etc -t overlay -o defaults,lowerdir=/etc:/var/persist/etc
+fi
+
+exec /lib/systemd/systemd