blob: a7054021f1976533096a10b32b8b2e4217a1d66b [file] [log] [blame]
Chris Sosaa73ec162010-05-04 03:18:021#!/bin/sh
2
Chris Sosaa66f3a42012-05-18 02:12:583# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Sosaa73ec162010-05-04 03:18:024# 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:4110# 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 20:26:2020
21# Constants for states.
22CLEAN_STATE="clean"
23OLD_STATE="old"
Chris Sosaa66f3a42012-05-18 02:12:5824RESET_STATE="reset"
25
26LSB_RELEASE="/etc/lsb-release"
27STATEFUL_DIR="/mnt/stateful_partition"
28UPDATE_STATE_FILE=".update_available"
Chris Sosa1bf56372010-09-08 20:26:2029
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-04 03:18:0235# Die on error.
36set -e
37
Chris Sosaca4a9252010-09-09 20:48:5138remove_quotes() {
39 echo "$1" | sed -e "s/^'//; s/'$//"
40}
41
Fang Deng0df48a12015-11-05 00:58:1342download_stateful_payload(){
43 # Download and unzip directories onto the stateful partition.
Keith Haddow431d3ac2017-11-15 22:34:5544 eval "curl --silent --max-time 300 \"${stateful_update_url}\"" |
Fang Deng0df48a12015-11-05 00:58:1345 tar --ignore-command-error --overwrite --directory=${STATEFUL_DIR} -xz
46}
47
Chris Sosa1bf56372010-09-08 20:26:2048update_dev_image () {
Chris Sosaa66f3a42012-05-18 02:12:5849 local base_update_url devserver_url
Yu-Ju Hong120ceab2013-12-10 06:47:5250 local local_payload_path
51 local path=$(remove_quotes "${FLAGS_ARGV}")
52
53 if [ -z "${path}" ]; then
Chris Sosa05491b12010-11-09 01:14:1654 if [ -f "${STATEFUL_DIR}${LSB_RELEASE}" ]; then
Chris Sosaa66f3a42012-05-18 02:12:5855 devserver_url=$(grep CHROMEOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
Chris Sosa8086c3f2011-03-05 00:30:3656 cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 20:26:2057 fi
Chris Sosaa66f3a42012-05-18 02:12:5858 if [ -z "${devserver_url}" ]; then
59 devserver_url=$(grep CHROMEOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
Chris Sosa1bf56372010-09-08 20:26:2060 fi
61 # Sanity check.
Chris Sosaa66f3a42012-05-18 02:12:5862 if [ -z "${devserver_url}" ]; then
Yu-Ju Hong120ceab2013-12-10 06:47:5263 echo >&2 "No CHROMEOS_DEVSERVER URL found in lsb-release file."
Chris Sosa1bf56372010-09-08 20:26:2064 exit 1
65 fi
66 # Devserver URL should never contain "/update"
Chris Sosaa66f3a42012-05-18 02:12:5867 devserver_url=$(echo ${devserver_url} | sed -e 's#/update##')
68 base_update_url="${devserver_url}/static"
Yu-Ju Hong120ceab2013-12-10 06:47:5269 # Determine whether the path is local.
70 elif [ -f "${path}" ] || [ "${path}" = "-" ]; then
Fang Deng0df48a12015-11-05 00:58:1371 local_payload_path=${path}
Yu-Ju Hong120ceab2013-12-10 06:47:5272 else
Fang Deng0df48a12015-11-05 00:58:1373 base_update_url=${path}
Eric Li94a671e2010-08-03 00:58:5074 fi
Chris Sosa1bf56372010-09-08 20:26:2075
Yu-Ju Hong120ceab2013-12-10 06:47:5276 if [ -n "${base_update_url}" ]; then
77 local stateful_update_url="${base_update_url}/stateful.tgz"
Fang Deng0df48a12015-11-05 00:58:1378 local download_exit_code=0
79 for i in `seq 1 2`; do
80 if [ $i -eq 1 ]; then
81 echo >&2 "Downloading stateful payload from ${stateful_update_url}"
82 else
83 echo >&2 "Downloading failed, retrying."
84 fi
85 if download_stateful_payload; then
86 download_exit_code=$?
87 echo >&2 "Downloading command returns code ${download_exit_code}."
88 break
89 else
90 download_exit_code=$?
91 echo >&2 "Downloading command returns code ${download_exit_code}."
92 fi
93 done
94 if [ ${download_exit_code} -ne 0 ]; then
95 return ${download_exit_code}
96 fi
Yu-Ju Hong120ceab2013-12-10 06:47:5297 echo >&2 "Successfully downloaded update."
98 else
99 echo >&2 "Reading local payload ${local_payload_path}"
Amin Hassani3e3f5d02018-12-11 00:55:58100 # Set timeout to four minutes to avoid waiting on stdin indefinitely.
101 timeout 240s tar --ignore-command-error --overwrite \
Yu-Ju Hong120ceab2013-12-10 06:47:52102 --directory=${STATEFUL_DIR} -xzf ${local_payload_path}
Amin Hassani3e3f5d02018-12-11 00:55:58103 exit_code=$?
104 if [ "${exit_code}" -ne 0 ]; then
105 echo >&2 "Failed to unzip and write the stateful update."
106 return "${exit_code}"
107 fi
Yu-Ju Hong120ceab2013-12-10 06:47:52108 echo >&2 "Successfully retrieved update."
109 fi
Chris Sosa05491b12010-11-09 01:14:16110
Chris Sosa8086c3f2011-03-05 00:30:36111 if [ ! -d "${STATEFUL_DIR}/var_new" ] ||
112 [ ! -d "${STATEFUL_DIR}/dev_image_new" ]; then
113 echo >&2 "Missing var or dev_image in stateful payload."
114 return 1
Eric Li94a671e2010-08-03 00:58:50115 fi
Chris Sosa1bf56372010-09-08 20:26:20116}
117
Chris Sosaa66f3a42012-05-18 02:12:58118reset_state () {
119 echo >&2 "Resetting stateful update state."
120 rm -f "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
121 rm -rf "${STATEFUL_DIR}/var_new"
122 rm -rf "${STATEFUL_DIR}/dev_image_new"
123}
124
Chris Sosa1bf56372010-09-08 20:26:20125update_old_state () {
Chris Sosa05491b12010-11-09 01:14:16126 echo >&2 "Performing standard stateful update."
Chris Sosaa66f3a42012-05-18 02:12:58127 echo -n "" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
Chris Sosa1bf56372010-09-08 20:26:20128}
129
130update_clean_state () {
131 echo >&2 "Restoring state to factory_install with dev_image."
Chris Sosaa66f3a42012-05-18 02:12:58132 echo -n "clobber" > "${STATEFUL_DIR}/${UPDATE_STATE_FILE}"
Chris Sosa1bf56372010-09-08 20:26:20133}
134
135main () {
Chris Sosaa66f3a42012-05-18 02:12:58136 if [ "${FLAGS_stateful_change}" = "${RESET_STATE}" ]; then
137 reset_state
138 elif update_dev_image; then
Chris Sosa8086c3f2011-03-05 00:30:36139 if [ "${FLAGS_stateful_change}" = "${OLD_STATE}" ]; then
140 update_old_state
141 elif [ "${FLAGS_stateful_change}" = "${CLEAN_STATE}" ]; then
142 update_clean_state
143 else
144 echo >&2 "Invalid state given to stateful update. Aborting..."
145 return 1
146 fi
Chris Sosa1bf56372010-09-08 20:26:20147 else
Chris Sosa8086c3f2011-03-05 00:30:36148 return 1
Eric Li94a671e2010-08-03 00:58:50149 fi
Chris Sosa1bf56372010-09-08 20:26:20150}
Sean O'Connora7f867e2010-05-28 00:53:32151
Chris Sosa1bf56372010-09-08 20:26:20152main $@