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 pigz "$@" |
| 26 else |
| 27 gzip "$@" |
| 28 fi |
| 29 } |
| 30 |
| 31 # Finds if current system has tools for part_* commands |
| 32 has_part_tools() { |
| 33 has_command cgpt || has_command parted |
| 34 } |
| 35 |
| 36 # Finds the best partition tool and print partition offset |
| 37 part_offset() { |
| 38 local file="$1" |
| 39 local partno="$2" |
| 40 |
| 41 if has_command cgpt; then |
| 42 cgpt show -b -i "$partno" "$file" |
| 43 elif has_command parted; then |
| 44 parted -m "$file" unit s print | |
| 45 grep "^$partno:" | cut -d ':' -f 2 | sed 's/s$//' |
| 46 else |
| 47 exit 1 |
| 48 fi |
| 49 } |
| 50 |
| 51 # Finds the best partition tool and print partition size |
| 52 part_size() { |
| 53 local file="$1" |
| 54 local partno="$2" |
| 55 |
| 56 if has_command cgpt; then |
| 57 cgpt show -s -i "$partno" "$file" |
| 58 elif has_command parted; then |
| 59 parted -m "$file" unit s print | |
| 60 grep "^$partno:" | cut -d ':' -f 4 | sed 's/s$//' |
| 61 else |
| 62 exit 1 |
| 63 fi |
| 64 } |
| 65 |
| 66 # Dumps a file by given offset and size (in sectors) |
| 67 dump_partial_file() { |
| 68 local file="$1" |
| 69 local offset="$2" |
| 70 local sectors="$3" |
| 71 local bs=512 |
| 72 |
| 73 # Try to use larger buffer if offset/size can be re-aligned. |
| 74 # 2M / 512 = 4096 |
| 75 local buffer_ratio=4096 |
| 76 if [ $((offset % buffer_ratio)) -eq 0 -a \ |
| 77 $((sectors % buffer_ratio)) -eq 0 ]; then |
| 78 offset=$((offset / buffer_ratio)) |
| 79 sectors=$((sectors / buffer_ratio)) |
| 80 bs=$((bs * buffer_ratio)) |
| 81 fi |
| 82 |
| 83 if has_command pv; then |
| 84 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
| 85 oflag=sync status=noxfer 2>/dev/null | |
| 86 pv -ptreb -B 4m -s $((sectors * $bs)) |
| 87 else |
| 88 dd if="$file" bs=$bs skip="$offset" count="$sectors" \ |
| 89 oflag=sync status=noxfer 2>/dev/null |
| 90 fi |
| 91 } |
| 92 |
| 93 # Dumps a specific partition from given image file |
| 94 dump_partition() { |
| 95 local file="$1" |
| 96 local part_num="$2" |
| 97 local offset="$(part_offset "$file" "$part_num")" || |
| 98 err_die "failed to dump partition #$part_num from: $file" |
| 99 local size="$(part_size "$file" "$part_num")" || |
| 100 err_die "failed to dump partition #$part_num from: $file" |
| 101 |
| 102 dump_partial_file "$file" "$offset" "$size" |
| 103 } |
| 104 |
OLD | NEW |