| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh |
| 2 | SYSCFG_UBIFS_MNT=/tmp/syscfg |
| 3 | . /lib/functions.sh |
| 4 | |
| 5 | # mtdpart: the ubifs syscfg partition |
| 6 | # rom: the read-only rootfs mount point |
| 7 | # overlay: the overlay upper directory |
| 8 | pre_check() { |
| 9 | # return 1 on failed |
| 10 | grep -qs ubifs /proc/filesystems || return 1 |
| 11 | grep -qs overlay /proc/filesystems || return 1 |
| 12 | [ ! -e $SYSCFG_UBIFS_MNT ] && mkdir -p $SYSCFG_UBIFS_MNT |
| 13 | |
| 14 | mtdpart="$(find_mtd_part rootfs_data)" |
| 15 | [ -z "$mtdpart" ] && return 1 |
| 16 | mtdpart_idx="$(echo $mtdpart | tr -d "/dev/mtdblock")" |
| 17 | |
| 18 | rom=$(awk '/jffs2 ro/ {print $2}' /proc/mounts) |
| 19 | overlays=$(awk '/ubifs/ {print $2}' /proc/mounts | grep overlay) |
| 20 | return 0 |
| 21 | } |
| 22 | |
| 23 | clean_mounted_overlayfs(){ |
| 24 | if [ ! -z "$overlays" ] |
| 25 | then |
| 26 | echo "first stop respawn app..." |
| 27 | /etc/init.d/odhcpd stop |
| 28 | /etc/init.d/log stop |
| 29 | /etc/init.d/dnsmasq stop |
| 30 | /etc/init.d/network stop |
| 31 | /etc/init.d/services.init stop |
| 32 | /etc/init.d/cm.init stop |
| 33 | /etc/init.d/sdcard_mount stop |
| 34 | |
| 35 | echo "then stop other left app..." |
| 36 | ps | sed '/ash\|PID\|firstboot\|sed/d;/\[.*\]$/d;s/^ \+//;s/ .*//;/^'$$'$/d;/^'$PPID'$/d' | xargs kill -9 |
| 37 | sleep 2 |
| 38 | |
| 39 | echo "start to umount overlay-fs..." |
| 40 | if [ -d /NVM/*data ]; then |
| 41 | /bin/umount /NVM/*data |
| 42 | fi |
| 43 | if [ -d /usr/*web ]; then |
| 44 | /bin/umount /usr/*web |
| 45 | fi |
| 46 | rm -rf /data/* |
| 47 | /bin/umount /data /mnt /log |
| 48 | /bin/umount /etc |
| 49 | /bin/umount /NVM |
| 50 | for overlay in $overlays; do |
| 51 | echo -n "delete all file under $overlay/ ... " |
| 52 | rm -rf $overlay/* |
| 53 | /bin/umount $overlay |
| 54 | done |
| 55 | echo "done" |
| 56 | return 0 |
| 57 | fi |
| 58 | } |
| 59 | |
| 60 | __try_ubifs_syscfg_mount() { |
| 61 | overlay_mountpoint=$1 |
| 62 | if [ -z $overlay_mountpoint ] |
| 63 | then |
| 64 | overlay_mountpoint=/overlay |
| 65 | fi |
| 66 | recover_ubifs=0 |
| 67 | [ ! -e /dev/ubi0 ] && ubiattach /dev/ubi_ctrl -m $mtdpart_idx -d 0 || recover_ubifs=1 |
| 68 | if [ $recover_ubifs -eq 0 ] |
| 69 | then |
| 70 | ubi0_nod_id=`cat /sys/class/ubi/ubi0/dev | tr -s ":" " "` |
| 71 | [ ! -e /dev/ubi0 ] && mknod /dev/ubi0 c ${ubi0_nod_id} |
| 72 | if [ ! -e /sys/class/ubi/ubi0_0/dev ] |
| 73 | then |
| 74 | # no volume |
| 75 | recover_ubifs=1 |
| 76 | else |
| 77 | # check for "data" volume |
| 78 | ubi0_0_nod_id=`cat /sys/class/ubi/ubi0_0/dev | tr -s ":" " "` |
| 79 | [ ! -e /dev/ubi0_0 ] && mknod /dev/ubi0_0 c ${ubi0_0_nod_id} |
| 80 | { ubinfo /dev/ubi0_0 | grep Name | grep -qs "data" ; } || \ |
| 81 | recover_ubifs=1 |
| 82 | fi |
| 83 | fi |
| 84 | if [ $recover_ubifs -eq 1 ] |
| 85 | then |
| 86 | echo "ubifs syscfg partition is damaged" |
| 87 | echo "try to recover by formatting $mtdpart..." |
| 88 | [ -e /dev/ubi0 ] && ubidetach -m $mtdpart_idx |
| 89 | ubiformat -y -q /dev/mtd$mtdpart_idx |
| 90 | ubiattach -m $mtdpart_idx /dev/ubi_ctrl |
| 91 | ubi0_nod_id=`cat /sys/class/ubi/ubi0/dev | tr -s ":" " "` |
| 92 | [ ! -e /dev/ubi0 ] && mknod /dev/ubi0 c ${ubi0_nod_id} |
| 93 | ubimkvol /dev/ubi0 -n 1 -N etc -t dynamic -s 2MiB |
| 94 | ubimkvol /dev/ubi0 -n 0 -N data -t dynamic --maxavsize |
| 95 | fi |
| 96 | |
| 97 | # finally mount the ubifs |
| 98 | mount -t ubifs -o noatime ubi0:data /data || return 1 |
| 99 | mount -t ubifs -o noatime ubi0:data /mnt || return 1 |
| 100 | mount -t ubifs -o noatime ubi0:data /log || return 1 |
| 101 | mount -t ubifs -o noatime ubi0:etc $overlay_mountpoint/etc || return 1 |
| 102 | return 0 |
| 103 | } |
| 104 | |
| 105 | try_ubifs_syscfg_mount() { |
| 106 | __try_ubifs_syscfg_mount || { |
| 107 | echo "roofs_data mount fail, try to recover by erase..." |
| 108 | mtd erase rootfs_data |
| 109 | __try_ubifs_syscfg_mount |
| 110 | } |
| 111 | |
| 112 | return 0 |
| 113 | } |
| 114 | |
| 115 | if [ "$1" != "-y" ] |
| 116 | then |
| 117 | read -p "This will erase all settings and remove any installed packages. Are you sure? [N/y]" answer |
| 118 | case $answer in |
| 119 | [Yy]* ) break;; |
| 120 | [Nn]* ) exit 0;; |
| 121 | * ) exit 0;; |
| 122 | esac |
| 123 | fi |
| 124 | |
| 125 | pre_check |
| 126 | if [ ! -z "$overlays" ] |
| 127 | then |
| 128 | echo "overlayfs is mounted" |
| 129 | clean_mounted_overlayfs |
| 130 | else |
| 131 | echo "try to mount overlayfs" |
| 132 | try_ubifs_syscfg_mount || exit 1 |
| 133 | overlays=$(awk '/ubifs/ {print $2}' /proc/mounts | grep overlay) |
| 134 | clean_mounted_overlayfs |
| 135 | fi |
| 136 | |