common.sh 5.7 KB

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