common.sh 5.8 KB

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