Packages

Nerves System - OpenWRT One (MediaTek MT7981B / Filogic 820)

Current section

Files

Jump to
nerves_system_openwrt_one rootfs_overlay usr sbin mount-data.sh
Raw

rootfs_overlay/usr/sbin/mount-data.sh

#!/bin/sh
#
# Mount /root (= /data via symlink, Nerves convention) from UBI
# volume 5 (rootfs_data). Lazily formats the volume with UBIFS on
# first boot or after corruption. Falls back to tmpfs if the
# persistent mount can't be brought up, so the system always boots
# even if NAND is unhealthy.
#
# Run via erlinit's --pre-run-exec, before the Erlang VM starts.
#
set -u
PATH=/usr/sbin:/usr/bin:/sbin:/bin
export PATH
DATA_DEV=/dev/ubi0_5
DATA_MNT=/root
MOUNT=/bin/mount
MKFS_UBIFS=/usr/sbin/mkfs.ubifs
mkdir -p "$DATA_MNT"
# Already mounted from a prior invocation? (e.g., supervised respawn)
if grep -q " $DATA_MNT " /proc/mounts; then
exit 0
fi
if [ ! -c "$DATA_DEV" ]; then
echo "mount-data: $DATA_DEV is not a UBI volume; using tmpfs at $DATA_MNT" >&2
"$MOUNT" -t tmpfs tmpfs "$DATA_MNT"
exit 0
fi
# First try mounting. If the volume already has a UBIFS, this just works.
if "$MOUNT" -t ubifs "$DATA_DEV" "$DATA_MNT" 2>/dev/null; then
exit 0
fi
# Mount failed — assume the volume is empty/corrupt and reformat.
echo "mount-data: formatting $DATA_DEV with UBIFS" >&2
if "$MKFS_UBIFS" -y -q "$DATA_DEV" \
&& "$MOUNT" -t ubifs "$DATA_DEV" "$DATA_MNT" 2>/dev/null; then
exit 0
fi
echo "mount-data: persistent mount failed; falling back to tmpfs at $DATA_MNT" >&2
"$MOUNT" -t tmpfs tmpfs "$DATA_MNT"