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 if image_has_command cgpt; then |
44 cgpt show -b -i "$partno" "$file" | 55 cgpt show -b -i "$partno" "$file" |
Nick Sanders
2010/11/18 05:12:08
Can you add parted to the chroot, and just use tha
Hung-Te
2010/11/18 05:49:31
cgpt is always in chroot. I made this because this
Nick Sanders
2010/11/18 06:18:35
Err, what I was saying is that we can easily ensur
Hung-Te
2010/11/18 06:46:25
Thanks for the suggestion. It's a good idea to hav
| |
45 elif has_command parted; then | 56 elif image_has_command parted; then |
46 parted -m "$file" unit s print | | 57 parted -m "$file" unit s print | awk -F ':' "/^$partno:/ { print int(\$2) }" |
47 grep "^$partno:" | cut -d ':' -f 2 | sed 's/s$//' | 58 elif [ -f "$unpack_file" ]; then |
59 awk "/ $partno *Label:/ { print \$2 }" "$unpack_file" | |
48 else | 60 else |
49 exit 1 | 61 exit 1 |
50 fi | 62 fi |
51 } | 63 } |
52 | 64 |
53 # Finds the best partition tool and print partition size | 65 # Finds the best partition tool and print partition size |
54 part_size() { | 66 image_part_size() { |
55 local file="$1" | 67 local file="$1" |
56 local partno="$2" | 68 local partno="$2" |
69 local unpack_file="$(dirname "$file")/unpack_partitions.sh" | |
57 | 70 |
58 if has_command cgpt; then | 71 if image_has_command cgpt; then |
59 cgpt show -s -i "$partno" "$file" | 72 cgpt show -s -i "$partno" "$file" |
60 elif has_command parted; then | 73 elif image_has_command parted; then |
61 parted -m "$file" unit s print | | 74 parted -m "$file" unit s print | awk -F ':' "/^$partno:/ { print int(\$4) }" |
62 grep "^$partno:" | cut -d ':' -f 4 | sed 's/s$//' | 75 elif [ -s "$unpack_file" ]; then |
76 awk "/ $partno *Label:/ { print \$3 }" "$unpack_file" | |
63 else | 77 else |
64 exit 1 | 78 exit 1 |
65 fi | 79 fi |
66 } | 80 } |
67 | 81 |
68 # Dumps a file by given offset and size (in sectors) | 82 # Dumps a file by given offset and size (in sectors) |
69 dump_partial_file() { | 83 image_dump_partial_file() { |
70 local file="$1" | 84 local file="$1" |
71 local offset="$2" | 85 local offset="$2" |
72 local sectors="$3" | 86 local sectors="$3" |
73 local bs=512 | 87 local bs=512 |
74 | 88 |
75 # Try to use larger buffer if offset/size can be re-aligned. | 89 # Try to use larger buffer if offset/size can be re-aligned. |
76 # 2M / 512 = 4096 | 90 # 2M / 512 = 4096 |
77 local buffer_ratio=4096 | 91 local buffer_ratio=4096 |
78 if [ $((offset % buffer_ratio)) -eq 0 -a \ | 92 if [ $((offset % buffer_ratio)) -eq 0 -a \ |
79 $((sectors % buffer_ratio)) -eq 0 ]; then | 93 $((sectors % buffer_ratio)) -eq 0 ]; then |
80 offset=$((offset / buffer_ratio)) | 94 offset=$((offset / buffer_ratio)) |
81 sectors=$((sectors / buffer_ratio)) | 95 sectors=$((sectors / buffer_ratio)) |
82 bs=$((bs * buffer_ratio)) | 96 bs=$((bs * buffer_ratio)) |
83 fi | 97 fi |
84 | 98 |
85 if has_command pv; then | 99 if image_has_command pv; then |
86 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ | 100 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
87 oflag=sync status=noxfer 2>/dev/null | | 101 oflag=sync status=noxfer 2>/dev/null | |
88 pv -ptreb -B 4m -s $((sectors * $bs)) | 102 pv -ptreb -B $bs -s $((sectors * bs)) |
89 else | 103 else |
90 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ | 104 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
91 oflag=sync status=noxfer 2>/dev/null | 105 oflag=sync status=noxfer 2>/dev/null |
92 fi | 106 fi |
93 } | 107 } |
94 | 108 |
95 # Dumps a specific partition from given image file | 109 # Dumps a specific partition from given image file |
96 dump_partition() { | 110 image_dump_partition() { |
97 local file="$1" | 111 local file="$1" |
98 local part_num="$2" | 112 local part_num="$2" |
99 local offset="$(part_offset "$file" "$part_num")" || | 113 local offset="$(image_part_offset "$file" "$part_num")" || |
100 err_die "failed to dump partition #$part_num from: $file" | 114 image_die "failed to find partition #$part_num from: $file" |
101 local size="$(part_size "$file" "$part_num")" || | 115 local size="$(image_part_size "$file" "$part_num")" || |
102 err_die "failed to dump partition #$part_num from: $file" | 116 image_die "failed to find partition #$part_num from: $file" |
103 | 117 |
104 dump_partial_file "$file" "$offset" "$size" | 118 image_dump_partial_file "$file" "$offset" "$size" |
105 } | 119 } |
106 | 120 |
121 # Maps a specific partition from given image file to a loop device | |
122 image_map_partition() { | |
123 local file="$1" | |
124 local part_num="$2" | |
125 local offset="$(image_part_offset "$file" "$part_num")" || | |
126 image_die "failed to find partition #$part_num from: $file" | |
127 local size="$(image_part_size "$file" "$part_num")" || | |
128 image_die "failed to find partition #$part_num from: $file" | |
129 | |
130 losetup --offset $((offset * 512)) --sizelimit=$((size * 512)) \ | |
131 -f --show "$file" | |
132 } | |
133 | |
134 # Unmaps a loop device created by image_map_partition | |
135 image_unmap_partition() { | |
136 local map_point="$1" | |
137 | |
138 losetup -d "$map_point" | |
139 } | |
140 | |
141 # Mounts a specific partition inside a given image file | |
142 image_mount_partition() { | |
143 local file="$1" | |
144 local part_num="$2" | |
145 local mount_point="$3" | |
146 local mount_opt="$4" | |
147 local offset="$(image_part_offset "$file" "$part_num")" || | |
148 image_die "failed to find partition #$part_num from: $file" | |
149 local size="$(image_part_size "$file" "$part_num")" || | |
150 image_die "failed to find partition #$part_num from: $file" | |
151 | |
152 if [ -z "$mount_opt" ]; then | |
153 # by default, mount as read-only. | |
154 mount_opt=",ro" | |
155 fi | |
156 | |
157 mount \ | |
158 -o "loop,offset=$((offset * 512)),sizelimit=$((size * 512)),$mount_opt" \ | |
159 "$file" \ | |
160 "$mount_point" | |
161 } | |
162 | |
163 # Unmounts a partition mount point by mount_partition | |
164 image_umount_partition() { | |
165 local mount_point="$1" | |
166 | |
167 umount -d "$mount_point" | |
168 } | |
OLD | NEW |