sysupgrade 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 DELAY=
  10. export CONF_IMAGE=
  11. # parse options
  12. while [ -n "$1" ]; do
  13. case "$1" in
  14. -i) export INTERACTIVE=1;;
  15. -d) export DELAY="$2"; shift;;
  16. -v) export VERBOSE="$(($VERBOSE + 1))";;
  17. -q) export VERBOSE="$(($VERBOSE - 1))";;
  18. -n) export SAVE_CONFIG=0;;
  19. -f) export CONF_IMAGE="$2"; shift;;
  20. -*)
  21. echo "Invalid option: $1"
  22. exit 1
  23. ;;
  24. *) break;;
  25. esac
  26. shift;
  27. done
  28. export CONFFILES=/tmp/sysupgrade.conffiles
  29. export CONF_TAR=/tmp/sysupgrade.tgz
  30. export ARGV="$*"
  31. export ARGC="$#"
  32. [ -z "$ARGV" ] && {
  33. cat <<EOF
  34. Usage: $0 [options] <image file or URL>
  35. Options:
  36. -d <delay> add a delay before rebooting
  37. -f <config> restore configuration from .tar.gz (file or url)
  38. -i interactive mode
  39. -n do not save configuration over reflash
  40. -q less verbose
  41. -v more verbose
  42. EOF
  43. exit 1
  44. }
  45. add_uci_conffiles() {
  46. local file="$1"
  47. find /etc/config /etc/passwd /etc/group /etc/dropbear /etc/firewall.user > "$file"
  48. return 0
  49. }
  50. # hooks
  51. sysupgrade_image_check="platform_check_image"
  52. sysupgrade_init_conffiles="add_uci_conffiles"
  53. include /lib/upgrade
  54. do_save_conffiles() {
  55. [ -z "$(rootfs_type)" ] && {
  56. echo "Cannot save config while running from ramdisk."
  57. ask_bool 0 "Abort" && exit
  58. return 0
  59. }
  60. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  61. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  62. v "Saving config files..."
  63. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  64. tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
  65. }
  66. type platform_check_image >/dev/null 2>/dev/null || {
  67. echo "Firmware upgrade is not implemented for this platform."
  68. exit 1
  69. }
  70. for check in $sysupgrade_image_check; do
  71. ( eval "$check \"\$ARGV\"" ) || {
  72. echo "Image check '$check' failed."
  73. exit 1
  74. }
  75. done
  76. if [ -n "$CONF_IMAGE" ]; then
  77. case "$(get_magic_word "$CONF_IMAGE")" in
  78. # .gz files
  79. 1f8b) ;;
  80. *)
  81. echo "Invalid config file. Please use only .tar.gz files"
  82. exit 1
  83. ;;
  84. esac
  85. get_image "$CONF_IMAGE" > "$CONF_TAR"
  86. export SAVE_CONFIG=1
  87. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  88. do_save_conffiles
  89. export SAVE_CONFIG=1
  90. else
  91. export SAVE_CONFIG=0
  92. fi
  93. run_hooks "" $sysupgrade_pre_upgrade
  94. v "Switching to ramdisk..."
  95. run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'