blob: fc83b61a9ebeac486513050a36aac73fc7fb403c [file] [log] [blame]
Chris Sosaa73ec162010-05-03 20:18:02 -07001#!/bin/sh
2
3# Copyright (c) 2010 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
Sonny Rao7e44ca22011-10-21 23:35:41 +000010# in order to support developers going from older images with the old shflags
11# we'll check for both new shflags and old shflags
12if [ -f /usr/share/misc/shflags ] ; then
13 . /usr/share/misc/shflags
14elif [ -f /usr/lib/shflags ] ; then
15 . /usr/lib/shflags
16else
17 echo >&2 "$0 Unable to source shflags"
18 exit 1
19fi
Chris Sosa1bf56372010-09-08 13:26:20 -070020
21# Constants for states.
22CLEAN_STATE="clean"
23OLD_STATE="old"
24
25DEFINE_string stateful_change "${OLD_STATE}" \
26 "The state of the new stateful partition - used in update testing."
27
28FLAGS "$@" || exit 1
29
Chris Sosaa73ec162010-05-03 20:18:02 -070030# Die on error.
31set -e
32
Chris Sosaca4a9252010-09-09 13:48:51 -070033remove_quotes() {
34 echo "$1" | sed -e "s/^'//; s/'$//"
35}
36
Chris Sosa1bf56372010-09-08 13:26:20 -070037update_dev_image () {
38 LSB_RELEASE="/etc/lsb-release"
39 STATEFUL_DIR="/mnt/stateful_partition"
Chris Sosaa73ec162010-05-03 20:18:02 -070040
Chris Sosaca4a9252010-09-09 13:48:51 -070041 if [ -n "${FLAGS_ARGV}" ]; then
42 BASE_UPDATE_URL=$(remove_quotes "${FLAGS_ARGV}")
Chris Sosa1bf56372010-09-08 13:26:20 -070043 else
Chris Sosa05491b12010-11-08 17:14:16 -080044 if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
Chris Sosa8086c3f2011-03-04 16:30:36 -080045 DEVSERVER_URL=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
46 cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 13:26:20 -070047 fi
Chris Sosa05491b12010-11-08 17:14:16 -080048 if [ -z "${DEVSERVER_URL}" ]; then
49 DEVSERVER_URL=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 13:26:20 -070050 fi
51 # Sanity check.
Chris Sosa05491b12010-11-08 17:14:16 -080052 if [ -z "${DEVSERVER_URL}" ]; then
Chris Sosa1bf56372010-09-08 13:26:20 -070053 echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file"
54 exit 1
55 fi
56 # Devserver URL should never contain "/update"
Chris Sosa05491b12010-11-08 17:14:16 -080057 DEVSERVER_URL=$(echo ${DEVSERVER_URL} | sed -e 's#/update##')
58 BASE_UPDATE_URL="${DEVSERVER_URL}/static"
Eric Li94a671e2010-08-02 17:58:50 -070059 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070060
Chris Sosa05491b12010-11-08 17:14:16 -080061 STATEFUL_UPDATE_URL="${BASE_UPDATE_URL}/stateful.tgz"
Chris Sosa4195fad2010-11-09 14:47:35 -080062 echo "Downloading stateful payload from ${STATEFUL_UPDATE_URL}"
63 # Download and unzip directories onto the stateful partition.
Chris Sosa8086c3f2011-03-04 16:30:36 -080064 eval "wget -qS -T 300 -O - \"${STATEFUL_UPDATE_URL}\"" |
Chris Sosaa7ae5ba2010-11-12 13:20:53 -080065 tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz
Chris Sosa1bf56372010-09-08 13:26:20 -070066 echo >&2 "Successfully downloaded update"
Chris Sosa05491b12010-11-08 17:14:16 -080067
Chris Sosa8086c3f2011-03-04 16:30:36 -080068 if [ ! -d "${STATEFUL_DIR}/var_new" ] ||
69 [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then
70 echo >&2 "Missing var or dev_image in stateful payload."
71 return 1
Eric Li94a671e2010-08-02 17:58:50 -070072 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070073}
74
75update_old_state () {
Chris Sosa05491b12010-11-08 17:14:16 -080076 echo >&2 "Performing standard stateful update."
Chris Sosa8086c3f2011-03-04 16:30:36 -080077 echo -n "" > "${STATEFUL_DIR}/.update_available"
Chris Sosa1bf56372010-09-08 13:26:20 -070078}
79
80update_clean_state () {
81 echo >&2 "Restoring state to factory_install with dev_image."
Chris Sosa8086c3f2011-03-04 16:30:36 -080082 echo -n "clobber" > "${STATEFUL_DIR}/.update_available"
Chris Sosa1bf56372010-09-08 13:26:20 -070083}
84
85main () {
Chris Sosa8086c3f2011-03-04 16:30:36 -080086 if update_dev_image; then
87 if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
88 update_old_state
89 elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
90 update_clean_state
91 else
92 echo >&2 "Invalid state given to stateful update. Aborting..."
93 return 1
94 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070095 else
Chris Sosa8086c3f2011-03-04 16:30:36 -080096 return 1
Eric Li94a671e2010-08-02 17:58:50 -070097 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070098}
Sean O'Connora7f867e2010-05-27 17:53:32 -070099
Chris Sosa1bf56372010-09-08 13:26:20 -0700100main $@