blob: 0f5afc58198db9c0909797d82e1f46b562125501 [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# This scripts performs update of stateful partition directories useful for
# dev_mode.
# in order to support developers going from older images with the old shflags
# we'll check for both new shflags and old shflags
if [ -f /usr/share/misc/shflags ] ; then
. /usr/share/misc/shflags
elif [ -f /usr/lib/shflags ] ; then
. /usr/lib/shflags
else
echo >&2 "$0 Unable to source shflags"
exit 1
fi
# Constants for states.
CLEAN_STATE="clean"
OLD_STATE="old"
RESET_STATE="reset"
LSB_RELEASE="/etc/lsb-release"
STATEFUL_DIR="/mnt/stateful_partition"
UPDATE_STATE_FILE=".update_available"
DEFINE_string stateful_change "${OLD_STATE}" \
"The state of the new stateful partition - used in update testing."
FLAGS "$@" || exit 1
# Die on error.
set -e
remove_quotes() {
echo "$1" | sed -e "s/^'//; s/'$//"
}
update_dev_image () {
local base_update_url devserver_url
if [ -n "${FLAGS_ARGV}" ]; then
base_update_url=$(remove_quotes "${FLAGS_ARGV}")
else
if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
cut -f 2 -d '=')
fi
if [ -z "${devserver_url}" ]; then
devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
fi
# Sanity check.
if [ -z "${devserver_url}" ]; then
echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file"
exit 1
fi
# Devserver URL should never contain "/update"
devserver_url=$(echo ${devserver_url} | sed -e 's#/update##')
base_update_url="${devserver_url}/static"
fi
local stateful_update_url="${base_update_url}/stateful.tgz"
echo >&2 "Downloading stateful payload from ${stateful_update_url}"
# Download and unzip directories onto the stateful partition.
eval "wget -qS -T 300 -O - \"${stateful_update_url}\"" |
tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz
echo >&2 "Successfully downloaded update"
if [ ! -d "${STATEFUL_DIR}/var_new" ] ||
[ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then
echo >&2 "Missing var or dev_image in stateful payload."
return 1
fi
}
reset_state () {
echo >&2 "Resetting stateful update state."
rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
rm -rf "${STATEFUL_DIR}/var_new"
rm -rf "${STATEFUL_DIR}/dev_image_new"
}
update_old_state () {
echo >&2 "Performing standard stateful update."
echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
}
update_clean_state () {
echo >&2 "Restoring state to factory_install with dev_image."
echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
}
main () {
if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then
reset_state
elif update_dev_image; then
if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
update_old_state
elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
update_clean_state
else
echo >&2 "Invalid state given to stateful update. Aborting..."
return 1
fi
else
return 1
fi
}
main $@