-
Notifications
You must be signed in to change notification settings - Fork 214
/
funcs.sh
2200 lines (2199 loc) · 81.4 KB
/
funcs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
. /usr/share/fog/lib/partition-funcs.sh
REG_LOCAL_MACHINE_XP="/ntfs/WINDOWS/system32/config/system"
REG_LOCAL_MACHINE_7="/ntfs/Windows/System32/config/SYSTEM"
# 1 to turn on massive debugging of partition table restoration
ismajordebug=0
#If a sub shell gets invoked and we lose kernel vars this will reimport them
oIFS=$IFS
for var in $(cat /proc/cmdline); do
IFS=$oIFS
read name value <<< $(echo "$var" | grep =.* | awk -F= '{name=$1;$1="";gsub(/[ \t]+$/,"",$0);gsub(/^[ \t]+/,"",$0); gsub(/[+][_][+]/," ",$0); value=$0; print name; print value;}')
IFS=$'\n'
[[ -z $value ]] && continue
value=$(echo $value | sed 's/\"//g')
printf -v "$name" -- "$value"
done
IFS=$oIFS
### If USB Boot device we need a way to get the kernel args properly
[[ $boottype == usb && -f /tmp/hinfo.txt ]] && . /tmp/hinfo.txt
# Below Are non parameterized functions
# These functions will run without any arguments
#
# Clears thes creen unless its a debug task
clearScreen() {
case $isdebug in
[Yy][Ee][Ss]|[Yy])
clear
;;
esac
}
# Displays the nice banner along with the running version
displayBanner() {
version=$(wget -qO - http://${web}service/getversion.php 2>/dev/null)
echo " +------------------------------------------+"
echo " | ..#######:. ..,#,.. .::##::. |"
echo " |.:###### .:;####:......;#;.. |"
echo " |...##... ...##;,;##::::.##... |"
echo " | ,# ...##.....##:::## ..:: |"
echo " | ## .::###,,##. . ##.::#.:######::.|"
echo " |...##:::###::....#. .. .#...#. #...#:::. |"
echo " |..:####:.. ..##......##::## .. # |"
echo " | # . ...##:,;##;:::#: ... ##.. |"
echo " | .# . .:;####;::::.##:::;#:.. |"
echo " | # ..:;###.. |"
echo " | |"
echo " +------------------------------------------+"
echo " | Free Computer Imaging Solution |"
echo " +------------------------------------------+"
echo " | Credits: http://fogproject.org/Credits |"
echo " | http://fogproject.org/Credits |"
echo " | Released under GPL Version 3 |"
echo " +------------------------------------------+"
echo " Version: $version"
}
# Gets all system mac addresses except for loopback
getMACAddresses() {
read ifaces <<< $(/sbin/ip -4 -o addr | awk -F'([ /])+' '/global/ {print $2}' | tr '[:space:]' '|' | sed -e 's/^[|]//g' -e 's/[|]$//g')
read mac_addresses <<< $(/sbin/ip -0 -o addr | awk "/$ifaces/ {print \$11}" | tr '[:space:]' '|' | sed -e 's/^[|]//g' -e 's/[|]$//g')
echo $mac_addresses
}
# Verifies that there is a network interface
verifyNetworkConnection() {
dots "Verifying network interface configuration"
local count=$(/sbin/ip addr | awk -F'[ /]+' '/global/{print $3}' | wc -l)
if [[ -z $count || $count -lt 1 ]]; then
echo "Failed"
debugPause
handleError "No network interfaces found (${FUNCNAME[0]})\n Args Passed: $*"
fi
echo "Done"
debugPause
}
# Verifies that the OS is valid for resizing
validResizeOS() {
[[ $osid != @([1-2]|4|[5-7]|9|50|51) ]] && handleError " * Invalid operating system id: $osname ($osid) (${FUNCNAME[0]})\n Args Passed: $*"
}
# Gets the information from the system for inventory
doInventory() {
sysman=$(dmidecode -s system-manufacturer)
sysproduct=$(dmidecode -s system-product-name)
sysversion=$(dmidecode -s system-version)
sysserial=$(dmidecode -s system-serial-number)
systype=$(dmidecode -t 3 | grep Type:)
biosversion=$(dmidecode -s bios-version)
biosvendor=$(dmidecode -s bios-vendor)
biosdate=$(dmidecode -s bios-release-date)
mbman=$(dmidecode -s baseboard-manufacturer)
mbproductname=$(dmidecode -s baseboard-product-name)
mbversion=$(dmidecode -s baseboard-version)
mbserial=$(dmidecode -s baseboard-serial-number)
mbasset=$(dmidecode -s baseboard-asset-tag)
cpuman=$(dmidecode -s processor-manufacturer)
cpuversion=$(dmidecode -s processor-version)
cpucurrent=$(dmidecode -t 4 | grep 'Current Speed:' | head -n1)
cpumax=$(dmidecode -t 4 | grep 'Max Speed:' | head -n1)
mem=$(cat /proc/meminfo | grep MemTotal)
hdinfo=$(hdparm -i $hd 2>/dev/null | grep Model=)
caseman=$(dmidecode -s chassis-manufacturer)
casever=$(dmidecode -s chassis-version)
caseserial=$(dmidecode -s chassis-serial-number)
casesasset=$(dmidecode -s chassis-asset-tag)
sysman64=$(echo $sysman | base64)
sysproduct64=$(echo $sysproduct | base64)
sysversion64=$(echo $sysversion | base64)
sysserial64=$(echo $sysserial | base64)
systype64=$(echo $systype | base64)
biosversion64=$(echo $biosversion | base64)
biosvendor64=$(echo $biosvendor | base64)
biosdate64=$(echo $biosdate | base64)
mbman64=$(echo $mbman | base64)
mbproductname64=$(echo $mbproductname | base64)
mbversion64=$(echo $mbversion | base64)
mbserial64=$(echo $mbserial | base64)
mbasset64=$(echo $mbasset | base64)
cpuman64=$(echo $cpuman | base64)
cpuversion64=$(echo $cpuversion | base64)
cpucurrent64=$(echo $cpucurrent | base64)
cpumax64=$(echo $cpumax | base64)
mem64=$(echo $mem | base64)
hdinfo64=$(echo $hdinfo | base64)
caseman64=$(echo $caseman | base64)
casever64=$(echo $casever | base64)
caseserial64=$(echo $caseserial | base64)
casesasset64=$(echo $casesasset | base64)
}
# Gets the location of the SAM registry if found
getSAMLoc() {
local path=""
local paths="/ntfs/WINDOWS/system32/config/SAM /ntfs/Windows/System32/config/SAM"
for path in $paths; do
[[ ! -f $path ]] && continue
sam="$path" && break
done
}
# Appends dots to the end of string up to 50 characters.
# Makes the output more aligned and organized.
#
# $1 String to append dots to
dots() {
local str="$*"
[[ -z $str ]] && handleError "No string passed (${FUNCNAME[0]})\n Args Passed: $*"
local pad=$(printf "%0.1s" "."{1..50})
printf " * %s%*.*s" "$str" 0 $((50-${#str})) "$pad"
}
# Enables write caching on the disk passed
# If the disk does not support write caching this does nothing
#
# $1 is the drive
enableWriteCache() {
local disk="$1"
[[ -z $disk ]] && handleError "No disk passed (${FUNCNAME[0]})\n Args Passed: $*"
wcache=$(hdparm -W $disk 2>/dev/null | tr -d '[[:space:]]' | awk -F= '/.*write-caching=/{print $2}')
if [[ -z $wcache || $wcache == notsupported ]]; then
echo " * Write caching not supported"
debugPause
return
fi
dots "Enabling write cache"
hdparm -W1 $disk >/dev/null 2>&1
case $? in
0)
echo "Enabled"
;;
*)
echo "Failed"
debugPause
handleWarning "Could not set caching status (${FUNCNAME[0]})"
return
;;
esac
debugPause
}
# Expands partitions, as needed/capable
#
# $1 is the partition
# $2 is the fixed size partitions (can be empty)
expandPartition() {
local part="$1"
local fixed="$2"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
local disk=""
local part_number=0
getDiskFromPartition "$part"
getPartitionNumber "$part"
local is_fixed=$(echo $fixed | awk "/(^$part_number:|:$part_number:|:$part_number$|^$part_number$)/{print 1}")
if [[ $is_fixed -eq 1 ]]; then
echo " * Not expanding ($part) fixed size"
debugPause
return
fi
local fstype=""
fsTypeSetting $part
case $fstype in
ntfs)
dots "Resizing $fstype volume ($part)"
ntfsresize $part -f -b -P </usr/share/fog/lib/EOFNTFSRESTORE >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not resize $part (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
debugPause
resetFlag "$part"
;;
extfs)
dots "Resizing $fstype volume ($part)"
e2fsck -fp $part >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
handleError "Could not check before resize (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
resize2fs $part >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
handleError "Could not resize $part (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
e2fsck -fp $part >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not check after resize (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
;;
*)
echo " * Not expanding ($part -- $fstype)"
debugPause
;;
esac
debugPause
runPartprobe "$disk"
}
# Gets the filesystem type of the partition passed
#
# $1 is the partition
fsTypeSetting() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
local blk_fs=$(blkid -po udev $part | awk -F= /FS_TYPE=/'{print $2}')
case $blk_fs in
btrfs)
fstype="btrfs"
;;
ext[2-4])
fstype="extfs"
;;
hfsplus)
fstype="hfsp"
;;
ntfs)
fstype="ntfs"
;;
swap)
fstype="swap"
;;
vfat)
fstype="fat"
;;
xfs)
fstype="xfs"
;;
*)
fstype="imager"
;;
esac
}
# Gets the disk part table UUID
#
# $1 is the disk
getDiskUUID() {
local disk="$1"
[[ -z $disk ]] && handleError "No disk passed (${FUNCNAME[0]})\n Args Passed: $*"
diskuuid=$(blkid -po udev $disk | awk -F= '/PART_TABLE_UUID=/{print $2}')
}
# Gets the partition entry name
#
# $1 is the partition
getPartName() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
partname=$(blkid -po udev $part | awk -F= '/PART_ENTRY_NAME=/{print $2}')
}
# Gets the partition entry type
#
# $1 is the partition
getPartType() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
parttype=$(blkid -po udev $part | awk -F= '/PART_ENTRY_TYPE=/{print $2}')
}
# Gets the partition fs UUID
#
# $1 is the partition
getPartFSUUID() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
partfsuuid=$(blkid -po udev $part | awk -F= '/FS_UUID=/{print $2}')
}
# Gets the partition entry UUID
#
# $1 is the partition
getPartUUID() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
partuuid=$(blkid -po udev $part | awk -F= '/PART_ENTRY_UUID=/{print $2}')
}
# Gets the entry schemed (dos, gpt, etc...)
#
# $1 is the partition
getPartitionEntryScheme() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
scheme=$(blkid -po udev $part | awk -F= '/PART_ENTRY_SCHEME=/{print $2}')
}
# Checks if the partition is dos extended (mbr with logical parts)
#
# $1 is the partition
partitionIsDosExtended() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
local scheme=""
getPartitionEntryScheme "$part"
debugEcho "scheme = $scheme" 1>&2
case $scheme in
dos)
echo "no"
;;
*)
local parttype=""
getPartType "$part"
debugEcho "parttype = $parttype" 1>&2
[[ $parttype == +(0x5|0xf) ]] && echo "yes" || echo "no"
;;
esac
debugPause
}
# Returns the block size of a partition
#
# $1 is the partition
# $2 is the variable to set
getPartBlockSize() {
local part="$1"
local varVar="$2"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $varVar ]] && handleError "No variable to set passed (${FUNCNAME[0]})\n Args Passed: $*"
printf -v "$varVar" $(blockdev --getpbsz $part)
}
# Prepares location info for uploads
#
# $1 is the image path
prepareUploadLocation() {
local imagePath="$1"
[[ -z $imagePath ]] && handleError "No image path passed (${FUNCNAME[0]})\n Args Passed: $*"
dots "Preparing backup location"
if [[ ! -d $imagePath ]]; then
mkdir -p $imagePath >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
handleError "Failed to create image capture path (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
fi
echo "Done"
debugPause
dots "Setting permission on $imagePath"
chmod -R 777 $imagePath >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Failed to set permissions (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
debugPause
dots "Removing any pre-existing files"
rm -Rf $imagePath/* >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not clean files (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
debugPause
}
# Shrinks partitions for upload (resizable images only)
#
# $1 is the partition
# $2 is the fstypes file location
# $3 is the fixed partition numbers empty ok
shrinkPartition() {
local part="$1"
local fstypefile="$2"
local fixed="$3"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $fstypefile ]] && handleError "No type file passed (${FUNCNAME[0]})\n Args Passed: $*"
local disk=""
local part_number=0
getDiskFromPartition "$part"
getPartitionNumber "$part"
local is_fixed=$(echo $fixed | awk "/(^$part_number:|:$part_number:|:$part_number$|^$part_number$)/{print 1}")
if [[ $is_fixed -eq 1 ]]; then
echo " * Not shrinking ($part) fixed size"
debugPause
return
fi
local fstype=""
fsTypeSetting "$part"
echo "$part $fstype" >> $fstypefile
local size=0
local tmpoutput=""
local sizentfsresize=0
local sizeextresize=0
local sizefd=0
local tmp_success=""
local test_string=""
local do_resizefs=0
local do_resizepart=0
local extminsize=0
local block_size=0
local sizeextresize=0
local adjustedfdsize=0
local part_block_size=0
case $fstype in
ntfs)
ntfsresize -f -i -v -P $part >/tmp/tmpoutput.txt 2>&1
if [[ ! $? -eq 0 ]]; then
echo " * Not shrinking ($part) trying fixed size"
debugPause
echo "$(cat "$imagePath/d1.fixed_size_partitions"):${part_number}" > "$imagePath/d1.fixed_size_partitions"
return
#handleError " * (${FUNCNAME[0]})\n Args Passed: $*\n\nFatal Error, unable to find size data out on $part. Cmd: ntfsresize -f -i -v -P $part"
fi
tmpoutput=$(cat /tmp/tmpoutput.txt)
size=$(cat /tmp/tmpoutput.txt | grep "You might resize" | cut -d" " -f5)
[[ -z $size ]] && handleError " * (${FUNCNAME[0]})\n Args Passed: $*\n\nFatal Error, Unable to determine possible ntfs size\n * To better help you debug we will run the ntfs resize\n\t but this time with full output, please wait!\n\t $(cat /tmp/tmpoutput.txt)"
rm /tmp/tmpoutput.txt >/dev/null 2>&1
sizentfsresize=$((size / 1000))
let sizentfsresize+=300000
sizentfsresize=$((sizentfsresize * 1${percent} / 100))
sizefd=$((sizentfsresize * 103 / 100))
echo " * Possible resize partition size: $sizentfsresize k"
dots "Running resize test $part"
ntfsresize -f -n -s ${sizentfsresize}k $part </usr/share/fog/lib/EOFNTFSRESTORE >/tmp/tmpoutput.txt 2>&1
local ntfsstatus="$?"
tmpoutput=$(cat /tmp/tmpoutput.txt)
test_string=$(cat /tmp/tmpoutput.txt | egrep -io "(ended successfully|bigger than the device size|volume size is already OK)" | tr -d '[[:space:]]')
echo "Done"
debugPause
rm /tmp/tmpoutput.txt >/dev/null 2>&1
case $test_string in
endedsuccessfully)
echo " * Resize test was successful"
do_resizefs=1
do_resizepart=1
ntfsstatus=0
;;
biggerthanthedevicesize)
echo " * Not resizing filesystem $part (part too small)"
ntfsstatus=0
;;
volumesizeisalreadyOK)
echo " * Not resizing filesystem $part (already OK)"
do_resizepart=1
ntfsstatus=0
;;
esac
[[ ! $ntfsstatus -eq 0 ]] && handleError "Resize test failed!\n $tmpoutput\n (${FUNCNAME[0]})\n Args Passed: $*"
if [[ $do_resizefs -eq 1 ]]; then
debugPause
dots "Resizing filesystem"
ntfsresize -f -s ${sizentfsresize}k $part < /usr/share/fog/lib/EOFNTFS >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not resize disk (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
fi
if [[ $do_resizepart -eq 1 ]]; then
debugPause
dots "Resizing partition $part"
getPartBlockSize "$part" "part_block_size"
case $osid in
[1-2]|4)
resizePartition "$part" "$sizentfsresize" "$imagePath"
[[ $osid -eq 2 ]] && correctVistaMBR "$disk"
;;
[5-7]|9)
[[ $part_number -eq $win7partcnt ]] && part_start=$(blkid -po udev $part 2>/dev/null | awk -F= '/PART_ENTRY_OFFSET=/{printf("%.0f\n",$2*'$part_block_size'/1000)}') || part_start=1048576
if [[ -z $part_start || $part_start -lt 1 ]]; then
echo "Failed"
debugPause
handleError "Unable to determine disk start location (${FUNCNAME[0]})\n Args Passed: $*"
fi
adjustedfdsize=$((sizefd + part_start))
resizePartition "$part" "$adjustedfdsize" "$imagePath"
;;
esac
echo "Done"
fi
resetFlag "$part"
;;
extfs)
dots "Checking $fstype volume ($part)"
e2fsck -fp $part >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "e2fsck failed to check $part (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
debugPause
extminsize=$(resize2fs -P $part 2>/dev/null | awk -F': ' '{print $2}')
block_size=$(dumpe2fs -h $part 2>/dev/null | awk /^Block\ size:/'{print $3}')
size=$((extminsize * block_size))
sizeextresize=$((size * 103 / 100 / 1024))
[[ -z $sizeextresize || $sizeextresize -lt 1 ]] && handleError "Error calculating the new size of extfs ($part) (${FUNCNAME[0]})\n Args Passed: $*"
dots "Shrinking $fstype volume ($part)"
resize2fs $part -M >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not shrink $fstype volume ($part) (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
debugPause
dots "Shrinking $part partition"
resizePartition "$part" "$sizeextresize" "$imagePath"
echo "Done"
debugPause
dots "Resizing $fstype volume ($part)"
resize2fs $part >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
handleError "Could resize $fstype volume ($part) (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
e2fsck -fp $part >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
debugPause
handleError "Could not check expanded volume ($part) (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
;;
*)
echo " * Not shrinking ($part $fstype)"
;;
esac
debugPause
}
# Resets the dirty bits on a partition
#
# $1 is the part
resetFlag() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
local fstype=""
fsTypeSetting "$part"
case $fstype in
ntfs)
dots "Clearing ntfs flag"
ntfsfix -b -d $part >/dev/null 2>&1
case $? in
0)
echo "Done"
;;
*)
echo "Failed"
;;
esac
;;
esac
}
# Counts the partitions containing the fs type as passed
#
# $1 is the disk
# $2 is the part type to look for
# $3 is the variable to store the count into. This is
# a variable variable
countPartTypes() {
local disk="$1"
local parttype="$2"
local varVar="$3"
[[ -z $disk ]] && handleError "No disk passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $parttype ]] && handleError "No partition type passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $varVar ]] && handleError "No variable to set passed (${FUNCNAME[0]})\n Args Passed: $*"
local count=0
local fstype=""
local parts=""
local part=""
getPartitions "$disk"
for part in $parts; do
fsTypeSetting "$part"
case $fstype in
$parttype)
let count+=1
;;
esac
done
printf -v "$varVar" "$count"
}
# Writes the image to the disk
#
# $1 = Source File
# $2 = Target
# $3 = mc task or not (not required)
writeImage() {
local file="$1"
local target="$2"
local mc="$3"
[[ -z $target ]] && handleError "No target to place image passed (${FUNCNAME[0]})\n Args Passed: $*"
mkfifo /tmp/pigz1
case $mc in
yes)
udp-receiver --nokbd --portbase $port --ttl 32 --mcast-rdv-address $storageip 2>/dev/null >/tmp/pigz1 &
;;
*)
[[ -z $file ]] && handleError "No source file passed (${FUNCNAME[0]})\n Args Passed: $*"
cat $file >/tmp/pigz1 &
;;
esac
if [[ $imgFormat -eq 1 || $imgLegacy -eq 1 ]]; then
echo " * Imaging using Partimage"
pigz -d -c </tmp/pigz1 | partimage restore $target stdin -f3 -b 2>/tmp/status.fog
else
echo " * Imaging using Partclone"
pigz -d -c </tmp/pigz1 | partclone.restore --ignore_crc -O $target -N -f 1
fi
[[ ! $? -eq 0 ]] && handleError "Image failed to restore and exited with exit code $? (${FUNCNAME[0]})\n Args Passed: $*"
rm -rf /tmp/pigz1 >/dev/null 2>&1
}
# Gets the valid restore parts. They're only
# valid if the partition data exists for
# the partitions on the server
#
# $1 = Disk (e.g. /dev/sdb)
# $2 = Disk number (e.g. 1)
# $3 = ImagePath (e.g. /net/foo)
getValidRestorePartitions() {
local disk="$1"
local disk_number="$2"
local imagePath="$3"
local setrestoreparts="$4"
[[ -z $disk ]] && handleError "No disk passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $disk_number ]] && handleError "No disk number passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $imagePath ]] && handleError "No image path passed (${FUNCNAME[0]})\n Args Passed: $*"
local valid_parts=""
local parts=""
local part=""
local imgpart=""
local part_number=0
getPartitions "$disk"
for part in $parts; do
getPartitionNumber "$part"
[[ $imgPartitionType != all && $imgPartitionType != $part_number ]] && continue
case $osid in
[1-2])
[[ ! -f $imagePath ]] && imgpart="$imagePath/d${disk_number}p${part_number}.img*" || imgpart="$imagePath"
;;
4|[5-7]|9)
[[ ! -f $imagePath/sys.img.000 ]] && imgpart="$imagePath/d${disk_number}p${part_number}.img*"
if [[ -z $imgpart ]]; then
case $win7partcnt in
1)
[[ $part_number -eq 1 ]] && imgpart="$imagePath/sys.img.*"
;;
2)
[[ $part_number -eq 1 ]] && imgpart="$imagePath/rec.img.000"
[[ $part_number -eq 2 ]] && imgpart="$imagePath/sys.img.*"
;;
3)
[[ $part_number -eq 1 ]] && imgpart="$imagePath/rec.img.000"
[[ $part_number -eq 2 ]] && imgpart="$imagePath/rec.img.001"
[[ $part_number -eq 3 ]] && imgpart="$imagePath/sys.img.*"
;;
esac
fi
;;
*)
imgpart="$imagePath/d${disk_number}p${part_number}.img*"
;;
esac
ls $imgpart >/dev/null 2>&1
[[ $? -eq 0 ]] && valid_parts="$valid_parts $part"
done
[[ -z $setrestoreparts ]] && restoreparts=$(echo $valid_parts | uniq | sort -V) || restoreparts="$(echo $setrestoreparts | uniq | sort -V)"
}
# Makes all swap partitions and sets uuid's in linux setups
#
# $1 = Disk (e.g. /dev/sdb)
# $2 = Disk number (e.g. 1)
# $3 = ImagePath (e.g. /net/foo)
# $4 = ImagePartitionType (e.g. all, mbr, 1, 2, 3, etc.)
makeAllSwapSystems() {
local disk="$1"
local disk_number="$2"
local imagePath="$3"
local imgPartitionType="$4"
[[ -z $disk ]] && handleError "No disk passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $disk_number ]] && handleError "No drive number passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $imagePath ]] && handleError "No image path passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $imgPartitionType ]] && handleError "No image partition type passed (${FUNCNAME[0]})\n Args Passed: $*"
local swapuuidfilename=""
swapUUIDFileName "$imagePath" "$disk_number"
local parts=""
local part=""
local part_number=0
getPartitions "$disk"
for part in $parts; do
getPartitionNumber "$part"
[[ $imgPartitionType == all || $imgPartitionType -eq $part_number ]] && makeSwapSystem "$swapuuidfilename" "$part"
done
runPartprobe "$disk"
}
# Changes the hostname on windows systems
#
# $1 = Partition
changeHostname() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ -z $hostname || $hostearly -eq 0 ]] && return
REG_HOSTNAME_KEY1="\ControlSet001\Services\Tcpip\Parameters\NV Hostname"
REG_HOSTNAME_KEY2="\ControlSet001\Services\Tcpip\Parameters\Hostname"
REG_HOSTNAME_KEY3="\ControlSet001\Services\Tcpip\Parameters\NV HostName"
REG_HOSTNAME_KEY4="\ControlSet001\Services\Tcpip\Parameters\HostName"
REG_HOSTNAME_KEY5="\ControlSet001\Control\ComputerName\ActiveComputerName\ComputerName"
REG_HOSTNAME_KEY6="\ControlSet001\Control\ComputerName\ComputerName\ComputerName"
REG_HOSTNAME_KEY7="\ControlSet001\services\Tcpip\Parameters\NV Hostname"
REG_HOSTNAME_KEY8="\ControlSet001\services\Tcpip\Parameters\Hostname"
REG_HOSTNAME_KEY9="\ControlSet001\services\Tcpip\Parameters\NV HostName"
REG_HOSTNAME_KEY10="\ControlSet001\services\Tcpip\Parameters\HostName"
REG_HOSTNAME_KEY11="\CurrentControlSet\Services\Tcpip\Parameters\NV Hostname"
REG_HOSTNAME_KEY12="\CurrentControlSet\Services\Tcpip\Parameters\Hostname"
REG_HOSTNAME_KEY13="\CurrentControlSet\Services\Tcpip\Parameters\NV HostName"
REG_HOSTNAME_KEY14="\CurrentControlSet\Services\Tcpip\Parameters\HostName"
REG_HOSTNAME_KEY15="\CurrentControlSet\Control\ComputerName\ActiveComputerName\ComputerName"
REG_HOSTNAME_KEY16="\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName"
REG_HOSTNAME_KEY17="\CurrentControlSet\services\Tcpip\Parameters\NV Hostname"
REG_HOSTNAME_KEY18="\CurrentControlSet\services\Tcpip\Parameters\Hostname"
REG_HOSTNAME_KEY19="\CurrentControlSet\services\Tcpip\Parameters\NV HostName"
REG_HOSTNAME_KEY20="\CurrentControlSet\services\Tcpip\Parameters\HostName"
dots "Mounting directory"
if [[ ! -d /ntfs ]]; then
mkdir -p /ntfs >/dev/null 2>&1
if [[ ! $? -eq 0 ]]; then
echo "Failed"
debugPause
handleError " * Could not create mount location (${FUNCNAME[0]})\n Args Passed: $*"
fi
fi
umount /ntfs >/dev/null 2>&1
ntfs-3g -o remove_hiberfile,rw $part /ntfs >/tmp/ntfs-mount-output 2>&1
case $? in
0)
echo "Done"
debugPause
;;
*)
echo "Failed"
debugPause
handleError " * Could not mount $part (${FUNCNAME[0]})\n Args Passed: $*\n Reason: $(cat /tmp/ntfs-mount-output)"
;;
esac
if [[ ! -f /usr/share/fog/lib/EOFREG ]]; then
key1="$REG_HOSTNAME_KEY1"
key2="$REG_HOSTNAME_KEY2"
key3="$REG_HOSTNAME_KEY3"
key4="$REG_HOSTNAME_KEY4"
key5="$REG_HOSTNAME_KEY5"
key6="$REG_HOSTNAME_KEY6"
key7="$REG_HOSTNAME_KEY7"
key8="$REG_HOSTNAME_KEY8"
key9="$REG_HOSTNAME_KEY9"
key10="$REG_HOSTNAME_KEY10"
key11="$REG_HOSTNAME_KEY11"
key12="$REG_HOSTNAME_KEY12"
key13="$REG_HOSTNAME_KEY13"
key14="$REG_HOSTNAME_KEY14"
key15="$REG_HOSTNAME_KEY15"
key16="$REG_HOSTNAME_KEY16"
key17="$REG_HOSTNAME_KEY17"
key18="$REG_HOSTNAME_KEY18"
key19="$REG_HOSTNAME_KEY19"
key20="$REG_HOSTNAME_KEY20"
case $osid in
1)
regfile="$REG_LOCAL_MACHINE_XP"
;;
2|4|[5-7]|9)
regfile="$REG_LOCAL_MACHINE_7"
;;
esac
echo "ed $key1" >/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key2" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key3" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key4" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key5" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key6" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key7" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key8" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key9" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key10" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key11" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key12" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key13" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key14" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key15" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key16" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key17" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key18" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key19" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "ed $key20" >>/usr/share/fog/lib/EOFREG
echo "$hostname" >>/usr/share/fog/lib/EOFREG
echo "q" >> /usr/share/fog/lib/EOFREG
echo "y" >> /usr/share/fog/lib/EOFREG
echo >> /usr/share/fog/lib/EOFREG
fi
if [[ -e $regfile ]]; then
dots "Changing hostname"
reged -e $regfile < /usr/share/fog/lib/EOFREG >/dev/null 2>&1
case $? in
[0-2])
echo "Done"
debugPause
;;
*)
echo "Failed"
debugPause
umount /ntfs >/dev/null 2>&1
echo " * Failed to change hostname"
return
;;
esac
fi
rm -rf /usr/share/fog/lib/EOFREG
umount /ntfs >/dev/null 2>&1
}
# Fixes windows 7/8 boot, though may need
# to be updated to only impact windows 7
# in which case we need a more dynamic method
#
# $1 is the partition
fixWin7boot() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
[[ $osid != [5-7] ]] && return
local fstype=""
fsTypeSetting "$part"
[[ $fstype != ntfs ]] && return
dots "Mounting partition"
if [[ ! -d /bcdstore ]]; then
mkdir -p /bcdstore >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
handleError " * Could not create mount location (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
fi
ntfs-3g -o remove_hiberfile,rw $part /bcdstore >/tmp/ntfs-mount-output 2>&1
case $? in
0)
echo "Done"
debugPause
;;
*)
echo "Failed"
debugPause
handleError " * Could not mount $part (${FUNCNAME[0]})\n Args Passed: $*\n Reason: $(cat /tmp/ntfs-mount-output)"
;;
esac
if [[ ! -f /bcdstore/Boot/BCD ]]; then
umount /bcdstore >/dev/null 2>&1
return
fi
dots "Backing up and replacing BCD"
mv /bcdstore/Boot/BCD{,.bak} >/dev/null 2>&1
case $? in
0)
;;
*)
echo "Failed"
debugPause
umount /bcdstore >/dev/null 2>&1
echo " * Could not create backup"
return
;;
esac
cp /usr/share/fog/BCD /bcdstore/Boot/BCD >/dev/null 2>&1
case $? in
0)
echo "Done"
debugPause
umount /bcdstore >/dev/null 2>&1
;;
*)
echo "Failed"
debugPause
umount /bcdstore >/dev/null 2>&1
echo " * Could not copy our bcd file"
return
;;
esac
umount /bcdstore >/dev/null 2>&1
}
# Clears out windows hiber and page files
#
# $1 is the partition
clearMountedDevices() {
local part="$1"
[[ -z $part ]] && handleError "No partition passed (${FUNCNAME[0]})\n Args Passed: $*"
if [[ ! -d /ntfs ]]; then
mkdir -p /ntfs >/dev/null 2>&1
case $? in
0)
umount /ntfs >/dev/null 2>&1
;;
*)
handleError "Could not create mount point /ntfs (${FUNCNAME[0]})\n Args Passed: $*"
;;
esac
fi
case $osid in
4|[5-7]|9)
local fstype=""
fsTypeSetting "$part"
REG_HOSTNAME_MOUNTED_DEVICES_7="\MountedDevices"