2
0

sysupgrade 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. # initialize defaults
  5. export MTD_CONFIG_ARGS=""
  6. export INTERACTIVE=0
  7. export VERBOSE=1
  8. export SAVE_CONFIG=1
  9. export SAVE_OVERLAY=0
  10. export SAVE_PARTITIONS=1
  11. export CONF_IMAGE=
  12. export CONF_BACKUP_LIST=0
  13. export CONF_BACKUP=
  14. export CONF_RESTORE=
  15. export NEED_IMAGE=
  16. export HELP=0
  17. export FORCE=0
  18. export TEST=0
  19. # parse options
  20. while [ -n "$1" ]; do
  21. case "$1" in
  22. -i) export INTERACTIVE=1;;
  23. -v) export VERBOSE="$(($VERBOSE + 1))";;
  24. -q) export VERBOSE="$(($VERBOSE - 1))";;
  25. -n) export SAVE_CONFIG=0;;
  26. -c) export SAVE_OVERLAY=1;;
  27. -p) export SAVE_PARTITIONS=0;;
  28. -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
  29. -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
  30. -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
  31. -f) export CONF_IMAGE="$2"; shift;;
  32. -F|--force) export FORCE=1;;
  33. -T|--test) export TEST=1;;
  34. -h|--help) export HELP=1; break;;
  35. -*)
  36. echo "Invalid option: $1"
  37. exit 1
  38. ;;
  39. *) break;;
  40. esac
  41. shift;
  42. done
  43. export CONFFILES=/tmp/sysupgrade.conffiles
  44. export CONF_TAR=/tmp/sysupgrade.tgz
  45. IMAGE="$1"
  46. [ -z "$IMAGE" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
  47. cat <<EOF
  48. Usage: $0 [<upgrade-option>...] <image file or URL>
  49. $0 [-q] [-i] <backup-command> <file>
  50. upgrade-option:
  51. -f <config> restore configuration from .tar.gz (file or url)
  52. -i interactive mode
  53. -c attempt to preserve all changed files in /etc/
  54. -n do not save configuration over reflash
  55. -p do not attempt to restore the partition table after flash.
  56. -T | --test
  57. Verify image and config .tar.gz but do not actually flash.
  58. -F | --force
  59. Flash image even if image checks fail, this is dangerous!
  60. -q less verbose
  61. -v more verbose
  62. -h | --help display this help
  63. backup-command:
  64. -b | --create-backup <file>
  65. create .tar.gz of files specified in sysupgrade.conf
  66. then exit. Does not flash an image. If file is '-',
  67. i.e. stdout, verbosity is set to 0 (i.e. quiet).
  68. -r | --restore-backup <file>
  69. restore a .tar.gz created with sysupgrade -b
  70. then exit. Does not flash an image. If file is '-',
  71. the archive is read from stdin.
  72. -l | --list-backup
  73. list the files that would be backed up when calling
  74. sysupgrade -b. Does not create a backup file.
  75. EOF
  76. exit 1
  77. }
  78. [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
  79. cat <<-EOF
  80. -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
  81. Do not specify both -b|-r and a firmware image.
  82. EOF
  83. exit 1
  84. }
  85. # prevent messages from clobbering the tarball when using stdout
  86. [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
  87. list_conffiles() {
  88. awk '
  89. BEGIN { conffiles = 0 }
  90. /^Conffiles:/ { conffiles = 1; next }
  91. !/^ / { conffiles = 0; next }
  92. conffiles == 1 { print }
  93. ' /usr/lib/opkg/status
  94. }
  95. list_changed_conffiles() {
  96. # Cannot handle spaces in filenames - but opkg cannot either...
  97. list_conffiles | while read file csum; do
  98. [ -r "$file" ] || continue
  99. echo "${csum} ${file}" | sha256sum -sc - || echo "$file"
  100. done
  101. }
  102. add_uci_conffiles() {
  103. local file="$1"
  104. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  105. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  106. -type f -o -type l 2>/dev/null;
  107. list_changed_conffiles ) | sort -u > "$file"
  108. return 0
  109. }
  110. add_overlayfiles() {
  111. local file="$1"
  112. find /overlay/upper/etc/ -type f -o -type l | sed \
  113. -e 's,^/overlay\/upper/,/,' \
  114. -e '\,/META_[a-zA-Z0-9]*$,d' \
  115. -e '\,/functions.sh$,d' \
  116. -e '\,/[^/]*-opkg$,d' \
  117. > "$file"
  118. return 0
  119. }
  120. # hooks
  121. sysupgrade_image_check="fwtool_check_image platform_check_image"
  122. if [ $SAVE_OVERLAY = 1 ]; then
  123. [ ! -d /overlay/upper/etc ] && {
  124. echo "Cannot find '/overlay/upper/etc', required for '-c'"
  125. exit 1
  126. }
  127. sysupgrade_init_conffiles="add_overlayfiles"
  128. else
  129. sysupgrade_init_conffiles="add_uci_conffiles"
  130. fi
  131. include /lib/upgrade
  132. do_save_conffiles() {
  133. local conf_tar="${1:-$CONF_TAR}"
  134. [ -z "$(rootfs_type)" ] && {
  135. echo "Cannot save config while running from ramdisk."
  136. ask_bool 0 "Abort" && exit
  137. rm -f "$conf_tar"
  138. return 0
  139. }
  140. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  141. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  142. v "Saving config files..."
  143. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  144. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  145. if [ "$?" -ne 0 ]; then
  146. echo "Failed to create the configuration backup."
  147. rm -f "$conf_tar"
  148. exit 1
  149. fi
  150. rm -f "$CONFFILES"
  151. }
  152. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  153. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  154. cat "$CONFFILES"
  155. rm -f "$CONFFILES"
  156. exit 0
  157. fi
  158. if [ -n "$CONF_BACKUP" ]; then
  159. do_save_conffiles "$CONF_BACKUP"
  160. exit $?
  161. fi
  162. if [ -n "$CONF_RESTORE" ]; then
  163. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  164. echo "Backup archive '$CONF_RESTORE' not found."
  165. exit 1
  166. fi
  167. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  168. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  169. exit $?
  170. fi
  171. type platform_check_image >/dev/null 2>/dev/null || {
  172. echo "Firmware upgrade is not implemented for this platform."
  173. exit 1
  174. }
  175. case "$IMAGE" in
  176. http://*)
  177. wget -O/tmp/sysupgrade.img "$IMAGE"
  178. IMAGE=/tmp/sysupgrade.img
  179. ;;
  180. esac
  181. IMAGE="$(readlink -f "$IMAGE")"
  182. case "$IMAGE" in
  183. '')
  184. echo "Image file not found."
  185. exit 1
  186. ;;
  187. /tmp/*) ;;
  188. *)
  189. v "Image not in /tmp, copying..."
  190. cp -f "$IMAGE" /tmp/sysupgrade.img
  191. IMAGE=/tmp/sysupgrade.img
  192. ;;
  193. esac
  194. export ARGV="$IMAGE"
  195. export ARGC=1
  196. for check in $sysupgrade_image_check; do
  197. ( $check "$IMAGE" ) || {
  198. if [ $FORCE -eq 1 ]; then
  199. echo "Image check '$check' failed but --force given - will update anyway!"
  200. break
  201. else
  202. echo "Image check '$check' failed."
  203. exit 1
  204. fi
  205. }
  206. done
  207. if [ -n "$CONF_IMAGE" ]; then
  208. case "$(get_magic_word $CONF_IMAGE cat)" in
  209. # .gz files
  210. 1f8b) ;;
  211. *)
  212. echo "Invalid config file. Please use only .tar.gz files"
  213. exit 1
  214. ;;
  215. esac
  216. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  217. export SAVE_CONFIG=1
  218. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  219. [ $TEST -eq 1 ] || do_save_conffiles
  220. export SAVE_CONFIG=1
  221. else
  222. [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
  223. export SAVE_CONFIG=0
  224. fi
  225. if [ $TEST -eq 1 ]; then
  226. exit 0
  227. fi
  228. if [ $SAVE_PARTITIONS -eq 0 ]; then
  229. touch /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  230. else
  231. rm -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  232. fi
  233. install_bin /sbin/upgraded
  234. v "Commencing upgrade. Closing all shell sessions."
  235. COMMAND='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
  236. if [ -n "$FAILSAFE" ]; then
  237. printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
  238. lock -u /tmp/.failsafe
  239. else
  240. ubus call system sysupgrade "{
  241. \"prefix\": $(json_string "$RAM_ROOT"),
  242. \"path\": $(json_string "$IMAGE"),
  243. \"command\": $(json_string "$COMMAND")
  244. }"
  245. fi