sysupgrade 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. sysupgrade_pre_upgrade="fwtool_pre_upgrade"
  123. if [ $SAVE_OVERLAY = 1 ]; then
  124. [ ! -d /overlay/upper/etc ] && {
  125. echo "Cannot find '/overlay/upper/etc', required for '-c'"
  126. exit 1
  127. }
  128. sysupgrade_init_conffiles="add_overlayfiles"
  129. else
  130. sysupgrade_init_conffiles="add_uci_conffiles"
  131. fi
  132. include /lib/upgrade
  133. do_save_conffiles() {
  134. local conf_tar="${1:-$CONF_TAR}"
  135. [ -z "$(rootfs_type)" ] && {
  136. echo "Cannot save config while running from ramdisk."
  137. ask_bool 0 "Abort" && exit
  138. rm -f "$conf_tar"
  139. return 0
  140. }
  141. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  142. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  143. v "Saving config files..."
  144. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  145. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  146. rm -f "$CONFFILES"
  147. }
  148. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  149. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  150. cat "$CONFFILES"
  151. rm -f "$CONFFILES"
  152. exit 0
  153. fi
  154. if [ -n "$CONF_BACKUP" ]; then
  155. do_save_conffiles "$CONF_BACKUP"
  156. exit $?
  157. fi
  158. if [ -n "$CONF_RESTORE" ]; then
  159. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  160. echo "Backup archive '$CONF_RESTORE' not found."
  161. exit 1
  162. fi
  163. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  164. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  165. exit $?
  166. fi
  167. type platform_check_image >/dev/null 2>/dev/null || {
  168. echo "Firmware upgrade is not implemented for this platform."
  169. exit 1
  170. }
  171. case "$IMAGE" in
  172. http://*)
  173. wget -O/tmp/sysupgrade.img "$IMAGE"
  174. IMAGE=/tmp/sysupgrade.img
  175. ;;
  176. esac
  177. IMAGE="$(readlink -f "$IMAGE")"
  178. case "$IMAGE" in
  179. '')
  180. echo "Image file not found."
  181. exit 1
  182. ;;
  183. /tmp/*) ;;
  184. *)
  185. v "Image not in /tmp, copying..."
  186. cp -f "$IMAGE" /tmp/sysupgrade.img
  187. IMAGE=/tmp/sysupgrade.img
  188. ;;
  189. esac
  190. export ARGV="$IMAGE"
  191. export ARGC=1
  192. for check in $sysupgrade_image_check; do
  193. ( $check "$IMAGE" ) || {
  194. if [ $FORCE -eq 1 ]; then
  195. echo "Image check '$check' failed but --force given - will update anyway!"
  196. break
  197. else
  198. echo "Image check '$check' failed."
  199. exit 1
  200. fi
  201. }
  202. done
  203. if [ -n "$CONF_IMAGE" ]; then
  204. case "$(get_magic_word $CONF_IMAGE cat)" in
  205. # .gz files
  206. 1f8b) ;;
  207. *)
  208. echo "Invalid config file. Please use only .tar.gz files"
  209. exit 1
  210. ;;
  211. esac
  212. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  213. export SAVE_CONFIG=1
  214. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  215. [ $TEST -eq 1 ] || do_save_conffiles
  216. export SAVE_CONFIG=1
  217. else
  218. [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
  219. export SAVE_CONFIG=0
  220. fi
  221. if [ $TEST -eq 1 ]; then
  222. exit 0
  223. fi
  224. if [ $SAVE_PARTITIONS -eq 0 ]; then
  225. touch /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  226. else
  227. rm -f /tmp/sysupgrade.always.overwrite.bootdisk.partmap
  228. fi
  229. run_hooks "" $sysupgrade_pre_upgrade
  230. install_bin /sbin/upgraded
  231. v "Commencing upgrade. All shell sessions will be closed now."
  232. COMMAND='. /lib/functions.sh; include /lib/upgrade; do_upgrade_stage2'
  233. if [ -n "$FAILSAFE" ]; then
  234. printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
  235. lock -u /tmp/.failsafe
  236. else
  237. ubus call system sysupgrade "{
  238. \"prefix\": $(json_string "$RAM_ROOT"),
  239. \"path\": $(json_string "$IMAGE"),
  240. \"command\": $(json_string "$COMMAND")
  241. }"
  242. fi