/[clonezilla-sysresccd]/trunk/create-clonezilla-sysresccd/files/reloc-img
ViewVC logotype

Annotation of /trunk/create-clonezilla-sysresccd/files/reloc-img

Parent Directory Parent Directory | Revision Log Revision Log


Revision 48 - (hide annotations)
Thu May 20 12:38:55 2010 UTC (13 years, 10 months ago) by sng
File size: 45524 byte(s)
- adding isofiles directory
- fixing license info on scripts

1 sng 47 #!/bin/bash
2 sng 48 ##############################################################################
3     # #
4     # reloc-img, (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    
23 sng 47 # DEBUG=true
24     versionNumber='0.7'
25    
26     function findDirectories(){
27     #
28     # <- selectedInputPartition
29     #
30     # -> activeDirectory - selected directory
31     #
32    
33     local lines
34     local dialogLines
35     local cur=1
36     local cmdLine=''
37     local thisLine
38    
39     local allDirs="$(find "$1" -maxdepth 1 -type d | sed 's|^\.$|/|' | sed '/ /d' | sed 's|^\./||' | sed "s|$1/||" | sed "s|$1||" | sed 's|^$|/|' | sort)"
40     lines="$(echo "$allDirs" | wc -l | sed 's|[ ]*||g')"
41     if [ "$lines" = "" ] || [ "$lines" = "0" ];then
42     activeDirectory='/'
43     return
44     fi
45    
46    
47     while [ "$cur" -le "$lines" ];do
48     thisLine="$(echo "$allDirs" | sed -n "$cur"p)"
49     cmdLine="$cmdLine"\"$thisLine\"" "
50     if [ "$thisLine" = "/" ];then
51     cmdLine="$cmdLine"" \"Top directory in the local device\" "
52     else
53     cmdLine="$cmdLine"\""$(ls -lad "$1"/"$thisLine" | awk '{print $6 " " $7}')\" "
54     fi
55     ((cur++))
56     done
57    
58     # echo "$cmdLine"
59     # exit
60    
61     if [ "$lines" -gt 10 ];then
62     dialogLines=10
63     else
64     if [ "$lines" -lt 4 ];then
65     dialogLines=4
66     else
67     dialogLines="$lines"
68     fi
69     fi
70    
71     echo "dialog --backtitle \"$backTitle\" --title \" Select directory \" --menu 'Please select the directory that holds your images' $((dialogLines+7)) 78 $dialogLines $cmdLine" > /root/dialog.$$
72    
73     . /root/dialog.$$ 2>/root/selectedDirectory.$$
74    
75     activeDirectory=$(cat /root/selectedDirectory.$$)
76    
77     rm /root/dialog.$$
78     }
79    
80     function findSystemPartitions(){
81     # <- NONE
82     #
83     # -> selectedInputPartition - the partition to scan for images
84     # -> selectedInputPartitionNumber - Index of selectedInputPartition in inputPartition array
85     #
86     # -> diskCount - Number of disks on the system
87     # -> inputDisk - Disks present on the system (array)
88     # -> diskSize - Size of each disk (array)
89     #
90     # -> partitionCount - Number of partitions on the system
91     # -> inputPartition - Partitions present on the system (array)
92     # -> partitionSize - The size of each partition (array)
93     # -> partitionType - The type of each partition (array)
94     # -> CD - Not empty if partition is a CD (array)
95     #
96     allDisks=$(cat /proc/partitions | sed 's|.* ||' | grep -ve '[0-9]$'| grep -v name |grep -ve '^$')
97    
98     [ -z "$allDisks" ] && {
99     dialog --backtitle "$backTitle" --title " No disks found!!! " --msgbox "There seems to be no disk connected to your system, therefore no restoration is possible" 7 70
100     cleanUP
101     exit 1
102     }
103    
104    
105     local cmdLine=''
106     diskCount=$(echo "$allDisks" | wc -l)
107    
108     local fDisks=''
109    
110     local count=1
111     while [ $count -le $diskCount ];do
112     inputDisk[$count]=$(echo "$allDisks" | sed -n ""$count"p")
113     fdisk -l /dev/"${inputDisk[$count]}" 2>/dev/null > /root/clsys-raw-fdisk-"$count".$$
114     # parted /dev/"${inputDisk[$count]}" print > /root/clsys-raw-parted-"$count".$$
115     diskSize[$count]=$(sed -n '1p' /root/clsys-raw-fdisk-"$count".$$ | sed 's|.*, ||' | sed 's| bytes||' | sed 's| ||g')
116     cat /root/clsys-raw-fdisk-"$count".$$ | grep -e '^/' | grep -v "Ext'd" | grep -ve "[Ee]xtended" | grep -ve '[sS][wW][aA][pP]' > /root/clsys-part-"$count".$$
117     ((count++))
118     done
119     # echo 33
120    
121     allPartitions=$(cat /root/clsys-part-*$$ | sed 's| .*||' | sed 's|/dev/||')
122     # allSizes=$(grep -e '^ [0-9]' /root/clsys-raw-parted-*.$$ | awk '{print $4}')
123     partitionCount=$(echo "$allPartitions" | wc -l)
124    
125     local cmdLine=''
126     count=1
127     while [ $count -le $partitionCount ];do
128     inputPartition[$count]=$(echo "$allPartitions" | sed -n ""$count"p")
129     getPartitionSizeFromFdisk "${inputPartition[$count]}"
130     partitionSize[$count]=$( echo "$THIS_PARTITION_SIZE" | sed 's|,|.|')
131     partitionType[$count]=$(grep "${inputPartition[$count]}" /root/clsys-raw-fdisk-*.$$| sed 's|*||' | awk '{printf "%s %s %s %s",$6,$7,$8,$9}' |sed 's| *$||')
132     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]}\""" "
133    
134     ((count++))
135     done
136    
137     # cleanUP
138     # exit
139    
140     # integrate CD-ROMs
141     local theCDs=$(cat /proc/sys/dev/cdrom/info | grep 'drive name' |sed 's|drive name:[ \t]*||' | awk '{for(i=1;i<=NF;i++) print $i}')
142     local cdCount=$(echo "$theCDs" | wc -l)
143    
144     count=1
145     while [ $count -le $cdCount ];do
146     ((partitionCount++))
147     inputPartition[$partitionCount]=$(echo "$theCDs" | sed -n ""$count"p")
148     CD[$partitionCount]=$count
149     cmdLine="$cmdLine""${inputPartition[$partitionCount]}"" ""\"Type: CD-ROM\""" "
150     ((count++))
151     done
152    
153     # integrate samba shares (cifs)
154     local smbShares=$(mount | sort | grep -e '[ ]cifs[ ]'| sed 's|on ||' |sed 's|type ||' | awk '$3 == "cifs" {print $2}')
155     #### DEBUG ####
156     [ -z "$DEBUG" ] || smbShares='/data'
157     #### END DEBUG ####
158    
159     [ -z "$smbShares" ] || {
160     local smbCount=$(echo "$smbShares" | wc -l)
161     count=1
162     while [ $count -le $smbCount ];do
163     ((partitionCount++))
164     inputPartition[$partitionCount]=$(echo "$smbShares" | sed -n ""$count"p")
165     SMB[$count]=$partitionCount
166     cmdLine="$cmdLine"smb"$count"" ""\"Type: Samba Share, $(echo "${inputPartition[$partitionCount]}"|sed 's|.*/||')\""" "
167     ((count++))
168     done
169     }
170    
171    
172     local nfsShares=$(mount | sort | grep -e '[ ]nfs[ ]'| sed 's|on ||' |sed 's|type ||' | awk '$3 == "nfs" {print $2}')
173     #### DEBUG ####
174     [ -z "$DEBUG" ] || nfsShares='/data'
175     #### END DEBUG ####
176     [ -z "$nfsShares" ] || {
177     local nfsCount=$(echo "$nfsShares" | wc -l)
178     count=1
179     while [ $count -le $nfsCount ];do
180     ((partitionCount++))
181     inputPartition[$partitionCount]=$(echo "$nfsShares" | sed -n ""$count"p")
182     NFS[$count]=$partitionCount
183     cmdLine="$cmdLine"nfs"$count"" ""\"Type: NFS Share, $(echo "${inputPartition[$partitionCount]}"|sed 's|.*/||')\""" "
184     ((count++))
185     done
186     }
187     # echo 66
188     selectedInputPartition=''
189    
190     echo "dialog --backtitle \"$backTitle\" --title \" Source Partition \" --menu 'Please select the location of your image file' $((partitionCount+7)) 78 $partitionCount $cmdLine" > /root/dialog.$$
191    
192     . /root/dialog.$$ 2>/root/selectedPartition.$$
193    
194     selectedInputPartition=$(cat /root/selectedPartition.$$)
195    
196     # set -x
197     # cleanUP
198     # exit
199    
200    
201     [ -z "$selectedInputPartition" ] && {
202     cleanUP
203     exit
204     }
205    
206     if [ ! -z $(echo "$selectedInputPartition"|grep -e '^smb[0-9][0-9]*$') ];then
207     local num=$(echo "$selectedInputPartition"|sed 's|smb||')
208     selectedInputPartitionNumber=${SMB[$num]}
209     workingRemote='y'
210     elif [ ! -z $(echo "$selectedInputPartition"|grep -e '^smb[0-9][0-9]*$') ];then
211     local num=$(echo "$selectedInputPartition"|sed 's|nfs||')
212     selectedInputPartitionNumber=${NFS[$num]}
213     workingRemote='y'
214     else
215     count=1
216     while [ "${inputPartition[$count]}" != "$selectedInputPartition" ];do
217     ((count++))
218     done
219     selectedInputPartitionNumber=$count
220     fi
221     # echo 100
222     # echo EOF
223     # read
224     # cleanUP
225     # exit
226    
227     }
228    
229     function findSambaShare(){
230     # used only when -i smb
231     echo 'Looking for Samba Shares (this may take some time)... '
232     partitionCount=0
233     local smbShares=$(mount | sort | awk '/cifs/ {
234     for(x=1; x<=NF; ++x){
235     s=substr($x,1,2)
236     if(s!="//"){
237     s=substr($x,1,1)
238     if(s=="/") mountpoint=$x
239     }if($x=="cifs"){
240     if(mountpoint!="") print mountpoint
241     }}}')
242     #### DEBUG ####
243     [ -z "$DEBUG" ] || smbShares='/data'
244     #### END DEBUG ####
245     [ -z "$smbShares" ] && {
246     echo
247     echo "No Samba Shares mounted"
248     exit 1
249     }
250     local smbCount=$(echo "$nfsShares" | wc -l)
251     count=1
252     while [ $count -le $smbCount ];do
253     ((partitionCount++))
254     inputPartition[$partitionCount]=$(echo "$smbShares" | sed -n ""$count"p")
255     SMB[$count]=$partitionCount
256     ((count++))
257     done
258     local num=1
259     selectedInputPartitionNumber=${SMB[$num]}
260     echo
261     }
262    
263     function findNFSShare(){
264     # used only when -i nfs
265     echo 'Looking for NFS Shares (this may take some time)... '
266     partitionCount=0
267     local nfsShares=$(mount | sort | awk '/nfs/ {
268     for(x=1; x<=NF; ++x){
269     s=substr($x,1,2)
270     if(s!="//"){
271     s=substr($x,1,1)
272     if(s=="/") mountpoint=$x
273     }if($x=="nfs"){
274     if(mountpoint!="") print mountpoint
275     }}}')
276     #### DEBUG ####
277     [ -z "$DEBUG" ] || nfsShares='/data'
278     #### END DEBUG ####
279     [ -z "$nfsShares" ] && {
280     echo
281     echo "No NFS Shares mounted"
282     exit 1
283     }
284    
285     local nfsCount=$(echo "$nfsShares" | wc -l)
286     count=1
287     while [ $count -le $nfsCount ];do
288     ((partitionCount++))
289     inputPartition[$partitionCount]=$(echo "$nfsShares" | sed -n ""$count"p")
290     NFS[$count]=$partitionCount
291     ((count++))
292     done
293     local num=1
294     selectedInputPartitionNumber=${NFS[$num]}
295     echo
296     }
297    
298    
299     function findImages(){
300     # <- folder to scan
301     #
302     # -> selectedInputImage - Number (index) in img array
303     # -> selectedInputImageSize - Size of disk / partition
304     # -> selectedInputImageID - Number - Partition ID (if user selected partition)
305     #
306     # -> imgCount - Number of images found
307     # -> partCount - Number of partitions found
308     #
309     # -> img - image name
310     # -> type - image type (partition/disk)
311     # -> activeItem - name of partition/disk
312     # -> imagePartitionSize - size of partition
313     # -> diskSizeFromImage - disk size
314     # -> partitionIdName - partition id in words
315     # -> id - description
316    
317     if [ -z "$onlyImages" ];then
318     dialog --backtitle "$backTitle" --title " Working " --infobox "Searching for image files" 3 29
319     fi
320     imgCount=0
321     partCount=0
322    
323     local count=1
324     local n
325     local tmp
326     local pCount
327     local tmpImg
328     local hasDisk=0
329     # set -x
330     for n in $(find "$1" -name parts 2>/dev/null);do
331     tmp="$(dirname "$n")"
332     if [ -z "$onlyImages" ];then
333     if [ "$(dirname "$tmp")" != "$1" ];then
334     continue
335     fi
336     fi
337     validateImage "$tmp" || continue
338     img[$count]="$(basename "$tmp")"
339    
340     [ -r "$(dirname "$n")"/disk ] && {
341     # it's a disk image
342     hasDisk=1
343     type[$count]=disk
344     tmp="$(dirname "$n")"/disk
345     activeItem[$count]=$(cat "$tmp")
346     partCount=$(cat "$(dirname "$n")"/parts | awk '{print NF}')
347     tmp=$(dirname "$n")
348     diskSizeFromImage[$count]=$(getDiskSizeFromImage "$tmp")
349     id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}"", size: ${diskSizeFromImage[$count]}, parts: $partCount"
350     # scan parts
351     pCount=1
352     tmpImg="${img[$count]}"
353     while [ $pCount -le $partCount ];do
354     ((count++))
355     img[$count]="$tmpImg"
356     type[$count]=part
357     activeItem[$count]=$(cat "$n" | awk '{for(i=1;i<=NF;i++) print $i}' | sed -n ""$pCount"p")
358     # set -x
359     calcInputSize "$tmp" "${activeItem[$count]}"
360     imagePartitionSize[$count]="$THIS_INPUT_SIZE"
361     partitionIdName[$count]=$(getPartitionIdNameFromImage /"$tmp" "${activeItem[$count]}")
362     ((pCount++))
363     id[$count]=" ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}"
364     done
365     } || {
366     # it's a partition image
367     type[$count]=part
368     activeItem[$count]=$(cat "$n")
369     partCount=1
370     calcInputSize /"$tmp" "${activeItem[$count]}"
371     imagePartitionSize[$count]="$THIS_INPUT_SIZE"
372     partitionIdName[$count]=$(getPartitionIdNameFromImage /"$tmp" "${activeItem[$count]}")
373     id[$count]="Image: ${img[$count]}, ${type[$count]}: ${activeItem[$count]}, size: ${imagePartitionSize[$count]}, type: ${partitionIdName[$count]}"
374     }
375     imgCount="$count"
376     ((count++))
377     done
378     # exit
379     [ $imgCount -eq 0 ] &&{
380     local msgType
381     [ -z "${CD[$selectedInputPartitionNumber]}" ] && msgType=Partition || msgType=CD-ROM
382     if [ -n "$(echo "$selectedInputPartition" | grep '/')" ];then
383     msgType=Folder
384     selectedInputPartition=/"$selectedInputPartition"
385     fi
386     if [ -z "$onlyImages" ];then
387     dialog --backtitle "$backTitle" --title " No images found!!! " --msgbox "$msgType"" $selectedInputPartition contains no valid image files
388    
389     Please try again selecting a different location" 7 60
390     else
391     echo "Error: $msgType $selectedInputPartition contains no valid image files!!!"
392     fi
393     cleanUP
394     exit 1
395     }
396     if [ -z "$onlyImages" ];then
397    
398     local cmdLine=''
399    
400     count=1
401     while [ $count -le $imgCount ];do
402     cmdLine="$cmdLine""$count"" \"""${id[$count]}""\" "
403     ((count++))
404     done
405    
406     selectedInputImage=''
407    
408     if [ $hasDisk -eq 1 ];then
409     local tit=" Partition/Disk to Restore "
410     local txt="Please select the partition/disk to restore"
411    
412    
413     # local msg="At least one of the images found contains a full disk backup
414     #
415     # Please use this image file to restore a full disk, ONLY in case the target disk is EXACTLY the same type as the original one (the disk that was backed up)
416     #
417     # In case the target disk is a different (bigger) one, you should NOT perform a full disk restore. Instead, exit this program, partition the target disk manually, in such a way that its partitions represent the partitions of the original disk, and then restore EACH partition seperatelly
418     #
419     # This way you will not have to resize the restored partitions on the target disk, after the entire restoration procedure has ended
420     #
421     # Do you want to continue?"
422     #
423     # dialog --backtitle "$backTitle" --title " Tips on disk restoration " --yesno "$msg" 20 72
424     #
425     # if [ $? -eq 1 ];then
426     # cleanUP
427     # exit
428     # fi
429    
430     else
431     local tit=" Target Partition "
432     local txt="Please select the partition to restore"
433     fi
434    
435     echo "dialog --backtitle \"$backTitle\" --title \""$tit"\" --menu \""$txt"\" $((imgCount+7)) 78 $imgCount $cmdLine" > /root/dialog.$$
436    
437     . /root/dialog.$$ 2>/root/selectedImage.$$
438     selectedInputImage=$(cat /root/selectedImage.$$)
439     [ -z "$selectedInputImage" ] && {
440     cleanUP
441     exit
442     }
443    
444     # set -x
445     # calculate image size and partition ID (if required)
446     if [ "${type[$selectedInputImage]}" = "part" ];then
447     calcInputSize /"$selectedInputPartition"/"${img[$selectedInputImage]}" "${activeItem[$selectedInputImage]}"
448     selectedInputImageSize="$THIS_INPUT_SIZE"
449     calcInputID /"$selectedInputPartition"/"${img[$selectedInputImage]}" "${activeItem[$selectedInputImage]}"
450     selectedInputImageID=$THIS_INPUT_ID
451     else
452     # calcInputSize /"$selectedInputPartition"/"${img[$selectedInputImage]}"
453     selectedInputImageSize="${diskSizeFromImage[$selectedInputImage]}"
454     fi
455     #
456     #
457     # cleanUP
458     # exit
459     else
460     echo "Image files found in: $selectedInputPartition"
461     count=1
462     until [ -z "${id[$count]}" ];do
463     echo " ${id[$count]}"
464     ((count++))
465     done
466     fi
467     }
468    
469     function getPartitionIdNameFromImage(){
470     local id=$(grep "$2" "$1"/*pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||')
471     sfdisk -T |sed 's|^[ ]||' | grep -e "^$id[ \t]" | sed "s|^$id *||"
472     }
473    
474     function getDiskSizeFromImage(){
475     local lin=$(awk 'BEGIN{FS="="
476     count=0}
477     {if (count==0) b=$2
478     else{
479     b=b" * "
480     b=b$2
481     }
482     count++}
483     END{print b}' "$1"/*chs.sf | bc)
484     local size=$(echo "$lin * 512" | bc)
485     toMGByte "$size"
486     }
487    
488     function getDiskSizeFromDisk(){
489     local size=$(fdisk -l /dev/"$1" | grep -e '^Disk /dev/' | sed 's|.*, *||' | sed 's| *bytes||')
490     toMGByte "$size"
491     }
492    
493     function toMGByte(){
494     if [ $(expr length "$1") -le 9 ];then
495     echo $(expr substr "$1" 1 3 )"MB"
496     elif [ $(expr length "$1") -eq 10 ];then
497     echo $(expr substr "$1" 1 2 | sed 's|\([0-9]\)$|.\1|')"GB"
498     elif [ $(expr length "$1") -lt 12 ];then
499     echo $(expr substr "$1" 1 3 | sed 's|\([0-9]\)$|.\1|')"GB"
500     else
501     echo $(expr substr "$1" 1 4 | sed 's|\([0-9]\)$|.\1|')"GB"
502     fi
503     }
504    
505     function validateImage(){
506     [ -d "$1"/parts ] && return 1
507     local sf=$(find "$1" -name "*-chs.sf" 2>/dev/null | wc -l)
508     [ $sf -eq 0 ] && return 1
509     local pt=$(find "$1" -name "*-pt.sf" 2>/dev/null | wc -l)
510     [ $pt -eq 0 ] && return 1
511     # modification to read unsplited images
512     # thanks to Viking (24/11/2008)
513     # local aa=$(find "$1" -name "*.aa" 2>/dev/null | wc -l)
514     # [ $aa -eq 0 ] && return 1
515     return 0
516     }
517    
518     function cleanUP(){
519     if [ ! -z "$selectedInputPartition" ];then
520     [ -z "$workingRemote" ] && umount /"$selectedInputPartition" 2>/dev/null
521     fi
522     cd
523     rm /root/*.$$ 2>/dev/null
524     rm -rf /home/partimag/reloc-image
525     umount /home/partimag 2>/dev/null
526     # umount -a 2>/dev/null
527     [ -z "$onlyImages" ] && clear
528     }
529    
530     function calcInputID(){
531     THIS_INPUT_ID=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*Id= *||' | sed 's|,.*$||')
532     }
533    
534     function calcInputSize(){
535     local bytesPerSector=512
536     local l
537     local pSize
538     local cylinders
539     local heads
540     local sectors
541     if [ -z "$2" ];then
542     cylinders=$(sed -n '/cylinders=/p' "$1"/*-chs.sf | sed 's|cylinders=||')
543     heads=$(sed -n '/heads=/p' "$1"/*-chs.sf | sed 's|heads=||')
544     sectors=$(sed -n '/sectors=/p' "$1"/*-chs.sf | sed 's|sectors=||')
545     THIS_INPUT_SIZE=$(echo "scale=2; $cylinders * $heads * $sectors * $bytesPerSector" | bc)
546     else
547     pSize=$(sed -n "/$2/p" "$1"/*-pt.sf | sed 's|.*size= *||' | sed 's|,.*||')
548     THIS_INPUT_SIZE=$(echo "scale=2; $pSize * $bytesPerSector" | bc)
549     fi
550    
551     l=$(expr length $THIS_INPUT_SIZE)
552     if [ $l -gt 9 ];then
553     THIS_INPUT_SIZE=$(echo "scale=2; $THIS_INPUT_SIZE / 1024 / 1024 / 1024" | bc)GB
554     else
555     THIS_INPUT_SIZE=$(echo "scale=3; $THIS_INPUT_SIZE / 1024 / 1024" | bc | sed 's|\.000|.00|')MB
556     fi
557     }
558    
559     function getPartitionSizeFromFdisk(){
560     local disk=$(echo "$1" | sed 's|[0-9]||')
561     if [ ! -e /root/closys-fdisk-"$disk".$$ ];then
562     fdisk -l /dev/"$disk" > /root/closys-fdisk-"$disk".$$
563     fi
564     local size=$(sed 's|*||' /root/closys-fdisk-"$disk".$$ | grep "$1" | awk '{print $4}' | sed 's|[+-]||')
565     local gB=$(echo "scale=2; $size / 1024 / 1024 " | bc | sed 's|^\.|0.|')GB
566     local mB=$(echo "scale=2; $size / 1024 " | bc)MB
567     if [ -z $(echo "$gB" | grep -e '^[0]') ];then
568     THIS_PARTITION_SIZE="$gB"
569     else
570     THIS_PARTITION_SIZE="$mB"
571     fi
572     }
573    
574     function getOutputPartition(){
575     ## select target partition
576     dialog --backtitle "$backTitle" --title " Working " --infobox "Searching for valid partitions" 3 34
577     local count=1
578     local cmdLine=''
579     local outputPartitionCount=0
580     local tmpDisk
581     local tmpID
582     local fatpartitions=''
583     local compResult
584     until [ -z "${inputPartition[$count]}" ];do
585     if [ "${inputPartition[$count]}" != "$selectedInputPartition" ] && [ -z "${CD[$count]}" ];then
586     tmpDisk=$(echo "${inputPartition[$count]}" | sed 's|[0-9]||')
587     tmpID=$(fdisk -l /dev/"$tmpDisk" 2>/dev/null| grep "${inputPartition[$count]}" | sed 's|*||' | awk '{print $5}')
588     if [ "$tmpID" = "$selectedInputImageID" ];then
589     comparePartitionsSize "$selectedInputImageSize" "${partitionSize[$count]}"
590     compResult=$?
591     if [ $compResult -eq 0 ];then
592     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]} (identical part)\""" "
593     ((outputPartitionCount++))
594     elif [ $compResult -eq 2 ];then
595     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]}\""" "
596     ((outputPartitionCount++))
597     needsResize[$count]='-r'
598     # echo "count = $count"
599     fi
600     fi
601     if [ "$tmpID" = "b" ] && [ "$selectedInputImageID" = "6" ];then
602     comparePartitionsSize "$selectedInputImageSize" "${partitionSize[$count]}"
603     compResult=$?
604     if [ $compResult -eq 0 ];then
605     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]} (identical part)\""" "
606     ((outputPartitionCount++))
607     elif [ $compResult -eq 2 ];then
608     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]}\""" "
609     ((outputPartitionCount++))
610     needsResize[$count]='-r'
611     # echo "count = $count"
612     fi
613     fi
614     if [ "$tmpID" = "c" ] && [ "$selectedInputImageID" = "6" ];then
615     comparePartitionsSize "$selectedInputImageSize" "${partitionSize[$count]}"
616     compResult=$?
617     if [ $compResult -eq 0 ];then
618     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]} (identical part)\""" "
619     ((outputPartitionCount++))
620     elif [ $compResult -eq 2 ];then
621     cmdLine="$cmdLine""${inputPartition[$count]}"" ""\"Size: ${partitionSize[$count]}, Type: ${partitionType[$count]}\""" "
622     ((outputPartitionCount++))
623     needsResize[$count]='-r'
624     # echo "count = $count"
625     fi
626     fi
627     fi
628     ((count++))
629     done
630    
631     if [ $outputPartitionCount -eq 0 ];then
632     dialog --backtitle "$backTitle" --title " No available partitions!!! " --msgbox "The program found no valid partitions for restoration!!!
633    
634     The selected (backed up) partition size is $selectedInputImageSize, type ${partitionIdName[$selectedInputImage]}, but none of the partitions found on your disks are either this big or of this type
635    
636     This means that you should repartition one of your disks and create a partition with size at least $selectedInputImageSize and type ${partitionIdName[$selectedInputImage]}" 12 70
637     cleanUP
638     exit
639     fi
640    
641     # cleanUP
642     # exit
643     tmpPartName=$(sfdisk -T | sed 's|^[ \t]||' | grep -e "^"$selectedInputImageID" " | sed "s|^"$selectedInputImageID"[ /t]*||" | sed 's|,.*$||')
644     if [ $outputPartitionCount -gt 0 ];then
645    
646     # chech partitions size
647     local thisHeight=$outputPartitionCount
648     local totHeigh=$((outputPartitionCount+8))
649     if [ $totHeigh -gt 20 ];then
650     totHeigh=20
651     thisHeight=$(totHeigh-8)
652     fi
653     echo "dialog --backtitle \"$backTitle\" --title \" Target Partition \" --menu \"Please select the partition where your image file will be restored
654     Original image type: $tmpPartName, size: $selectedInputImageSize\" $totHeigh 78 $thisHeight $cmdLine" > /root/dialog.$$
655    
656     . /root/dialog.$$ 2>/root/outputPartition.$$
657     selectedOutputPartition=$(cat /root/outputPartition.$$)
658     [ -z "$selectedOutputPartition" ] && {
659     cleanUP
660     exit
661     }
662     else
663     dialog --backtitle "$backTitle" --title " No available partitions!!! " --msgbox "The program found no valid partitions for restoration!!!
664    
665     The selected (backed up) partition type is $tmpPartName (ID = $selectedInputImageID), but none of the partitions found on your disks are of this type
666    
667     This means that you should repartition one of your disks and create a $tmpPartName partition with ID $selectedInputImageID, or change an existing ID to $selectedInputImageID" 12 70
668     cleanUP
669     exit 1
670     fi
671     # set -x
672     count=1
673     until [ -z "${inputPartition[$count]}" ];do
674     if [ "${inputPartition[$count]}" = "$selectedOutputPartition" ];then
675     selectedOutputPartitionIndex=$count
676     break
677     fi
678     ((count++))
679     done
680     set +x
681    
682     }
683    
684     function comparePartitionsSize(){
685     #
686     # partition size format: xxxx.x[kMG]B
687     #
688     # <- $1 $2 - the sizes
689     #
690     # -> 0 - equal
691     # -> 1 - $1 > $2
692     # -> 2 - $2 > $1
693     # set -x
694     if [ "$1" = "$2" ];then return 0;fi
695     local count=1
696     while [ $count -le 2 ];do
697     local size[$count]=$(echo "$1" | sed 's|[kMG]B||')
698     # echo "size[$count] = ${size[$count]}"
699     local factor[$count]=$(echo "$1" | sed 's|.*\([kMG]\)B|\1|')
700     # echo "factor[$count] = ${factor[$count]}"
701     getMultiplier ${factor[$count]}
702     local multiplier[$count]=$THIS_MULTIPLIER
703     # echo "multiplier[$count] = ${multiplier[$count]}"
704     shift
705     ((count++))
706     done
707     return $(echo "a=${size[1]} * ${multiplier[1]};
708     b=${size[2]} * ${multiplier[2]};
709     if (a==b) print 0
710     if (a>b) print 1
711     if (a<b) print 2" | bc)
712     }
713    
714     function getMultiplier(){
715     # echo $#
716     case "$1" in
717     k) THIS_MULTIPLIER=1024;;
718     M) THIS_MULTIPLIER=1048576;;
719     G) THIS_MULTIPLIER=1073741824;;
720     esac
721     }
722    
723     function getRestoreParameters(){
724     # used only in disk restore, when disks not equal
725     # # Get restore parameters
726     dialog --separate-output --backtitle "$backTitle" --title " Restore options " --checklist "Please select your restore options" 10 78 3 "-k" "Keep target disk partition table (do not re-partition)" on "-r" "Resize partitions on target disk to maximum size" on "-t" "Do not write the MBR to the target disk" off 2>/root/params.$$
727     if [ $? -ne 0 ];then
728     cleanUP
729     exit
730     fi
731     count=1
732     while [ $count -le 3 ];do
733     param[$count]=$(sed -n ""$count"p" /root/params.$$ | sed 's/[ ]*//' )
734     ((count++))
735     done
736     }
737    
738     function preparePartitionImage(){
739     dialog --backtitle "$backTitle" --title " Working " --infobox "Preparing temporary image file" 3 34
740     if [ -d /home/partimag ];then
741     umount /home/partimag 2>/dev/null
742     rm -rf /home/partimag/* 2>/dev/null
743     else
744     rm -rf /home/partimag 2>/dev/null
745     mkdir /home/partimag
746     fi
747     rm -rf /home/partimag/reloc-image 2>/dev/null
748     mkdir /home/partimag/reloc-image
749     # echo /"$selectedInputPartition"/"${img[$selectedInputImage]}"
750     local inTmpDisk=$(echo "${activeItem[$selectedInputImage]}" | sed 's|[0-9]||')
751     local outTmpDisk=$(echo "$selectedOutputPartition" | sed 's|[0-9]||')
752     local outTmpPartition=$(echo "$selectedOutputPartition" | sed 's|[a-z]*||')
753     local tmpFile
754     local outFile
755     local count
756     #set -x
757     for n in $(find /"$selectedInputPartition"/"${img[$selectedInputImage]}" -name "*");do
758     tmpFile=$(basename "$n")
759     if [ "$tmpFile" != "${img[$selectedInputImage]}" ];then
760     # echo "$tmpFile"
761     outFile=$(echo "$tmpFile" | sed "s|${activeItem[$selectedInputImage]}|$selectedOutputPartition|" |sed "s|$inTmpDisk|$outTmpDisk|")
762     # echo "${activeItem[$selectedInputImage]} => $selectedOutputPartition"
763     # echo ln -s "$n" /home/partimag/reloc-image/"$outFile"
764     ln -s "$n" /home/partimag/reloc-image/"$outFile"
765     fi
766     done
767     if [ "${activeItem[$selectedInputImage]}" != "$selectedOutputPartition" ];then
768     [ -e /home/partimag/reloc-image/disk ] && {
769     rm /home/partimag/reloc-image/disk
770     echo "$outTmpDisk" > /home/partimag/reloc-image/disk
771     }
772     sed "s|${activeItem[$selectedInputImage]}|$selectedOutputPartition|" /home/partimag/reloc-image/parts | sed "s|$inTmpDisk|$outTmpDisk|" > /home/partimag/reloc-image/parts.tmp
773     rm /home/partimag/reloc-image/parts
774     mv /home/partimag/reloc-image/parts.tmp /home/partimag/reloc-image/parts
775     rm /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
776     echo "# partition table of /dev/"$outTmpDisk"" > /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
777     echo "unit: sectors" >> /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
778     echo >> /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
779     count=1
780     while [ $count -lt $outTmpPartition ];do
781     echo "/dev/"$outTmpDisk""$count" : start= 0, size= 0, Id= 0" >> /home/partimag/reloc-image/"$outTmpDisk"-pt.sf.tmp
782     ((count++))
783     done
784     grep "${activeItem[$selectedInputImage]}" /home/partimag/reloc-image/"$outTmpDisk"-pt.sf | sed "s|${activeItem[$selectedInputImage]}|$selectedOutputPartition|" >> /home/partimag/reloc-image/"$outTmpDisk"-pt.sf.tmp
785     rm /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
786     mv /home/partimag/reloc-image/"$outTmpDisk"-pt.sf.tmp /home/partimag/reloc-image/"$outTmpDisk"-pt.sf
787     fi
788     }
789    
790     function doPartitionRestore(){
791     # set -x
792     if [ -z "${needsResize[$selectedOutputPartitionIndex]}" ];then
793     msg="The temporary image file is now ready, and you are about to start the Restore operation
794    
795     Are you sure you want to restore
796     the backup of partition ${activeItem[$selectedInputImage]}
797     to
798     partition $selectedOutputPartition?"
799     else
800     msg="The temporary image file is now ready, and you are about to start the Restore operation
801    
802     Are you sure you want to restore and resize
803     the backup of partition ${activeItem[$selectedInputImage]}
804     to
805     partition $selectedOutputPartition?"
806     fi
807     dialog --backtitle "$backTitle" --title "Ready to Restore" --yesno "$msg" 11 70
808    
809     if [ $? -eq 0 ];then
810     # Do restore
811     COMMAND="ocs-sr -p true -k -t "${needsResize[$selectedOutputPartitionIndex]}" -b restoreparts reloc-image $selectedOutputPartition"
812     fi
813     }
814    
815     function getOutputDisk(){
816     #
817     # -> diskGeometry - system disk geometry (array)
818     # item empty if disk smaller than image
819     # or not same number of partitions
820     # or partitions not same type
821     # -> equalDisk - denotes an equal disk geometry (array)
822     # item ont empty if disk is equal to image disk
823     # -> diskToRestoreCount - Number of available disks to restore
824     local cmdLine
825     diskToRestoreCount=0
826     getDiskGeometry
827     selectedImageDiskGeometry=$THIS_GEOMETRY
828    
829     local count=1
830     local mountedDisk=$(echo "$selectedInputPartition" | sed 's|[0-9]||')
831     # set -x
832     while [ $count -le $diskCount ];do
833     if [ "${inputDisk[$count]}" != "$mountedDisk" ];then
834     getDiskGeometry /dev/"${inputDisk[$count]}"
835     if [ "$THIS_GEOMETRY" = "$selectedImageDiskGeometry" ];then
836     # we have two identical disks
837     diskGeometry[$count]="$THIS_GEOMETRY"
838     equalDisk[$count]=1
839     ((diskToRestoreCount++))
840     cmdLine="$cmdLine""\"""${inputDisk[$count]}""\""" ""\"""Size: $(getDiskSizeFromDisk ${inputDisk[$count]}) (identical disk)""\" "
841     else
842     # disks are different
843     local selGeo=$(echo "$selectedImageDiskGeometry" |sed 's|,|*|g')
844     local thisGeo=$(echo "$THIS_GEOMETRY" |sed 's|,|*|g')
845     local compResult=$(echo "a=$selGeo;
846     b=$thisGeo;
847     if (a==b) print 0
848     if (a>b) print 1
849     if (a<b) print 2" | bc)
850     if [ $compResult -eq 2 ];then
851     # disk is large enough
852     # do we have same number of partitions and same type?
853     compResult=$(checkDisksPartitions)
854     if [ $compResult -eq 0 ];then
855     diskGeometry[$count]="$THIS_GEOMETRY"
856     ((diskToRestoreCount++))
857     cmdLine="$cmdLine""\"""${inputDisk[$count]}""\""" ""\"""Size: $(getDiskSizeFromDisk ${inputDisk[$count]})""\" "
858     fi
859     fi
860     fi
861    
862     fi
863     ((count++))
864     done
865     if [ $diskToRestoreCount -eq 0 ];then
866     dialog --backtitle "$backTitle" --title " No disks found!!! " --msgbox "Your system has no valid disks to restore this image file!!!
867    
868     In order to perform a disk restoration, you must have:
869    
870     a. A disk with the same geometry as the source disk
871     In this case you will perform a full disk restore, creating
872     partitions and restoring the MBR, or
873    
874     b. A prepartitioned larger disk which contains exactly the same
875     partitions as the source disk (same partition number and id)
876     In this case no partition creation will take place, but the
877     target partitions will be resized to maximum size" 16 70
878     cleanUP
879     exit
880     fi
881    
882     # make dialog
883     echo "dialog --backtitle \"$backTitle\" --title \" Target Disk \" --menu \"Please select the disk where your image file will be restored
884     Original image: ${img[$selectedInputImage]}, disk: ${activeItem[$selectedInputImage]}, size: $selectedInputImageSize\" $((diskToRestoreCount+8)) 78 $diskToRestoreCount $cmdLine" > /root/dialog.$$
885    
886     . /root/dialog.$$ 2>/root/selectedDisk.$$
887     selectedOutputDisk=$(cat /root/selectedDisk.$$)
888    
889     [ -z "$selectedOutputDisk" ] && {
890     cleanUP
891     exit
892     }
893    
894    
895     }
896    
897     function getDiskGeometry(){
898     if [ -z "$1" ];then
899     # get image disk geometry
900     THIS_GEOMETRY=$(awk 'BEGIN{FS="="
901     count=0}
902     {if (count==0) b=$2
903     else{
904     b=b","
905     b=b$2
906     }
907     count++}
908     END{print b}' /"$selectedInputPartition"/"${img[$selectedInputImage]}"/*-chs.sf)
909     else
910     # get disk geometry
911     THIS_GEOMETRY=$(sfdisk -g "$1" | sed 's|^.*:||' | sed 's|cylinders||' | sed 's|heads||' | sed 's|sectors/track||' | sed 's| *||g')
912     fi
913    
914     }
915    
916     function checkDisksPartitions(){
917     local imgData=$(grep -e '^/dev/' /"$selectedInputPartition"/"${img[$selectedInputImage]}"/*-pt.sf | sed 's| : .*Id= *|:|')
918    
919     local diskData=$(sfdisk -l /dev/${inputDisk[$count]} 2>/dev/null| grep -e '^/dev/' | awk '{
920     if ($2=="*")
921     print $1 ":" $7 ", bootable"
922     else
923     print $1 ":" $6}' | sed "s|${inputDisk[$count]}|${activeItem[$selectedInputImage]}|")
924    
925     if [ "$imgData" != "$diskData" ];then
926     echo 1
927     return
928     fi
929    
930     # check partitions size
931     local numOfPartitions=$(grep 'size=' /"$selectedInputPartition"/"${img[$selectedInputImage]}"/*-pt.sf | wc -l)
932     local imgPartitionSize
933     local diskPartitionSize
934     local partCount=1
935    
936     sfdisk -d /dev/${inputDisk[$count]} > /root/disk-${inputDisk[$count]}.$$
937     while [ $partCount -le $numOfPartitions ];do
938     imgPartitionSize=$(grep 'size=' /"$selectedInputPartition"/"${img[$selectedInputImage]}"/*-pt.sf | sed -n ""$partCount"p"| sed 's|.*size= *||' | sed 's|,.*$||' | sed 's|[+-]||')
939     diskPartitionSize=$(grep 'size=' /root/disk-${inputDisk[$count]}.$$ | sed -n ""$partCount"p"| sed 's|.*size= *||' | sed 's|,.*$||' | sed 's|[+-]||')
940     local compResult=$(echo "a=$imgPartitionSize;
941     b=$diskPartitionSize;
942     if (a==b) print 0
943     if (a>b) print 1
944     if (a<b) print 2" | bc)
945     if [ $compResult -eq 2 ];then
946     echo 1
947     return
948     fi
949     ((partCount++))
950     done
951     echo 0
952     }
953    
954    
955    
956     function prepareDiskImage(){
957     dialog --backtitle "$backTitle" --title " Working " --infobox "Preparing temporary image file" 3 34
958     if [ -d /home/partimag ];then
959     umount /home/partimag 2>/dev/null
960     rm -rf /home/partimag/* 2>/dev/null
961     else
962     rm -rf /home/partimag 2>/dev/null
963     mkdir /home/partimag
964     fi
965     rm -rf /home/partimag/reloc-image 2>/dev/null
966     mkdir /home/partimag/reloc-image
967    
968     # count is not local, will be used in doDiskRestore
969     count=1
970     until [ -z "${inputDisk[$count]}" ];do
971     # if [ "${activeItem[$count]}" = "$selectedOutputDisk" ];then
972     if [ "${inputDisk[$count]}" = "$selectedOutputDisk" ];then
973     break
974     fi
975     ((count++))
976     done
977    
978     local tmpFile
979     for n in $(find /"$selectedInputPartition"/"${img[$selectedInputImage]}" -name "*");do
980     tmpFile=$(basename "$n")
981     if [ "$tmpFile" != "${img[$selectedInputImage]}" ];then
982     outFile=$(echo "$tmpFile" |sed "s|${activeItem[$selectedInputImage]}|$selectedOutputDisk|")
983     ln -s "$n" /home/partimag/reloc-image/"$outFile"
984     fi
985     done
986    
987     if [ "${activeItem[$selectedInputImage]}" != "$selectedOutputDisk" ];then
988     rm /home/partimag/reloc-image/disk
989     echo "$selectedOutputDisk" > /home/partimag/reloc-image/disk
990     rm /home/partimag/reloc-image/parts
991     echo "$(cat /"$selectedInputPartition"/"${img[$selectedInputImage]}"/parts | sed "s|${activeItem[$selectedInputImage]}|$selectedOutputDisk|")" > /home/partimag/reloc-image/parts
992     rm /home/partimag/reloc-image/"$selectedOutputDisk"-pt.sf
993     sfdisk -d /dev/"$selectedOutputDisk" > /home/partimag/reloc-image/"$selectedOutputDisk"-pt.sf
994     if [ -z "${equalDisk[$count]}" ];then
995     rm /home/partimag/reloc-image/"$selectedOutputDisk"-chs.sf
996     sfdisk -d /dev/"$selectedOutputDisk" > /home/partimag/reloc-image/"$selectedOutputDisk"-chs.sf
997     fi
998     fi
999     }
1000    
1001     function doDiskRestore(){
1002     if [ -z "${equalDisk[$count]}" ];then
1003     getRestoreParameters
1004     # disks not equal, do resize, restore MBR, don't create partitions
1005    
1006     msg="The temporary image file is now ready, and you are about to start the Restore operation
1007    
1008     You are about to restore
1009     the backup of disk ${activeItem[$selectedInputImage]}
1010     to
1011     disk $selectedOutputDisk
1012    
1013     Info: The target disk is larger than the backed up disk
1014     Selected Restore options will be be applied
1015    
1016     Do you want to continue?"
1017     dialog --backtitle "$backTitle" --title "Ready to Restore" --yesno "$msg" 16 70
1018    
1019     if [ $? -eq 0 ];then
1020     # Do restore
1021     COMMAND="ocs-sr -p true -e -b "${param[1]}" "${param[2]}" "${param[3]}" restoredisk reloc-image $selectedOutputDisk"
1022     fi
1023     else
1024     # disks are equal, restore MBR, do partitioning, no resize
1025     msg="The temporary image file is now ready, and you are about to start the Restore operation
1026    
1027     You are about to restore
1028     the backup of disk ${activeItem[$selectedInputImage]}
1029     to
1030     disk $selectedOutputDisk
1031    
1032     Info: The disks are identical, so the target disk will be
1033     partitioned and the MBR will be restored
1034    
1035     Do you want to continue?"
1036     dialog --backtitle "$backTitle" --title "Ready to Restore" --yesno "$msg" 16 70
1037    
1038     if [ $? -eq 0 ];then
1039     # Do restore
1040     COMMAND="ocs-sr -p true -e -b restoredisk reloc-image $selectedOutputDisk"
1041     fi
1042     fi
1043     }
1044    
1045     function testRoot(){
1046     if [ "$(whoami)" != "root" ];then
1047     printVersion
1048     echo "Error: This script must be executed by root..."
1049     exit
1050     fi
1051     }
1052    
1053     function printVersion(){
1054     echo "Clonezilla Live Image Restoration with Relocation
1055     $(basename $0) v. $versionNumber - (C) 2007-2008 S. Georgaras <sng@hellug.gr>
1056     "
1057     }
1058    
1059     function printHelp(){
1060     printVersion
1061     echo "Usage: $(basename $0) <options>
1062    
1063     Available options:
1064     i [location] Print info about Clonezilla Live Image files found in [location]
1065     [location] may be a hard disk partition, a CD-ROM device name or
1066     smb - the first Samba Share
1067     nfs - the first NFS Share
1068     Remote shares must be mounted beforehand
1069     v Print version info and exit
1070     h Print this screen and exit"
1071     }
1072    
1073     #
1074     #
1075     # Script starts here
1076     #
1077     #
1078    
1079     # findDirectories /home/spiros/multibootcd/files
1080     # #findDirectories /home/partimag
1081     #
1082     # exit
1083    
1084    
1085     #toMGByte "$1"
1086     #exit
1087    
1088    
1089    
1090    
1091     # grep -e '^/dev/' /data/test-img/hda-pt.sf | sed 's| : .*Id= *|:|'
1092     #
1093     #
1094     # sfdisk -l /dev/hda 2>/dev/null| grep -e '^/dev/' | awk '{
1095     # if ($2=="*")
1096     # print $1 ":" $7 ", bootable"
1097     # else
1098     # print $1 ":" $6}'
1099     # exit
1100    
1101    
1102     # getPartitionIdNameFromImage /data/usb250-img sda4
1103     # exit
1104    
1105     # set -x
1106     # getDiskSizeFromImage /data/usb250-img
1107     # exit
1108    
1109     # echo "$partitionList"
1110     # exit
1111    
1112     # getMultiplier M f g
1113     # echo "$THIS_MULTIPLIER"
1114    
1115     # set -x
1116     # comparePartitionsSize 2GB 2GB
1117     # echo $?
1118     # exit
1119     # set -x
1120     # getPartitionSizeFromFdisk hda1
1121     # echo "$THIS_PARTITION_SIZE"
1122     # exit
1123    
1124    
1125     # mountedInputPartition=/data
1126     # # set -x
1127     # findImages "$mountedInputPartition"
1128    
1129     # set -x
1130     # count=1
1131     # until [ -z "${img[$count]}" ];do
1132     # echo "${id[$count]}"
1133     # ((count++))
1134     # done
1135     #
1136     # exit
1137    
1138    
1139     # calcInputSize /data/usb250-img
1140     # calcInputSize /data/usb250-img sda4
1141     # calcInputID /data/usb250-img sda4
1142     # exit
1143    
1144     while getopts hvi: opt
1145     do
1146     case "$opt" in
1147     i)
1148     onlyImages=true
1149     diskToScan=$(echo "$OPTARG" | sed 's|^/dev/||')
1150     ;;
1151     v)
1152     printVersion
1153     exit;;
1154     h)
1155     printHelp
1156     exit;;
1157     esac
1158     done
1159     shift $(( OPTIND - 1 ))
1160    
1161     testRoot
1162     backTitle="Clonezilla Live Image Restoration with Relocation"
1163    
1164     for CHK_DIR in /home/partimag /tmp/local-dev;do
1165     CHK_HOME_PARTIMAG="$(mount | grep "$CHK_DIR")"
1166     if [ -n "$CHK_HOME_PARTIMAG" ];then
1167     CHK_MSG="
1168     A local partition or shared folder is already mounted
1169     under "$CHK_DIR". reloc-img will now terminate to
1170     prevent loss of data
1171    
1172     Please unmount it and try again"
1173     CHK_TITLE=" "$CHK_DIR" already mounted!!! "
1174    
1175     if [ -z "$onlyImages" ];then
1176    
1177     dialog --backtitle "$backTitle" --title "$CHK_TITLE" --msgbox "$CHK_MSG" 10 66
1178     else
1179     echo "$CHK_MSG"
1180     echo
1181     fi
1182     # Don't use cleanUP here, we want user to see the mounted partition
1183     # cleanUP
1184     exit
1185     fi
1186     done
1187    
1188    
1189     trap cleanUP 2
1190    
1191     if [ ! -z "$onlyImages" ];then
1192     printVersion
1193    
1194    
1195     if [ "$diskToScan" = "smb" ];then
1196     # if [ $(mount |grep cifs | wc -l) -eq 0 ];then
1197     # echo "Error: No Samba Share mounted!!!"
1198     # cleanUP
1199     # exit
1200     # fi
1201     # set -x
1202     selectedInputPartition=smb1
1203     workingRemote='y'
1204     findSambaShare
1205     elif [ "$diskToScan" = "nfs" ];then
1206     if [ $(mount |grep nfs | wc -l) -eq 0 ];then
1207     echo "Error: No NFS Share mounted!!!"
1208     cleanUP
1209     exit
1210     fi
1211     selectedInputPartition=nfs1
1212     workingRemote='y'
1213     findNFSShare
1214     else
1215     test1=$(cat /proc/partitions | grep "$diskToScan")
1216     if [ -z "$test1" ];then
1217     # It is not a disk partition
1218     # Is it a CD-ROM?
1219     test1=$(cat /proc/sys/dev/cdrom/info | grep 'drive name' |sed 's|drive name:[ \t]*||' | awk '{for(i=1;i<=NF;i++) print $i}' | grep "$diskToScan")
1220     if [ -z "$test1" ];then
1221     echo "Error: No such partition or CD-ROM: $diskToScan"
1222     cleanUP
1223     exit
1224     fi
1225     fi
1226     selectedInputPartition="$diskToScan"
1227     fi
1228     else
1229    
1230    
1231    
1232    
1233     msg="This program is part of Clonezilla-SysRescCD, and is provided with the hope that it will be error-free and useful. If you have found a bug, or the program does not behave as expected, please report it to
1234     Spiros Georgaras <sng@hellug.gr>
1235    
1236     It will help you restore a Clonezilla Image file to a disk/partition different from the one originally backed up.
1237    
1238     For example, you will be able to restore your backed up hdb disk to a (new) disk, hdc, or your backed up hda4 partition to a (new) partition, hdb3.
1239    
1240     The target disk/partition must be the same partition type and at least equal (size-wise) to the old - backed up - disk/partition
1241    
1242     Please make sure all removable devices are connected to the system before pressing OK, and that no local partition is mounted
1243    
1244     All data in the target disk/partition will be OVERWRITTEN!!!
1245    
1246     ----------------------
1247     THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY!
1248     USE AT YOUR OWN RISK!
1249     ----------------------"
1250    
1251     msg1="Do you want to continue?"
1252     dialog --backtitle "$backTitle" --title " Wellcome " \
1253     --msgbox "$msg" 20 72 \
1254     --and-widget --yesno "$msg1" 5 45 || {
1255     clear
1256     exit
1257     }
1258    
1259     dialog --backtitle "$backTitle" --title " Working " --infobox "Determining system disks and partitions" 3 43
1260    
1261     findSystemPartitions
1262     # mount selectedInputPartition
1263     dialog --backtitle "$backTitle" --title " Working " --infobox "Mounting selected device" 3 28
1264    
1265     fi
1266    
1267    
1268    
1269    
1270     [ -z "$selectedInputPartition" ] && {
1271     cleanUP
1272     echo "Error: selectedInputPartition is empty!!!"
1273     exit 1
1274     }
1275     # set -x
1276     if [ -z "$workingRemote" ];then
1277     # mount partition
1278     mkdir /"$selectedInputPartition" 2>/dev/null
1279     [ -d /"$selectedInputPartition" ] || {
1280     cleanUP
1281     echo "Error: Could not create folder: /$selectedInputPartition"
1282     exit 1
1283     }
1284     #set -x
1285     if [ -z "$DEBUG" ];then
1286     mkdir /"$selectedInputPartition" 2>/dev/null
1287     if [ -z "${CD[$selectedInputPartitionNumber]}" ];then
1288     # This is a disk partition
1289     mount /dev/"$selectedInputPartition" /"$selectedInputPartition" 2>/dev/null
1290     mountResult=$?
1291     else
1292     # This is a CD
1293     # Is it mounted?
1294     rm /home/partimag 2>/dev/null
1295     mkdir /home/partimag 2>/dev/null
1296     if [ -z "$(mount | grep "/dev/$selectedInputPartition")" ];then
1297     # No it's not, mount it
1298     echo "mount /dev/\"$selectedInputPartition\" /\"$selectedInputPartition\"" > /root/mountCommand.$$
1299     . /root/mountCommand.$$ && mountResult=0 || mountResult=1
1300     else
1301     # Yes it is ,mount through bind
1302     bindDir=$(mount | grep "/dev/$selectedInputPartition" | sed 's|.* on ||' | sed 's| type.*||')
1303     # echo "mount --bind \"$bindDir\" /\"$selectedInputPartition\" 2>/dev/null" > /root/mountCommand.$$
1304    
1305    
1306     if [ -d "$bindDir"/home/partimag ]; then bindDir="$bindDir"/home/partimag;fi
1307    
1308     echo "mount --bind \"$bindDir\" /\"$selectedInputPartition\" 2>/dev/null" > /root/mountCommand.$$
1309    
1310    
1311    
1312     chmod +x /root/mountCommand.$$
1313     . /root/mountCommand.$$ && mountResult=0 || mountResult=1
1314     fi
1315     fi
1316     else
1317     ### start DEBUG section
1318     mount --bind /data /"$selectedInputPartition"
1319     mountResult=$?
1320     ### end DEBUG section
1321     fi
1322     #read
1323    
1324     # Chech mounting result
1325     if [ $mountResult -ne 0 ];then
1326     if [ -z "$onlyImages" ];then
1327     if [ -z "${CD[$selectedInputPartitionNumber]}" ];then
1328     dialog --backtitle "$backTitle" --title " Mount error!!! " --msgbox "Partition $selectedInputPartition could not be mounted
1329    
1330     Please unmount all local partitions, and try again" 7 70
1331     cleanUP
1332     exit 1
1333     else
1334     while [ $mountResult -ne 0 ];do
1335     dialog --backtitle "$backTitle" --title " Mount error!!! " --yesno "CD-ROM $selectedInputPartition could not be mounted. This may be caused because you did not load the disk in the CD-ROM drive. Make sure the disk is in the drive and press Yes
1336    
1337     Do you want to try again?" 9 70
1338     if [ $? -eq 0 ];then
1339     dialog --backtitle "$backTitle" --title " Working " --infobox "Mounting selected device" 3 28
1340     . /root/mountCommand.$$ && mountResult=0 || mountResult=1
1341     else
1342     cleanUP
1343     exit 1
1344     fi
1345     done
1346     fi
1347     else
1348     echo "Error: Could not mount $selectedInputPartition"
1349     cleanUP
1350     exit 1
1351     fi
1352    
1353     fi
1354     else
1355     selectedInputPartition=$(echo "${inputPartition[$selectedInputPartitionNumber]}" | sed 's|^/||')
1356     fi
1357    
1358    
1359     ########################################################
1360     #
1361     # find directories...
1362     #
1363     ########################################################
1364     if [ -z "$onlyImages" ];then
1365     findDirectories /"$selectedInputPartition"
1366     if [ -z "$activeDirectory" ];then
1367     cleanUP
1368     exit
1369     fi
1370    
1371     if [ "$activeDirectory" != "/" ];then
1372     selectedInputPartition="$selectedInputPartition"/"$activeDirectory"
1373     fi
1374     fi
1375     ########################################################
1376    
1377    
1378     findImages /"$selectedInputPartition"
1379    
1380     if [ ! -z "$onlyImages" ];then
1381     cleanUP
1382     exit
1383     fi
1384    
1385    
1386     if [ "${type[$selectedInputImage]}" = "part" ];then
1387     getOutputPartition
1388     preparePartitionImage
1389     doPartitionRestore
1390     else
1391     getOutputDisk
1392     prepareDiskImage
1393     doDiskRestore
1394     fi
1395     # checking=aaa
1396     if [ ! -z "$COMMAND" ];then
1397     if [ -z "$checking" ];then
1398     $COMMAND
1399     else
1400     echo "The command that would be executed is:
1401     $COMMAND"
1402     # read
1403     fi
1404     fi
1405     cleanUP

Properties

Name Value
svn:executable *

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26