From 376e8f084be7e8b39f01c95016dc21b4e6fac70c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 6 Feb 2017 18:36:17 +0100 Subject: mount-nv: move the image analysis code to its own function get_image_type_from_dump() now takes all image bytes in arguments and returns the verdict. It will make it easier to call it multiple times. --- scripts/mount-nv | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/scripts/mount-nv b/scripts/mount-nv index 2de6544..2c2ce3f 100755 --- a/scripts/mount-nv +++ b/scripts/mount-nv @@ -84,15 +84,12 @@ get_part_size() { } -# Returns in $REPLY the identified type of the image located on the device in -# $1, and returns zero. If the device cannot be read, 1 is returned and REPLY -# is left undefined. A read access is made to the device. Since identifying -# ext2 requires at least 1082 bytes, we read all that at once. -get_image_type() { - local dev=$1 - +# Returns in $REPLY the identified type based on the beginning of the image +# consisting in hexadecimal bytes in "$*". Up to 1082 args may be needed! In +# case of success, it stores the response in REPLY and returns zero. If the +# image cannot be identified, 1 is returned and REPLY is left undefined. +get_image_type_from_dump() { REPLY="" - set -- $(od -v -An -tx1 -N1082 2>/dev/null < "$dev") case "${1}${2}${3}${4}" in "") return 1 ;; ffffffff) REPLY="empty" ;; @@ -139,6 +136,22 @@ get_image_type() { return 1 } +# Returns in $REPLY the identified type of the image located on the device in +# $1, and returns zero. If the device cannot be read, 1 is returned and REPLY +# is left undefined. A read access is made to the device. Since identifying +# ext2 requires at least 1082 bytes, we read all that at once. +get_image_type() { + local dev=$1 + + REPLY="" + get_image_type_from_dump $(od -v -An -tx1 -N1082 2>/dev/null < "$dev") + + [ -n "$REPLY" ] && return 0 + + # not identified + return 1 +} + # usage: $0 [$arg] usage() { [ -n "$1" ] && echo "Unknown argument: $1" >&2 -- 1.7.12.1