sysupgrade 12 KB

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