common.sh 5.9 KB

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