blob: 9cc87d6d24d2b11146f03434df6e152d46c5a181 [file] [log] [blame]
Chris Sosaa73ec162010-05-03 20:18:02 -07001#!/bin/sh
2
Chris Sosaa66f3a42012-05-17 19:12:58 -07003# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosaa73ec162010-05-03 20:18:02 -07004# 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"
Chris Sosaa66f3a42012-05-17 19:12:58 -070024RESET_STATE="reset"
25
26LSB_RELEASE="/etc/lsb-release"
27STATEFUL_DIR="/mnt/stateful_partition"
28UPDATE_STATE_FILE=".update_available"
Chris Sosa1bf56372010-09-08 13:26:20 -070029
30DEFINE_string stateful_change "${OLD_STATE}" \
31 "The state of the new stateful partition - used in update testing."
32
33FLAGS "$@" || exit 1
34
Chris Sosaa73ec162010-05-03 20:18:02 -070035# Die on error.
36set -e
37
Chris Sosaca4a9252010-09-09 13:48:51 -070038remove_quotes() {
39 echo "$1" | sed -e "s/^'//; s/'$//"
40}
41
Chris Sosa1bf56372010-09-08 13:26:20 -070042update_dev_image () {
Chris Sosaa66f3a42012-05-17 19:12:58 -070043 local base_update_url devserver_url
Yu-Ju Hong120ceab2013-12-09 22:47:52 -080044 local local_payload_path
45 local path=$(remove_quotes "${FLAGS_ARGV}")
46
47 if [ -z "${path}" ]; then
Chris Sosa05491b12010-11-08 17:14:16 -080048 if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
Chris Sosaa66f3a42012-05-17 19:12:58 -070049 devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
Chris Sosa8086c3f2011-03-04 16:30:36 -080050 cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 13:26:20 -070051 fi
Chris Sosaa66f3a42012-05-17 19:12:58 -070052 if [ -z "${devserver_url}" ]; then
53 devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 13:26:20 -070054 fi
55 # Sanity check.
Chris Sosaa66f3a42012-05-17 19:12:58 -070056 if [ -z "${devserver_url}" ]; then
Yu-Ju Hong120ceab2013-12-09 22:47:52 -080057 echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file."
Chris Sosa1bf56372010-09-08 13:26:20 -070058 exit 1
59 fi
60 # Devserver URL should never contain "/update"
Chris Sosaa66f3a42012-05-17 19:12:58 -070061 devserver_url=$(echo ${devserver_url} | sed -e 's#/update##')
62 base_update_url="${devserver_url}/static"
Yu-Ju Hong120ceab2013-12-09 22:47:52 -080063 # Determine whether the path is local.
64 elif [ -f "${path}" ] || [ "${path}" = "-" ]; then
65 local_payload_path=${path}
66 else
67 base_update_url=${path}
Eric Li94a671e2010-08-02 17:58:50 -070068 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070069
Yu-Ju Hong120ceab2013-12-09 22:47:52 -080070 if [ -n "${base_update_url}" ]; then
71 local stateful_update_url="${base_update_url}/stateful.tgz"
72 echo >&2 "Downloading stateful payload from ${stateful_update_url}"
73 # Download and unzip directories onto the stateful partition.
74 eval "wget -qS -T 300 -O - \"${stateful_update_url}\"" |
75 tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz
76 echo >&2 "Successfully downloaded update."
77 else
78 echo >&2 "Reading local payload ${local_payload_path}"
79 # Set timeout to two minutes to avoid waiting on stdin indefinitely.
80 timeout 120s tar --ignore-command-error --overwrite \
81 --directory=${STATEFUL_DIR} -xzf ${local_payload_path}
82 echo >&2 "Successfully retrieved update."
83 fi
Chris Sosa05491b12010-11-08 17:14:16 -080084
Chris Sosa8086c3f2011-03-04 16:30:36 -080085 if [ ! -d "${STATEFUL_DIR}/var_new" ] ||
86 [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then
87 echo >&2 "Missing var or dev_image in stateful payload."
88 return 1
Eric Li94a671e2010-08-02 17:58:50 -070089 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070090}
91
Chris Sosaa66f3a42012-05-17 19:12:58 -070092reset_state () {
93 echo >&2 "Resetting stateful update state."
94 rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
95 rm -rf "${STATEFUL_DIR}/var_new"
96 rm -rf "${STATEFUL_DIR}/dev_image_new"
97}
98
Chris Sosa1bf56372010-09-08 13:26:20 -070099update_old_state () {
Chris Sosa05491b12010-11-08 17:14:16 -0800100 echo >&2 "Performing standard stateful update."
Chris Sosaa66f3a42012-05-17 19:12:58 -0700101 echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
Chris Sosa1bf56372010-09-08 13:26:20 -0700102}
103
104update_clean_state () {
105 echo >&2 "Restoring state to factory_install with dev_image."
Chris Sosaa66f3a42012-05-17 19:12:58 -0700106 echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
Chris Sosa1bf56372010-09-08 13:26:20 -0700107}
108
109main () {
Chris Sosaa66f3a42012-05-17 19:12:58 -0700110 if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then
111 reset_state
112 elif update_dev_image; then
Chris Sosa8086c3f2011-03-04 16:30:36 -0800113 if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
114 update_old_state
115 elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
116 update_clean_state
117 else
118 echo >&2 "Invalid state given to stateful update. Aborting..."
119 return 1
120 fi
Chris Sosa1bf56372010-09-08 13:26:20 -0700121 else
Chris Sosa8086c3f2011-03-04 16:30:36 -0800122 return 1
Eric Li94a671e2010-08-02 17:58:50 -0700123 fi
Chris Sosa1bf56372010-09-08 13:26:20 -0700124}
Sean O'Connora7f867e2010-05-27 17:53:32 -0700125
Chris Sosa1bf56372010-09-08 13:26:20 -0700126main $@