sysupgrade 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/sh
  2. . /etc/functions.sh
  3. # initialize defaults
  4. RAMFS_COPY_BIN="" # extra programs for temporary ramfs root
  5. RAMFS_COPY_DATA="" # extra data files
  6. export INTERACTIVE=0
  7. export VERBOSE=1
  8. export SAVE_CONFIG=1
  9. export SAVE_OVERLAY=0
  10. export DELAY=
  11. export CONF_IMAGE=
  12. # parse options
  13. while [ -n "$1" ]; do
  14. case "$1" in
  15. -i) export INTERACTIVE=1;;
  16. -d) export DELAY="$2"; shift;;
  17. -v) export VERBOSE="$(($VERBOSE + 1))";;
  18. -q) export VERBOSE="$(($VERBOSE - 1))";;
  19. -n) export SAVE_CONFIG=0;;
  20. -c) export SAVE_OVERLAY=1;;
  21. -f) export CONF_IMAGE="$2"; shift;;
  22. -*)
  23. echo "Invalid option: $1"
  24. exit 1
  25. ;;
  26. *) break;;
  27. esac
  28. shift;
  29. done
  30. export CONFFILES=/tmp/sysupgrade.conffiles
  31. export CONF_TAR=/tmp/sysupgrade.tgz
  32. export ARGV="$*"
  33. export ARGC="$#"
  34. [ -z "$ARGV" ] && {
  35. cat <<EOF
  36. Usage: $0 [options] <image file or URL>
  37. Options:
  38. -d <delay> add a delay before rebooting
  39. -f <config> restore configuration from .tar.gz (file or url)
  40. -i interactive mode
  41. -c attempt to preserve all changed files in /etc/
  42. -n do not save configuration over reflash
  43. -q less verbose
  44. -v more verbose
  45. EOF
  46. exit 1
  47. }
  48. add_uci_conffiles() {
  49. local file="$1"
  50. ( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  51. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  52. -type f 2>/dev/null;
  53. opkg list-changed-conffiles ) | sort -u > "$file"
  54. return 0
  55. }
  56. add_overlayfiles() {
  57. local file="$1"
  58. find /overlay/etc/ -type f | sed \
  59. -e 's,^/overlay/,/,' \
  60. -e '\,/META_[a-zA-Z0-9]*$,d' \
  61. -e '\,/functions.sh$,d' \
  62. -e '\,/[^/]*-opkg$,d' \
  63. > "$file"
  64. return 0
  65. }
  66. # hooks
  67. sysupgrade_image_check="platform_check_image"
  68. [ $SAVE_OVERLAY = 0 -o ! -d /overlay/etc ] && \
  69. sysupgrade_init_conffiles="add_uci_conffiles" || \
  70. sysupgrade_init_conffiles="add_overlayfiles"
  71. include /lib/upgrade
  72. do_save_conffiles() {
  73. [ -z "$(rootfs_type)" ] && {
  74. echo "Cannot save config while running from ramdisk."
  75. ask_bool 0 "Abort" && exit
  76. return 0
  77. }
  78. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  79. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  80. v "Saving config files..."
  81. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  82. tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
  83. }
  84. type platform_check_image >/dev/null 2>/dev/null || {
  85. echo "Firmware upgrade is not implemented for this platform."
  86. exit 1
  87. }
  88. for check in $sysupgrade_image_check; do
  89. ( eval "$check \"\$ARGV\"" ) || {
  90. echo "Image check '$check' failed."
  91. exit 1
  92. }
  93. done
  94. if [ -n "$CONF_IMAGE" ]; then
  95. case "$(get_magic_word $CONF_IMAGE cat)" in
  96. # .gz files
  97. 1f8b) ;;
  98. *)
  99. echo "Invalid config file. Please use only .tar.gz files"
  100. exit 1
  101. ;;
  102. esac
  103. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  104. export SAVE_CONFIG=1
  105. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  106. do_save_conffiles
  107. export SAVE_CONFIG=1
  108. else
  109. export SAVE_CONFIG=0
  110. fi
  111. run_hooks "" $sysupgrade_pre_upgrade
  112. if [ -n "$(rootfs_type)" ]; then
  113. v "Switching to ramdisk..."
  114. run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'
  115. else
  116. do_upgrade
  117. fi