| 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 modify a keyfob-based chromeos system image for testability. | 7 # Script to modify a keyfob-based chromeos system image for testability. |
| 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. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 FILENAME="chromiumos_image.bin" | 39 FILENAME="chromiumos_image.bin" |
| 40 FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" | 40 FLAGS_image="${IMAGES_DIR}/$(ls -t $IMAGES_DIR 2>&-| head -1)/${FILENAME}" |
| 41 fi | 41 fi |
| 42 | 42 |
| 43 # Abort early if we can't find the image | 43 # Abort early if we can't find the image |
| 44 if [ ! -f $FLAGS_image ] ; then | 44 if [ ! -f $FLAGS_image ] ; then |
| 45 echo "No image found at $FLAGS_image" | 45 echo "No image found at $FLAGS_image" |
| 46 exit 1 | 46 exit 1 |
| 47 fi | 47 fi |
| 48 | 48 |
| 49 # Make sure anything mounted in the rootfs is cleaned up ok on exit. | 49 # Make sure anything mounted in the rootfs/stateful is cleaned up ok on exit. |
| 50 cleanup_rootfs_mounts() { | 50 cleanup_mounts() { |
| 51 # Occasionally there are some daemons left hanging around that have our | 51 # Occasionally there are some daemons left hanging around that have our |
| 52 # root image file system open. We do a best effort attempt to kill them. | 52 # root/stateful image file system open. We do a best effort attempt to kill |
| 53 PIDS=`sudo lsof -t "${ROOT_FS_DIR}" | sort | uniq` | 53 # them. |
| 54 PIDS=`sudo lsof -t "$1" | sort | uniq` |
| 54 for pid in ${PIDS} | 55 for pid in ${PIDS} |
| 55 do | 56 do |
| 56 local cmdline=`cat /proc/$pid/cmdline` | 57 local cmdline=`cat /proc/$pid/cmdline` |
| 57 echo "Killing process that has open file on our rootfs: $cmdline" | 58 echo "Killing process that has open file on the mounted directory: $cmdline" |
| 58 sudo kill $pid || /bin/true | 59 sudo kill $pid || /bin/true |
| 59 done | 60 done |
| 60 } | 61 } |
| 61 | 62 |
| 62 cleanup_rootfs_loop() { | 63 cleanup_loop() { |
| 63 sudo umount "${LOOP_DEV}" | 64 sudo umount "$1" |
| 64 sleep 1 # in case $LOOP_DEV is in use | 65 sleep 1 # in case the loop device is in use |
| 65 sudo losetup -d "${LOOP_DEV}" | 66 sudo losetup -d "$1" |
| 66 } | 67 } |
| 67 | 68 |
| 68 cleanup() { | 69 cleanup() { |
| 69 # Disable die on error. | 70 # Disable die on error. |
| 70 set +e | 71 set +e |
| 71 | 72 |
| 72 cleanup_rootfs_mounts | 73 cleanup_mounts "${ROOT_FS_DIR}" |
| 73 if [ -n "${LOOP_DEV}" ] | 74 if [ -n "${ROOT_LOOP_DEV}" ] |
| 74 then | 75 then |
| 75 cleanup_rootfs_loop | 76 cleanup_loop "${ROOT_LOOP_DEV}" |
| 76 fi | 77 fi |
| 78 rmdir "${ROOT_FS_DIR}" |
| 77 | 79 |
| 78 rmdir "${ROOT_FS_DIR}" | 80 cleanup_mounts "${STATEFUL_DIR}" |
| 81 if [ -n "${STATEFUL_LOOP_DEV}" ] |
| 82 then |
| 83 cleanup_loop "${STATEFUL_LOOP_DEV}" |
| 84 fi |
| 85 rmdir "${STATEFUL_DIR}" |
| 79 | 86 |
| 80 # Turn die on error back on. | 87 # Turn die on error back on. |
| 81 set -e | 88 set -e |
| 82 } | 89 } |
| 83 | 90 |
| 84 # main process begins here. | 91 # main process begins here. |
| 85 | 92 |
| 86 # Make sure this is really what the user wants, before nuking the device | 93 # Make sure this is really what the user wants, before nuking the device |
| 87 if [ $FLAGS_yes -ne $FLAGS_TRUE ]; then | 94 if [ $FLAGS_yes -ne $FLAGS_TRUE ]; then |
| 88 read -p "Modifying image ${FLAGS_image} for test; are you sure (y/N)? " SURE | 95 read -p "Modifying image ${FLAGS_image} for test; are you sure (y/N)? " SURE |
| 89 SURE="${SURE:0:1}" # Get just the first character | 96 SURE="${SURE:0:1}" # Get just the first character |
| 90 if [ "$SURE" != "y" ]; then | 97 if [ "$SURE" != "y" ]; then |
| 91 echo "Ok, better safe than sorry." | 98 echo "Ok, better safe than sorry." |
| 92 exit 1 | 99 exit 1 |
| 93 fi | 100 fi |
| 94 else | 101 else |
| 95 echo "Modifying image ${FLAGS_image} for test..." | 102 echo "Modifying image ${FLAGS_image} for test..." |
| 96 fi | 103 fi |
| 97 | 104 |
| 98 set -e | 105 set -e |
| 99 | 106 |
| 100 ROOT_FS_DIR=$(dirname "${FLAGS_image}")/rootfs | 107 ROOT_FS_DIR=$(dirname "${FLAGS_image}")/rootfs |
| 101 mkdir -p "${ROOT_FS_DIR}" | 108 mkdir -p "${ROOT_FS_DIR}" |
| 102 | 109 |
| 110 STATEFUL_DIR=$(dirname "${FLAGS_image}")/stateful_partition |
| 111 mkdir -p "${STATEFUL_DIR}" |
| 112 |
| 103 trap cleanup EXIT | 113 trap cleanup EXIT |
| 104 | 114 |
| 105 # Figure out how to loop mount the rootfs partition. It should be partition 3 | 115 # Figure out how to loop mount the rootfs partition. It should be partition 3 |
| 106 # on the disk image. | 116 # on the disk image. |
| 107 offset=$(partoffset "${FLAGS_image}" 3) | 117 offset=$(partoffset "${FLAGS_image}" 3) |
| 108 | 118 |
| 109 LOOP_DEV=$(sudo losetup -f) | 119 ROOT_LOOP_DEV=$(sudo losetup -f) |
| 110 if [ -z "$LOOP_DEV" ]; then | 120 if [ -z "$ROOT_LOOP_DEV" ]; then |
| 111 echo "No free loop device" | 121 echo "No free loop device" |
| 112 exit 1 | 122 exit 1 |
| 113 fi | 123 fi |
| 114 sudo losetup -o $(( $offset * 512 )) "${LOOP_DEV}" "${FLAGS_image}" | 124 sudo losetup -o $(( $offset * 512 )) "${ROOT_LOOP_DEV}" "${FLAGS_image}" |
| 115 sudo mount "${LOOP_DEV}" "${ROOT_FS_DIR}" | 125 sudo mount "${ROOT_LOOP_DEV}" "${ROOT_FS_DIR}" |
| 126 |
| 127 # The stateful partition should be partition 1 on the disk image. |
| 128 offset=$(partoffset "${FLAGS_image}" 1) |
| 129 |
| 130 STATEFUL_LOOP_DEV=$(sudo losetup -f) |
| 131 if [ -z "$STATEFUL_LOOP_DEV" ]; then |
| 132 echo "No free loop device" |
| 133 exit 1 |
| 134 fi |
| 135 sudo losetup -o $(( $offset * 512 )) "${STATEFUL_LOOP_DEV}" "${FLAGS_image}" |
| 136 sudo mount "${STATEFUL_LOOP_DEV}" "${STATEFUL_DIR}" |
| 137 STATEFUL_DIR="${STATEFUL_DIR}" |
| 116 | 138 |
| 117 MOD_TEST_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts" | 139 MOD_TEST_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_test_scripts" |
| 118 # Run test setup script to modify the image | 140 # Run test setup script to modify the image |
| 119 sudo GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \ | 141 sudo GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \ |
| 120 "${MOD_TEST_ROOT}/test_setup.sh" | 142 "${MOD_TEST_ROOT}/test_setup.sh" |
| 121 | 143 |
| 122 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then | 144 if [ ${FLAGS_factory} -eq ${FLAGS_TRUE} ]; then |
| 123 MOD_FACTORY_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_factory_scripts" | 145 MOD_FACTORY_ROOT="${GCLIENT_ROOT}/src/scripts/mod_for_factory_scripts" |
| 124 # Run factory setup script to modify the image | 146 # Run factory setup script to modify the image |
| 125 sudo GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \ | 147 sudo GCLIENT_ROOT="${GCLIENT_ROOT}" ROOT_FS_DIR="${ROOT_FS_DIR}" \ |
| 126 QUALDB="${FLAGS_qualdb}" "${MOD_FACTORY_ROOT}/factory_setup.sh" | 148 STATEFUL_DIR="${STATEFUL_DIR}/dev_image" QUALDB="${FLAGS_qualdb}" \ |
| 149 "${MOD_FACTORY_ROOT}/factory_setup.sh" |
| 127 fi | 150 fi |
| 128 | 151 |
| 129 cleanup | 152 cleanup |
| 130 trap - EXIT | 153 trap - EXIT |
| 131 | 154 |
| OLD | NEW |