meta-facebook: add emmc-init package
Add a service and script for detecting the state of the eMMC
device and formatting it if needed.
Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I4618d1691dedcff582772a0749c5797ad052f238
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/meta-facebook/recipes-fb/emmc-init/files/emmc-init b/meta-facebook/recipes-fb/emmc-init/files/emmc-init
new file mode 100644
index 0000000..0ee9007
--- /dev/null
+++ b/meta-facebook/recipes-fb/emmc-init/files/emmc-init
@@ -0,0 +1,48 @@
+#!/bin/bash -e
+
+DEVICE_NODE="/dev/mmcblk0"
+DEFAULT_FS_TYPE="btrfs"
+
+#
+# check device node exist
+#
+if [[ ! -b "${DEVICE_NODE}" ]]; then
+ echo "${DEVICE_NODE} is not available"
+ exit 0
+fi
+
+#
+# check if device mounted correctly
+#
+if ! (mount || true) | grep "${DEVICE_NODE}"> /dev/null; then
+ echo "${DEVICE_NODE} is not mounted"
+else
+ echo "${DEVICE_NODE} is mounted"
+ exit 0
+fi
+
+#
+# check filesystem
+#
+FS_TYPE_CHECK="$(blkid "${DEVICE_NODE}" | sed 's/.*TYPE="\([^"]*\).*/\1/' || true)"
+
+if [[ "${FS_TYPE_CHECK}" = "" ]]; then
+ FS_TYPE="unknown"
+else
+ FS_TYPE="${FS_TYPE_CHECK}"
+fi
+
+case "${FS_TYPE}" in
+ "${DEFAULT_FS_TYPE}")
+ echo "Filesystem (${FS_TYPE}) found on ${DEVICE_NODE}"
+ ;;
+
+ *)
+ echo "No or unexpected filesystem (${FS_TYPE}) found on ${DEVICE_NODE}"
+ if ! output=$(mkfs."${DEFAULT_FS_TYPE}" --force "${DEVICE_NODE}" 2>&1); then
+ echo "failed to create ${DEFAULT_FS_TYPE} on ${DEVICE_NODE}:"
+ echo "${output}"
+ exit 1
+ fi
+ ;;
+esac
diff --git a/meta-facebook/recipes-fb/emmc-init/files/emmc-init.service b/meta-facebook/recipes-fb/emmc-init/files/emmc-init.service
new file mode 100644
index 0000000..1a5ddcf
--- /dev/null
+++ b/meta-facebook/recipes-fb/emmc-init/files/emmc-init.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=eMMC initialize check
+Before=mnt-data.mount
+
+[Service]
+ExecStart=/usr/libexec/emmc-init/emmc-init
+Type=oneshot
+RemainAfterExit=yes
+
+[Install]
+WantedBy=multi-user.target