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 # This script contains common utility function to deal with disk images, | 7 # This script contains common utility function to deal with disk images, |
8 # especially for being redistributed into platforms without complete Chromium OS | 8 # especially for being redistributed into platforms without complete Chromium OS |
9 # developing environment. | 9 # developing environment. |
10 | 10 |
11 # Check if given command is available in current system | 11 # Checks if given command is available in current system |
12 has_command() { | 12 image_has_command() { |
13 type "$1" >/dev/null 2>&1 | 13 type "$1" >/dev/null 2>&1 |
14 } | 14 } |
15 | 15 |
16 err_die() { | 16 # Prints error message and exit as 1 (error) |
| 17 image_die() { |
17 echo "ERROR: $@" >&2 | 18 echo "ERROR: $@" >&2 |
18 exit 1 | 19 exit 1 |
19 } | 20 } |
20 | 21 |
21 # Finds the best gzip compressor and invoke it. | 22 # Finds the best gzip compressor and invoke it |
22 gzip_compress() { | 23 image_gzip_compress() { |
23 if has_command pigz; then | 24 if image_has_command pigz; then |
24 # echo " ** Using parallel gzip **" >&2 | 25 # echo " ** Using parallel gzip **" >&2 |
25 # Tested with -b 32, 64, 128(default), 256, 1024, 16384, and -b 32 (max | 26 # Tested with -b 32, 64, 128(default), 256, 1024, 16384, and -b 32 (max |
26 # window size of Deflate) seems to be the best in output size. | 27 # window size of Deflate) seems to be the best in output size. |
27 pigz -b 32 "$@" | 28 pigz -b 32 "$@" |
28 else | 29 else |
29 gzip "$@" | 30 gzip "$@" |
30 fi | 31 fi |
31 } | 32 } |
32 | 33 |
| 34 # Finds the best bzip2 compressor and invoke it |
| 35 image_bzip2_compress() { |
| 36 if image_has_command pbzip2; then |
| 37 pbzip2 "$@" |
| 38 else |
| 39 bzip2 "$@" |
| 40 fi |
| 41 } |
| 42 |
33 # Finds if current system has tools for part_* commands | 43 # Finds if current system has tools for part_* commands |
34 has_part_tools() { | 44 image_has_part_tools() { |
35 has_command cgpt || has_command parted | 45 image_has_command cgpt || image_has_command parted |
36 } | 46 } |
37 | 47 |
38 # Finds the best partition tool and print partition offset | 48 # Finds the best partition tool and print partition offset |
39 part_offset() { | 49 image_part_offset() { |
40 local file="$1" | 50 local file="$1" |
41 local partno="$2" | 51 local partno="$2" |
| 52 local unpack_file="$(dirname "$file")/unpack_partitions.sh" |
42 | 53 |
43 if has_command cgpt; then | 54 # TODO parted is available on most Linux so we may deprecate other code path |
| 55 if image_has_command cgpt; then |
44 cgpt show -b -i "$partno" "$file" | 56 cgpt show -b -i "$partno" "$file" |
45 elif has_command parted; then | 57 elif image_has_command parted; then |
46 parted -m "$file" unit s print | | 58 parted -m "$file" unit s print | awk -F ':' "/^$partno:/ { print int(\$2) }" |
47 grep "^$partno:" | cut -d ':' -f 2 | sed 's/s$//' | 59 elif [ -f "$unpack_file" ]; then |
| 60 awk "/ $partno *Label:/ { print \$2 }" "$unpack_file" |
48 else | 61 else |
49 exit 1 | 62 exit 1 |
50 fi | 63 fi |
51 } | 64 } |
52 | 65 |
53 # Finds the best partition tool and print partition size | 66 # Finds the best partition tool and print partition size |
54 part_size() { | 67 image_part_size() { |
55 local file="$1" | 68 local file="$1" |
56 local partno="$2" | 69 local partno="$2" |
| 70 local unpack_file="$(dirname "$file")/unpack_partitions.sh" |
57 | 71 |
58 if has_command cgpt; then | 72 # TODO parted is available on most Linux so we may deprecate other code path |
| 73 if image_has_command cgpt; then |
59 cgpt show -s -i "$partno" "$file" | 74 cgpt show -s -i "$partno" "$file" |
60 elif has_command parted; then | 75 elif image_has_command parted; then |
61 parted -m "$file" unit s print | | 76 parted -m "$file" unit s print | awk -F ':' "/^$partno:/ { print int(\$4) }" |
62 grep "^$partno:" | cut -d ':' -f 4 | sed 's/s$//' | 77 elif [ -s "$unpack_file" ]; then |
| 78 awk "/ $partno *Label:/ { print \$3 }" "$unpack_file" |
63 else | 79 else |
64 exit 1 | 80 exit 1 |
65 fi | 81 fi |
66 } | 82 } |
67 | 83 |
68 # Dumps a file by given offset and size (in sectors) | 84 # Dumps a file by given offset and size (in sectors) |
69 dump_partial_file() { | 85 image_dump_partial_file() { |
70 local file="$1" | 86 local file="$1" |
71 local offset="$2" | 87 local offset="$2" |
72 local sectors="$3" | 88 local sectors="$3" |
73 local bs=512 | 89 local bs=512 |
74 | 90 |
75 # Try to use larger buffer if offset/size can be re-aligned. | 91 # Try to use larger buffer if offset/size can be re-aligned. |
76 # 2M / 512 = 4096 | 92 # 2M / 512 = 4096 |
77 local buffer_ratio=4096 | 93 local buffer_ratio=4096 |
78 if [ $((offset % buffer_ratio)) -eq 0 -a \ | 94 if [ $((offset % buffer_ratio)) -eq 0 -a \ |
79 $((sectors % buffer_ratio)) -eq 0 ]; then | 95 $((sectors % buffer_ratio)) -eq 0 ]; then |
80 offset=$((offset / buffer_ratio)) | 96 offset=$((offset / buffer_ratio)) |
81 sectors=$((sectors / buffer_ratio)) | 97 sectors=$((sectors / buffer_ratio)) |
82 bs=$((bs * buffer_ratio)) | 98 bs=$((bs * buffer_ratio)) |
83 fi | 99 fi |
84 | 100 |
85 if has_command pv; then | 101 if image_has_command pv; then |
86 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ | 102 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
87 oflag=sync status=noxfer 2>/dev/null | | 103 oflag=sync status=noxfer 2>/dev/null | |
88 pv -ptreb -B 4m -s $((sectors * $bs)) | 104 pv -ptreb -B $bs -s $((sectors * bs)) |
89 else | 105 else |
90 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ | 106 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
91 oflag=sync status=noxfer 2>/dev/null | 107 oflag=sync status=noxfer 2>/dev/null |
92 fi | 108 fi |
93 } | 109 } |
94 | 110 |
95 # Dumps a specific partition from given image file | 111 # Dumps a specific partition from given image file |
96 dump_partition() { | 112 image_dump_partition() { |
97 local file="$1" | 113 local file="$1" |
98 local part_num="$2" | 114 local part_num="$2" |
99 local offset="$(part_offset "$file" "$part_num")" || | 115 local offset="$(image_part_offset "$file" "$part_num")" || |
100 err_die "failed to dump partition #$part_num from: $file" | 116 image_die "failed to find partition #$part_num from: $file" |
101 local size="$(part_size "$file" "$part_num")" || | 117 local size="$(image_part_size "$file" "$part_num")" || |
102 err_die "failed to dump partition #$part_num from: $file" | 118 image_die "failed to find partition #$part_num from: $file" |
103 | 119 |
104 dump_partial_file "$file" "$offset" "$size" | 120 image_dump_partial_file "$file" "$offset" "$size" |
105 } | 121 } |
106 | 122 |
| 123 # Maps a specific partition from given image file to a loop device |
| 124 image_map_partition() { |
| 125 local file="$1" |
| 126 local part_num="$2" |
| 127 local offset="$(image_part_offset "$file" "$part_num")" || |
| 128 image_die "failed to find partition #$part_num from: $file" |
| 129 local size="$(image_part_size "$file" "$part_num")" || |
| 130 image_die "failed to find partition #$part_num from: $file" |
| 131 |
| 132 losetup --offset $((offset * 512)) --sizelimit=$((size * 512)) \ |
| 133 -f --show "$file" |
| 134 } |
| 135 |
| 136 # Unmaps a loop device created by image_map_partition |
| 137 image_unmap_partition() { |
| 138 local map_point="$1" |
| 139 |
| 140 losetup -d "$map_point" |
| 141 } |
| 142 |
| 143 # Mounts a specific partition inside a given image file |
| 144 image_mount_partition() { |
| 145 local file="$1" |
| 146 local part_num="$2" |
| 147 local mount_point="$3" |
| 148 local mount_opt="$4" |
| 149 local offset="$(image_part_offset "$file" "$part_num")" || |
| 150 image_die "failed to find partition #$part_num from: $file" |
| 151 local size="$(image_part_size "$file" "$part_num")" || |
| 152 image_die "failed to find partition #$part_num from: $file" |
| 153 |
| 154 if [ -z "$mount_opt" ]; then |
| 155 # by default, mount as read-only. |
| 156 mount_opt=",ro" |
| 157 fi |
| 158 |
| 159 mount \ |
| 160 -o "loop,offset=$((offset * 512)),sizelimit=$((size * 512)),$mount_opt" \ |
| 161 "$file" \ |
| 162 "$mount_point" |
| 163 } |
| 164 |
| 165 # Unmounts a partition mount point by mount_partition |
| 166 image_umount_partition() { |
| 167 local mount_point="$1" |
| 168 |
| 169 umount -d "$mount_point" |
| 170 } |
OLD | NEW |