sysupgrade 10 KB

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