sysupgrade 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . /lib/functions/system.sh
  4. . /usr/share/libubox/jshn.sh
  5. # File-local constants
  6. CONF_TAR=/tmp/sysupgrade.tgz
  7. ETCBACKUP_DIR=/etc/backup
  8. INSTALLED_PACKAGES=${ETCBACKUP_DIR}/installed_packages.txt
  9. COMMAND=/lib/upgrade/do_stage2
  10. # File-local globals
  11. SAVE_OVERLAY=0
  12. SAVE_OVERLAY_PATH=
  13. SAVE_PARTITIONS=1
  14. SAVE_INSTALLED_PKGS=0
  15. SKIP_UNCHANGED=0
  16. CONF_IMAGE=
  17. CONF_BACKUP_LIST=0
  18. CONF_BACKUP=
  19. CONF_RESTORE=
  20. NEED_IMAGE=
  21. HELP=0
  22. TEST=0
  23. # Globals accessed in other files
  24. export MTD_ARGS=""
  25. export MTD_CONFIG_ARGS=""
  26. export INTERACTIVE=0
  27. export VERBOSE=1
  28. export SAVE_CONFIG=1
  29. export IGNORE_MINOR_COMPAT=0
  30. export FORCE=0
  31. export CONFFILES=/tmp/sysupgrade.conffiles
  32. # parse options
  33. while [ -n "$1" ]; do
  34. case "$1" in
  35. -i) export INTERACTIVE=1;;
  36. -v) export VERBOSE="$(($VERBOSE + 1))";;
  37. -q) export VERBOSE="$(($VERBOSE - 1))";;
  38. -n) export SAVE_CONFIG=0;;
  39. -c) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
  40. -o) SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
  41. -p) SAVE_PARTITIONS=0;;
  42. -k) SAVE_INSTALLED_PKGS=1;;
  43. -u) SKIP_UNCHANGED=1;;
  44. -b|--create-backup) CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
  45. -r|--restore-backup) CONF_RESTORE="$2" NEED_IMAGE=1; shift;;
  46. -l|--list-backup) CONF_BACKUP_LIST=1;;
  47. -f) CONF_IMAGE="$2"; shift;;
  48. -F|--force) export FORCE=1;;
  49. -T|--test) TEST=1;;
  50. -h|--help) HELP=1; break;;
  51. --ignore-minor-compat-version) export IGNORE_MINOR_COMPAT=1;;
  52. -*)
  53. echo "Invalid option: $1" >&2
  54. exit 1
  55. ;;
  56. *) break;;
  57. esac
  58. shift;
  59. done
  60. print_help() {
  61. cat <<EOF
  62. Usage: $0 [<upgrade-option>...] <image file or URL>
  63. $0 [-q] [-i] [-c] [-u] [-o] [-k] <backup-command> <file>
  64. upgrade-option:
  65. -f <config> restore configuration from .tar.gz (file or url)
  66. -i interactive mode
  67. -c attempt to preserve all changed files in /etc/
  68. -o attempt to preserve all changed files in /, except those
  69. from packages but including changed confs.
  70. -u skip from backup files that are equal to those in /rom
  71. -n do not save configuration over reflash
  72. -p do not attempt to restore the partition table after flash.
  73. -k include in backup a list of current installed packages at
  74. $INSTALLED_PACKAGES
  75. -T | --test
  76. Verify image and config .tar.gz but do not actually flash.
  77. -F | --force
  78. Flash image even if image checks fail, this is dangerous!
  79. --ignore-minor-compat-version
  80. Flash image even if the minor compat version is incompatible.
  81. -q less verbose
  82. -v more verbose
  83. -h | --help display this help
  84. backup-command:
  85. -b | --create-backup <file>
  86. create .tar.gz of files specified in sysupgrade.conf
  87. then exit. Does not flash an image. If file is '-',
  88. i.e. stdout, verbosity is set to 0 (i.e. quiet).
  89. -r | --restore-backup <file>
  90. restore a .tar.gz created with sysupgrade -b
  91. then exit. Does not flash an image. If file is '-',
  92. the archive is read from stdin.
  93. -l | --list-backup
  94. list the files that would be backed up when calling
  95. sysupgrade -b. Does not create a backup file.
  96. EOF
  97. }
  98. IMAGE="$1"
  99. if [ $HELP -gt 0 ]; then
  100. print_help
  101. exit 0
  102. fi
  103. if [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 ]; then
  104. print_help
  105. exit 1
  106. fi
  107. [ -n "$IMAGE" -a -n "$NEED_IMAGE" ] && {
  108. cat <<-EOF
  109. -b|--create-backup and -r|--restore-backup do not perform a firmware upgrade.
  110. Do not specify both -b|-r and a firmware image.
  111. EOF
  112. exit 1
  113. }
  114. # prevent messages from clobbering the tarball when using stdout
  115. [ "$CONF_BACKUP" = "-" ] && export VERBOSE=0
  116. list_conffiles() {
  117. awk '
  118. BEGIN { conffiles = 0 }
  119. /^Conffiles:/ { conffiles = 1; next }
  120. !/^ / { conffiles = 0; next }
  121. conffiles == 1 { print }
  122. ' /usr/lib/opkg/status
  123. }
  124. list_changed_conffiles() {
  125. # Cannot handle spaces in filenames - but opkg cannot either...
  126. list_conffiles | while read file csum; do
  127. [ -r "$file" ] || continue
  128. echo "${csum} ${file}" | busybox sha256sum -sc - || echo "$file"
  129. done
  130. }
  131. list_static_conffiles() {
  132. local filter=$1
  133. find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
  134. /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
  135. \( -type f -o -type l \) $filter 2>/dev/null
  136. }
  137. build_list_of_backup_config_files() {
  138. local file="$1"
  139. ( list_static_conffiles "$find_filter"; list_changed_conffiles ) |
  140. sort -u > "$file"
  141. return 0
  142. }
  143. build_list_of_backup_overlay_files() {
  144. local file="$1"
  145. local packagesfiles=$1.packagesfiles
  146. touch "$packagesfiles"
  147. if [ "$SAVE_OVERLAY_PATH" = / ]; then
  148. local conffiles=$1.conffiles
  149. local keepfiles=$1.keepfiles
  150. list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
  151. # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
  152. # ignore those aready controlled by opkg conffiles
  153. list_static_conffiles | sort -u |
  154. grep -h -v -x -F -f $conffiles > "$keepfiles"
  155. # backup conffiles, but only those changed if '-u'
  156. [ $SKIP_UNCHANGED = 1 ] &&
  157. list_changed_conffiles | sort -u > "$conffiles"
  158. # do not backup files from packages, except those listed
  159. # in conffiles and keep.d
  160. {
  161. find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
  162. find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
  163. -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
  164. cut -f2 -d:
  165. } | grep -v -x -F -f $conffiles |
  166. grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
  167. rm -f "$keepfiles" "$conffiles"
  168. fi
  169. # busybox grep bug when file is empty
  170. [ -s "$packagesfiles" ] || echo > $packagesfiles
  171. ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
  172. -e 's,^\.,,' \
  173. -e '\,^/etc/board.json$,d' \
  174. -e '\,/[^/]*-opkg$,d' \
  175. -e '\,^/etc/urandom.seed$,d' \
  176. -e "\,^$INSTALLED_PACKAGES$,d" \
  177. -e '\,^/usr/lib/opkg/.*,d' \
  178. ) | grep -v -x -F -f $packagesfiles > "$file"
  179. rm -f "$packagesfiles"
  180. return 0
  181. }
  182. if [ $SAVE_OVERLAY = 1 ]; then
  183. [ ! -d /overlay/upper/etc ] && {
  184. echo "Cannot find '/overlay/upper/etc', required for '-c' or '-o'" >&2
  185. exit 1
  186. }
  187. sysupgrade_init_conffiles="build_list_of_backup_overlay_files"
  188. else
  189. sysupgrade_init_conffiles="build_list_of_backup_config_files"
  190. fi
  191. find_filter=""
  192. if [ $SKIP_UNCHANGED = 1 ]; then
  193. [ ! -d /rom/ ] && {
  194. echo "'/rom/' is required by '-u'"
  195. exit 1
  196. }
  197. find_filter='( ( -exec test -e /rom/{} ; -exec cmp -s /{} /rom/{} ; ) -o -print )'
  198. fi
  199. include /lib/upgrade
  200. create_backup_archive() {
  201. local conf_tar="$1"
  202. local disabled
  203. local err
  204. [ "$(rootfs_type)" = "tmpfs" ] && {
  205. echo "Cannot save config while running from ramdisk." >&2
  206. ask_bool 0 "Abort" && exit
  207. return 0
  208. }
  209. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  210. ask_bool 0 "Edit config file list" && vi "$CONFFILES"
  211. [ "$conf_tar" != "-" ] || conf_tar=""
  212. v "Saving config files..."
  213. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  214. sed -i -e 's,^/,,' "$CONFFILES"
  215. set -o pipefail
  216. {
  217. local ret=0
  218. if [ $ret -eq 0 ]; then
  219. for service in /etc/init.d/*; do
  220. if ! $service enabled; then
  221. disabled="$disabled$service disable\n"
  222. fi
  223. done
  224. disabled="$disabled\nexit 0"
  225. tar_print_member "/etc/uci-defaults/10_disable_services" "$(echo -e $disabled)" || ret=1
  226. fi
  227. # Part of archive with installed packages info
  228. if [ $ret -eq 0 ]; then
  229. if [ "$SAVE_INSTALLED_PKGS" -eq 1 ]; then
  230. # Format: pkg-name<TAB>{rom,overlay,unknown}
  231. # rom is used for pkgs in /rom, even if updated later
  232. tar_print_member "$INSTALLED_PACKAGES" "$(find /usr/lib/opkg/info -name "*.control" \( \
  233. \( -exec test -f /rom/{} \; -exec echo {} rom \; \) -o \
  234. \( -exec test -f /overlay/upper/{} \; -exec echo {} overlay \; \) -o \
  235. \( -exec echo {} unknown \; \) \
  236. \) | sed -e 's,.*/,,;s/\.control /\t/')" || ret=1
  237. fi
  238. fi
  239. # Rest of archive with config files and ending padding
  240. if [ $ret -eq 0 ]; then
  241. tar c${TAR_V} -C / -T "$CONFFILES" || ret=1
  242. fi
  243. [ $ret -eq 0 ]
  244. } | gzip > "${conf_tar:-/proc/self/fd/1}"
  245. err=$?
  246. set +o pipefail
  247. if [ "$err" -ne 0 ]; then
  248. echo "Failed to create the configuration backup."
  249. [ -f "$conf_tar" ] && rm -f "$conf_tar"
  250. fi
  251. rm -f "$CONFFILES"
  252. return "$err"
  253. }
  254. if [ $CONF_BACKUP_LIST -eq 1 ]; then
  255. run_hooks "$CONFFILES" $sysupgrade_init_conffiles
  256. [ "$SAVE_INSTALLED_PKGS" -eq 1 ] && echo ${INSTALLED_PACKAGES} >> "$CONFFILES"
  257. cat "$CONFFILES"
  258. rm -f "$CONFFILES"
  259. exit 0
  260. fi
  261. if [ -n "$CONF_BACKUP" ]; then
  262. create_backup_archive "$CONF_BACKUP"
  263. exit
  264. fi
  265. if [ -n "$CONF_RESTORE" ]; then
  266. if [ "$CONF_RESTORE" != "-" ] && [ ! -f "$CONF_RESTORE" ]; then
  267. echo "Backup archive '$CONF_RESTORE' not found." >&2
  268. exit 1
  269. fi
  270. [ "$VERBOSE" -gt 1 ] && TAR_V="v" || TAR_V=""
  271. v "Restoring config files..."
  272. if [ "$(type -t platform_restore_backup)" == 'platform_restore_backup' ]; then
  273. platform_restore_backup "$TAR_V"
  274. else
  275. tar -C / -x${TAR_V}zf "$CONF_RESTORE"
  276. fi
  277. exit $?
  278. fi
  279. type platform_check_image >/dev/null 2>/dev/null || {
  280. echo "Firmware upgrade is not implemented for this platform." >&2
  281. exit 1
  282. }
  283. case "$IMAGE" in
  284. http://*|\
  285. https://*)
  286. wget -O/tmp/sysupgrade.img "$IMAGE" || exit 1
  287. IMAGE=/tmp/sysupgrade.img
  288. ;;
  289. esac
  290. IMAGE="$(readlink -f "$IMAGE")"
  291. case "$IMAGE" in
  292. '')
  293. echo "Image file not found." >&2
  294. exit 1
  295. ;;
  296. /tmp/*) ;;
  297. *)
  298. v "Image not in /tmp, copying..."
  299. cp -f "$IMAGE" /tmp/sysupgrade.img
  300. IMAGE=/tmp/sysupgrade.img
  301. ;;
  302. esac
  303. json_load "$(/usr/libexec/validate_firmware_image "$IMAGE")" || {
  304. echo "Failed to check image"
  305. exit 1
  306. }
  307. json_get_var valid "valid"
  308. [ "$valid" -eq 0 ] && {
  309. if [ $FORCE -eq 1 ]; then
  310. echo "Image check failed but --force given - will update anyway!" >&2
  311. else
  312. echo "Image check failed." >&2
  313. exit 1
  314. fi
  315. }
  316. if [ -n "$CONF_IMAGE" ]; then
  317. case "$(get_magic_word $CONF_IMAGE cat)" in
  318. # .gz files
  319. 1f8b) ;;
  320. *)
  321. echo "Invalid config file. Please use only .tar.gz files" >&2
  322. exit 1
  323. ;;
  324. esac
  325. get_image "$CONF_IMAGE" "cat" > "$CONF_TAR"
  326. export SAVE_CONFIG=1
  327. elif ask_bool $SAVE_CONFIG "Keep config files over reflash"; then
  328. [ $TEST -eq 1 ] || create_backup_archive "$CONF_TAR" || exit
  329. export SAVE_CONFIG=1
  330. else
  331. [ $TEST -eq 1 ] || rm -f "$CONF_TAR"
  332. export SAVE_CONFIG=0
  333. fi
  334. if [ $TEST -eq 1 ]; then
  335. exit 0
  336. fi
  337. install_bin /sbin/upgraded
  338. v "Commencing upgrade. Closing all shell sessions."
  339. if [ -n "$FAILSAFE" ]; then
  340. printf '%s\x00%s\x00%s' "$RAM_ROOT" "$IMAGE" "$COMMAND" >/tmp/sysupgrade
  341. lock -u /tmp/.failsafe
  342. else
  343. json_init
  344. json_add_string prefix "$RAM_ROOT"
  345. json_add_string path "$IMAGE"
  346. [ $FORCE -eq 1 ] && json_add_boolean force 1
  347. [ $SAVE_CONFIG -eq 1 ] && json_add_string backup "$CONF_TAR"
  348. json_add_string command "$COMMAND"
  349. json_add_object options
  350. json_add_int save_partitions "$SAVE_PARTITIONS"
  351. json_close_object
  352. ubus call system sysupgrade "$(json_dump)"
  353. fi