common.sh 5.7 KB

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