forked from coreos/dev-util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateful_update
executable file
·110 lines (93 loc) · 3.04 KB
/
stateful_update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/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="/media/state"
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 COREOS_DEVSERVER ${STATEFUL_DIR}${LSB_RELEASE} |
cut -f 2 -d '=')
fi
if [ -z "${devserver_url}" ]; then
devserver_url=$(grep COREOS_DEVSERVER ${LSB_RELEASE} | cut -f 2 -d '=')
fi
# Sanity check.
if [ -z "${devserver_url}" ]; then
echo >&2 "No COREOS_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 "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}/overlays_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}/overlays_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 $@