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 GPT=cgpt | 9 GPT=cgpt |
10 | 10 |
| 11 # List of Temporary files and mount points. |
| 12 TEMP_FILE_LIST=$(mktemp) |
| 13 TEMP_DIR_LIST=$(mktemp) |
| 14 |
11 # Read GPT table to find the starting location of a specific partition. | 15 # Read GPT table to find the starting location of a specific partition. |
12 # Args: DEVICE PARTNUM | 16 # Args: DEVICE PARTNUM |
13 # Returns: offset (in sectors) of partition PARTNUM | 17 # Returns: offset (in sectors) of partition PARTNUM |
14 partoffset() { | 18 partoffset() { |
15 sudo $GPT show -b -i $2 $1 | 19 sudo $GPT show -b -i $2 $1 |
16 } | 20 } |
17 | 21 |
18 # Read GPT table to find the size of a specific partition. | 22 # Read GPT table to find the size of a specific partition. |
19 # Args: DEVICE PARTNUM | 23 # Args: DEVICE PARTNUM |
20 # Returns: size (in sectors) of partition PARTNUM | 24 # Returns: size (in sectors) of partition PARTNUM |
(...skipping 12 matching lines...) Expand all Loading... |
33 } | 37 } |
34 | 38 |
35 # Extract a partition to a file | 39 # Extract a partition to a file |
36 # Args: IMAGE PARTNUM OUTPUTFILE | 40 # Args: IMAGE PARTNUM OUTPUTFILE |
37 extract_image_partition() { | 41 extract_image_partition() { |
38 local image=$1 | 42 local image=$1 |
39 local partnum=$2 | 43 local partnum=$2 |
40 local output_file=$3 | 44 local output_file=$3 |
41 local offset=$(partoffset "$image" "$partnum") | 45 local offset=$(partoffset "$image" "$partnum") |
42 local size=$(partsize "$image" "$partnum") | 46 local size=$(partsize "$image" "$partnum") |
43 dd if=$image of=$output_file bs=512 skip=$offset count=$size | 47 dd if=$image of=$output_file bs=512 skip=$offset count=$size conv=notrunc |
44 } | 48 } |
45 | 49 |
| 50 # Replace a partition in an image from file |
| 51 # Args: IMAGE PARTNUM INPUTFILE |
| 52 replace_image_partition() { |
| 53 local image=$1 |
| 54 local partnum=$2 |
| 55 local input_file=$3 |
| 56 local offset=$(partoffset "$image" "$partnum") |
| 57 local size=$(partsize "$image" "$partnum") |
| 58 dd if=$input_file of=$image bs=512 seek=$offset count=$size conv=notrunc |
| 59 } |
| 60 |
| 61 # Create a new temporary file and return its name. |
| 62 # File is automatically cleaned when cleanup_temps_and_mounts() is called. |
| 63 make_temp_file() { |
| 64 local tempfile=$(mktemp) |
| 65 echo "$tempfile" >> $TEMP_FILE_LIST |
| 66 echo $tempfile |
| 67 } |
| 68 |
| 69 # Create a new temporary directory and return its name. |
| 70 # Directory is automatically deleted and any filesystem mounted on it unmounted |
| 71 # when cleanup_temps_and_mounts() is called. |
| 72 make_temp_dir() { |
| 73 local tempdir=$(mktemp -d) |
| 74 echo "$tempdir" >> $TEMP_DIR_LIST |
| 75 echo $tempdir |
| 76 } |
| 77 |
| 78 cleanup_temps_and_mounts() { |
| 79 for i in "$(cat $TEMP_FILE_LIST)"; do |
| 80 rm -f $i |
| 81 done |
| 82 set +e # umount may fail for unmounted directories |
| 83 for i in "$(cat $TEMP_DIR_LIST)"; do |
| 84 if [ -n "$i" ]; then |
| 85 sudo umount -d $i 2>/dev/null |
| 86 rm -rf $i |
| 87 fi |
| 88 done |
| 89 set -e |
| 90 rm -rf $TEMP_DIR_LIST $TEMP_FILE_LIST |
| 91 } |
| 92 |
| 93 trap "cleanup_temps_and_mounts" EXIT |
OLD | NEW |