Chris Sosa | a73ec16 | 2010-05-04 03:18:02 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Sosa | a73ec16 | 2010-05-04 03:18:02 | [diff] [blame] | 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 | |
Sonny Rao | 7e44ca2 | 2011-10-21 23:35:41 | [diff] [blame] | 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 |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 20 | |
| 21 | # Constants for states. |
| 22 | CLEAN_STATE="clean" |
| 23 | OLD_STATE="old" |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 24 | RESET_STATE="reset" |
| 25 | |
| 26 | LSB_RELEASE="/etc/lsb-release" |
| 27 | STATEFUL_DIR="/mnt/stateful_partition" |
| 28 | UPDATE_STATE_FILE=".update_available" |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 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 | |
Chris Sosa | a73ec16 | 2010-05-04 03:18:02 | [diff] [blame] | 35 | # Die on error. |
| 36 | set -e |
| 37 | |
Chris Sosa | ca4a925 | 2010-09-09 20:48:51 | [diff] [blame] | 38 | remove_quotes() { |
| 39 | echo "$1" | sed -e "s/^'//; s/'$//" |
| 40 | } |
| 41 | |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 42 | update_dev_image () { |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 43 | local base_update_url devserver_url |
Chris Sosa | ca4a925 | 2010-09-09 20:48:51 | [diff] [blame] | 44 | if [ -n "${FLAGS_ARGV}" ]; then |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 45 | base_update_url=$(remove_quotes "${FLAGS_ARGV}") |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 46 | else |
Chris Sosa | 05491b1 | 2010-11-09 01:14:16 | [diff] [blame] | 47 | if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 48 | devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} | |
Chris Sosa | 8086c3f | 2011-03-05 00:30:36 | [diff] [blame] | 49 | cut -f 2 -d '=') |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 50 | fi |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 51 | if [ -z "${devserver_url}" ]; then |
| 52 | devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=') |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 53 | fi |
| 54 | # Sanity check. |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 55 | if [ -z "${devserver_url}" ]; then |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 56 | echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file" |
| 57 | exit 1 |
| 58 | fi |
| 59 | # Devserver URL should never contain "/update" |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 60 | devserver_url=$(echo ${devserver_url} | sed -e 's#/update##') |
| 61 | base_update_url="${devserver_url}/static" |
Eric Li | 94a671e | 2010-08-03 00:58:50 | [diff] [blame] | 62 | fi |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 63 | |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 64 | local stateful_update_url="${base_update_url}/stateful.tgz" |
| 65 | echo "Downloading stateful payload from ${stateful_update_url}" |
Chris Sosa | 4195fad | 2010-11-09 22:47:35 | [diff] [blame] | 66 | # Download and unzip directories onto the stateful partition. |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 67 | eval "wget -qS -T 300 -O - \"${stateful_update_url}\"" | |
Chris Sosa | a7ae5ba | 2010-11-12 21:20:53 | [diff] [blame] | 68 | tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 69 | echo >&2 "Successfully downloaded update" |
Chris Sosa | 05491b1 | 2010-11-09 01:14:16 | [diff] [blame] | 70 | |
Chris Sosa | 8086c3f | 2011-03-05 00:30:36 | [diff] [blame] | 71 | if [ ! -d "${STATEFUL_DIR}/var_new" ] || |
| 72 | [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then |
| 73 | echo >&2 "Missing var or dev_image in stateful payload." |
| 74 | return 1 |
Eric Li | 94a671e | 2010-08-03 00:58:50 | [diff] [blame] | 75 | fi |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 76 | } |
| 77 | |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 78 | reset_state () { |
| 79 | echo >&2 "Resetting stateful update state." |
| 80 | rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
| 81 | rm -rf "${STATEFUL_DIR}/var_new" |
| 82 | rm -rf "${STATEFUL_DIR}/dev_image_new" |
| 83 | } |
| 84 | |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 85 | update_old_state () { |
Chris Sosa | 05491b1 | 2010-11-09 01:14:16 | [diff] [blame] | 86 | echo >&2 "Performing standard stateful update." |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 87 | echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | update_clean_state () { |
| 91 | echo >&2 "Restoring state to factory_install with dev_image." |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 92 | echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}" |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | main () { |
Chris Sosa | a66f3a4 | 2012-05-18 02:12:58 | [diff] [blame] | 96 | if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then |
| 97 | reset_state |
| 98 | elif update_dev_image; then |
Chris Sosa | 8086c3f | 2011-03-05 00:30:36 | [diff] [blame] | 99 | if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then |
| 100 | update_old_state |
| 101 | elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then |
| 102 | update_clean_state |
| 103 | else |
| 104 | echo >&2 "Invalid state given to stateful update. Aborting..." |
| 105 | return 1 |
| 106 | fi |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 107 | else |
Chris Sosa | 8086c3f | 2011-03-05 00:30:36 | [diff] [blame] | 108 | return 1 |
Eric Li | 94a671e | 2010-08-03 00:58:50 | [diff] [blame] | 109 | fi |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 110 | } |
Sean O'Connor | a7f867e | 2010-05-28 00:53:32 | [diff] [blame] | 111 | |
Chris Sosa | 1bf5637 | 2010-09-08 20:26:20 | [diff] [blame] | 112 | main $@ |