| 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 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images" | 13 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images" |
| 14 # Default to the most recent image | 14 # Default to the most recent image |
| 15 DEFAULT_FROM="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)" | 15 DEFAULT_FROM="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)" |
| 16 | 16 |
| 17 # Script can be run either inside or outside the chroot. | 17 # Script can be run either inside or outside the chroot. |
| 18 if [ $INSIDE_CHROOT -eq 1 ] | 18 if [ $INSIDE_CHROOT -eq 1 ] |
| 19 then | 19 then |
| 20 # Inside the chroot, so output to usb.img in the same dir as the other | 20 # Inside the chroot, so output to usb.img in the same dir as the other |
| 21 # images. | 21 # images. |
| 22 DEFAULT_TO="${DEFAULT_FROM}/usb.img" | 22 DEFAULT_TO="${DEFAULT_FROM}/usb.img" |
| 23 DEFAULT_TO_HELP="Destination file for USB image." | 23 DEFAULT_TO_HELP="Destination file for USB image." |
| 24 AUTOTEST_SRC=/usr/local/autotest |
| 24 else | 25 else |
| 25 # Outside the chroot, so output to the default device for a usb key. | 26 # Outside the chroot, so output to the default device for a usb key. |
| 26 DEFAULT_TO="/dev/sdb" | 27 DEFAULT_TO="/dev/sdb" |
| 27 DEFAULT_TO_HELP="Destination device for USB keyfob." | 28 DEFAULT_TO_HELP="Destination device for USB keyfob." |
| 29 AUTOTEST_SRC=${DEFAULT_CHROOT_DIR}/usr/local/autotest |
| 28 fi | 30 fi |
| 29 | 31 |
| 30 # Flags | 32 # Flags |
| 31 DEFINE_string from "$DEFAULT_FROM" \ | 33 DEFINE_string from "$DEFAULT_FROM" \ |
| 32 "Directory containing rootfs.image and mbr.image" | 34 "Directory containing rootfs.image and mbr.image" |
| 33 DEFINE_string to "$DEFAULT_TO" "$DEFAULT_TO_HELP" | 35 DEFINE_string to "$DEFAULT_TO" "$DEFAULT_TO_HELP" |
| 34 DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" "y" | 36 DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" "y" |
| 37 DEFINE_boolean install_autotest $FLAGS_FALSE \ |
| 38 "Whether to install autotest to the stateful partition." |
| 35 | 39 |
| 36 # Parse command line | 40 # Parse command line |
| 37 FLAGS "$@" || exit 1 | 41 FLAGS "$@" || exit 1 |
| 38 eval set -- "${FLAGS_ARGV}" | 42 eval set -- "${FLAGS_ARGV}" |
| 39 | 43 |
| 40 # Die on any errors. | 44 # Die on any errors. |
| 41 set -e | 45 set -e |
| 42 | 46 |
| 43 # Convert args to paths. Need eval to un-quote the string so that shell | 47 # Convert args to paths. Need eval to un-quote the string so that shell |
| 44 # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. | 48 # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 45 FLAGS_from=`eval readlink -f $FLAGS_from` | 49 FLAGS_from=`eval readlink -f $FLAGS_from` |
| 46 FLAGS_to=`eval readlink -f $FLAGS_to` | 50 FLAGS_to=`eval readlink -f $FLAGS_to` |
| 47 | 51 |
| 48 function do_cleanup { | 52 function do_cleanup { |
| 49 sudo losetup -d "$LOOP_DEV" | 53 sudo losetup -d "$LOOP_DEV" |
| 50 } | 54 } |
| 51 | 55 |
| 56 STATEFUL_DIR=${FLAGS_from}/stateful_partition |
| 57 mkdir -p "${STATEFUL_DIR}" |
| 58 |
| 59 function install_autotest { |
| 60 if [ -d ${AUTOTEST_SRC} ] |
| 61 then |
| 62 echo -ne "Install autotest into stateful partition..." |
| 63 local autotest_client="/home/autotest-client" |
| 64 sudo mkdir -p "${STATEFUL_DIR}${autotest_client}" |
| 65 sudo cp -fpru ${AUTOTEST_SRC}/client/* \ |
| 66 "${STATEFUL_DIR}${autotest_client}" |
| 67 sudo chmod 755 "${STATEFUL_DIR}${autotest_client}" |
| 68 sudo chown -R 1000:1000 "${STATEFUL_DIR}${autotest_client}" |
| 69 echo "Done." |
| 70 sudo umount "${STATEFUL_DIR}" |
| 71 else |
| 72 echo "/usr/local/autotest under ${DEFAULT_CHROOT_DIR} is not installed." |
| 73 echo "Please call make_autotest.sh inside chroot first." |
| 74 sudo umount "${STATEFUL_DIR}" |
| 75 exit -1 |
| 76 fi |
| 77 } |
| 78 |
| 52 # Copy MBR and rootfs to output image | 79 # Copy MBR and rootfs to output image |
| 53 if [ -b "$FLAGS_to" ] | 80 if [ -b "$FLAGS_to" ] |
| 54 then | 81 then |
| 55 # Output to a block device (i.e., a real USB key), so need sudo dd | 82 # Output to a block device (i.e., a real USB key), so need sudo dd |
| 56 echo "Copying USB image ${FLAGS_from} to device ${FLAGS_to}..." | 83 echo "Copying USB image ${FLAGS_from} to device ${FLAGS_to}..." |
| 57 | 84 |
| 58 # Warn if it looks like they supplied a partition as the destination. | 85 # Warn if it looks like they supplied a partition as the destination. |
| 59 if echo $FLAGS_to | grep -q '[0-9]$'; then | 86 if echo $FLAGS_to | grep -q '[0-9]$'; then |
| 60 drive=$(echo $FLAGS_to | sed -re 's/[0-9]+$//') | 87 drive=$(echo $FLAGS_to | sed -re 's/[0-9]+$//') |
| 61 if [ -b "$drive" ]; then | 88 if [ -b "$drive" ]; then |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 then | 129 then |
| 103 echo "No free loop device. Free up a loop device or reboot. exiting." | 130 echo "No free loop device. Free up a loop device or reboot. exiting." |
| 104 exit 1 | 131 exit 1 |
| 105 fi | 132 fi |
| 106 | 133 |
| 107 trap do_cleanup EXIT | 134 trap do_cleanup EXIT |
| 108 | 135 |
| 109 echo "Creating stateful partition..." | 136 echo "Creating stateful partition..." |
| 110 sudo losetup -o 512 "$LOOP_DEV" "$FLAGS_to" | 137 sudo losetup -o 512 "$LOOP_DEV" "$FLAGS_to" |
| 111 sudo mkfs.ext3 -F -b 4096 -L C-STATE "$LOOP_DEV" $(( $PART_SIZE / 4096 )) | 138 sudo mkfs.ext3 -F -b 4096 -L C-STATE "$LOOP_DEV" $(( $PART_SIZE / 4096 )) |
| 139 if [ $FLAGS_install_autotest -eq $FLAGS_TRUE ] |
| 140 then |
| 141 sudo mount "${LOOP_DEV}" "${STATEFUL_DIR}" |
| 142 install_autotest |
| 143 fi |
| 112 sync | 144 sync |
| 113 sudo losetup -d "$LOOP_DEV" | 145 sudo losetup -d "$LOOP_DEV" |
| 114 sync | 146 sync |
| 115 | 147 |
| 116 trap - EXIT | 148 trap - EXIT |
| 117 | 149 |
| 118 echo "Copying MBR..." | 150 echo "Copying MBR..." |
| 119 sudo "${SCRIPTS_DIR}"/file_copy.py \ | 151 sudo "${SCRIPTS_DIR}"/file_copy.py \ |
| 120 if="${FLAGS_from}/mbr.image" of="$FLAGS_to" | 152 if="${FLAGS_from}/mbr.image" of="$FLAGS_to" |
| 121 sync | 153 sync |
| 122 echo "Done." | 154 echo "Done." |
| 123 else | 155 else |
| 124 # Output to a file, so just cat the source images together | 156 # Output to a file, so just cat the source images together |
| 125 | 157 |
| 126 PART_SIZE=$(stat -c%s "${FLAGS_from}/rootfs.image") | 158 PART_SIZE=$(stat -c%s "${FLAGS_from}/rootfs.image") |
| 127 | 159 |
| 128 echo "Creating empty stateful partition" | 160 echo "Creating empty stateful partition" |
| 129 dd if=/dev/zero of="${FLAGS_from}/stateful_partition.image" bs=1 count=1 \ | 161 dd if=/dev/zero of="${FLAGS_from}/stateful_partition.image" bs=1 count=1 \ |
| 130 seek=$(($PART_SIZE - 1)) | 162 seek=$(($PART_SIZE - 1)) |
| 131 mkfs.ext3 -F -L C-STATE "${FLAGS_from}/stateful_partition.image" | 163 mkfs.ext3 -F -L C-STATE "${FLAGS_from}/stateful_partition.image" |
| 132 | 164 |
| 165 if [ $FLAGS_install_autotest -eq $FLAGS_TRUE ] |
| 166 then |
| 167 sudo mount -o loop "${FLAGS_from}/stateful_partition.image" \ |
| 168 "${STATEFUL_DIR}" |
| 169 install_autotest |
| 170 fi |
| 171 |
| 133 # Create a sparse output file | 172 # Create a sparse output file |
| 134 dd if=/dev/zero of="${FLAGS_to}" bs=1 count=1 \ | 173 dd if=/dev/zero of="${FLAGS_to}" bs=1 count=1 \ |
| 135 seek=$(( ($PART_SIZE * 2) + 512 - 1)) | 174 seek=$(( ($PART_SIZE * 2) + 512 - 1)) |
| 136 | 175 |
| 137 echo "Copying USB image to file ${FLAGS_to}..." | 176 echo "Copying USB image to file ${FLAGS_to}..." |
| 138 | 177 |
| 139 dd if="${FLAGS_from}/mbr.image" of="$FLAGS_to" conv=notrunc | 178 dd if="${FLAGS_from}/mbr.image" of="$FLAGS_to" conv=notrunc |
| 140 dd if="${FLAGS_from}/stateful_partition.image" of="$FLAGS_to" seek=1 bs=512 \ | 179 dd if="${FLAGS_from}/stateful_partition.image" of="$FLAGS_to" seek=1 bs=512 \ |
| 141 conv=notrunc | 180 conv=notrunc |
| 142 cat "${FLAGS_from}/rootfs.image" >> "$FLAGS_to" | 181 cat "${FLAGS_from}/rootfs.image" >> "$FLAGS_to" |
| 143 | 182 |
| 144 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" | 183 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" |
| 145 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" | 184 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" |
| 146 echo "where /dev/sdb is the entire keyfob." | 185 echo "where /dev/sdb is the entire keyfob." |
| 147 if [ $INSIDE_CHROOT -eq 1 ] | 186 if [ $INSIDE_CHROOT -eq 1 ] |
| 148 then | 187 then |
| 149 echo "NOTE: Since you are currently inside the chroot, and you'll need to" | 188 echo "NOTE: Since you are currently inside the chroot, and you'll need to" |
| 150 echo "run dd outside the chroot, the path to the USB image will be" | 189 echo "run dd outside the chroot, the path to the USB image will be" |
| 151 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." | 190 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." |
| 152 fi | 191 fi |
| 153 fi | 192 fi |
| OLD | NEW |