OLD | NEW |
1 #!/bin/bash | 1 #!/bin/bash |
2 | 2 |
3 # Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 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 | 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 get_default_board | 13 get_default_board |
14 | 14 |
15 # Flags | 15 # Flags |
16 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" | 16 DEFINE_string board "${DEFAULT_BOARD}" "Board for which the image was built" |
17 DEFINE_string from "" \ | 17 DEFINE_string from "" \ |
18 "Directory containing rootfs.image and mbr.image" | 18 "Directory containing rootfs.image and mbr.image" |
19 DEFINE_string to "" "${DEFAULT_TO_HELP}" | 19 DEFINE_string to "" "${DEFAULT_TO_HELP}" |
20 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" | 20 DEFINE_boolean yes ${FLAGS_FALSE} "Answer yes to all prompts" "y" |
21 DEFINE_boolean install_autotest ${FLAGS_FALSE} \ | 21 DEFINE_boolean install_autotest ${FLAGS_FALSE} \ |
22 "Whether to install autotest to the stateful partition." | 22 "Whether to install autotest to the stateful partition." |
23 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ | 23 DEFINE_boolean copy_kernel ${FLAGS_FALSE} \ |
24 "Copy the kernel to the fourth partition." | 24 "Copy the kernel to the fourth partition." |
25 DEFINE_boolean test_image "${FLAGS_FALSE}" \ | 25 DEFINE_boolean test_image "${FLAGS_FALSE}" \ |
26 "Uses test image if available, otherwise creates one as rootfs_test.image." | 26 "Uses test image if available, otherwise creates one as rootfs_test.image." |
| 27 DEFINE_string build_root "/build" \ |
| 28 "The root location for board sysroots." |
27 | 29 |
28 # Parse command line | 30 # Parse command line |
29 FLAGS "$@" || exit 1 | 31 FLAGS "$@" || exit 1 |
30 eval set -- "${FLAGS_ARGV}" | 32 eval set -- "${FLAGS_ARGV}" |
31 | 33 |
32 # Inside the chroot, so output to usb.img in the same dir as the other | 34 # Inside the chroot, so output to usb.img in the same dir as the other |
33 # Script can be run either inside or outside the chroot. | 35 # Script can be run either inside or outside the chroot. |
34 if [ ${INSIDE_CHROOT} -eq 1 ] | 36 if [ ${INSIDE_CHROOT} -eq 1 ] |
35 then | 37 then |
36 AUTOTEST_SRC="/usr/local/autotest/${FLAGS_board}" | 38 SYSROOT="${FLAGS_build_root}/${FLAGS_board}" |
37 else | 39 else |
38 AUTOTEST_SRC="${DEFAULT_CHROOT_DIR}/usr/local/autotest/${FLAGS_board}" | 40 SYSROOT="${DEFAULT_CHROOT_DIR}${FLAGS_build_root}/${FLAGS_board}" |
39 fi | 41 fi |
| 42 AUTOTEST_SRC="${SYSROOT}/usr/local/autotest" |
40 | 43 |
41 # Die on any errors. | 44 # Die on any errors. |
42 set -e | 45 set -e |
43 | 46 |
44 # No board, no default and no image set then we can't find the image | 47 # No board, no default and no image set then we can't find the image |
45 if [ -z ${FLAGS_from} ] && [ -z ${FLAGS_board} ] ; then | 48 if [ -z ${FLAGS_from} ] && [ -z ${FLAGS_board} ] ; then |
46 setup_board_warning | 49 setup_board_warning |
47 exit 1 | 50 exit 1 |
48 fi | 51 fi |
49 | 52 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 echo "WARNING! Stateful partition not found. Creating clean stateful" | 93 echo "WARNING! Stateful partition not found. Creating clean stateful" |
91 STATEFUL_LOOP_DEV=$(sudo losetup -f) | 94 STATEFUL_LOOP_DEV=$(sudo losetup -f) |
92 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then | 95 if [ -z "${STATEFUL_LOOP_DEV}" ] ; then |
93 echo "No free loop device. Free up a loop device or reboot. exiting. " | 96 echo "No free loop device. Free up a loop device or reboot. exiting. " |
94 exit 1 | 97 exit 1 |
95 fi | 98 fi |
96 set -x | 99 set -x |
97 dd if=/dev/zero of="${STATEFUL_IMG}" bs=1 count=1 \ | 100 dd if=/dev/zero of="${STATEFUL_IMG}" bs=1 count=1 \ |
98 seek=$(( (${PART_SIZE} - 1) )) | 101 seek=$(( (${PART_SIZE} - 1) )) |
99 set +x | 102 set +x |
| 103 trap do_cleanup INT TERM EXIT |
100 sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG" | 104 sudo losetup "$STATEFUL_LOOP_DEV" "$STATEFUL_IMG" |
101 sudo mkfs.ext3 "$STATEFUL_LOOP_DEV" | 105 sudo mkfs.ext3 "$STATEFUL_LOOP_DEV" |
102 sudo tune2fs -L "C-STATE" -c 0 -i 0 "$STATEFUL_LOOP_DEV" | 106 sudo tune2fs -L "C-STATE" -c 0 -i 0 "$STATEFUL_LOOP_DEV" |
103 sudo losetup -d "${STATEFUL_LOOP_DEV}" | 107 sudo losetup -d "${STATEFUL_LOOP_DEV}" |
| 108 trap - INT TERM EXIT |
104 fi | 109 fi |
105 | 110 |
106 # Modifies image for test if requested | 111 # Modifies image for test if requested |
107 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then | 112 if [ ${FLAGS_test_image} -eq ${FLAGS_TRUE} ] ; then |
108 if [ ! -f "${FLAGS_from}/rootfs_test.image" ] ; then | 113 if [ ! -f "${FLAGS_from}/rootfs_test.image" ] ; then |
109 echo "Test image not found, creating test image from original ... " | 114 echo "Test image not found, creating test image from original ... " |
110 cp "${FLAGS_from}/rootfs.image" "${FLAGS_from}/rootfs_test.image" | 115 cp "${FLAGS_from}/rootfs.image" "${FLAGS_from}/rootfs_test.image" |
111 "${SCRIPTS_DIR}/mod_image_for_test.sh" \ | 116 "${SCRIPTS_DIR}/mod_image_for_test.sh" \ |
112 --image "${FLAGS_from}/rootfs_test.image" | 117 --image "${FLAGS_from}/rootfs_test.image" |
113 fi | 118 fi |
114 # Use the test image instead | 119 # Use the test image instead |
115 ROOTFS_IMAGE="${FLAGS_from}/rootfs_test.image" | 120 ROOTFS_IMAGE="${FLAGS_from}/rootfs_test.image" |
116 fi | 121 fi |
117 | 122 |
118 function do_cleanup { | 123 function do_cleanup { |
119 sudo losetup -d "${LOOP_DEV}" | 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 echo "Cleaned" |
| 129 fi |
120 } | 130 } |
121 | 131 |
122 function install_autotest { | 132 function install_autotest { |
| 133 echo "Detecting autotest at ${AUTOTEST_SRC}" |
123 if [ -d ${AUTOTEST_SRC} ] | 134 if [ -d ${AUTOTEST_SRC} ] |
124 then | 135 then |
125 local stateful_loop_dev=$(sudo losetup -f) | 136 local stateful_loop_dev=$(sudo losetup -f) |
126 if [ -z "${stateful_loop_dev}" ] | 137 if [ -z "${stateful_loop_dev}" ] |
127 then | 138 then |
128 echo "No free loop device. Free up a loop device or reboot. exiting." | 139 echo "No free loop device. Free up a loop device or reboot. exiting." |
129 exit 1 | 140 exit 1 |
130 fi | 141 fi |
131 | 142 trap do_cleanup INT TERM EXIT |
| 143 STATEFUL_LOOP_DEV=$stateful_loop_dev |
| 144 echo "Mounting ${STATEFUL_DIR} loopback" |
| 145 sudo losetup "${stateful_loop_dev}" "${STATEFUL_DIR}.image" |
132 sudo mount "${stateful_loop_dev}" "${STATEFUL_DIR}" | 146 sudo mount "${stateful_loop_dev}" "${STATEFUL_DIR}" |
133 | 147 |
134 echo -ne "Install autotest into stateful partition..." | 148 echo -ne "Install autotest into stateful partition..." |
135 » local autotest_client="/home/autotest-client" | 149 local autotest_client="/home/autotest-client" |
136 sudo mkdir -p "${STATEFUL_DIR}${autotest_client}" | 150 sudo mkdir -p "${STATEFUL_DIR}${autotest_client}" |
137 sudo cp -fpru ${AUTOTEST_SRC}/client/* \ | 151 sudo cp -fpru ${AUTOTEST_SRC}/client/* \ |
138 » "${STATEFUL_DIR}${autotest_client}" | 152 "${STATEFUL_DIR}${autotest_client}" |
139 sudo chmod 755 "${STATEFUL_DIR}${autotest_client}" | 153 sudo chmod 755 "${STATEFUL_DIR}${autotest_client}" |
140 sudo chown -R 1000:1000 "${STATEFUL_DIR}${autotest_client}" | 154 sudo chown -R 1000:1000 "${STATEFUL_DIR}${autotest_client}" |
141 | 155 |
142 sudo umount ${STATEFUL_DIR} | 156 sudo umount ${STATEFUL_DIR} |
143 sudo losetup -d "${stateful_loop_dev}" | 157 sudo losetup -d "${stateful_loop_dev}" |
| 158 trap - INT TERM EXIT |
144 else | 159 else |
145 echo "/usr/local/autotest under ${DEFAULT_CHROOT_DIR} is not installed." | 160 echo "/usr/local/autotest under ${DEFAULT_CHROOT_DIR} is not installed." |
146 echo "Please call make_autotest.sh inside chroot first." | 161 echo "Please call make_autotest.sh inside chroot first." |
147 sudo umount "${STATEFUL_DIR}" | |
148 exit -1 | 162 exit -1 |
149 fi | 163 fi |
150 } | 164 } |
151 | 165 |
152 # Copy MBR and rootfs to output image | 166 # Copy MBR and rootfs to output image |
153 if [ -b "${FLAGS_to}" ] | 167 if [ -b "${FLAGS_to}" ] |
154 then | 168 then |
155 # Output to a block device (i.e., a real USB key), so need sudo dd | 169 # Output to a block device (i.e., a real USB key), so need sudo dd |
156 echo "Copying USB image ${FLAGS_from} to device ${FLAGS_to}..." | 170 echo "Copying USB image ${FLAGS_from} to device ${FLAGS_to}..." |
157 | 171 |
(...skipping 22 matching lines...) Expand all Loading... |
180 exit 1 | 194 exit 1 |
181 fi | 195 fi |
182 fi | 196 fi |
183 | 197 |
184 echo "attempting to unmount any mounts on the USB device" | 198 echo "attempting to unmount any mounts on the USB device" |
185 for i in "${FLAGS_to}"* | 199 for i in "${FLAGS_to}"* |
186 do | 200 do |
187 ! sudo umount "$i" | 201 ! sudo umount "$i" |
188 done | 202 done |
189 sleep 3 | 203 sleep 3 |
190 | 204 |
191 if [ ${FLAGS_install_autotest} -eq ${FLAGS_TRUE} ] ; then | 205 if [ ${FLAGS_install_autotest} -eq ${FLAGS_TRUE} ] ; then |
192 install_autotest | 206 install_autotest |
193 fi | 207 fi |
194 | 208 |
195 # Write stateful partition to first partition. | 209 # Write stateful partition to first partition. |
196 echo "Copying stateful partition ..." | 210 echo "Copying stateful partition ..." |
197 sudo "${SCRIPTS_DIR}"/file_copy.py \ | 211 sudo "${SCRIPTS_DIR}"/file_copy.py \ |
198 if="${STATEFUL_IMG}" of="${FLAGS_to}" bs=4M \ | 212 if="${STATEFUL_IMG}" of="${FLAGS_to}" bs=4M \ |
199 seek_bytes=512 | 213 seek_bytes=512 |
200 | 214 |
201 # Write root fs to third partition. | 215 # Write root fs to third partition. |
202 echo "Copying root fs partition ..." | 216 echo "Copying root fs partition ..." |
203 sudo "${SCRIPTS_DIR}"/file_copy.py \ | 217 sudo "${SCRIPTS_DIR}"/file_copy.py \ |
204 if="${ROOTFS_IMAGE}" of="${FLAGS_to}" bs=4M \ | 218 if="${ROOTFS_IMAGE}" of="${FLAGS_to}" bs=4M \ |
205 seek_bytes=$(( (${PART_SIZE} * 2) + 512 )) | 219 seek_bytes=$(( (${PART_SIZE} * 2) + 512 )) |
206 | 220 |
207 trap - EXIT | 221 trap - EXIT |
208 | 222 |
209 if [ ${FLAGS_copy_kernel} -eq ${FLAGS_TRUE} ] | 223 if [ ${FLAGS_copy_kernel} -eq ${FLAGS_TRUE} ] |
210 then | 224 then |
211 echo "Copying Kernel..." | 225 echo "Copying Kernel..." |
212 "${SCRIPTS_DIR}"/kernel_fetcher.sh \ | 226 "${SCRIPTS_DIR}"/kernel_fetcher.sh \ |
213 --from "${FLAGS_from}" \ | 227 --from "${FLAGS_from}" \ |
214 --to "${FLAGS_to}" \ | 228 --to "${FLAGS_to}" \ |
215 --offset "$(( (${PART_SIZE} * 3) + 512 ))" | 229 --offset "$(( (${PART_SIZE} * 3) + 512 ))" |
216 fi | 230 fi |
(...skipping 26 matching lines...) Expand all Loading... |
243 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" | 257 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" |
244 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" | 258 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" |
245 echo "where /dev/sdb is the entire keyfob." | 259 echo "where /dev/sdb is the entire keyfob." |
246 if [ ${INSIDE_CHROOT} -eq 1 ] | 260 if [ ${INSIDE_CHROOT} -eq 1 ] |
247 then | 261 then |
248 echo "NOTE: Since you are currently inside the chroot, and you'll need to" | 262 echo "NOTE: Since you are currently inside the chroot, and you'll need to" |
249 echo "run dd outside the chroot, the path to the USB image will be" | 263 echo "run dd outside the chroot, the path to the USB image will be" |
250 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." | 264 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." |
251 fi | 265 fi |
252 fi | 266 fi |
OLD | NEW |