Allen Li | d13a1be | 2020-04-01 23:09:54 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | # This scripts performs update of stateful partition directories useful for |
| 8 | # dev_mode. |
| 9 | |
| 10 | # in order to support developers going from older images with the old shflags |
| 11 | # we'll check for both new shflags and old shflags |
| 12 | if [ -f /usr/share/misc/shflags ] ; then |
| 13 | . /usr/share/misc/shflags |
| 14 | elif [ -f /usr/lib/shflags ] ; then |
| 15 | . /usr/lib/shflags |
| 16 | else |
| 17 | echo >&2 "$0 Unable to source shflags" |
| 18 | exit 1 |
| 19 | fi |
| 20 | |
| 21 | # Constants for states. |
| 22 | CLEAN_STATE="clean" |
| 23 | OLD_STATE="old" |
| 24 | RESET_STATE="reset" |
| 25 | |
| 26 | LSB_RELEASE="/etc/lsb-release" |
| 27 | STATEFUL_DIR="/mnt/stateful_partition" |
| 28 | UPDATE_STATE_FILE=".update_available" |
| 29 | |
| 30 | DEFINE_string stateful_change "${OLD_STATE}" \ |
| 31 | "The state of the new stateful partition - used in update testing." |
| 32 | |
| 33 | FLAGS "$@" || exit 1 |
| 34 | |
| 35 | # Die on error. |
| 36 | set -e |
| 37 | |
| 38 | remove_quotes() { |
| 39 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 40 | } |
| 41 | |
| 42 | download_stateful_payload(){ |
| 43 | # Download and unzip directories onto the stateful partition. |
| 44 | eval "curl --silent --max-time 300 \"${stateful_update_url}\"" | |
| 45 | tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz |
| 46 | } |
| 47 | |
| 48 | update_dev_image () { |
| 49 | local base_update_url devserver_url |
| 50 | local local_payload_path |
| 51 | local path=$(remove_quotes "${FLAGS_ARGV}") |
| 52 | |
| 53 | if [ -z "${path}" ]; then |
| 54 | if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then |
| 55 | devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} | |
| 56 | cut -f 2 -d '=') |
| 57 | fi |
| 58 | if [ -z "${devserver_url}" ]; then |
| 59 | devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=') |
| 60 | fi |
| 61 | # Sanity check. |
| 62 | if [ -z "${devserver_url}" ]; then |
| 63 | echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file." |
| 64 | exit 1 |
| 65 | fi |
| 66 | # Devserver URL should never contain "/update" |
| 67 | devserver_url=$(echo ${devserver_url} | sed -e 's#/update##') |
| 68 | base_update_url="${devserver_url}/static" |
| 69 | # Determine whether the path is local. |
| 70 | elif [ -f "${path}" ] || [ "${path}" = "-" ]; then |
| 71 | local_payload_path=${path} |
| 72 | else |
| 73 | base_update_url=${path} |
| 74 | fi |
| 75 | |
| 76 | if [ -n "${base_update_url}" ]; then |
| 77 | local stateful_update_url="${base_update_url}/stateful.tgz" |
| 78 | local download_exit_code=0 |
| 79 | for i in `seq 1 2`; do |
| 80 | if [ $i -eq 1 ]; then |
| 81 | echo >&2 "Downloading stateful payload from ${stateful_update_url}" |
| 82 | else |
| 83 | echo >&2 "Downloading failed, retrying." |
| 84 | fi |
| 85 | if download_stateful_payload; then |
| 86 | download_exit_code=$? |
| 87 | echo >&2 "Downloading command returns code ${download_exit_code}." |
| 88 | break |
| 89 | else |
| 90 | download_exit_code=$? |
| 91 | echo >&2 "Downloading command returns code ${download_exit_code}." |
| 92 | fi |
| 93 | done |
| 94 | if [ ${download_exit_code} -ne 0 ]; then |
| 95 | return ${download_exit_code} |
| 96 | fi |
| 97 | echo >&2 "Successfully downloaded update." |
| 98 | else |
| 99 | echo >&2 "Reading local payload ${local_payload_path}" |
| 100 | # Set timeout to four minutes to avoid waiting on stdin indefinitely. |
| 101 | timeout 240s tar --ignore-command-error --overwrite \ |
| 102 | --directory=${STATEFUL_DIR} -xzf ${local_payload_path} |
| 103 | exit_code=$? |
| 104 | if [ "${exit_code}" -ne 0 ]; then |
| 105 | echo >&2 "Failed to unzip and write the stateful update." |
| 106 | return "${exit_code}" |
| 107 | fi |
| 108 | echo >&2 "Successfully retrieved update." |
| 109 | fi |
| 110 | |
| 111 | if [ ! -d "${STATEFUL_DIR}/var_new" ] || |
| 112 | [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then |
| 113 | echo >&2 "Missing var or dev_image in stateful payload." |
| 114 | return 1 |
| 115 | fi |
| 116 | } |
| 117 | |
| 118 | reset_state () { |
| 119 | echo >&2 "Resetting stateful update state." |
| 120 | rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
| 121 | rm -rf "${STATEFUL_DIR}/var_new" |
| 122 | rm -rf "${STATEFUL_DIR}/dev_image_new" |
| 123 | } |
| 124 | |
| 125 | update_old_state () { |
| 126 | echo >&2 "Performing standard stateful update." |
| 127 | echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
| 128 | } |
| 129 | |
| 130 | update_clean_state () { |
| 131 | echo >&2 "Restoring state to factory_install with dev_image." |
| 132 | echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
| 133 | } |
| 134 | |
| 135 | main () { |
| 136 | if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then |
| 137 | reset_state |
| 138 | elif update_dev_image; then |
| 139 | if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then |
| 140 | update_old_state |
| 141 | elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then |
| 142 | update_clean_state |
| 143 | else |
| 144 | echo >&2 "Invalid state given to stateful update. Aborting..." |
| 145 | return 1 |
| 146 | fi |
| 147 | else |
| 148 | return 1 |
| 149 | fi |
| 150 | } |
| 151 | |
| 152 | main $@ |