OLD | NEW |
| (Empty) |
1 #!/bin/bash | |
2 | |
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 # Script to convert the output of build_image.sh to a usb image. | |
8 | |
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. | |
11 . "/usr/lib/crosutils/common.sh" | |
12 | |
13 # Load functions and constants for chromeos-install | |
14 . "/usr/lib/installer/chromeos-common.sh" | |
15 | |
16 get_default_board | |
17 | |
18 # Flags | |
19 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" | |
20 DEFINE_string from "" \ | |
21 "Directory containing chromiumos_image.bin" | |
22 DEFINE_string to "/dev/sdX" "${DEFAULT_TO_HELP}" | |
23 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" | |
24 DEFINE_boolean force_copy ${FLAGS_FALSE} "Always rebuild test image" | |
25 DEFINE_boolean force_non_usb ${FLAGS_FALSE} \ | |
26 "Write out image even if target (--to) doesn't look like a USB disk" | |
27 DEFINE_boolean factory_install ${FLAGS_FALSE} \ | |
28 "Whether to generate a factory install shim." | |
29 DEFINE_boolean factory ${FLAGS_FALSE} \ | |
30 "Whether to generate a factory runin image. Implies aututest and test" | |
31 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ | |
32 "Copy the kernel to the fourth partition." | |
33 DEFINE_boolean test_image "${FLAGS_FALSE}" \ | |
34 "Copies normal image to chromiumos_test_image.bin, modifies it for test." | |
35 DEFINE_string image_name "chromiumos_image.bin" \ | |
36 "Base name of the image" i | |
37 DEFINE_string build_root "/build" \ | |
38 "The root location for board sysroots." | |
39 | |
40 # Parse command line | |
41 FLAGS "$@" || exit 1 | |
42 eval set -- "${FLAGS_ARGV}" | |
43 | |
44 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then | |
45 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then | |
46 echo "Factory test image is incompatible with factory install shim." | |
47 exit 1 | |
48 fi | |
49 fi | |
50 | |
51 # Require autotest for manucaturing image. | |
52 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then | |
53 echo "Factory image requires --test_image, setting." | |
54 FLAGS_test_image=${FLAGS_TRUE} | |
55 fi | |
56 | |
57 # Require test for for factory install shim. | |
58 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then | |
59 echo "Factory install shim requires --test_image, setting." | |
60 FLAGS_test_image=${FLAGS_TRUE} | |
61 fi | |
62 | |
63 | |
64 # Die on any errors. | |
65 set -e | |
66 | |
67 # No board, no default and no image set then we can't find the image | |
68 if [ -z ${FLAGS_from} ] && [ -z ${FLAGS_board} ] ; then | |
69 setup_board_warning | |
70 exit 1 | |
71 fi | |
72 | |
73 # We have a board name but no image set. Use image at default location | |
74 if [ -z "${FLAGS_from}" ]; then | |
75 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images/${FLAGS_board}" | |
76 FLAGS_from="${IMAGES_DIR}/$(ls -t ${IMAGES_DIR} 2>&-| head -1)" | |
77 fi | |
78 | |
79 if [ ! -d "${FLAGS_from}" ] ; then | |
80 echo "Cannot find image directory ${FLAGS_from}" | |
81 exit 1 | |
82 fi | |
83 | |
84 if [ "${FLAGS_to}" == "/dev/sdX" ]; then | |
85 echo "You must specify a file or device to write to using --to." | |
86 disks=$(list_usb_disks) | |
87 if [ -n "$disks" ]; then | |
88 echo "Available USB disks:" | |
89 for disk in $disks; do | |
90 echo " /dev/$disk:" | |
91 echo " Manufacturer: $(get_disk_info $disk manufacturer)" | |
92 echo " Product: $(get_disk_info $disk product)" | |
93 echo " Size: $[$(cat /sys/block/$disk/size) * 512] bytes" | |
94 done | |
95 fi | |
96 exit 1 | |
97 fi | |
98 | |
99 # Convert args to paths. Need eval to un-quote the string so that shell | |
100 # chars like ~ are processed; just doing FOO=`readlink -f ${FOO}` won't work. | |
101 FLAGS_from=`eval readlink -f ${FLAGS_from}` | |
102 FLAGS_to=`eval readlink -f ${FLAGS_to}` | |
103 | |
104 # One last check to make sure user is not shooting themselves in the foot | |
105 if [ -b "${FLAGS_to}" ]; then | |
106 if list_usb_disks | grep -q '^'${FLAGS_to##*/}'$'; then | |
107 disk_manufacturer=$(get_disk_info ${FLAGS_to##*/} manufacturer) | |
108 disk_product=$(get_disk_info ${FLAGS_to##*/} product) | |
109 elif [ ${FLAGS_force_non_usb} -ne ${FLAGS_TRUE} ]; then | |
110 # Safeguard against writing to a real non-USB disk | |
111 echo "Error: Device ${FLAGS_to} does not appear to be a USB disk!" | |
112 echo " To override this safeguard, use the --force_non_usb flag" | |
113 exit 1 | |
114 fi | |
115 fi | |
116 | |
117 # Use this image as the source image to copy | |
118 SRC_IMAGE="${FLAGS_from}/${FLAGS_image_name}" | |
119 | |
120 STATEFUL_DIR="${FLAGS_from}/stateful_partition" | |
121 mkdir -p "${STATEFUL_DIR}" | |
122 | |
123 function do_cleanup { | |
124 echo "Cleaning loopback devices: ${STATEFUL_LOOP_DEV}" | |
125 if [ "${STATEFUL_LOOP_DEV}" != "" ]; then | |
126 sudo umount "${STATEFUL_DIR}" | |
127 sudo losetup -d "${STATEFUL_LOOP_DEV}" | |
128 rmdir "${STATEFUL_DIR}" | |
129 echo "Cleaned" | |
130 fi | |
131 } | |
132 | |
133 | |
134 # If we're asked to modify the image for test, then let's make a copy and | |
135 # modify that instead. | |
136 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then | |
137 if [ ! -f "${FLAGS_from}/chromiumos_test_image.bin" ] || \ | |
138 [ ${FLAGS_force_copy} -eq ${FLAGS_TRUE} ] ; then | |
139 # Copy it. | |
140 echo "Creating test image from original..." | |
141 cp -f "${SRC_IMAGE}" "${FLAGS_from}/chromiumos_test_image.bin" | |
142 | |
143 # Check for manufacturing image. | |
144 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ] ; then | |
145 EXTRA_ARGS="--factory" | |
146 fi | |
147 | |
148 # Check for instqall shim. | |
149 if [ ${FLAGS_factory_install} -eq ${FLAGS_TRUE} ] ; then | |
150 EXTRA_ARGS="--factory_install" | |
151 fi | |
152 | |
153 # Modify it. Pass --yes so that cros_mod_image_for_test.sh won't ask us if | |
154 # we really want to modify the image; the user gave their assent already | |
155 # with --test-image and the original image is going to be preserved. | |
156 "/usr/bin/cros_mod_image_for_test.sh" --image \ | |
157 "${FLAGS_from}/chromiumos_test_image.bin" --board=${FLAGS_board} \ | |
158 ${EXTRA_ARGS} --yes | |
159 echo "Done with cros_mod_image_for_test." | |
160 else | |
161 echo "Using cached test image." | |
162 fi | |
163 SRC_IMAGE="${FLAGS_from}/chromiumos_test_image.bin" | |
164 echo "Source test image is: ${SRC_IMAGE}" | |
165 fi | |
166 | |
167 | |
168 # Let's do it. | |
169 if [ -b "${FLAGS_to}" ] | |
170 then | |
171 # Output to a block device (i.e., a real USB key), so need sudo dd | |
172 echo "Copying USB image ${SRC_IMAGE} to device ${FLAGS_to}..." | |
173 | |
174 # Warn if it looks like they supplied a partition as the destination. | |
175 if echo "${FLAGS_to}" | grep -q '[0-9]$'; then | |
176 drive=$(echo "${FLAGS_to}" | sed -re 's/[0-9]+$//') | |
177 if [ -b "${drive}" ]; then | |
178 echo | |
179 echo "NOTE: It looks like you may have supplied a partition as the " | |
180 echo "destination. This script needs to write to the drive's device " | |
181 echo "node instead (i.e. ${drive} rather than ${FLAGS_to})." | |
182 echo | |
183 fi | |
184 fi | |
185 | |
186 # Make sure this is really what the user wants, before nuking the device | |
187 if [ ${FLAGS_yes} -ne ${FLAGS_TRUE} ] | |
188 then | |
189 sudo fdisk -l "${FLAGS_to}" 2>/dev/null | grep Disk | head -1 | |
190 [ -n "$disk_manufacturer" ] && echo "Manufacturer: $disk_manufacturer" | |
191 [ -n "$disk_product" ] && echo "Product: $disk_product" | |
192 echo "This will erase all data on this device:" | |
193 read -p "Are you sure (y/N)? " SURE | |
194 SURE="${SURE:0:1}" # Get just the first character | |
195 if [ "${SURE}" != "y" ] | |
196 then | |
197 echo "Ok, better safe than sorry." | |
198 exit 1 | |
199 fi | |
200 fi | |
201 | |
202 echo "Attempting to unmount any mounts on the USB device..." | |
203 for i in $(mount | grep ^"${FLAGS_to}" | awk '{print $1}') | |
204 do | |
205 if sudo umount "$i" 2>&1 >/dev/null | grep "not found"; then | |
206 echo | |
207 echo "The device you have specified is already mounted at some point " | |
208 echo "that is not visible from inside the chroot. Please unmount the " | |
209 echo "device manually from outside the chroot and try again." | |
210 echo | |
211 exit 1 | |
212 fi | |
213 done | |
214 sleep 3 | |
215 | |
216 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." | |
217 sudo dd if="${SRC_IMAGE}" of="${FLAGS_to}" bs=4M | |
218 sync | |
219 | |
220 echo "Done." | |
221 else | |
222 # Output to a file, so just make a copy. | |
223 echo "Copying ${SRC_IMAGE} to ${FLAGS_to}..." | |
224 cp -f "${SRC_IMAGE}" "${FLAGS_to}" | |
225 | |
226 echo "Done. To copy to a USB drive, outside the chroot, do something like:" | |
227 echo " sudo dd if=${FLAGS_to} of=/dev/sdX bs=4M" | |
228 echo "where /dev/sdX is the entire drive." | |
229 if [ ${INSIDE_CHROOT} -eq 1 ] | |
230 then | |
231 example=$(basename "${FLAGS_to}") | |
232 echo "NOTE: Since you are currently inside the chroot, and you'll need to" | |
233 echo "run dd outside the chroot, the path to the USB image will be" | |
234 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/$example)." | |
235 fi | |
236 fi | |
OLD | NEW |