Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(225)

Unified Diff: image_common.sh

Issue 4824003: crosutils: refine memento image / factory package creation (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/crosutils.git@master
Patch Set: should not compress twice Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | make_factory_package.sh » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: image_common.sh
diff --git a/image_common.sh b/image_common.sh
new file mode 100644
index 0000000000000000000000000000000000000000..59cbc8446b97668d31a85ca5f99c683b9fa7dbb6
--- /dev/null
+++ b/image_common.sh
@@ -0,0 +1,106 @@
+#!/bin/bash
+
+# Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+# This script contains common utility function for dealing with image,
+# especially for being redistributed into platforms without complete Chromium OS
+# development environment.
+
+# Check if given command is available in current system
+has_command() {
+ type "$1" >/dev/null 2>&1
+}
+
+err_die() {
+ echo "ERROR: $@" >&2
+ exit 1
+}
+
+# Finds the best gzip compressor and invoke it.
+gzip_compress() {
+ if has_command pigz; then
+ # echo " ** Using parallel gzip **" >&2
+ # Tested with -b 32, 64, 128(default), 256, 1024, 16384, and -b 32 (max
+ # window size of Deflate) seems to be the best in output size.
+ pigz -b 32 "$@"
+ else
+ gzip "$@"
+ fi
+}
+
+# Finds if current system has tools for part_* commands
+has_part_tools() {
+ has_command cgpt || has_command parted
+}
+
+# Finds the best partition tool and print partition offset
+part_offset() {
+ local file="$1"
+ local partno="$2"
+
+ if has_command cgpt; then
+ cgpt show -b -i "$partno" "$file"
+ elif has_command parted; then
+ parted -m "$file" unit s print |
+ grep "^$partno:" | cut -d ':' -f 2 | sed 's/s$//'
+ else
+ exit 1
+ fi
+}
+
+# Finds the best partition tool and print partition size
+part_size() {
+ local file="$1"
+ local partno="$2"
+
+ if has_command cgpt; then
+ cgpt show -s -i "$partno" "$file"
+ elif has_command parted; then
+ parted -m "$file" unit s print |
+ grep "^$partno:" | cut -d ':' -f 4 | sed 's/s$//'
+ else
+ exit 1
+ fi
+}
+
+# Dumps a file by given offset and size (in sectors)
+dump_partial_file() {
+ local file="$1"
+ local offset="$2"
+ local sectors="$3"
+ local bs=512
+
+ # Try to use larger buffer if offset/size can be re-aligned.
+ # 2M / 512 = 4096
+ local buffer_ratio=4096
+ if [ $((offset % buffer_ratio)) -eq 0 -a \
+ $((sectors % buffer_ratio)) -eq 0 ]; then
+ offset=$((offset / buffer_ratio))
+ sectors=$((sectors / buffer_ratio))
+ bs=$((bs * buffer_ratio))
+ fi
+
+ if has_command pv; then
+ dd if="$file" bs=$bs skip="$offset" count="$sectors" \
+ oflag=sync status=noxfer 2>/dev/null |
+ pv -ptreb -B 4m -s $((sectors * $bs))
+ else
+ dd if="$file" bs=$bs skip="$offset" count="$sectors" \
+ oflag=sync status=noxfer 2>/dev/null
+ fi
+}
+
+# Dumps a specific partition from given image file
+dump_partition() {
+ local file="$1"
+ local part_num="$2"
+ local offset="$(part_offset "$file" "$part_num")" ||
+ err_die "failed to dump partition #$part_num from: $file"
+ local size="$(part_size "$file" "$part_num")" ||
+ err_die "failed to dump partition #$part_num from: $file"
+
+ dump_partial_file "$file" "$offset" "$size"
+}
+
« no previous file with comments | « no previous file | make_factory_package.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698