OLD | NEW |
(Empty) | |
| 1 #!/bin/bash |
| 2 |
| 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 |
| 5 # found in the LICENSE file. |
| 6 |
| 7 # This script contains common utility function for dealing with image, |
| 8 # especially for being redistributed into platforms without complete Chromium OS |
| 9 # development environment. |
| 10 |
| 11 # Check if given command is available in current system |
| 12 has_command() { |
| 13 type "$1" >/dev/null 2>&1 |
| 14 } |
| 15 |
| 16 err_die() { |
| 17 echo "ERROR: $@" >&2 |
| 18 exit 1 |
| 19 } |
| 20 |
| 21 # Finds the best gzip compressor and invoke it. |
| 22 gzip_compress() { |
| 23 if has_command pigz; then |
| 24 # echo " ** Using parallel gzip **" >&2 |
| 25 # 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 pigz -b 32 "$@" |
| 28 else |
| 29 gzip "$@" |
| 30 fi |
| 31 } |
| 32 |
| 33 # Finds if current system has tools for part_* commands |
| 34 has_part_tools() { |
| 35 has_command cgpt || has_command parted |
| 36 } |
| 37 |
| 38 # Finds the best partition tool and print partition offset |
| 39 part_offset() { |
| 40 local file="$1" |
| 41 local partno="$2" |
| 42 |
| 43 if has_command cgpt; then |
| 44 cgpt show -b -i "$partno" "$file" |
| 45 elif has_command parted; then |
| 46 parted -m "$file" unit s print | |
| 47 grep "^$partno:" | cut -d ':' -f 2 | sed 's/s$//' |
| 48 else |
| 49 exit 1 |
| 50 fi |
| 51 } |
| 52 |
| 53 # Finds the best partition tool and print partition size |
| 54 part_size() { |
| 55 local file="$1" |
| 56 local partno="$2" |
| 57 |
| 58 if has_command cgpt; then |
| 59 cgpt show -s -i "$partno" "$file" |
| 60 elif has_command parted; then |
| 61 parted -m "$file" unit s print | |
| 62 grep "^$partno:" | cut -d ':' -f 4 | sed 's/s$//' |
| 63 else |
| 64 exit 1 |
| 65 fi |
| 66 } |
| 67 |
| 68 # Dumps a file by given offset and size (in sectors) |
| 69 dump_partial_file() { |
| 70 local file="$1" |
| 71 local offset="$2" |
| 72 local sectors="$3" |
| 73 local bs=512 |
| 74 |
| 75 # Try to use larger buffer if offset/size can be re-aligned. |
| 76 # 2M / 512 = 4096 |
| 77 local buffer_ratio=4096 |
| 78 if [ $((offset % buffer_ratio)) -eq 0 -a \ |
| 79 $((sectors % buffer_ratio)) -eq 0 ]; then |
| 80 offset=$((offset / buffer_ratio)) |
| 81 sectors=$((sectors / buffer_ratio)) |
| 82 bs=$((bs * buffer_ratio)) |
| 83 fi |
| 84 |
| 85 if has_command pv; then |
| 86 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
| 87 oflag=sync status=noxfer 2>/dev/null | |
| 88 pv -ptreb -B 4m -s $((sectors * $bs)) |
| 89 else |
| 90 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
| 91 oflag=sync status=noxfer 2>/dev/null |
| 92 fi |
| 93 } |
| 94 |
| 95 # Dumps a specific partition from given image file |
| 96 dump_partition() { |
| 97 local file="$1" |
| 98 local part_num="$2" |
| 99 local offset="$(part_offset "$file" "$part_num")" || |
| 100 err_die "failed to dump partition #$part_num from: $file" |
| 101 local size="$(part_size "$file" "$part_num")" || |
| 102 err_die "failed to dump partition #$part_num from: $file" |
| 103 |
| 104 dump_partial_file "$file" "$offset" "$size" |
| 105 } |
| 106 |
OLD | NEW |