sysupgrade 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. # initialize defaults
  5. RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
  6. RAMFS_COPY_DATA="" # extra data files
  7. export MTD_CONFIG_ARGS=""
  8. export INTERACTIVE=0
  9. export VERBOSE=1
  10. export SAVE_CONFIG=1
  11. export SAVE_OVERLAY=0
  12. export DELAY=
  13. export CONF_IMAGE=
  14. export CONF_BACKUP_LIST=0
  15. export CONF_BACKUP=
  16. export CONF_RESTORE=
  17. export NEED_IMAGE=
  18. export HELP=0
  19. export FORCE=0
  20. export TEST=0
  21. # parse options
  22. while [ -n "$1" ]; do
  23. case "$1" in
  24. -i) export INTERACTIVE=1;;
  25. -d) export DELAY="$2"; shift;;
  26. -v) export VERBOSE="$(($VERBOSE + 1))";;
  27. -q) export VERBOSE="$(($VERBOSE - 1))";;
  28. -n) export SAVE_CONFIG=0;;
  29. -c) export SAVE_OVERLAY=1;;
  30. -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
  31. -r|--restore-backup) export CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
  32. -l|--list-backup) export CONF_BACKUP_LIST=1; break;;
  33. -f) export CONF_IMAGE="$2"; shift;;
  34. -F|--force) export FORCE=1;;
  35. -T|--test) export TEST=1;;
  36. -h|--help) export HELP=1; break;;
  37. -*)
  38. echo "Invalid option: $1"
  39. exit 1
  40. ;;
  41. *) break;;
  42. esac
  43. shift;
  44. done
  45. export CONFFILES=/tmp/sysupgrade.conffiles
  46. export CONF_TAR=/tmp/sysupgrade.tgz
  47. export ARGV="$*"
  48. export ARGC="$#"
  49. [ -z "$ARGV" -a -z "$NEED_IMAGE" -o $HELP -gt 0 ] && {
  50. cat <<EOF
  51. Usage: $0 [<upgrade-option>...] <image file or URL>
  52. $0 [-q] [-i] <backup-command> <file>
  53. upgrade-option:
  54. -d <delay> add a delay before rebooting
  55. -f <config> restore configuration from .tar.gz (file or url)
  56. -i interactive mode
  57. -c attempt to preserve all changed files in /etc/
  58. -n do not save configuration over reflash
  59. -T | --test
  60. Verify image and config .tar.gz but do not actually flash.
  61. -F | --force
  62. Flash image even if image checks fail, this is dangerous!
  63. -q less verbose
  64. -v more verbose
  65. -h | --help display this help
  66. backup-command:
  67. -b | --create-backup <file>
  68. create .tar.gz of files specified in sysupgrade.conf
  69. then exit. Does not flash an image. If file is '-',
  70. i.e. stdout, verbosity is set to 0 (i.e. quiet).
  71. -r | --restore-backup <file>
  72. restore a .tar.gz created with sysupgrade -b
  73. then exit. Does not flash an image. If file is '-',
  74. the archive is read from stdin.
  75. -l | --list-backup
  76. list the files that would be backed up when calling
  77. sysupgrade -b. Does not create a backup file.
  78. EOF
  79. exit 1
  80. }
  81. [ -n "$ARGV" -a -n "$NEED_IMAGE" ] && {
  82. cat <<-EOF
  83. -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
  84. Do not specify both -b|-r and a firmware image.
  85. EOF
  86. exit 1
  87. }
  88. # prevent messages from clobbering the tarball when using stdout
  89. [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
  90. add_uci_conffiles() {
  91. local file="$1"
  92. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  93. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  94. -type f -o -type l 2>/dev/null;
  95. opkg list-changed-conffiles ) | sort -u > "$file"
  96. return 0
  97. }
  98. add_overlayfiles() {
  99. local file="$1"
  100. if [ -d /overlay/upper ]; then
  101. local overlaydir="/overlay/upper"
  102. else
  103. local overlaydir="/overlay"
  104. fi
  105. find $overlaydir/etc/ -type f -o -type l | sed \
  106. -e 's,^/overlay/,/,' \
  107. -e '\,/META_[a-zA-Z0-9]*$,d' \
  108. -e '\,/functions.sh$,d' \
  109. -e '\,/[^/]*-opkg$,d' \
  110. > "$file"
  111. return 0
  112. }
  113. # hooks
  114. sysupgrade_image_check="platform_check_image"
  115. [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
  116. sysupgrade_init_conffiles="add_uci_conffiles" || \
  117. sysupgrade_init_conffiles="add_overlayfiles"
  118. include /lib/upgrade
  119. [ "$1" = "nand" ] && nand_upgrade_stage2 $@
  120. do_save_conffiles() {
  121. local conf_tar="${1:-$CONF_TAR}"
  122. [ -z "$(rootfs_type)" ] && {
  123. echo "Cannot save config while running from ramdisk."
  124. ask_bool 0 "Abort" && exit
  125. return 0
  126. }
  127. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  128. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  129. v "Saving config files..."
  130. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  131. tar c${TAR_V}zf "$conf_tar" -T "$CONFFILES" 2>/dev/null
  132. rm -f "$CONFFILES"
  133. }
  134. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  135. add_uci_conffiles "$CONFFILES"
  136. cat "$CONFFILES"
  137. rm -f "$CONFFILES"
  138. exit 0
  139. fi
  140. if [ -n "$CONF_BACKUP" ]; then
  141. do_save_conffiles "$CONF_BACKUP"
  142. exit $?
  143. fi
  144. if [ -n "$CONF_RESTORE" ]; then
  145. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  146. echo "Backup archive '$CONF_RESTORE' not found."
  147. exit 1
  148. fi
  149. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  150. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  151. exit $?
  152. fi
  153. type platform_check_image >/dev/null 2>/dev/null || {
  154. echo "Firmware upgrade is not implemented for this platform."
  155. exit 1
  156. }
  157. for check in $sysupgrade_image_check; do
  158. ( eval "$check \"\$ARGV\"" ) || {
  159. if [ $FORCE -eq 1 ]; then
  160. echo "Image check '$check' failed but --force given - will update anyway!"
  161. break
  162. else
  163. echo "Image check '$check' failed."
  164. exit 1
  165. fi
  166. }
  167. done
  168. if [ -n "$CONF_IMAGE" ]; then
  169. case "$(get_magic_word $CONF_IMAGE cat)" in
  170. # .gz files
  171. 1f8b) ;;
  172. *)
  173. echo "Invalid config file. Please use only .tar.gz files"
  174. exit 1
  175. ;;
  176. esac
  177. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  178. export SAVE_CONFIG=1
  179. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  180. [ $TEST -eq 1 ] || do_save_conffiles
  181. export SAVE_CONFIG=1
  182. else
  183. export SAVE_CONFIG=0
  184. fi
  185. if [ $TEST -eq 1 ]; then
  186. exit 0
  187. fi
  188. run_hooks "" $sysupgrade_pre_upgrade
  189. # Some platforms/devices may want different sysupgrade process, e.g. without
  190. # killing processes yet or calling ubus system upgrade method.
  191. # This is needed e.g. on NAND devices where we just want to trigger stage1 at
  192. # this point.
  193. if type 'platform_pre_upgrade' >/dev/null 2>/dev/null; then
  194. platform_pre_upgrade "$ARGV"
  195. fi
  196. ubus call system upgrade
  197. touch /tmp/sysupgrade
  198. if [ ! -f /tmp/failsafe ] ; then
  199. kill_remaining TERM
  200. sleep 3
  201. kill_remaining KILL
  202. fi
  203. if [ -n "$(rootfs_type)" ]; then
  204. v "Switching to ramdisk..."
  205. run_ramfs '. /lib/functions.sh; include /lib/upgrade; do_upgrade'
  206. else
  207. do_upgrade
  208. fi