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