Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(894)

Side by Side Diff: image_to_usb.sh

Issue 5271010: Factored out the code to copy an image and modify it for test (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: Final upload to check before push Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « common.sh ('k') | image_to_vm.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/bin/bash 1 #!/bin/bash
2 2
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. 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 4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file. 5 # found in the LICENSE file.
6 6
7 # Script to convert the output of build_image.sh to a usb image. 7 # Script to convert the output of build_image.sh to a usb image.
8 8
9 # --- BEGIN COMMON.SH BOILERPLATE --- 9 # --- BEGIN COMMON.SH BOILERPLATE ---
10 # Load common CrOS utilities. Inside the chroot this file is installed in 10 # Load common CrOS utilities. Inside the chroot this file is installed in
11 # /usr/lib/crosutils. Outside the chroot we find it relative to the script's 11 # /usr/lib/crosutils. Outside the chroot we find it relative to the script's
12 # location. 12 # location.
13 find_common_sh() { 13 find_common_sh() {
(...skipping 19 matching lines...) Expand all
33 INSTALLER_ROOT=$(dirname "$(readlink -f "$0")") 33 INSTALLER_ROOT=$(dirname "$(readlink -f "$0")")
34 34
35 . "${INSTALLER_ROOT}/chromeos-common.sh" || \ 35 . "${INSTALLER_ROOT}/chromeos-common.sh" || \
36 die "Unable to load chromeos-common.sh" 36 die "Unable to load chromeos-common.sh"
37 37
38 get_default_board 38 get_default_board
39 39
40 # Flags 40 # Flags
41 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" 41 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built"
42 DEFINE_string from "" \ 42 DEFINE_string from "" \
43 "Directory containing chromiumos_image.bin" 43 "Directory containing ${CHROMEOS_IMAGE_NAME}"
44 DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}" 44 DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}"
45 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" 45 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y"
46 DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" 46 DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
47 DEFINE_boolean force_non_usb ${FLAGS_FALSE} \ 47 DEFINE_boolean force_non_usb ${FLAGS_FALSE} \
48 "Write out image even if target (--to) doesn't look like a USB disk" 48 "Write out image even if target (--to) doesn't look like a USB disk"
49 DEFINE_boolean factory_install ${FLAGS_FALSE} \ 49 DEFINE_boolean factory_install ${FLAGS_FALSE} \
50 "Whether to generate a factory install shim." 50 "Whether to generate a factory install shim."
51 DEFINE_boolean factory ${FLAGS_FALSE} \ 51 DEFINE_boolean factory ${FLAGS_FALSE} \
52 "Whether to generate a factory runin image. Implies aututest and test" 52 "Whether to generate a factory runin image. Implies aututest and test"
53 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ 53 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \
54 "Copy the kernel to the fourth partition." 54 "Copy the kernel to the fourth partition."
55 DEFINE_boolean test_image "${FLAGS_FALSE}" \ 55 DEFINE_boolean test_image "${FLAGS_FALSE}" \
56 "Copies normal image to chromiumos_test_image.bin, modifies it for test." 56 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
57 DEFINE_string image_name "chromiumos_image.bin" \ 57 DEFINE_string image_name "${CHROMEOS_IMAGE_NAME}" \
58 "Base name of the image" i 58 "Base name of the image" i
59 DEFINE_string build_root "/build" \ 59 DEFINE_string build_root "/build" \
60 "The root location for board sysroots." 60 "The root location for board sysroots."
61 DEFINE_boolean install ${FLAGS_FALSE} "Install to the usb device." 61 DEFINE_boolean install ${FLAGS_FALSE} "Install to the usb device."
62 DEFINE_string arch "" "Architecture of the image." 62 DEFINE_string arch "" "Architecture of the image."
63 63
64 # Parse command line 64 # Parse command line
65 FLAGS "$@" || exit 1 65 FLAGS "$@" || exit 1
66 eval set -- "${FLAGS_ARGV}" 66 eval set -- "${FLAGS_ARGV}"
67 67
(...skipping 28 matching lines...) Expand all
96 96
97 # No board set during install 97 # No board set during install
98 if [ -z "${FLAGS_board}" ] && [ ${FLAGS_install} -eq ${FLAGS_TRUE} ]; then 98 if [ -z "${FLAGS_board}" ] && [ ${FLAGS_install} -eq ${FLAGS_TRUE} ]; then
99 setup_board_warning 99 setup_board_warning
100 exit 1 100 exit 1
101 fi 101 fi
102 102
103 # We have a board name but no image set. Use image at default location 103 # We have a board name but no image set. Use image at default location
104 if [ -z "${FLAGS_from}" ]; then 104 if [ -z "${FLAGS_from}" ]; then
105 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" 105 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
106
107 # Get latest image directory
106 FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)" 108 FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)"
107 fi 109 fi
108 110
109 if [ ! -d "${FLAGS_from}" ] ; then 111 if [ ! -d "${FLAGS_from}" ] ; then
110 echo "Cannot find image directory ${FLAGS_from}" 112 echo "Cannot find image directory ${FLAGS_from}"
111 exit 1 113 exit 1
112 fi 114 fi
113 115
114 if [ "${FLAGS_to}" == "/dev/sdX" ]; then 116 if [ "${FLAGS_to}" == "/dev/sdX" ]; then
115 echo "You must specify a file or device to write to using --to." 117 echo "You must specify a file or device to write to using --to."
(...skipping 30 matching lines...) Expand all
146 disk_manufacturer=$(get_disk_info ${FLAGS_to##*/} manufacturer) 148 disk_manufacturer=$(get_disk_info ${FLAGS_to##*/} manufacturer)
147 disk_product=$(get_disk_info ${FLAGS_to##*/} product) 149 disk_product=$(get_disk_info ${FLAGS_to##*/} product)
148 elif [ ${FLAGS_force_non_usb} -ne ${FLAGS_TRUE} ]; then 150 elif [ ${FLAGS_force_non_usb} -ne ${FLAGS_TRUE} ]; then
149 # Safeguard against writing to a real non-USB disk 151 # Safeguard against writing to a real non-USB disk
150 echo "Error: Device ${FLAGS_to} does not appear to be a USB disk!" 152 echo "Error: Device ${FLAGS_to} does not appear to be a USB disk!"
151 echo " To override this safeguard, use the --force_non_usb flag" 153 echo " To override this safeguard, use the --force_non_usb flag"
152 exit 1 154 exit 1
153 fi 155 fi
154 fi 156 fi
155 157
156 # Use this image as the source image to copy
157 SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}"
158
159 STATEFUL_DIR="${FLAGS_from}/stateful_partition" 158 STATEFUL_DIR="${FLAGS_from}/stateful_partition"
160 mkdir -p "${STATEFUL_DIR}" 159 mkdir -p "${STATEFUL_DIR}"
161 160
162 function do_cleanup { 161 function do_cleanup {
163 echo "Cleaning loopback devices: ${STATEFUL_LOOP_DEV}" 162 echo "Cleaning loopback devices: ${STATEFUL_LOOP_DEV}"
164 if [ "${STATEFUL_LOOP_DEV}" != "" ]; then 163 if [ "${STATEFUL_LOOP_DEV}" != "" ]; then
165 sudo umount "${STATEFUL_DIR}" 164 sudo umount "${STATEFUL_DIR}"
166 sudo losetup -d "${STATEFUL_LOOP_DEV}" 165 sudo losetup -d "${STATEFUL_LOOP_DEV}"
167 rmdir "${STATEFUL_DIR}" 166 rmdir "${STATEFUL_DIR}"
168 echo "Cleaned" 167 echo "Cleaned"
169 fi 168 fi
170 } 169 }
171 170
172
173 # If we're asked to modify the image for test, then let's make a copy and
174 # modify that instead.
175 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then 171 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
176 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ 172 # Make a test image - this returns the test filename in CHROMEOS_RETURN_VAL
177 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then 173 prepare_test_image "${FLAGS_from}" "${FLAGS_image_name}"
178 # Copy it. 174 SRC_IMAGE="${CHROMEOS_RETURN_VAL}"
179 echo "Creating test image from original..." 175 else
180 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" 176 # Use the standard image
181 177 SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}"
182 # Check for manufacturing image.
183 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
184 EXTRA_ARGS="--factory"
185 fi
186
187 # Check for instqall shim.
188 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
189 EXTRA_ARGS="--factory_install"
190 fi
191
192 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
193 # really want to modify the image; the user gave their assent already with
194 # --test-image and the original image is going to be preserved.
195 "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \
196 "${FLAGS_from}/chromiumos_test_image.bin" --board=${FLAGS_board} \
197 ${EXTRA_ARGS} --yes
198 echo "Done with mod_image_for_test."
199 else
200 echo "Using cached test image."
201 fi
202 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
203 echo "Source test image is: ${SRC_IMAGE}"
204 fi 178 fi
205 179
206
207 # Let's do it. 180 # Let's do it.
208 if [ -b "${FLAGS_to}" ]; then 181 if [ -b "${FLAGS_to}" ]; then
209 # Output to a block device (i.e., a real USB key), so need sudo dd 182 # Output to a block device (i.e., a real USB key), so need sudo dd
210 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then 183 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then
211 echo "Copying USB image ${SRC_IMAGE} to device ${FLAGS_to}..." 184 echo "Copying USB image ${SRC_IMAGE} to device ${FLAGS_to}..."
212 else 185 else
213 echo "Installing USB image ${SRC_IMAGE} to device ${FLAGS_to}..." 186 echo "Installing USB image ${SRC_IMAGE} to device ${FLAGS_to}..."
214 fi 187 fi
215 188
216 # Warn if it looks like they supplied a partition as the destination. 189 # Warn if it looks like they supplied a partition as the destination.
(...skipping 29 matching lines...) Expand all
246 echo "The device you have specified is already mounted at some point " 219 echo "The device you have specified is already mounted at some point "
247 echo "that is not visible from inside the chroot. Please unmount the " 220 echo "that is not visible from inside the chroot. Please unmount the "
248 echo "device manually from outside the chroot and try again." 221 echo "device manually from outside the chroot and try again."
249 echo 222 echo
250 exit 1 223 exit 1
251 fi 224 fi
252 done 225 done
253 sleep 3 226 sleep 3
254 227
255 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then 228 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then
256 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." 229 echo "Copying with dd ${SRC_IMAGE} to ${FLAGS_to}..."
257 if type pv >/dev/null 2>&1; then 230 sudo ${COMMON_PV_CAT} "${SRC_IMAGE}" |
258 # pv displays file size in k=1024 while dd uses k=1000. 231 sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer
259 # To prevent confusion, we suppress the summary report from dd.
260 sudo pv -ptreb -B 4m "${SRC_IMAGE}" |
261 sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer
262 else
263 sudo dd if="${SRC_IMAGE}" of="${FLAGS_to}" bs=4M oflag=sync
264 fi
265 sync 232 sync
266 else 233 else
267 if [ ${INSIDE_CHROOT} -ne 1 ]; then 234 if [ ${INSIDE_CHROOT} -ne 1 ]; then
268 echo "Installation must be done from inside the chroot." 235 echo "Installation must be done from inside the chroot."
269 exit 1 236 exit 1
270 fi 237 fi
271 238
272 echo "Installing ${SRC_IMAGE} to ${FLAGS_to}..." 239 echo "Installing ${SRC_IMAGE} to ${FLAGS_to}..."
273 "${FLAGS_build_root}/${FLAGS_board}/usr/sbin/chromeos-install" \ 240 "${FLAGS_build_root}/${FLAGS_board}/usr/sbin/chromeos-install" \
274 --yes \ 241 --yes \
275 --skip_src_removable \ 242 --skip_src_removable \
276 --skip_dst_removable \ 243 --skip_dst_removable \
277 --arch="${FLAGS_arch}" \ 244 --arch="${FLAGS_arch}" \
278 --payload_image="${SRC_IMAGE}" \ 245 --payload_image="${SRC_IMAGE}" \
279 --dst="${FLAGS_to}" 246 --dst="${FLAGS_to}"
280 fi 247 fi
281 echo "Done." 248 echo "Done."
282 else 249 else
283 # Output to a file, so just make a copy. 250 # Output to a file, so just make a copy.
284 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." 251 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..."
285 cp -f "${SRC_IMAGE}" "${FLAGS_to}" 252 ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}"
286 253
287 echo "Done. To copy to a USB drive, do something like:" 254 echo "Done. To copy to a USB drive, do something like:"
288 echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M oflag=sync" 255 echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M oflag=sync"
289 echo "where /dev/sdX is the entire drive." 256 echo "where /dev/sdX is the entire drive."
290 fi 257 fi
OLDNEW
« no previous file with comments | « common.sh ('k') | image_to_vm.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698