diff options
Diffstat (limited to 'recipes-core/multitech/overlayfs-init-jffs2/overlayfs')
-rwxr-xr-x | recipes-core/multitech/overlayfs-init-jffs2/overlayfs | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/recipes-core/multitech/overlayfs-init-jffs2/overlayfs b/recipes-core/multitech/overlayfs-init-jffs2/overlayfs new file mode 100755 index 0000000..1bf0a58 --- /dev/null +++ b/recipes-core/multitech/overlayfs-init-jffs2/overlayfs @@ -0,0 +1,157 @@ +#!/bin/bash +# Command to erase user data file system + +MNT_USER="/mnt/user" +OVERLAY="$MNT_USER/overlay" +WORKDIR="$MNT_USER/work" +UPPERDIR="$MNT_USER/upper" +LOWERDIR="/" +USER_ORIG="$UPPERDIR/orig" +USER_PARTITION="User data" +TABLE=$'\n'"$(cat /proc/mtd)" + + +# Erase the file system +DO_ERASE_PERSISTENT="/mnt/user/.persistent/mts_do_erase_persistent" +# Remove files not hidden +DO_CLEAR_PERSISTENT="/mnt/user/.persistent/mts_do_clear_persistent" + +pid=$$ +prefix="<3>overlayrootfs-generator[${pid}]: " +loginfo() { echo "${prefix}$@" >/dev/kmsg ; } +logpipe() { + OIFS="${IFS}" + IFS=$'\n' + while read ln ; do + echo -n "${prefix}${ln}" >/dev/kmsg + done + IFS="${OIFS}" +} + +if [[ $TABLE =~ [[:space:]]mtd([[:digit:]]+)[^\"]*\"$USER_PARTITION\" ]] ; then + USER_DEV=/dev/mtd${BASH_REMATCH[1]} +else + loginfo "Cannot find a User data partition" + exit 0 +fi + +fs="/dev /proc /sys /run /var/volatile /dev/pts /sys/kernel/debug /sys/kernel/config" + + +mnt_user() { + mkdir -p $MNT_USER + mount -t jffs2 $USER_DEV $MNT_USER +} + +my_switch_root() { + NEW_ROOT=$1 + loginfo mountinfo + mount --make-rprivate / + loginfo "my_switch_root: Mount file systems" + for f in $fs ; do + if [[ -d $NEW_ROOT/$f ]] ; then + loginfo "my_switch_root: mount -o noatime,move --bind $f $NEW_ROOT/$f" + mount -o noatime,move --bind $f $NEW_ROOT/$f + else + # loginfo "my_switch_root: Skipping $NEW_ROOT/$f" + : + fi + done + /sbin/pivot_root $NEW_ROOT $NEW_ROOT/orig +} + +do_remove_old() { + shopt -s dotglob + rm -rf $MNT_USER/*.old + shopt -u dotglob +} + +# select files for deletion +do_clear_old() { + # when "clear user data" is requested - mark all non-hidden files for deletion + loginfo "Clearing user data. Persistent data will be left " + + 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" 2>&1 | logpipe + done + do_remove_old + rm -f $DO_CLEAR_PERSISTENT +} + +# Most efficient way to clear UBIFS file system +do_erase_old() { + loginfo "Erasing user data" + # Sys must be mounted to use ubifs utilities + mount -t sysfs sysfs /sys + + umount -l $MNT_USER + loginfo "Erase user-data partition $USER_DEV" + ls -l ${USER_DEV} 2>&1 | logpipe + flash_erase -j ${USER_DEV} 0 0 2>&1 | logpipe + mnt_user +} + +do_rw_mount() { + loginfo "Starting RW overlayfs" + mount -t tmpfs inittemp /mnt + + mnt_user # user_data is now accessible + do_remove_old + cat /proc/mounts | logpipe + loginfo "ls -lad /mnt" + ls -lad /mnt 2>&1 | logpipe + loginfo "ls -lad /mnt/user" + ls -lad /mnt/user 2>&1 | logpipe + loginfo "ls -lad /mnt/user/.persistent" + ls -lad /mnt/user/.persistent 2>&1 | logpipe + loginfo "ls -la /mnt/user/.persistent" + ls -la /mnt/user/.persistent 2>&1 | logpipe + loginfo "ls -lad $DO_ERASE_PERSISTENT" + ls -lad "$DO_ERASE_PERSISTENT" 2>&1 | logpipe + + if [[ -f $DO_ERASE_PERSISTENT ]] ; then + loginfo "do_erase_old is next" + do_erase_old + elif [[ -f $DO_CLEAR_PERSISTENT ]] ; then + do_clear_old + fi + + mkdir -p $UPPERDIR $WORKDIR $OVERLAY $USER_ORIG ${MNT_USER}/.persistent + loginfo "Mounting..." + mount -o index=off,metacopy=off,noatime,lowerdir=$LOWERDIR,upperdir=$UPPERDIR,workdir=$WORKDIR,rw -t overlay overlay $OVERLAY + loginfo "/proc/mounts:" + cat /proc/mounts | grep overlay | logpipe + + mkdir -p ${OVERLAY}/var/persistent + + mount --bind ${MNT_USER}/.persistent ${OVERLAY}/var/persistent + loginfo "Switch root to $OVERLAY" + my_switch_root $OVERLAY + cat /proc/mounts | grep overlay | logpipe + mount -o remount,rw / + cat /proc/mounts | grep overlay | logpipe + cat /proc/mounts | grep tmp | logpipe + umount -l /tmp >/dev/null 2>&1 + umount -l /var/volatile >/dev/null 2>&1 + cgroupmnts=$(sed -r -e 's/[^[:space:]]*[[:space:]]//' -e 's/[[:space:]]+.*//g' /proc/mounts | grep '^/orig') + for mp in ${cgroupmnts} ; do + if [[ $mp != /orig ]] && [[ -d $mp ]] ; then + umount -l $mp >/dev/null 2>&1 + fi + done +} + +do_start() { + do_rw_mount +} + +case $1 in +start) + do_start +;; +*) + echo "Usage: $0 {start}" +;; +esac |