summaryrefslogtreecommitdiff
path: root/recipes-core/multitech/overlayfs-init/overlayfs.init
diff options
context:
space:
mode:
Diffstat (limited to 'recipes-core/multitech/overlayfs-init/overlayfs.init')
-rw-r--r--recipes-core/multitech/overlayfs-init/overlayfs.init88
1 files changed, 88 insertions, 0 deletions
diff --git a/recipes-core/multitech/overlayfs-init/overlayfs.init b/recipes-core/multitech/overlayfs-init/overlayfs.init
new file mode 100644
index 0000000..4da6217
--- /dev/null
+++ b/recipes-core/multitech/overlayfs-init/overlayfs.init
@@ -0,0 +1,88 @@
+#!/bin/bash
+
+MNT_USER="/mnt/user"
+OVERLAY="$MNT_USER/overlay"
+WORKDIR="$MNT_USER/work"
+UPPERDIR="$MNT_USER/upper"
+LOWERDIR="/"
+USER_ORIG="$UPPERDIR/orig"
+USER_PARTITION="/dev/mtdblock9"
+DO_ERASE_USERDATA_FILE="/mnt/user/.persistent/mts_do_erase_userdata"
+
+loginfo() { logger -s -t overlayfs "info: $@" 1>&2 ; }
+
+mnt_user() {
+ mkdir -p $MNT_USER
+ mount -t jffs2 $USER_PARTITION $MNT_USER
+}
+
+switch_root() {
+ NEW_ROOT=$1
+ mount -o noatime,move --bind /run $NEW_ROOT/run
+ mount -o noatime,move --bind /dev $NEW_ROOT/dev
+ mount -o noatime,move --bind /proc $NEW_ROOT/proc
+ mount -o noatime,move --bind /sys $NEW_ROOT/sys
+ /sbin/pivot_root $NEW_ROOT $NEW_ROOT/orig
+}
+
+# remove old hidden and non-hidden files and folders
+do_remove_old() {
+ shopt -s dotglob
+ rm -rf $MNT_USER/*.old
+ shopt -u dotglob
+}
+
+# select files for deletion
+do_select_old() {
+ if [[ -f $DO_ERASE_USERDATA_FILE ]]; then
+ # when "erase user data" is requested - mark all hidden and non-hidden files for deletion
+ shopt -s dotglob
+ trap "shopt -u dotglob" RETURN
+ loginfo "Erasing user data"
+ else
+ # when "clear user data" is requested - mark all non-hidden files for deletion
+ loginfo "Clearing user data"
+ fi
+
+ for FILE_PATH in "$MNT_USER"/*; do
+ FILE_NAME=$(basename "$FILE_PATH")
+ # rename all files and folders that exist in /mnt/user
+ mv "$FILE_PATH" "$MNT_USER/$FILE_NAME.old"
+ done
+}
+
+do_rw_mount() {
+ loginfo "Starting RW overlayfs"
+ mount -t tmpfs inittemp /mnt
+ mnt_user
+
+ do_remove_old
+
+ if u-boot printenv default_reset_f; then
+ do_select_old
+ do_remove_old
+ u-boot setenv default_reset_f
+ fi
+
+ mkdir -p $UPPERDIR $WORKDIR $OVERLAY $USER_ORIG ${MNT_USER}/.persistent
+ loginfo "Mounting..."
+ mount -o noatime,lowerdir=$LOWERDIR,upperdir=$UPPERDIR,workdir=$WORKDIR -t overlay overlay $OVERLAY
+
+ mkdir -p ${OVERLAY}/var/persistent
+ mount --bind ${MNT_USER}/.persistent ${OVERLAY}/var/persistent
+
+ switch_root $OVERLAY
+}
+
+do_start() {
+ do_rw_mount
+}
+
+case $1 in
+start)
+ do_start
+;;
+*)
+ echo "Usage: $0 {start}"
+;;
+esac