common.sh 4.8 KB

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