1 |
#!/bin/bash |
2 |
############################################################################## |
3 |
# # |
4 |
# imginfo, (C) 2007-2010 S. Georgaras <sng@hellug.gr> # |
5 |
# # |
6 |
# This file is part of Clonezilla-SysRescCD. # |
7 |
# # |
8 |
# Clonezilla-SysRescCD is free software: you can redistribute it and/or # |
9 |
# modify it under the terms of the GNU General Public License as published # |
10 |
# by the Free Software Foundation, either version 2 of the License, or # |
11 |
# (at your option) any later version. # |
12 |
# # |
13 |
# Clonezilla-SysRescCD is distributed in the hope that it will be useful, # |
14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of # |
15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
16 |
# GNU General Public License for more details. # |
17 |
# # |
18 |
# You should have received a copy of the GNU General Public License along # |
19 |
# with Clonezilla-SysRescCD. If not, see <http://www.gnu.org/licenses/>. # |
20 |
# # |
21 |
############################################################################## |
22 |
# DEBUG=true |
23 |
versionNumber='0.1' |
24 |
|
25 |
function readImage(){ |
26 |
# set -x |
27 |
# n="${partElm[$oCount]}" |
28 |
n="$1" |
29 |
tmp="$(dirname "$n")" |
30 |
validateImage "$tmp" || { |
31 |
((oCount++)) |
32 |
return 1 |
33 |
} |
34 |
img[$count]="$(basename "$tmp")" |
35 |
|
36 |
[ -r "$(dirname "$n")"/disk ] && { |
37 |
hasDisk=1 |
38 |
type[$count]=disk |
39 |
tmp="$(dirname "$n")"/disk |
40 |
activeItem[$count]=$(cat "$tmp") |
41 |
partCount=$(cat "$(dirname "$n")"/parts | awk '{print NF}') |
42 |
tmp=$(dirname "$n") |
43 |
diskSizeFromImage[$count]=$(getDiskSizeFromImage "$tmp") |
44 |
id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}"", size: ${diskSizeFromImage[$count]}, parts: $partCount" |
45 |
# scan parts |
46 |
pCount=1 |
47 |
tmpImg="${img[$count]}" |
48 |
while [ $pCount -le $partCount ];do |
49 |
((count++)) |
50 |
img[$count]="$tmpImg" |
51 |
type[$count]=part |
52 |
activeItem[$count]=$(cat "$n" | awk '{for(i=1;i<=NF;i++) print $i}' | sed -n ""$pCount"p") |
53 |
calcInputSize "$tmp" "${activeItem[$count]}" |
54 |
imagePartitionSize[$count]="$THIS_INPUT_SIZE" |
55 |
partitionIdName[$count]=$(getPartitionIdNameFromImage "$tmp" "${activeItem[$count]}") |
56 |
((pCount++)) |
57 |
id[$count]=" ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}" |
58 |
done |
59 |
} || { |
60 |
type[$count]=part |
61 |
activeItem[$count]=$(cat "$n") |
62 |
partCount=1 |
63 |
calcInputSize /"$tmp" "${activeItem[$count]}" |
64 |
imagePartitionSize[$count]="$THIS_INPUT_SIZE" |
65 |
partitionIdName[$count]=$(getPartitionIdNameFromImage "$tmp" "${activeItem[$count]}") |
66 |
id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}" |
67 |
} |
68 |
imgCount="$count" |
69 |
((count++)) |
70 |
((oCount++)) |
71 |
} |
72 |
|
73 |
|
74 |
function findImages(){ |
75 |
# <- folder to scan |
76 |
# |
77 |
# -> selectedInputImage - Number (index) in img array |
78 |
# -> selectedInputImageSize - Size of disk / partition |
79 |
# -> selectedInputImageID - Number - Partition ID (if user selected partition) |
80 |
# |
81 |
# -> imgCount - Number of images found |
82 |
# -> partCount - Number of partitions found |
83 |
# |
84 |
# -> img - image name |
85 |
# -> type - image type (partition/disk) |
86 |
# -> activeItem - name of partition/disk |
87 |
# -> imagePartitionSize - size of partition |
88 |
# -> diskSizeFromImage - disk size |
89 |
# -> partitionIdName - partition id in words |
90 |
# -> id - description |
91 |
|
92 |
imgCount=0 |
93 |
partCount=0 |
94 |
|
95 |
local count=1 |
96 |
local n |
97 |
local tmp |
98 |
local pCount |
99 |
local tmpImg |
100 |
local hasDisk=0 |
101 |
# set -x |
102 |
|
103 |
allParts="$(find -L "$1" -mindepth 2 -maxdepth 2 -name parts | sed 's|^\./||' 2>/dev/null)" |
104 |
|
105 |
psCount=1 |
106 |
partElm[$psCount]="$(echo "$allParts" | sed -n ""$psCount"p")" |
107 |
while [ -n "${partElm[$psCount]}" ];do |
108 |
((psCount++)) |
109 |
partElm[$psCount]="$(echo "$allParts" | sed -n ""$psCount"p")" |
110 |
done |
111 |
|
112 |
oCount=1 |
113 |
while [ "$oCount" -lt "$psCount" ];do |
114 |
readImage "${partElm[$oCount]}" |
115 |
done |
116 |
# exit |
117 |
[ $imgCount -eq 0 ] &&{ |
118 |
local msgType |
119 |
[ -z "${CD[$selectedInputPartitionNumber]}" ] && msgType=Partition || msgType=CD-ROM |
120 |
if [ -n "$(echo "$selectedInputPartition" | grep '/')" ];then |
121 |
msgType=Folder |
122 |
selectedInputPartition=/"$selectedInputPartition" |
123 |
fi |
124 |
|
125 |
if [ -z "$onSubDirs" ];then |
126 |
setterm -bold on |
127 |
echo -n "No image files found in: " |
128 |
setterm -foreground magenta |
129 |
echo -n "$startDir" |
130 |
setterm -foreground white |
131 |
setterm -bold off |
132 |
echo ' ' |
133 |
fi |
134 |
exit 1 |
135 |
} |
136 |
|
137 |
|
138 |
setterm -bold on |
139 |
echo -n "Image files found in: " |
140 |
setterm -foreground magenta |
141 |
echo "$startDir" |
142 |
setterm -foreground white |
143 |
setterm -bold off |
144 |
count=1 |
145 |
until [ -z "${id[$count]}" ];do |
146 |
echo " ${id[$count]}" |
147 |
((count++)) |
148 |
done |
149 |
} |
150 |
|
151 |
function getPartitionIdNameFromImage(){ |
152 |
local id=$(grep "$2" "$1"/*pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||') |
153 |
sfdisk -T |sed 's|^[ ]||' | grep -e "^$id[ \t]" | sed "s|^$id *||" |
154 |
} |
155 |
|
156 |
function getDiskSizeFromImage(){ |
157 |
local lin=$(awk 'BEGIN{FS="=" |
158 |
count=0} |
159 |
{if (count==0) b=$2 |
160 |
else{ |
161 |
b=b" * " |
162 |
b=b$2 |
163 |
} |
164 |
count++} |
165 |
END{print b}' "$1"/*chs.sf | bc) |
166 |
local size=$(echo "$lin * 512" | bc) |
167 |
toMGByte "$size" |
168 |
} |
169 |
|
170 |
function getDiskSizeFromDisk(){ |
171 |
local size=$(fdisk -l /dev/"$1" | grep -e '^Disk /dev/' | sed 's|.*, *||' | sed 's| *bytes||') |
172 |
toMGByte "$size" |
173 |
} |
174 |
|
175 |
function toMGByte(){ |
176 |
if [ $(expr length "$1") -le 9 ];then |
177 |
echo $(expr substr "$1" 1 3 )"MB" |
178 |
elif [ $(expr length "$1") -eq 10 ];then |
179 |
echo $(expr substr "$1" 1 2 | sed 's|\([0-9]\)$|.\1|')"GB" |
180 |
elif [ $(expr length "$1") -lt 12 ];then |
181 |
echo $(expr substr "$1" 1 3 | sed 's|\([0-9]\)$|.\1|')"GB" |
182 |
else |
183 |
echo $(expr substr "$1" 1 4 | sed 's|\([0-9]\)$|.\1|')"GB" |
184 |
fi |
185 |
} |
186 |
|
187 |
function validateImage(){ |
188 |
######################################### |
189 |
# accept only disk images # |
190 |
######################################### |
191 |
# [ -r "$(dirname "$n")"/disk ] && return 0 |
192 |
######################################### |
193 |
[ -d "$1"/parts ] && return 1 |
194 |
local sf=$(find "$1" -name "*-chs.sf" 2>/dev/null | wc -l) |
195 |
[ $sf -eq 0 ] && return 1 |
196 |
local pt=$(find "$1" -name "*-pt.sf" 2>/dev/null | wc -l) |
197 |
[ $pt -eq 0 ] && return 1 |
198 |
# modification to read unsplited images |
199 |
# thanks to Viking (24/11/2008) |
200 |
# local aa=$(find "$1" -name "*.aa" 2>/dev/null | wc -l) |
201 |
# [ $aa -eq 0 ] && return 1 |
202 |
return 0 |
203 |
} |
204 |
|
205 |
function calcInputID(){ |
206 |
THIS_INPUT_ID=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||') |
207 |
} |
208 |
|
209 |
function calcInputSize(){ |
210 |
# set -x |
211 |
local bytesPerSector=512 |
212 |
local l |
213 |
local pSize |
214 |
local cylinders |
215 |
local heads |
216 |
local sectors |
217 |
if [ -z "$2" ];then |
218 |
cylinders=$(sed -n '/cylinders=/p' "$1"/*-chs.sf | sed 's|cylinders=||') |
219 |
heads=$(sed -n '/heads=/p' "$1"/*-chs.sf | sed 's|heads=||') |
220 |
sectors=$(sed -n '/sectors=/p' "$1"/*-chs.sf | sed 's|sectors=||') |
221 |
THIS_INPUT_SIZE=$(echo "scale=2; $cylinders * $heads * $sectors * $bytesPerSector" | bc) |
222 |
else |
223 |
pSize=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*size= *||' | sed 's|,.*||') |
224 |
THIS_INPUT_SIZE=$(echo "scale=2; $pSize * $bytesPerSector" | bc) |
225 |
fi |
226 |
|
227 |
l=$(expr length $THIS_INPUT_SIZE) |
228 |
if [ $l -gt 9 ];then |
229 |
THIS_INPUT_SIZE=$(echo "scale=2; $THIS_INPUT_SIZE / 1024 / 1024 / 1024" | bc)GB |
230 |
else |
231 |
THIS_INPUT_SIZE=$(echo "scale=3; $THIS_INPUT_SIZE / 1024 / 1024" | bc | sed 's|\.000|.00|')MB |
232 |
fi |
233 |
# set +x |
234 |
} |
235 |
|
236 |
function testRoot(){ |
237 |
if [ "$(whoami)" != "root" ];then |
238 |
printVersion |
239 |
echo "Error: This script must be executed by root..." |
240 |
exit |
241 |
fi |
242 |
} |
243 |
|
244 |
function printVersion(){ |
245 |
echo "Clonezilla Live Image Information |
246 |
$(basename $0) v. $versionNumber - (C) 2009 S. Georgaras <sng@hellug.gr> |
247 |
" |
248 |
} |
249 |
|
250 |
function printHelp(){ |
251 |
printVersion |
252 |
echo "Usage: $(basename $0) <options> <directory> |
253 |
|
254 |
Available options: |
255 |
s Search in sub-directories too |
256 |
i [name] Pring info for image [name] |
257 |
v Print version info and exit |
258 |
h Print this screen and exit" |
259 |
|
260 |
} |
261 |
|
262 |
# |
263 |
# |
264 |
# Script starts here |
265 |
# |
266 |
# |
267 |
# set -x |
268 |
while getopts hvdsi: opt |
269 |
do |
270 |
case "$opt" in |
271 |
i) |
272 |
testRoot |
273 |
imgOnly=1 |
274 |
startDir="$OPTARG" |
275 |
;; |
276 |
s) |
277 |
testRoot |
278 |
subDirs=1 |
279 |
;; |
280 |
d) |
281 |
testRoot |
282 |
onSubDirs=1 |
283 |
;; |
284 |
v) |
285 |
printVersion |
286 |
exit;; |
287 |
h) |
288 |
printHelp |
289 |
exit;; |
290 |
esac |
291 |
done |
292 |
shift $(( OPTIND - 1 )) |
293 |
|
294 |
testRoot |
295 |
# set -x |
296 |
[ -z "$startDir" ] && startDir="$1" |
297 |
if [ -z "$startDir" ];then |
298 |
startDir="$PWD" |
299 |
fi |
300 |
|
301 |
# remove ending / |
302 |
startDir="$(echo "$startDir" | sed 's|/$||')" |
303 |
|
304 |
# convert ./xxx -> /sss/ddd/xxx |
305 |
# xxx -> /sss/ddd/xxx |
306 |
if [ "$(echo "$startDir" |sed -n '/^\.\//p')" != "" ] || [ "$(echo "$startDir" | sed -n '/^\//p')" = "" ];then |
307 |
if [ -d "$PWD"/`echo "$startDir" | sed 's|\.*/||'` ];then |
308 |
startDir="$PWD"/`echo "$startDir" | sed 's|\.*/||'` |
309 |
pathFixed=1 |
310 |
elif [ -d /home/partimag/`echo "$startDir" | sed 's|\.*/||'` ];then |
311 |
startDir=/home/partimag/`echo "$startDir" | sed 's|\.*/||'` |
312 |
pathFixed=1 |
313 |
fi |
314 |
fi |
315 |
|
316 |
if [ ! -d "$startDir" ];then |
317 |
printVersion |
318 |
echo "Error: Folder \"$( basename "$startDir")\" not found!!!" |
319 |
exit 1 |
320 |
fi |
321 |
|
322 |
if [ -z "$imgOnly" ];then |
323 |
findImages "$startDir" |
324 |
if [ ! -z "$subDirs" ];then |
325 |
for n in "$startDir"/*;do |
326 |
if [ -d "$n" ] && [ -r "$n" ];then |
327 |
"$0" -d "$n" |
328 |
fi |
329 |
done |
330 |
fi |
331 |
else |
332 |
readImage "$startDir"/parts |
333 |
count=0 |
334 |
if [ -z "${id[$count]}" ];then |
335 |
printVersion |
336 |
echo "Error: Image \"$startDir\" does not exist!!!" |
337 |
exit 1 |
338 |
fi |
339 |
until [ -z "${id[$count]}" ];do |
340 |
echo " ${id[$count]}" |
341 |
((count++)) |
342 |
done |
343 |
fi |