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

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: Minor changes Created 10 years 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
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 # Load common constants. This should be the first executable line. 9 # Load common constants. This should be the first executable line.
10 # The path to common.sh should be relative to your script's location. 10 # The path to common.sh should be relative to your script's location.
11 . "$(dirname "$0")/common.sh" 11 . "$(dirname "$0")/common.sh"
12 12
13 # Load functions and constants for chromeos-install 13 # Load functions and constants for chromeos-install
14 . "$(dirname "$0")/chromeos-common.sh" 14 . "$(dirname "$0")/chromeos-common.sh"
15 15
16 get_default_board 16 get_default_board
17 17
18 # Flags 18 # Flags
19 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" 19 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built"
20 DEFINE_string from "" \ 20 DEFINE_string from "" \
21 "Directory containing chromiumos_image.bin" 21 "Directory containing ${CHROMEOS_IMAGE_NAME}"
22 DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}" 22 DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}"
23 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" 23 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y"
24 DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" 24 DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image"
25 DEFINE_boolean force_non_usb ${FLAGS_FALSE} \ 25 DEFINE_boolean force_non_usb ${FLAGS_FALSE} \
26 "Write out image even if target (--to) doesn't look like a USB disk" 26 "Write out image even if target (--to) doesn't look like a USB disk"
27 DEFINE_boolean factory_install ${FLAGS_FALSE} \ 27 DEFINE_boolean factory_install ${FLAGS_FALSE} \
28 "Whether to generate a factory install shim." 28 "Whether to generate a factory install shim."
29 DEFINE_boolean factory ${FLAGS_FALSE} \ 29 DEFINE_boolean factory ${FLAGS_FALSE} \
30 "Whether to generate a factory runin image. Implies aututest and test" 30 "Whether to generate a factory runin image. Implies aututest and test"
31 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ 31 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \
32 "Copy the kernel to the fourth partition." 32 "Copy the kernel to the fourth partition."
33 DEFINE_boolean test_image "${FLAGS_FALSE}" \ 33 DEFINE_boolean test_image "${FLAGS_FALSE}" \
34 "Copies normal image to chromiumos_test_image.bin, modifies it for test." 34 "Copies normal image to ${CHROMEOS_TEST_IMAGE_NAME}, modifies it for test."
35 DEFINE_string image_name "chromiumos_image.bin" \ 35 DEFINE_string image_name "${CHROMEOS_IMAGE_NAME}" \
36 "Base name of the image" i 36 "Base name of the image" i
37 DEFINE_string build_root "/build" \ 37 DEFINE_string build_root "/build" \
38 "The root location for board sysroots." 38 "The root location for board sysroots."
39 DEFINE_boolean install ${FLAGS_FALSE} "Install to the usb device." 39 DEFINE_boolean install ${FLAGS_FALSE} "Install to the usb device."
40 DEFINE_string arch "" "Architecture of the image." 40 DEFINE_string arch "" "Architecture of the image."
41 41
42 # Parse command line 42 # Parse command line
43 FLAGS "$@" || exit 1 43 FLAGS "$@" || exit 1
44 eval set -- "${FLAGS_ARGV}" 44 eval set -- "${FLAGS_ARGV}"
45 45
(...skipping 28 matching lines...) Expand all
74 74
75 # No board set during install 75 # No board set during install
76 if [ -z "${FLAGS_board}" ] && [ ${FLAGS_install} -eq ${FLAGS_TRUE} ]; then 76 if [ -z "${FLAGS_board}" ] && [ ${FLAGS_install} -eq ${FLAGS_TRUE} ]; then
77 setup_board_warning 77 setup_board_warning
78 exit 1 78 exit 1
79 fi 79 fi
80 80
81 # We have a board name but no image set. Use image at default location 81 # We have a board name but no image set. Use image at default location
82 if [ -z "${FLAGS_from}" ]; then 82 if [ -z "${FLAGS_from}" ]; then
83 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" 83 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}"
84
85 # Get latest image directory
84 FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)" 86 FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)"
85 fi 87 fi
86 88
87 if [ ! -d "${FLAGS_from}" ] ; then 89 if [ ! -d "${FLAGS_from}" ] ; then
88 echo "Cannot find image directory ${FLAGS_from}" 90 echo "Cannot find image directory ${FLAGS_from}"
89 exit 1 91 exit 1
90 fi 92 fi
91 93
92 if [ "${FLAGS_to}" == "/dev/sdX" ]; then 94 if [ "${FLAGS_to}" == "/dev/sdX" ]; then
93 echo "You must specify a file or device to write to using --to." 95 echo "You must specify a file or device to write to using --to."
(...skipping 30 matching lines...) Expand all
124 disk_manufacturer=$(get_disk_info ${FLAGS_to##*/} manufacturer) 126 disk_manufacturer=$(get_disk_info ${FLAGS_to##*/} manufacturer)
125 disk_product=$(get_disk_info ${FLAGS_to##*/} product) 127 disk_product=$(get_disk_info ${FLAGS_to##*/} product)
126 elif [ ${FLAGS_force_non_usb} -ne ${FLAGS_TRUE} ]; then 128 elif [ ${FLAGS_force_non_usb} -ne ${FLAGS_TRUE} ]; then
127 # Safeguard against writing to a real non-USB disk 129 # Safeguard against writing to a real non-USB disk
128 echo "Error: Device ${FLAGS_to} does not appear to be a USB disk!" 130 echo "Error: Device ${FLAGS_to} does not appear to be a USB disk!"
129 echo " To override this safeguard, use the --force_non_usb flag" 131 echo " To override this safeguard, use the --force_non_usb flag"
130 exit 1 132 exit 1
131 fi 133 fi
132 fi 134 fi
133 135
134 # Use this image as the source image to copy
135 SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}"
136
137 STATEFUL_DIR="${FLAGS_from}/stateful_partition" 136 STATEFUL_DIR="${FLAGS_from}/stateful_partition"
138 mkdir -p "${STATEFUL_DIR}" 137 mkdir -p "${STATEFUL_DIR}"
139 138
140 function do_cleanup { 139 function do_cleanup {
141 echo "Cleaning loopback devices: ${STATEFUL_LOOP_DEV}" 140 echo "Cleaning loopback devices: ${STATEFUL_LOOP_DEV}"
142 if [ "${STATEFUL_LOOP_DEV}" != "" ]; then 141 if [ "${STATEFUL_LOOP_DEV}" != "" ]; then
143 sudo umount "${STATEFUL_DIR}" 142 sudo umount "${STATEFUL_DIR}"
144 sudo losetup -d "${STATEFUL_LOOP_DEV}" 143 sudo losetup -d "${STATEFUL_LOOP_DEV}"
145 rmdir "${STATEFUL_DIR}" 144 rmdir "${STATEFUL_DIR}"
146 echo "Cleaned" 145 echo "Cleaned"
147 fi 146 fi
148 } 147 }
149 148
149 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then
150 # Make a test image as requested, this will modify SRC_IMAGE
151 SRC_IMAGE=$(prepare_test_image "${FLAGS_from}" "${FLAGS_image_name}")
150 152
151 # If we're asked to modify the image for test, then let's make a copy and 153 # No need to check with the user, as we are preserving the base image
152 # modify that instead. 154 FLAGS_yes="${FLAGS_TRUE}"
153 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then 155 else
154 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ 156 # Use the standard image
155 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then 157 SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}"
156 # Copy it.
157 echo "Creating test image from original..."
158 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin"
159
160 # Check for manufacturing image.
161 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then
162 EXTRA_ARGS="--factory"
163 fi
164
165 # Check for instqall shim.
166 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then
167 EXTRA_ARGS="--factory_install"
168 fi
169
170 # Modify it. Pass --yes so that mod_image_for_test.sh won't ask us if we
171 # really want to modify the image; the user gave their assent already with
172 # --test-image and the original image is going to be preserved.
173 "${SCRIPTS_DIR}/mod_image_for_test.sh" --image \
174 "${FLAGS_from}/chromiumos_test_image.bin" --board=${FLAGS_board} \
175 ${EXTRA_ARGS} --yes
176 echo "Done with mod_image_for_test."
177 else
178 echo "Using cached test image."
179 fi
180 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin"
181 echo "Source test image is: ${SRC_IMAGE}"
182 fi 158 fi
183 159
184
185 # Let's do it. 160 # Let's do it.
186 if [ -b "${FLAGS_to}" ] 161 if [ -b "${FLAGS_to}" ]
187 then 162 then
188 # Output to a block device (i.e., a real USB key), so need sudo dd 163 # Output to a block device (i.e., a real USB key), so need sudo dd
189 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then 164 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then
190 echo "Copying USB image ${SRC_IMAGE} to device ${FLAGS_to}..." 165 echo "Copying USB image ${SRC_IMAGE} to device ${FLAGS_to}..."
191 else 166 else
192 echo "Installing USB image ${SRC_IMAGE} to device ${FLAGS_to}..." 167 echo "Installing USB image ${SRC_IMAGE} to device ${FLAGS_to}..."
193 fi 168 fi
194 169
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 echo "The device you have specified is already mounted at some point " 203 echo "The device you have specified is already mounted at some point "
229 echo "that is not visible from inside the chroot. Please unmount the " 204 echo "that is not visible from inside the chroot. Please unmount the "
230 echo "device manually from outside the chroot and try again." 205 echo "device manually from outside the chroot and try again."
231 echo 206 echo
232 exit 1 207 exit 1
233 fi 208 fi
234 done 209 done
235 sleep 3 210 sleep 3
236 211
237 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then 212 if [ ${FLAGS_install} -ne ${FLAGS_TRUE} ]; then
238 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." 213 echo "Copying with dd ${SRC_IMAGE} to ${FLAGS_to}..."
239 if type pv >/dev/null 2>&1; then 214 sudo ${COMMON_PV_CAT} "${SRC_IMAGE}" |
240 # pv displays file size in k=1024 while dd uses k=1000. 215 sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer
241 # To prevent confusion, we suppress the summary report from dd.
242 sudo pv -ptreb -B 4m "${SRC_IMAGE}" |
243 sudo dd of="${FLAGS_to}" bs=4M oflag=sync status=noxfer
244 else
245 sudo dd if="${SRC_IMAGE}" of="${FLAGS_to}" bs=4M oflag=sync
246 fi
247 sync 216 sync
248 else 217 else
249 if [ ${INSIDE_CHROOT} -ne 1 ]; then 218 if [ ${INSIDE_CHROOT} -ne 1 ]; then
250 echo "Installation must be done from inside the chroot." 219 echo "Installation must be done from inside the chroot."
251 exit 1 220 exit 1
252 fi 221 fi
253 222
254 echo "Installing ${SRC_IMAGE} to ${FLAGS_to}..." 223 echo "Installing ${SRC_IMAGE} to ${FLAGS_to}..."
255 "${FLAGS_build_root}/${FLAGS_board}/usr/sbin/chromeos-install" \ 224 "${FLAGS_build_root}/${FLAGS_board}/usr/sbin/chromeos-install" \
256 --yes \ 225 --yes \
257 --skip_src_removable \ 226 --skip_src_removable \
258 --skip_dst_removable \ 227 --skip_dst_removable \
259 --arch="${FLAGS_arch}" \ 228 --arch="${FLAGS_arch}" \
260 --payload_image="${SRC_IMAGE}" \ 229 --payload_image="${SRC_IMAGE}" \
261 --dst="${FLAGS_to}" 230 --dst="${FLAGS_to}"
262 fi 231 fi
263 echo "Done." 232 echo "Done."
264 else 233 else
265 # Output to a file, so just make a copy. 234 # Output to a file, so just make a copy.
266 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." 235 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..."
267 cp -f "${SRC_IMAGE}" "${FLAGS_to}" 236 ${COMMON_PV_CAT} "${SRC_IMAGE}" >"${FLAGS_to}"
268 237
269 echo "Done. To copy to a USB drive, do something like:" 238 echo "Done. To copy to a USB drive, do something like:"
270 echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M oflag=sync" 239 echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M oflag=sync"
271 echo "where /dev/sdX is the entire drive." 240 echo "where /dev/sdX is the entire drive."
272 fi 241 fi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698