0018-base-files-sysupgrade-cleanup.patch 7.6 KB

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