#!/bin/bash ############################################################################## # # # imginfo, (C) 2007-2010 S. Georgaras # # # # This file is part of Clonezilla-SysRescCD. # # # # Clonezilla-SysRescCD is free software: you can redistribute it and/or # # modify it under the terms of the GNU General Public License as published # # by the Free Software Foundation, either version 2 of the License, or # # (at your option) any later version. # # # # Clonezilla-SysRescCD is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License along # # with Clonezilla-SysRescCD. If not, see . # # # ############################################################################## # DEBUG=true versionNumber='0.2' function readImage(){ # set -x # n="${partElm[$oCount]}" n="$1" tmp="$(dirname "$n")" validateImage "$tmp" || { ((oCount++)) return 1 } img[$count]="$(basename "$tmp")" sizeInDisk[$count]=`du -hc "$(dirname "$n")" | sed -e '$!d' -e 's|[ \t].*||'`B diskModel[$count]="Disk "`head -n 1 "$(dirname "$n")"/*parted` [ -r "$(dirname "$n")"/disk ] && { hasDisk=1 type[$count]=disk tmp="$(dirname "$n")"/disk activeItem[$count]=$(cat "$tmp") partCount=$(cat "$(dirname "$n")"/parts | awk '{print NF}') tmp=$(dirname "$n") diskSizeFromImage[$count]=$(getDiskSizeFromImage "$tmp") if [ -z "$EXTENDED" ];then id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}"", size: ${diskSizeFromImage[$count]}, parts: $partCount" else id[$count]="Image: ${img[$count]}, image size: ${sizeInDisk[$count]}, ${type[$count]}: ${activeItem[$count]}"", size: ${diskSizeFromImage[$count]}, parts: $partCount, ${diskModel[$count]}" fi # scan parts pCount=1 tmpImg="${img[$count]}" while [ $pCount -le $partCount ];do ((count++)) img[$count]="$tmpImg" type[$count]=part activeItem[$count]=$(cat "$n" | awk '{for(i=1;i<=NF;i++) print $i}' | sed -n ""$pCount"p") calcInputSize "$tmp" "${activeItem[$count]}" imagePartitionSize[$count]="$THIS_INPUT_SIZE" partitionIdName[$count]=$(getPartitionIdNameFromImage "$tmp" "${activeItem[$count]}") ((pCount++)) id[$count]=" ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}" done } || { type[$count]=part activeItem[$count]=$(cat "$n") partCount=1 calcInputSize /"$tmp" "${activeItem[$count]}" imagePartitionSize[$count]="$THIS_INPUT_SIZE" partitionIdName[$count]=$(getPartitionIdNameFromImage "$tmp" "${activeItem[$count]}") if [ -z "$EXTENDED" ];then id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}" else id[$count]="Image: ${img[$count]}, image size: ${sizeInDisk[$count]}, ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}, ${diskModel[$count]}" fi } imgCount="$count" ((count++)) ((oCount++)) } function findImages(){ # <- folder to scan # # -> selectedInputImage - Number (index) in img array # -> selectedInputImageSize - Size of disk / partition # -> selectedInputImageID - Number - Partition ID (if user selected partition) # # -> imgCount - Number of images found # -> partCount - Number of partitions found # # -> img - image name # -> type - image type (partition/disk) # -> activeItem - name of partition/disk # -> imagePartitionSize - size of partition # -> diskSizeFromImage - disk size # -> partitionIdName - partition id in words # -> id - description imgCount=0 partCount=0 local count=1 local n local tmp local pCount local tmpImg local hasDisk=0 # set -x allParts="$(find -L "$1" -mindepth 2 -maxdepth 2 -name parts | sed 's|^\./||' 2>/dev/null)" psCount=1 partElm[$psCount]="$(echo "$allParts" | sed -n ""$psCount"p")" while [ -n "${partElm[$psCount]}" ];do ((psCount++)) partElm[$psCount]="$(echo "$allParts" | sed -n ""$psCount"p")" done oCount=1 while [ "$oCount" -lt "$psCount" ];do readImage "${partElm[$oCount]}" done # exit [ $imgCount -eq 0 ] &&{ local msgType [ -z "${CD[$selectedInputPartitionNumber]}" ] && msgType=Partition || msgType=CD-ROM if [ -n "$(echo "$selectedInputPartition" | grep '/')" ];then msgType=Folder selectedInputPartition=/"$selectedInputPartition" fi if [ -z "$onSubDirs" ];then setterm -bold on echo -n "No image files found in: " setterm -foreground magenta echo -n "$startDir" setterm -foreground white setterm -bold off echo ' ' fi exit 1 } setterm -bold on echo -n "Image files found in: " setterm -foreground magenta echo "$startDir" setterm -foreground white setterm -bold off count=1 until [ -z "${id[$count]}" ];do echo " ${id[$count]}" ((count++)) done } function getPartitionIdNameFromImage(){ local id=$(grep "$2" "$1"/*pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||') sfdisk -T |sed 's|^[ ]||' | grep -e "^$id[ \t]" | sed "s|^$id *||" } function getDiskSizeFromImage(){ local lin=$(awk 'BEGIN{FS="=" count=0} {if (count==0) b=$2 else{ b=b" * " b=b$2 } count++} END{print b}' "$1"/*chs.sf | bc) local size=$(echo "$lin * 512" | bc) toMGByte "$size" } function getDiskSizeFromDisk(){ local size=$(fdisk -l /dev/"$1" | grep -e '^Disk /dev/' | sed 's|.*, *||' | sed 's| *bytes||') toMGByte "$size" } function toMGByte(){ if [ $(expr length "$1") -le 9 ];then echo $(expr substr "$1" 1 3 )"MB" elif [ $(expr length "$1") -eq 10 ];then echo $(expr substr "$1" 1 2 | sed 's|\([0-9]\)$|.\1|')"GB" elif [ $(expr length "$1") -lt 12 ];then echo $(expr substr "$1" 1 3 | sed 's|\([0-9]\)$|.\1|')"GB" else echo $(expr substr "$1" 1 4 | sed 's|\([0-9]\)$|.\1|')"GB" fi } function validateImage(){ ######################################### # accept only disk images # ######################################### # [ -r "$(dirname "$n")"/disk ] && return 0 ######################################### [ -d "$1"/parts ] && return 1 local sf=$(find "$1" -name "*-chs.sf" 2>/dev/null | wc -l) [ $sf -eq 0 ] && return 1 local pt=$(find "$1" -name "*-pt.sf" 2>/dev/null | wc -l) [ $pt -eq 0 ] && return 1 # modification to read unsplited images # thanks to Viking (24/11/2008) # local aa=$(find "$1" -name "*.aa" 2>/dev/null | wc -l) # [ $aa -eq 0 ] && return 1 return 0 } function calcInputID(){ THIS_INPUT_ID=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||') } function calcInputSize(){ # set -x local bytesPerSector=512 local l local pSize local cylinders local heads local sectors if [ -z "$2" ];then cylinders=$(sed -n '/cylinders=/p' "$1"/*-chs.sf | sed 's|cylinders=||') heads=$(sed -n '/heads=/p' "$1"/*-chs.sf | sed 's|heads=||') sectors=$(sed -n '/sectors=/p' "$1"/*-chs.sf | sed 's|sectors=||') THIS_INPUT_SIZE=$(echo "scale=2; $cylinders * $heads * $sectors * $bytesPerSector" | bc) else pSize=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*size= *||' | sed 's|,.*||') THIS_INPUT_SIZE=$(echo "scale=2; $pSize * $bytesPerSector" | bc) fi l=$(expr length $THIS_INPUT_SIZE) if [ $l -gt 9 ];then THIS_INPUT_SIZE=$(echo "scale=2; $THIS_INPUT_SIZE / 1024 / 1024 / 1024" | bc)GB else THIS_INPUT_SIZE=$(echo "scale=3; $THIS_INPUT_SIZE / 1024 / 1024" | bc | sed 's|\.000|.00|')MB fi # set +x } function testRoot(){ if [ "$(whoami)" != "root" ];then printVersion echo "Error: This script must be executed by root..." exit fi } function printVersion(){ echo "Clonezilla Live Image Information $(basename $0) v. $versionNumber - (C) 2009-2010 S. Georgaras " } function printHelp(){ printVersion echo "Usage: $(basename $0) Available options: s Search in sub-directories too i [name] Pring info for image [name] e Print extended info (image size and disk model) v Print version info and exit h Print this screen and exit" } # # # Script starts here # # # set -x while getopts hvdsei: opt do case "$opt" in i) testRoot imgOnly=1 startDir="$OPTARG" ;; s) testRoot subDirs=1 ;; d) testRoot onSubDirs=1 ;; v) printVersion exit;; h) printHelp exit;; e) EXTENDED=y;; esac done shift $(( OPTIND - 1 )) testRoot # set -x [ -z "$startDir" ] && startDir="$1" if [ -z "$startDir" ];then startDir="$PWD" fi # remove ending / startDir="$(echo "$startDir" | sed 's|/$||')" # convert ./xxx -> /sss/ddd/xxx # xxx -> /sss/ddd/xxx if [ "$(echo "$startDir" |sed -n '/^\.\//p')" != "" ] || [ "$(echo "$startDir" | sed -n '/^\//p')" = "" ];then if [ -d "$PWD"/`echo "$startDir" | sed 's|\.*/||'` ];then startDir="$PWD"/`echo "$startDir" | sed 's|\.*/||'` pathFixed=1 elif [ -d /home/partimag/`echo "$startDir" | sed 's|\.*/||'` ];then startDir=/home/partimag/`echo "$startDir" | sed 's|\.*/||'` pathFixed=1 fi fi if [ ! -d "$startDir" ];then printVersion echo "Error: Folder \"$( basename "$startDir")\" not found!!!" exit 1 fi if [ -z "$imgOnly" ];then findImages "$startDir" if [ ! -z "$subDirs" ];then for n in "$startDir"/*;do if [ -d "$n" ] && [ -r "$n" ];then "$0" -d "$n" fi done fi else readImage "$startDir"/parts count=0 if [ -z "${id[$count]}" ];then printVersion echo "Error: Image \"$startDir\" does not exist!!!" exit 1 fi until [ -z "${id[$count]}" ];do echo " ${id[$count]}" ((count++)) done fi