| OLD | NEW |
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 | 2 |
| 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 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 # Determine script directory | 7 # Determine script directory |
| 8 SCRIPT_DIR=$(dirname $0) | 8 SCRIPT_DIR=$(dirname $0) |
| 9 PROG=$(basename $0) | 9 PROG=$(basename $0) |
| 10 GPT=cgpt | 10 GPT=cgpt |
| 11 | 11 |
| 12 # The tag when the rootfs is changed. |
| 13 TAG_NEEDS_TO_BE_SIGNED="/root/.need_to_be_signed" |
| 14 |
| 15 # Load shflags |
| 16 if [[ -f /usr/lib/shflags ]]; then |
| 17 . /usr/lib/shflags |
| 18 else |
| 19 . "${SCRIPT_DIR}/lib/shflags/shflags" |
| 20 fi |
| 21 |
| 12 # List of Temporary files and mount points. | 22 # List of Temporary files and mount points. |
| 13 TEMP_FILE_LIST=$(mktemp) | 23 TEMP_FILE_LIST=$(mktemp) |
| 14 TEMP_DIR_LIST=$(mktemp) | 24 TEMP_DIR_LIST=$(mktemp) |
| 15 | 25 |
| 16 # Read GPT table to find the starting location of a specific partition. | 26 # Read GPT table to find the starting location of a specific partition. |
| 17 # Args: DEVICE PARTNUM | 27 # Args: DEVICE PARTNUM |
| 18 # Returns: offset (in sectors) of partition PARTNUM | 28 # Returns: offset (in sectors) of partition PARTNUM |
| 19 partoffset() { | 29 partoffset() { |
| 20 sudo $GPT show -b -i $2 $1 | 30 sudo $GPT show -b -i $2 $1 |
| 21 } | 31 } |
| 22 | 32 |
| 23 # Read GPT table to find the size of a specific partition. | 33 # Read GPT table to find the size of a specific partition. |
| 24 # Args: DEVICE PARTNUM | 34 # Args: DEVICE PARTNUM |
| 25 # Returns: size (in sectors) of partition PARTNUM | 35 # Returns: size (in sectors) of partition PARTNUM |
| 26 partsize() { | 36 partsize() { |
| 27 sudo $GPT show -s -i $2 $1 | 37 sudo $GPT show -s -i $2 $1 |
| 28 } | 38 } |
| 29 | 39 |
| 40 # Tags a file system as "needs to be resigned". |
| 41 # Args: MOUNTDIRECTORY |
| 42 tag_as_needs_to_be_resigned() { |
| 43 local mount_dir="$1" |
| 44 sudo touch "$mount_dir/$TAG_NEEDS_TO_BE_SIGNED" |
| 45 } |
| 46 |
| 47 # Determines if the target file system has the tag for resign |
| 48 # Args: MOUNTDIRECTORY |
| 49 # Returns: $FLAGS_TRUE if the tag is there, otherwise $FLAGS_FALSE |
| 50 has_needs_to_be_resigned_tag() { |
| 51 local mount_dir="$1" |
| 52 if [ -f "$mount_dir/$TAG_NEEDS_TO_BE_SIGNED" ]; then |
| 53 return ${FLAGS_TRUE} |
| 54 else |
| 55 return ${FLAGS_FALSE} |
| 56 fi |
| 57 } |
| 58 |
| 59 # Determines if the target file system is a Chrome OS root fs |
| 60 # Args: MOUNTDIRECTORY |
| 61 # Returns: $FLAGS_TRUE if MOUNTDIRECTORY looks like root fs, |
| 62 # otherwise $FLAGS_FALSE |
| 63 is_rootfs_partition() { |
| 64 local mount_dir="$1" |
| 65 if [ -f "$mount_dir/$(dirname "$TAG_NEEDS_TO_BE_SIGNED")" ]; then |
| 66 return ${FLAGS_TRUE} |
| 67 else |
| 68 return ${FLAGS_FALSE} |
| 69 fi |
| 70 } |
| 71 |
| 30 # Mount a partition read-only from an image into a local directory | 72 # Mount a partition read-only from an image into a local directory |
| 31 # Args: IMAGE PARTNUM MOUNTDIRECTORY | 73 # Args: IMAGE PARTNUM MOUNTDIRECTORY |
| 32 mount_image_partition_ro() { | 74 mount_image_partition_ro() { |
| 33 local image=$1 | 75 local image=$1 |
| 34 local partnum=$2 | 76 local partnum=$2 |
| 35 local mount_dir=$3 | 77 local mount_dir=$3 |
| 36 local offset=$(partoffset "$image" "$partnum") | 78 local offset=$(partoffset "$image" "$partnum") |
| 37 sudo mount -o loop,ro,offset=$((offset * 512)) "$image" "$mount_dir" | 79 sudo mount -o loop,ro,offset=$((offset * 512)) "$image" "$mount_dir" |
| 38 } | 80 } |
| 39 | 81 |
| 40 # Mount a partition from an image into a local directory | 82 # Mount a partition from an image into a local directory |
| 41 # Args: IMAGE PARTNUM MOUNTDIRECTORY | 83 # Args: IMAGE PARTNUM MOUNTDIRECTORY |
| 42 mount_image_partition() { | 84 mount_image_partition() { |
| 43 local image=$1 | 85 local image=$1 |
| 44 local partnum=$2 | 86 local partnum=$2 |
| 45 local mount_dir=$3 | 87 local mount_dir=$3 |
| 46 local offset=$(partoffset "$image" "$partnum") | 88 local offset=$(partoffset "$image" "$partnum") |
| 47 sudo mount -o loop,offset=$((offset * 512)) "$image" "$mount_dir" | 89 sudo mount -o loop,offset=$((offset * 512)) "$image" "$mount_dir" |
| 90 if is_rootfs_partition "$mount_dir"; then |
| 91 tag_as_needs_to_be_resigned "$mount_dir" |
| 92 fi |
| 48 } | 93 } |
| 49 | 94 |
| 50 # Extract a partition to a file | 95 # Extract a partition to a file |
| 51 # Args: IMAGE PARTNUM OUTPUTFILE | 96 # Args: IMAGE PARTNUM OUTPUTFILE |
| 52 extract_image_partition() { | 97 extract_image_partition() { |
| 53 local image=$1 | 98 local image=$1 |
| 54 local partnum=$2 | 99 local partnum=$2 |
| 55 local output_file=$3 | 100 local output_file=$3 |
| 56 local offset=$(partoffset "$image" "$partnum") | 101 local offset=$(partoffset "$image" "$partnum") |
| 57 local size=$(partsize "$image" "$partnum") | 102 local size=$(partsize "$image" "$partnum") |
| (...skipping 28 matching lines...) Expand all Loading... |
| 86 echo $tempdir | 131 echo $tempdir |
| 87 } | 132 } |
| 88 | 133 |
| 89 cleanup_temps_and_mounts() { | 134 cleanup_temps_and_mounts() { |
| 90 for i in "$(cat $TEMP_FILE_LIST)"; do | 135 for i in "$(cat $TEMP_FILE_LIST)"; do |
| 91 rm -f $i | 136 rm -f $i |
| 92 done | 137 done |
| 93 set +e # umount may fail for unmounted directories | 138 set +e # umount may fail for unmounted directories |
| 94 for i in "$(cat $TEMP_DIR_LIST)"; do | 139 for i in "$(cat $TEMP_DIR_LIST)"; do |
| 95 if [ -n "$i" ]; then | 140 if [ -n "$i" ]; then |
| 141 if has_needs_to_be_resigned_tag "$i"; then |
| 142 echo "Warning: image may be modified. Please resign image." |
| 143 fi |
| 96 sudo umount -d $i 2>/dev/null | 144 sudo umount -d $i 2>/dev/null |
| 97 rm -rf $i | 145 rm -rf $i |
| 98 fi | 146 fi |
| 99 done | 147 done |
| 100 set -e | 148 set -e |
| 101 rm -rf $TEMP_DIR_LIST $TEMP_FILE_LIST | 149 rm -rf $TEMP_DIR_LIST $TEMP_FILE_LIST |
| 102 } | 150 } |
| 103 | 151 |
| 104 trap "cleanup_temps_and_mounts" EXIT | 152 trap "cleanup_temps_and_mounts" EXIT |
| 105 | 153 |
| OLD | NEW |