| 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 THIS_DIR="$(dirname "$0")" |
| 12 . "${THIS_DIR}/common.sh" |
| 12 | 13 |
| 13 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images" | 14 # Default to the current directory or the most recent image if none found. |
| 14 # Default to the most recent image | 15 DEFAULT_FROM= |
| 15 DEFAULT_FROM="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)" | 16 if [ -f "${THIS_DIR}/rootfs.image" ]; then |
| 17 DEFAULT_FROM="$THIS_DIR" |
| 18 else |
| 19 IMAGES_DIR="${DEFAULT_BUILD_ROOT}/images" |
| 20 DEFAULT_FROM="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)" |
| 21 fi |
| 16 | 22 |
| 17 # Script can be run either inside or outside the chroot. | 23 # Script can be run either inside or outside the chroot. |
| 18 if [ $INSIDE_CHROOT -eq 1 ] | 24 if [ $INSIDE_CHROOT -eq 1 ] |
| 19 then | 25 then |
| 20 # Inside the chroot, so output to usb.img in the same dir as the other | 26 # Inside the chroot, so output to usb.img in the same dir as the other |
| 21 # images. | 27 # images. |
| 22 DEFAULT_TO="${DEFAULT_FROM}/usb.img" | 28 DEFAULT_TO="${DEFAULT_FROM}/usb.img" |
| 23 DEFAULT_TO_HELP="Destination file for USB image." | 29 DEFAULT_TO_HELP="Destination file for USB image." |
| 24 else | 30 else |
| 25 # Outside the chroot, so output to the default device for a usb key. | 31 # Outside the chroot, so output to the default device for a usb key. |
| 26 DEFAULT_TO="/dev/sdb" | 32 DEFAULT_TO="/dev/sdb" |
| 27 DEFAULT_TO_HELP="Destination device for USB keyfob." | 33 DEFAULT_TO_HELP="Destination device for USB keyfob." |
| 28 fi | 34 fi |
| 29 | 35 |
| 30 # Flags | 36 # Flags |
| 31 DEFINE_string from "$DEFAULT_FROM" \ | 37 DEFINE_string from "$DEFAULT_FROM" \ |
| 32 "Directory containing rootfs.image and mbr.image" | 38 "Directory containing rootfs.image and mbr.image" |
| 33 DEFINE_string to "$DEFAULT_TO" "$DEFAULT_TO_HELP" | 39 DEFINE_string to "$DEFAULT_TO" "$DEFAULT_TO_HELP" |
| 34 DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" "y" | 40 DEFINE_boolean yes $FLAGS_FALSE "Answer yes to all prompts" "y" |
| 41 DEFINE_string data_part_size_mb '' \ |
| 42 "Override automatic calculation of user data partition size with value in MB." |
| 35 | 43 |
| 36 # Parse command line | 44 # Parse command line |
| 37 FLAGS "$@" || exit 1 | 45 FLAGS "$@" || exit 1 |
| 38 eval set -- "${FLAGS_ARGV}" | 46 eval set -- "${FLAGS_ARGV}" |
| 39 | 47 |
| 40 # Die on any errors. | 48 # Die on any errors. |
| 41 set -e | 49 set -e |
| 42 | 50 |
| 43 # Convert args to paths. Need eval to un-quote the string so that shell | 51 # 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. | 52 # chars like ~ are processed; just doing FOO=`readlink -f $FOO` won't work. |
| 45 FLAGS_from=`eval readlink -f $FLAGS_from` | 53 FLAGS_from=`eval readlink -f $FLAGS_from` |
| 46 FLAGS_to=`eval readlink -f $FLAGS_to` | 54 FLAGS_to=`eval readlink -f $FLAGS_to` |
| 47 | 55 if [ -b "$FLAGS_to" ] ; then |
| 48 function do_cleanup { | 56 echo "attempting to unmount any mounts on the destination device" |
| 49 sudo losetup -d "$LOOP_DEV" | |
| 50 } | |
| 51 | |
| 52 # Copy MBR and rootfs to output image | |
| 53 if [ -b "$FLAGS_to" ] | |
| 54 then | |
| 55 # 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}..." | |
| 57 | |
| 58 # Make sure this is really what the user wants, before nuking the device | |
| 59 if [ $FLAGS_yes -ne $FLAGS_TRUE ] | |
| 60 then | |
| 61 echo "This will erase all data on this device:" | |
| 62 sudo fdisk -l "$FLAGS_to" | grep Disk | head -1 | |
| 63 read -p "Are you sure (y/N)? " SURE | |
| 64 SURE="${SURE:0:1}" # Get just the first character | |
| 65 if [ "$SURE" != "y" ] | |
| 66 then | |
| 67 echo "Ok, better safe than sorry." | |
| 68 exit 1 | |
| 69 fi | |
| 70 fi | |
| 71 | |
| 72 echo "attempting to unmount any mounts on the USB device" | |
| 73 for i in "$FLAGS_to"* | 57 for i in "$FLAGS_to"* |
| 74 do | 58 do |
| 75 ! sudo umount "$i" | 59 ! sudo umount "$i" |
| 76 done | 60 done |
| 77 sleep 3 | 61 sleep 3 |
| 62 fi |
| 78 | 63 |
| 79 PART_SIZE=$(stat -c%s "${FLAGS_from}/rootfs.image") # Bytes | 64 MBR="${FLAGS_from}/mbr.image" |
| 65 GPT="${FLAGS_from}/gpt" |
| 66 if [ -n "$FLAGS_data_part_size_mb" ] ; then |
| 67 STATEFUL_PART_SIZE=$((FLAGS_data_part_size_mb * 1024 * 1024)) |
| 68 fi |
| 69 . "${FLAGS_from}/chromeos_install_functions.sh" |
| 80 | 70 |
| 81 echo "Copying root fs..." | 71 # Make sure that the destination file/device is ok to blow away. |
| 82 sudo "${SCRIPTS_DIR}"/file_copy.py \ | 72 do_verify "${FLAGS_from}/rootfs.image" "$FLAGS_to" "minimal" |
| 83 if="${FLAGS_from}/rootfs.image" \ | |
| 84 of="$FLAGS_to" bs=4M \ | |
| 85 seek_bytes=$(( ($PART_SIZE * 2) + 512 )) | |
| 86 | 73 |
| 87 # Set up loop device | 74 # Perform standard installation to destination. |
| 88 LOOP_DEV=$(sudo losetup -f) | 75 install_chromeos "${FLAGS_from}/rootfs.image" "$FLAGS_to" "C" "minimal" |
| 89 if [ -z "$LOOP_DEV" ] | |
| 90 then | |
| 91 echo "No free loop device. Free up a loop device or reboot. exiting." | |
| 92 exit 1 | |
| 93 fi | |
| 94 | 76 |
| 95 trap do_cleanup EXIT | 77 if [ -b "$FLAGS_to" ] ; then |
| 96 | 78 echo "Done. You may remove your device and try to boot it." |
| 97 echo "Creating stateful partition..." | |
| 98 sudo losetup -o 512 "$LOOP_DEV" "$FLAGS_to" | |
| 99 sudo mkfs.ext3 -F -b 4096 -L C-STATE "$LOOP_DEV" $(( $PART_SIZE / 4096 )) | |
| 100 sync | |
| 101 sudo losetup -d "$LOOP_DEV" | |
| 102 sync | |
| 103 | |
| 104 trap - EXIT | |
| 105 | |
| 106 echo "Copying MBR..." | |
| 107 sudo "${SCRIPTS_DIR}"/file_copy.py \ | |
| 108 if="${FLAGS_from}/mbr.image" of="$FLAGS_to" | |
| 109 sync | |
| 110 echo "Done." | |
| 111 else | 79 else |
| 112 # Output to a file, so just cat the source images together | 80 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" |
| 113 | |
| 114 PART_SIZE=$(stat -c%s "${FLAGS_from}/rootfs.image") | |
| 115 | |
| 116 echo "Creating empty stateful partition" | |
| 117 dd if=/dev/zero of="${FLAGS_from}/stateful_partition.image" bs=1 count=1 \ | |
| 118 seek=$(($PART_SIZE - 1)) | |
| 119 mkfs.ext3 -F -L C-STATE "${FLAGS_from}/stateful_partition.image" | |
| 120 | |
| 121 # Create a sparse output file | |
| 122 dd if=/dev/zero of="${FLAGS_to}" bs=1 count=1 \ | |
| 123 seek=$(( ($PART_SIZE * 2) + 512 - 1)) | |
| 124 | |
| 125 echo "Copying USB image to file ${FLAGS_to}..." | |
| 126 | |
| 127 dd if="${FLAGS_from}/mbr.image" of="$FLAGS_to" conv=notrunc | |
| 128 dd if="${FLAGS_from}/stateful_partition.image" of="$FLAGS_to" seek=1 bs=512 \ | |
| 129 conv=notrunc | |
| 130 cat "${FLAGS_from}/rootfs.image" >> "$FLAGS_to" | |
| 131 | |
| 132 echo "Done. To copy to USB keyfob, outside the chroot, do something like:" | |
| 133 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" | 81 echo " sudo dd if=${FLAGS_to} of=/dev/sdb bs=4M" |
| 134 echo "where /dev/sdb is the entire keyfob." | 82 echo "where /dev/sdb is the entire keyfob." |
| 135 if [ $INSIDE_CHROOT -eq 1 ] | 83 if [ $INSIDE_CHROOT -eq 1 ] |
| 136 then | 84 then |
| 137 echo "NOTE: Since you are currently inside the chroot, and you'll need to" | 85 echo "NOTE: Since you are currently inside the chroot, and you'll need to" |
| 138 echo "run dd outside the chroot, the path to the USB image will be" | 86 echo "run dd outside the chroot, the path to the USB image will be" |
| 139 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." | 87 echo "different (ex: ~/chromeos/trunk/src/build/images/SOME_DIR/usb.img)." |
| 140 fi | 88 fi |
| 141 fi | 89 fi |
| OLD | NEW |