0111-base-files-sysupgrade-cleanup.patch 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. From: Matthias Schiffer <mschiffer@universe-factory.net>
  2. Date: Sun, 23 Apr 2017 23:33:14 +0200
  3. Subject: base-files: sysupgrade cleanup
  4. Some functions only used by stage2 are moved there from common.sh.
  5. One piece that could still use more cleanup is platform_pre_upgrade: many
  6. targets reference files from there are aren't available in the ramfs, so
  7. we need to evaluate it before the switch; conversely, flash writes happen
  8. in that function on some targets. Targets that do the latter should be
  9. fixed eventually to use platform_do_upgrade for that purpose.
  10. Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
  11. diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh
  12. index 23f18ceb775caac057ee7a757922954f812f77ce..fc59bf2323498d332159b00eb7ab443bfe6b147e 100644
  13. --- a/package/base-files/files/lib/upgrade/common.sh
  14. +++ b/package/base-files/files/lib/upgrade/common.sh
  15. @@ -30,103 +30,6 @@ install_bin() { # <file> [ <symlink> ... ]
  16. }; done
  17. }
  18. -supivot() { # <new_root> <old_root>
  19. - /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
  20. - mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
  21. - /bin/mount -o noatime,move /proc $1/proc && \
  22. - pivot_root $1 $1$2 || {
  23. - /bin/umount -l $1 $1
  24. - return 1
  25. - }
  26. -
  27. - /bin/mount -o noatime,move $2/sys /sys
  28. - /bin/mount -o noatime,move $2/dev /dev
  29. - /bin/mount -o noatime,move $2/tmp /tmp
  30. - /bin/mount -o noatime,move $2/overlay /overlay 2>&-
  31. - return 0
  32. -}
  33. -
  34. -run_ramfs() { # <command> [...]
  35. - install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
  36. - /sbin/pivot_root /usr/bin/wget /sbin/reboot /bin/sync /bin/dd \
  37. - /bin/grep /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" \
  38. - /bin/dd /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
  39. - /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
  40. - /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
  41. - /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find
  42. -
  43. - install_bin /sbin/mtd
  44. - install_bin /sbin/mount_root
  45. - install_bin /sbin/snapshot
  46. - install_bin /sbin/snapshot_tool
  47. - install_bin /usr/sbin/ubiupdatevol
  48. - install_bin /usr/sbin/ubiattach
  49. - install_bin /usr/sbin/ubiblock
  50. - install_bin /usr/sbin/ubiformat
  51. - install_bin /usr/sbin/ubidetach
  52. - install_bin /usr/sbin/ubirsvol
  53. - install_bin /usr/sbin/ubirmvol
  54. - install_bin /usr/sbin/ubimkvol
  55. - install_bin /usr/sbin/partx
  56. - for file in $RAMFS_COPY_BIN; do
  57. - install_bin ${file//:/ }
  58. - done
  59. - install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
  60. -
  61. - [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
  62. -
  63. - supivot $RAM_ROOT /mnt || {
  64. - echo "Failed to switch over to ramfs. Please reboot."
  65. - exit 1
  66. - }
  67. -
  68. - /bin/mount -o remount,ro /mnt
  69. - /bin/umount -l /mnt
  70. -
  71. - grep /overlay /proc/mounts > /dev/null && {
  72. - /bin/mount -o noatime,remount,ro /overlay
  73. - /bin/umount -l /overlay
  74. - }
  75. -
  76. - # spawn a new shell from ramdisk to reduce the probability of cache issues
  77. - exec /bin/busybox ash -c "$*"
  78. -}
  79. -
  80. -kill_remaining() { # [ <signal> [ <loop> ] ]
  81. - local sig="${1:-TERM}"
  82. - local loop="${2:-0}"
  83. - local run=true
  84. - local stat
  85. -
  86. - echo -n "Sending $sig to remaining processes ... "
  87. -
  88. - while $run; do
  89. - run=false
  90. - for stat in /proc/[0-9]*/stat; do
  91. - [ -f "$stat" ] || continue
  92. -
  93. - local pid name state ppid rest
  94. - read pid name state ppid rest < $stat
  95. - name="${name#(}"; name="${name%)}"
  96. -
  97. - # Skip PID1, ourself and our children
  98. - [ $pid -ne 1 -a $pid -ne $$ -a $ppid -ne $$ ] || continue
  99. -
  100. - local cmdline
  101. - read cmdline < /proc/$pid/cmdline
  102. -
  103. - # Skip kernel threads
  104. - [ -n "$cmdline" ] || continue
  105. -
  106. - echo -n "$name "
  107. - kill -$sig $pid 2>/dev/null
  108. -
  109. - [ $loop -eq 1 ] && run=true
  110. - done
  111. - done
  112. - echo ""
  113. -}
  114. -
  115. run_hooks() {
  116. local arg="$1"; shift
  117. for func in "$@"; do
  118. diff --git a/package/base-files/files/lib/upgrade/stage2 b/package/base-files/files/lib/upgrade/stage2
  119. index 4e2aa3a23c3bab07a795762a30a4d4f701081934..cc8047d988e39ca9ba27d2588744aad469d1d978 100755
  120. --- a/package/base-files/files/lib/upgrade/stage2
  121. +++ b/package/base-files/files/lib/upgrade/stage2
  122. @@ -24,6 +24,104 @@ export CONF_TAR=/tmp/sysupgrade.tgz
  123. include /lib/upgrade
  124. +supivot() { # <new_root> <old_root>
  125. + /bin/mount | grep "on $1 type" 2>&- 1>&- || /bin/mount -o bind $1 $1
  126. + mkdir -p $1$2 $1/proc $1/sys $1/dev $1/tmp $1/overlay && \
  127. + /bin/mount -o noatime,move /proc $1/proc && \
  128. + pivot_root $1 $1$2 || {
  129. + /bin/umount -l $1 $1
  130. + return 1
  131. + }
  132. +
  133. + /bin/mount -o noatime,move $2/sys /sys
  134. + /bin/mount -o noatime,move $2/dev /dev
  135. + /bin/mount -o noatime,move $2/tmp /tmp
  136. + /bin/mount -o noatime,move $2/overlay /overlay 2>&-
  137. + return 0
  138. +}
  139. +
  140. +switch_to_ramfs() {
  141. + install_bin /bin/busybox /bin/ash /bin/sh /bin/mount /bin/umount \
  142. + /sbin/pivot_root /sbin/reboot /bin/sync /bin/dd /bin/grep \
  143. + /bin/cp /bin/mv /bin/tar /usr/bin/md5sum "/usr/bin/[" /bin/dd \
  144. + /bin/vi /bin/ls /bin/cat /usr/bin/awk /usr/bin/hexdump \
  145. + /bin/sleep /bin/zcat /usr/bin/bzcat /usr/bin/printf /usr/bin/wc \
  146. + /bin/cut /usr/bin/printf /bin/sync /bin/mkdir /bin/rmdir \
  147. + /bin/rm /usr/bin/basename /bin/kill /bin/chmod /usr/bin/find \
  148. + /bin/mknod
  149. +
  150. + install_bin /sbin/mtd
  151. + install_bin /sbin/mount_root
  152. + install_bin /sbin/snapshot
  153. + install_bin /sbin/snapshot_tool
  154. + install_bin /usr/sbin/ubiupdatevol
  155. + install_bin /usr/sbin/ubiattach
  156. + install_bin /usr/sbin/ubiblock
  157. + install_bin /usr/sbin/ubiformat
  158. + install_bin /usr/sbin/ubidetach
  159. + install_bin /usr/sbin/ubirsvol
  160. + install_bin /usr/sbin/ubirmvol
  161. + install_bin /usr/sbin/ubimkvol
  162. + install_bin /usr/sbin/partx
  163. + install_bin /usr/sbin/losetup
  164. + install_bin /usr/sbin/mkfs.ext4
  165. + for file in $RAMFS_COPY_BIN; do
  166. + install_bin ${file//:/ }
  167. + done
  168. + install_file /etc/resolv.conf /lib/*.sh /lib/functions/*.sh /lib/upgrade/*.sh $RAMFS_COPY_DATA
  169. +
  170. + [ -L "/lib64" ] && ln -s /lib $RAM_ROOT/lib64
  171. +
  172. + supivot $RAM_ROOT /mnt || {
  173. + echo "Failed to switch over to ramfs. Please reboot."
  174. + exit 1
  175. + }
  176. +
  177. + /bin/mount -o remount,ro /mnt
  178. + /bin/umount -l /mnt
  179. +
  180. + grep /overlay /proc/mounts > /dev/null && {
  181. + /bin/mount -o noatime,remount,ro /overlay
  182. + /bin/umount -l /overlay
  183. + }
  184. +}
  185. +
  186. +kill_remaining() { # [ <signal> [ <loop> ] ]
  187. + local sig="${1:-TERM}"
  188. + local loop="${2:-0}"
  189. + local run=true
  190. + local stat
  191. +
  192. + echo -n "Sending $sig to remaining processes ... "
  193. +
  194. + while $run; do
  195. + run=false
  196. + for stat in /proc/[0-9]*/stat; do
  197. + [ -f "$stat" ] || continue
  198. +
  199. + local pid name state ppid rest
  200. + read pid name state ppid rest < $stat
  201. + name="${name#(}"; name="${name%)}"
  202. +
  203. + # Skip PID1, ourself and our children
  204. + [ $pid -ne 1 -a $pid -ne $$ -a $ppid -ne $$ ] || continue
  205. +
  206. + local cmdline
  207. + read cmdline < /proc/$pid/cmdline
  208. +
  209. + # Skip kernel threads
  210. + [ -n "$cmdline" ] || continue
  211. +
  212. + echo -n "$name "
  213. + kill -$sig $pid 2>/dev/null
  214. +
  215. + [ $loop -eq 1 ] && run=true
  216. + done
  217. + done
  218. + echo ""
  219. +}
  220. +
  221. +
  222. killall -9 telnetd
  223. killall -9 dropbear
  224. killall -9 ash
  225. @@ -44,7 +142,8 @@ fi
  226. if [ -n "$(rootfs_type)" ]; then
  227. echo "Switching to ramdisk..."
  228. - run_ramfs "$COMMAND"
  229. -else
  230. - exec /bin/busybox ash -c "$COMMAND"
  231. + switch_to_ramfs
  232. fi
  233. +
  234. +# Exec new shell from ramfs
  235. +exec /bin/busybox ash -c "$COMMAND"
  236. diff --git a/package/base-files/files/sbin/sysupgrade b/package/base-files/files/sbin/sysupgrade
  237. index d48416f6de244b97f426ae0b83d425ac5c1bc5b3..2060679e61948e4661a408df009ff3ef3c6faa91 100755
  238. --- a/package/base-files/files/sbin/sysupgrade
  239. +++ b/package/base-files/files/sbin/sysupgrade
  240. @@ -1,7 +1,5 @@
  241. #!/bin/sh
  242. -[ "$1" = "nand" ] && exec /lib/upgrade/stage2 "$2" "$3"
  243. -
  244. . /lib/functions.sh
  245. . /lib/functions/system.sh