common.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/sh
  2. RAM_ROOT=/tmp/root
  3. ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
  4. libs() { ldd $* | awk '{print $3}'; }
  5. install_file() { # <file> [ <file> ... ]
  6. for file in "$@"; do
  7. dest="$RAM_ROOT/$file"
  8. [ -f $file -a ! -f $dest ] && {
  9. dir="$(dirname $dest)"
  10. mkdir -p "$dir"
  11. cp $file $dest
  12. }
  13. done
  14. }
  15. install_bin() { # <file> [ <symlink> ... ]
  16. src=$1
  17. files=$1
  18. [ -x "$src" ] && files="$src $(libs $src)"
  19. install_file $files
  20. [ -e /lib/ld-linux.so.3 ] && {
  21. install_file /lib/ld-linux.so.3
  22. }
  23. shift
  24. for link in "$@"; do {
  25. dest="$RAM_ROOT/$link"
  26. dir="$(dirname $dest)"
  27. mkdir -p "$dir"
  28. [ -f "$dest" ] || ln -s $src $dest
  29. }; done
  30. }
  31. supivot() { # <new_root> <old_root>
  32. mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
  33. mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
  34. mount -o noatime,move /proc $1/proc && \
  35. pivot_root $1 $1$2 || {
  36. umount $1 $1
  37. return 1
  38. }
  39. mount -o noatime,move $2/sys /sys
  40. mount -o noatime,move $2/dev /dev
  41. mount -o noatime,move $2/tmp /tmp
  42. mount -o noatime,move $2/overlay /overlay 2>&-
  43. return 0
  44. }
  45. run_ramfs() { # <command> [...]
  46. install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
  47. /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \
  48. /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
  49. /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
  50. /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc
  51. install_bin /sbin/mtd
  52. for file in $RAMFS_COPY_BIN; do
  53. install_bin $file
  54. done
  55. install_file /etc/resolv.conf /lib/functions.sh /lib/functions.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
  56. supivot $RAM_ROOT /mnt || {
  57. echo "Failed to switch over to ramfs. Please reboot."
  58. exit 1
  59. }
  60. mount -o remount,ro /mnt
  61. umount -l /mnt
  62. grep /overlay /proc/mounts > /dev/null && {
  63. mount -o noatime,remount,ro /overlay
  64. umount -l /overlay
  65. }
  66. # spawn a new shell from ramdisk to reduce the probability of cache issues
  67. exec /bin/busybox ash -c "$*"
  68. }
  69. kill_remaining() { # [ <signal> ]
  70. local sig="${1:-TERM}"
  71. echo -n "Sending $sig to remaining processes ... "
  72. local stat
  73. for stat in /proc/[0-9]*/stat; do
  74. [ -f "$stat" ] || continue
  75. local pid name state ppid rest
  76. read pid name state ppid rest < $stat
  77. name="${name#(}"; name="${name%)}"
  78. local cmdline
  79. read cmdline < /proc/$pid/cmdline
  80. # Skip kernel threads
  81. [ -n "$cmdline" ] || continue
  82. case "$name" in
  83. # Skip essential services
  84. *ash*|*init*|*watchdog*|*ssh*|*dropbear*|*telnet*|*login*|*hostapd*|*wpa_supplicant*) : ;;
  85. # Killable process
  86. *)
  87. if [ $pid -ne $$ ] && [ $ppid -ne $$ ]; then
  88. echo -n "$name "
  89. kill -$sig $pid 2>/dev/null
  90. fi
  91. ;;
  92. esac
  93. done
  94. echo ""
  95. }
  96. run_hooks() {
  97. local arg="$1"; shift
  98. for func in "$@"; do
  99. eval "$func $arg"
  100. done
  101. }
  102. ask_bool() {
  103. local default="$1"; shift;
  104. local answer="$default"
  105. [ "$INTERACTIVE" -eq 1 ] && {
  106. case "$default" in
  107. 0) echo -n "$* (y/N): ";;
  108. *) echo -n "$* (Y/n): ";;
  109. esac
  110. read answer
  111. case "$answer" in
  112. y*) answer=1;;
  113. n*) answer=0;;
  114. *) answer="$default";;
  115. esac
  116. }
  117. [ "$answer" -gt 0 ]
  118. }
  119. v() {
  120. [ "$VERBOSE" -ge 1 ] && echo "$@"
  121. }
  122. rootfs_type() {
  123. mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
  124. }
  125. get_image() { # <source> [ <command> ]
  126. local from="$1"
  127. local conc="$2"
  128. local cmd
  129. case "$from" in
  130. http://*|ftp://*) cmd="wget -O- -q";;
  131. *) cmd="cat";;
  132. esac
  133. if [ -z "$conc" ]; then
  134. local magic="$(eval $cmd $from | dd bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
  135. case "$magic" in
  136. 1f8b) conc="zcat";;
  137. 425a) conc="bzcat";;
  138. esac
  139. fi
  140. eval "$cmd $from ${conc:+| $conc}"
  141. }
  142. get_magic_word() {
  143. get_image "$@" | dd bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
  144. }
  145. get_magic_long() {
  146. get_image "$@" | dd bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  147. }
  148. refresh_mtd_partitions() {
  149. mtd refresh rootfs
  150. }
  151. jffs2_copy_config() {
  152. if grep rootfs_data /proc/mtd >/dev/null; then
  153. # squashfs+jffs2
  154. mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
  155. else
  156. # jffs2
  157. mtd jffs2write "$CONF_TAR" rootfs
  158. fi
  159. }
  160. default_do_upgrade() {
  161. sync
  162. if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
  163. get_image "$1" | mtd -j "$CONF_TAR" write - "${PART_NAME:-image}"
  164. else
  165. get_image "$1" | mtd write - "${PART_NAME:-image}"
  166. fi
  167. }
  168. do_upgrade() {
  169. v "Performing system upgrade..."
  170. if type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
  171. platform_do_upgrade "$ARGV"
  172. else
  173. default_do_upgrade "$ARGV"
  174. fi
  175. [ "$SAVE_CONFIG" -eq 1 -a -n "$USE_REFRESH" ] && {
  176. v "Refreshing partitions"
  177. if type 'platform_refresh_partitions' >/dev/null 2>/dev/null; then
  178. platform_refresh_partitions
  179. else
  180. refresh_mtd_partitions
  181. fi
  182. if type 'platform_copy_config' >/dev/null 2>/dev/null; then
  183. platform_copy_config
  184. else
  185. jffs2_copy_config
  186. fi
  187. }
  188. v "Upgrade completed"
  189. [ -n "$DELAY" ] && sleep "$DELAY"
  190. ask_bool 1 "Reboot" && {
  191. v "Rebooting system..."
  192. reboot -f
  193. sleep 5
  194. echo b 2>/dev/null >/proc/sysrq-trigger
  195. }
  196. }