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-bsp/u-boot/files/0001-config-ast-common-Add-conditional-factory-reset-comm.patch b/common/recipes-bsp/u-boot/files/0001-config-ast-common-Add-conditional-factory-reset-comm.patch
new file mode 100644
index 0000000..7cef7aa
--- /dev/null
+++ b/common/recipes-bsp/u-boot/files/0001-config-ast-common-Add-conditional-factory-reset-comm.patch
@@ -0,0 +1,46 @@
+From 90cdc1aa6079d0f2d5894807cbfee4e8125e2b5b Mon Sep 17 00:00:00 2001
+From: "Edward A. James" <eajames@us.ibm.com>
+Date: Thu, 9 Nov 2017 11:39:10 -0600
+Subject: [PATCH] config/ast-common: Add conditional factory reset command
+
+Factory reset (removing persistant rwfs) has to occur before we've
+mounted the rwfs. However, the variable to determine whether or not to
+do the reset is stored in the u-boot env. This is tricky to access
+before rwfs is mounted except in u-boot. So, check it before every boot.
+
+Signed-off-by: Edward A. James <eajames@us.ibm.com>
+---
+ include/configs/ast-common.h | 6 +++++-
+ 1 file changed, 5 insertions(+), 1 deletion(-)
+
+diff --git a/include/configs/ast-common.h b/include/configs/ast-common.h
+index 8150265..41d196a 100644
+--- a/include/configs/ast-common.h
++++ b/include/configs/ast-common.h
+@@ -121,6 +121,8 @@
+ #define CONFIG_ENV_SIZE			0x10000	/* Total Size of Environment Sector */
+ #define CONFIG_ENV_SIZE_REDUND		0x10000
+ 
++#define CONFIG_RWFS_SIZE		0x400000
++
+ #if 0
+ #define CONFIG_BOOTCOMMAND	\
+ 	"fdt addr 20080000; " \
+@@ -137,11 +139,13 @@
+ 	"kernelname=kernel-0 \0" \
+ 	"loadaddr=" __stringify(CONFIG_LOADADDR) "\0" \
+ 	"set_bootargs=setenv bootargs " CONFIG_BOOTARGS " ubi.block=\${ubiblock} root=\${root} \0" \
+-	"obmc_bootcmd=ubi part obmc-ubi; ubi read ${loadaddr} ${kernelname}; bootm ${loadaddr} \0" \
++	"obmc_bootcmd=ubi part obmc-ubi; run do_rwreset; ubi read ${loadaddr} ${kernelname}; bootm ${loadaddr} \0" \
+ 	"verify=yes\0"	\
+ 	"spi_dma=yes\0" \
+ 	"mtdids=" MTDIDS_DEFAULT "\0" \
+ 	"mtdparts=" MTDPARTS_DEFAULT "\0" \
++	"rwfs_size=" __stringify(CONFIG_RWFS_SIZE) "\0" \
++	"do_rwreset=if test \"\${rwreset}\" = \"true\"; then ubi remove rwfs; ubi create rwfs \${rwfs_size}; fi \0" \
+ 	""
+ 
+ #endif	/* __AST_COMMON_CONFIG_H */
+-- 
+1.8.3.1
+
diff --git a/common/recipes-bsp/u-boot/u-boot.inc b/common/recipes-bsp/u-boot/u-boot.inc
index 5cc320f..a5be9f8 100644
--- a/common/recipes-bsp/u-boot/u-boot.inc
+++ b/common/recipes-bsp/u-boot/u-boot.inc
@@ -10,7 +10,8 @@
 
 SRC_URI += "${@bb.utils.contains('MACHINE_FEATURES', 'obmc-ubi-fs', \
     'file://0001-config-ast-common-hack-bootopts.patch \
-     file://0004-config-ast-common-ubi-bootops.patch', '', d)}"
+     file://0004-config-ast-common-ubi-bootops.patch \
+     file://0001-config-ast-common-Add-conditional-factory-reset-comm.patch', '', d)}"
 
 python do_configure () {
     if ((d.getVar("MACHINE_FEATURES", True)) == "obmc-ubi-fs"):
diff --git a/common/recipes-core/base-files/base-files/fstab b/common/recipes-core/base-files/base-files/fstab
index 7d6ac6c..4a07668 100644
--- a/common/recipes-core/base-files/base-files/fstab
+++ b/common/recipes-core/base-files/base-files/fstab
@@ -4,8 +4,6 @@
 proc                 /proc                proc       defaults              0  0
 devpts               /dev/pts             devpts     mode=0620,gid=5       0  0
 tmpfs                /run                 tmpfs      mode=0755,nodev,nosuid,strictatime 0  0
-ubi0:rwfs            /var                 ubifs      defaults,x-systemd.requires=obmc-flash-bmc-reset.service 0  0
-overlay              /etc                 overlay    defaults,x-systemd.requires=prepare-overlay.service,lowerdir=/etc,upperdir=/var/persist/etc,workdir=/var/persist/etc-work 0  0
 tmpfs                /var/volatile        tmpfs      defaults              0  0
 tmpfs                /media               tmpfs      defaults              0  0
-/var/persist/home    /home                none       bind,x-systemd.requires=prepare-overlay.service 0  0
+/var/persist/home    /home                none       bind                  0  0
diff --git a/common/recipes-core/base-files/base-files_%.bbappend b/common/recipes-core/base-files/base-files_%.bbappend
index f282cde..e632d56 100644
--- a/common/recipes-core/base-files/base-files_%.bbappend
+++ b/common/recipes-core/base-files/base-files_%.bbappend
@@ -2,7 +2,7 @@
 
 FILESEXTRAPATHS_prepend := "${@mf_enabled(d, 'obmc-ubi-fs', '${THISDIR}/${PN}:')}"
 
-RDEPENDS_${PN}_append += "${@mf_enabled(d, 'obmc-ubi-fs', 'mount-overlay-help')}"
+RDEPENDS_${PN}_append += "${@mf_enabled(d, 'obmc-ubi-fs', 'preinit-mounts')}"
 
 do_install_append() {
     install -d ${D}/srv
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