sysupgrade 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. export GZIPED=
  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. -f) export CONF_IMAGE="$2"; shift;;
  21. -g) export GZIPED=1;;
  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. -g gziped image
  41. -i interactive mode
  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 /etc/config /etc/passwd /etc/group /etc/dropbear /etc/firewall.user > "$file"
  51. return 0
  52. }
  53. # hooks
  54. sysupgrade_image_check="platform_check_image"
  55. sysupgrade_init_conffiles="add_uci_conffiles"
  56. include /lib/upgrade
  57. do_save_conffiles() {
  58. [ -z "$(rootfs_type)" ] && {
  59. echo "Cannot save config while running from ramdisk."
  60. ask_bool 0 "Abort" && exit
  61. return 0
  62. }
  63. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  64. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  65. v "Saving config files..."
  66. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  67. tar c${TAR_V}zf "$CONF_TAR" -T "$CONFFILES" 2>/dev/null
  68. }
  69. type platform_check_image >/dev/null 2>/dev/null || {
  70. echo "Firmware upgrade is not implemented for this platform."
  71. exit 1
  72. }
  73. for check in $sysupgrade_image_check; do
  74. ( eval "$check \"\$ARGV\"" ) || {
  75. echo "Image check '$check' failed."
  76. exit 1
  77. }
  78. done
  79. if [ -n "$CONF_IMAGE" ]; then
  80. case "$(get_magic_word "$CONF_IMAGE")" in
  81. # .gz files
  82. 1f8b) ;;
  83. *)
  84. echo "Invalid config file. Please use only .tar.gz files"
  85. exit 1
  86. ;;
  87. esac
  88. get_image "$CONF_IMAGE" > "$CONF_TAR"
  89. export SAVE_CONFIG=1
  90. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  91. do_save_conffiles
  92. export SAVE_CONFIG=1
  93. else
  94. export SAVE_CONFIG=0
  95. fi
  96. run_hooks "" $sysupgrade_pre_upgrade
  97. v "Switching to ramdisk..."
  98. run_ramfs '. /etc/functions.sh; include /lib/upgrade; do_upgrade'