blob: 7f5ddaec7e996bdf90bffddbca6b3778680b8431 [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
Chris Sosa1bf56372010-09-08 13:26:20 -070010. /usr/lib/shflags
11
12# Constants for states.
13CLEAN_STATE="clean"
14OLD_STATE="old"
15
16DEFINE_string stateful_change "${OLD_STATE}" \
17 "The state of the new stateful partition - used in update testing."
18
19FLAGS "$@" || exit 1
20
Chris Sosaa73ec162010-05-03 20:18:02 -070021# Die on error.
22set -e
23
Chris Sosaca4a9252010-09-09 13:48:51 -070024remove_quotes() {
25 echo "$1" | sed -e "s/^'//; s/'$//"
26}
27
Chris Sosa1bf56372010-09-08 13:26:20 -070028update_dev_image () {
29 LSB_RELEASE="/etc/lsb-release"
30 STATEFUL_DIR="/mnt/stateful_partition"
Chris Sosa05491b12010-11-08 17:14:16 -080031 WORK_DIR="${STATEFUL_DIR}/stateful_update"
Chris Sosaa73ec162010-05-03 20:18:02 -070032
Chris Sosaca4a9252010-09-09 13:48:51 -070033 if [ -n "${FLAGS_ARGV}" ]; then
34 BASE_UPDATE_URL=$(remove_quotes "${FLAGS_ARGV}")
Chris Sosa1bf56372010-09-08 13:26:20 -070035 else
Chris Sosa05491b12010-11-08 17:14:16 -080036 if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
37 DEVSERVER_URL=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} | \
Chris Sosa1bf56372010-09-08 13:26:20 -070038 cut -f 2 -d '=')
39 fi
Chris Sosa05491b12010-11-08 17:14:16 -080040 if [ -z "${DEVSERVER_URL}" ]; then
41 DEVSERVER_URL=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 13:26:20 -070042 fi
43 # Sanity check.
Chris Sosa05491b12010-11-08 17:14:16 -080044 if [ -z "${DEVSERVER_URL}" ]; then
Chris Sosa1bf56372010-09-08 13:26:20 -070045 echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file"
46 exit 1
47 fi
48 # Devserver URL should never contain "/update"
Chris Sosa05491b12010-11-08 17:14:16 -080049 DEVSERVER_URL=$(echo ${DEVSERVER_URL} | sed -e 's#/update##')
50 BASE_UPDATE_URL="${DEVSERVER_URL}/static"
Eric Li94a671e2010-08-02 17:58:50 -070051 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070052
Chris Sosa05491b12010-11-08 17:14:16 -080053 STATEFUL_UPDATE_URL="${BASE_UPDATE_URL}/stateful.tgz"
Chris Sosa1bf56372010-09-08 13:26:20 -070054
55 # Prepare directories for update.
Chris Sosa05491b12010-11-08 17:14:16 -080056 rm -rf "${WORK_DIR}"
57 mkdir "${WORK_DIR}"
Chris Sosa1bf56372010-09-08 13:26:20 -070058
Chris Sosa05491b12010-11-08 17:14:16 -080059 echo "Download stateful image from ${STATEFUL_UPDATE_URL}"
Chris Sosa1bf56372010-09-08 13:26:20 -070060 # Unzip mount and copy the relevant directories.
61 # Get the update.
Chris Sosa05491b12010-11-08 17:14:16 -080062 eval "wget -qS -T 300 -O - \"${STATEFUL_UPDATE_URL}\"" | \
63 tar -C ${WORK_DIR} -xz
Chris Sosa1bf56372010-09-08 13:26:20 -070064 echo >&2 "Successfully downloaded update"
Chris Sosa05491b12010-11-08 17:14:16 -080065
66 if [ -d "${WORK_DIR}/var" ] && [ -d "${WORK_DIR}/dev_image" ] ; then
Chris Sosad1371712010-09-28 14:53:43 -070067 echo >&2 "Notifying startup that an update is available"
Chris Sosa05491b12010-11-08 17:14:16 -080068 touch "${STATEFUL_DIR}/.update_available"
Chris Sosa1bf56372010-09-08 13:26:20 -070069 else
70 echo >&2 "No update available"
Eric Li94a671e2010-08-02 17:58:50 -070071 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070072}
73
74update_old_state () {
Chris Sosa05491b12010-11-08 17:14:16 -080075 echo >&2 "Performing standard stateful update."
Chris Sosa1bf56372010-09-08 13:26:20 -070076}
77
78update_clean_state () {
79 echo >&2 "Restoring state to factory_install with dev_image."
80 echo "fast test" > "/mnt/stateful_partition/factory_install_reset"
81}
82
83main () {
84 update_dev_image
85 if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
86 update_old_state
87 elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
88 update_clean_state
89 else
90 echo >&2 "Invalid state given to stateful update. Aborting..."
Eric Li94a671e2010-08-02 17:58:50 -070091 exit 1
92 fi
Chris Sosa1bf56372010-09-08 13:26:20 -070093}
Sean O'Connora7f867e2010-05-27 17:53:32 -070094
Chris Sosa1bf56372010-09-08 13:26:20 -070095main $@