-
Notifications
You must be signed in to change notification settings - Fork 0
/
KVM.pm
executable file
·2807 lines (2598 loc) · 128 KB
/
KVM.pm
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
#!/usr/bin/perl
# Utility drawer for Appliance.
# $Id: KVM.pm.in,v 1.6 2011/07/07 20:29:55
#
# $Log: KVM,v $
# Revision 1.0 2011/07/07 20:29:55 Abhinav
#package KVM;
require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK =
qw(&write_log $PATH_TO_APPLIANCE_LOG &fail &success &initialize &search &addVIRTUALMACHINE &assignPIP &rebootVM &checkUPTIME &enableprivateNET &createnetworkXML &disprivNETWORK &vmCLONE &createBACKUP &createcustomIMAGE &delBACKUP &delcustomIMAGE &vmREVERTSS &vmSNAPSHOT &attachVolume &detachVolume &deallocatePIP &restorecustomIMAGE &restoreSTIMAGE &assignPIPwin);
#
$debug = 0; # set to zero to turn off debugging
$PATH_TO_APPLIANCE_LOG = "/var/log/kvmrpc.log";
sub fail { 0 }
sub success { 1 }
#use strict;
use DBI;
use File::Remote;
use Symbol;
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin:/bin:/usr/local/bin";
## FUNCTIONS start here .
####################################################################
# write_log($msg): appends a time/date stamp and $msg to the log file.
####################################################################
sub write_log {
my $msg = $_[0];
$msg = date_string() . $msg;
if ( open( LOGFILE, ">> $PATH_TO_APPLIANCE_LOG" ) ) {
print LOGFILE $msg . "\n";
close LOGFILE;
}
else {
print("Couldn't open $PATH_TO_APPLIANCE_LOG!!!");
}
if ($debug) {
print $msg. "\n";
}
}
####################################################################
# date_string: returns a timestamp suitable for log files.
####################################################################
sub date_string {
# This should be fast -- no shell commands in here.
# This routine benchmarked at approx .18 ms,
# so we can run this more than 1000 times/second
# QF March 31 1998 (code ripped from bunyan --gb)
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time);
$mon++; # Map 0-11 => 1-12
return sprintf "%s:%s:%s:%s:%s:%s # ",
$year + 1900,
$mon < 10 ? "0$mon" : $mon,
$mday < 10 ? "0$mday" : $mday,
$hour < 10 ? "0$hour" : $hour,
$min < 10 ? "0$min" : $min,
$sec < 10 ? "0$sec" : $sec;
}
####################################################################
# Change: alters a line in a file
# Change($file, $find, $replacewith)
####################################################################
sub Change {
my $file = $_[0];
my $find = $_[1];
my $replace = $_[2];
my $foundit = 0;
my @infile;
write_log("Changing [$find] to [$replace] in $file...");
if ( -f $file ) {
unless ( -w $file ) {
write_log(
" $file isn't writeable! Can't change [$find] to [$replace]!");
return fail;
}
}
else {
write_log(" Can't \"Change\" a file that doesnt exist! [$file]");
return fail;
}
unless ( open( INFILE, "$file" ) ) {
write_log(" Unable to open $file for reading");
return fail;
}
@infile = <INFILE>;
close INFILE;
unless ( open( OUTFILE, "> $file" ) ) {
write_log(" Unable to truncate $file for writing");
return fail;
}
foreach (@infile) {
$foundit++ if /$find/;
s/$find/$replace/;
print OUTFILE;
}
close OUTFILE;
if ( $foundit == 0 ) {
write_log("WARNING: Never found [$find] in $file");
}
return success;
}
####################################################################
# search: search a line in file
# search($find, $file)
####################################################################
sub search {
my $find = $_[0];
my $file = $_[1];
my @infile;
write_log("Searching [$find] in $file...");
if ( -f $file ) {
}
else {
write_log(" Can't \"Change\" a file that doesnt exist! [$file]");
return fail;
}
unless ( open( INFILE, "$file" ) ) {
write_log(" Unable to open $file for reading");
return fail;
}
@infile = <INFILE>;
close INFILE;
foreach (@infile) {
return $_ if /$find/;
}
return fail;
}
####################################################################
# Append: adds a line to a file
# Append($file, $line);
####################################################################
sub Append {
my $file = $_[0];
my $line = $_[1] or return fail;
write_log("Appending [$line] to [$file]");
if ( -f $file ) {
unless ( -w $file ) {
write_log(" $file isn't writeable! Can't append [$line]!");
return fail;
}
}
if ( open( OUT, ">> $file" ) ) {
print OUT "$line\n";
close OUT;
}
else {
write_log("Couldn't open $file for appending!");
return fail;
}
return success;
}
####################################################################
# inititalize: Initialize a blank file
# initialize($file);
####################################################################
sub initialize {
my $file = $_[0] or return fail;
write_log("Initializing [$file]");
if ( -f $file ) {
unless ( -w $file ) {
write_log(" $file isn't writeable! Can't append [$line]!");
return fail;
}
}
if ( open( OUT, "> $file" ) ) {
print OUT "";
close OUT;
}
else {
write_log("Couldn't open $file for Initializing!");
return fail;
}
return success;
}
####################################################################
# replace: Replace a file with other
# replace($file, $line);
####################################################################
sub replace {
my $file = $_[0];
my $file2 = $_[1] or return fail;
write_log("Moving [$file] if it exists to $file2");
system("rm -rf $file2");
system("mv $file $file2");
return success;
}
####################################################################
# remove: Remove a file
# remove($file);
####################################################################
sub remove {
my $file = $_[0] or return fail;
write_log("REMoving [$file] if it exists ");
system("rm -rf $file");
return success;
}
###################################################################
# ModifyFile: It modifies a file for deleting a stanza which is
# between Marks
#
# Usage : ModifyFile --delete /path/file startmark.endmark
##################################################################
sub ModifyFile {
my $file = shift;
my $opening = shift;
my $closing = shift;
chomp($opening);
chomp($closing);
write_log("Parameters :: $file , $opening ,$closing");
open FILE, "$file" or print "FATAL:: Failed to open $file";
my $copy = 1;
write_log("Reading the file $file ");
while (<FILE>) {
chomp $_;
if ( /$opening/ && $copy eq 1 ) {
$copy = 0;
write_log("Found Starting of the Share Block ");
}
if ( $copy eq 1 ) {
$newfiletext .= $_ . "\n";
}
if ( $_ eq $closing && $copy eq 0 ) {
write_log("Found Closing of the Share Block ");
$copy = 1;
}
}
close FILE;
open FILE, ">$file" or print "FATAL::can't write on $file";
print FILE $newfiletext;
close FILE;
}
###########################################################################
## Append- Appedsa line in the file.
############################################################################
sub Append {
my $file = $_[0];
my $line = $_[1] or return fail;
write_log("Appending [$line] to [$file]");
if ( -f $file ) {
unless ( -w $file ) {
write_log(" $file isn't writeable! Can't append [$line]!");
return fail;
}
}
if ( open( OUT, ">> $file" ) ) {
print OUT "$line\n";
close OUT;
}
else {
write_log("Couldn't open $file for appending!");
return fail;
}
return success;
#################################################################################################
# Assign Additional Public IP's to guest machines
# #################################################################################################
sub assignPIP {
my $cust_id = shift;
my $server_id = shift;
my $cust_name = shift;
my $os_type = shift;
my $os_name = shift;
my $wan_primary_ip = shift;
my $wan_sec_ip_list = shift;
my $noaddip = shift;
######################################################################################################
# Check for Machine's Existance
write_log("WAN Primary ip : $wan_primary_ip and WAN Secondry ip list :$wan_sec_ip_list and noaddip :$noaddip \n");
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);
if ( !-f "/etc/libvirt/qemu/$cust_id-$server_id.xml" ) {
write_log("Machine $cust_id-$server_id doesn't exists");
return fail;
}
else {
write_log("Machine name $cust_id-$server_id exist ");
system("virsh list | grep -i $cust_id-$server_id >/dev/null");
###### Check for guest machine state
if ( $? == 0 ) {
write_log(" Machine is Running.Powering it off ");
system("virsh shutdown $cust_id-$server_id >/dev/null");
while($? == 0) {
write_log(" Machine still shutting down.");
system("virsh list|grep -i $cust_id-$server_id >/dev/null");
sleep(5);
}
if ( -d "/mnt/$cust_id-$server_id" ) {
write_log(
" Machine is already Monuted. Assigning IP's to it");
}
else {
write_log("Mouting Disk");
system("mkdir -p /mnt/$cust_id-$server_id");
system("guestmount -a /var/lib/libvirt/images/$cust_id-$server_id.qcow2 -i --rw /mnt/$cust_id-$server_id");
}
}
if ( $os_type eq "linux" ) {
my $nooldip = $ipsno + $noaddip;
if ( ( $os_name eq "centos" ) || ( $os_name eq "redhat" ) ) {
for ( $i = $noaddip ; $i < $nooldip ; $i++ ) {
open( OUT,"/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3:$i");
my $filei ="/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3:$i";
$line7 = "DEVICE=ens3:$i";
$line8 = "ONBOOT=yes";
$line9 = "IPADDR=$ips[$i]";
$line10 = "NETMASK=255.255.255.0";
if ( $response =
Append( $filei, $line7 )
&& Append( $filei, $line8 )
&& Append( $filei, $line9 )
&& Append( $filei, $line10 ) )
{
write_log(
"Additional IP- $ips[$i] assigned successfully"
);
qx{perl -i.bak -pe "s#(\s*)(?=<parameter name='IP' value='$wan_primary_ip'/>)#\$1<parameter name='IP' value='$ips[$i]'/>\n#\n" /etc/libvirt/qemu/$cust_id-$server_id.xml};
}
}
system("umount /mnt/$cust_id-$server_id");
system("rm -rf /mnt/$cust_id-$server_id");
system("cp /etc/libvirt/qemu/$cust_id-$server_id.xml /etc/libvirt/qemu/$cust_id-$server_id.xml-bak");
system("virsh undefine $cust_id-$server_id >/dev/null");
system("mv /etc/libvirt/qemu/$cust_id-$server_id.xml-bak /etc/libvirt/qemu/$cust_id-$server_id.xml");
system("virsh define /etc/libvirt/qemu/$cust_id-$server_id.xml >/dev/null");
system("virsh start $cust_id-$server_id >/dev/null");
return success;
}
elsif ( ( $os_name eq "debian" ) || ( $os_name eq "ubuntu" ) ) {
my $file = "/mnt/$cust_id-$server_id/etc/network/interfaces";
for ( $i = $noaddip ; $i < $nooldip ; $i++ ) {
$line7 = "auto eth0:$i";
$line8 = "iface eth0:$i inet static";
$line9 = "address $ips[$i-$noaddip]";
$line10 = "netmask 255.255.255.0";
if ( $response =
Append( $file, $line7 )
&& Append( $file, $line8 )
&& Append( $file, $line9 )
&& Append( $file, $line10 ) )
{
write_log("Additional IP- $ips[$i-$noaddip] assigned successfully");
qx{perl -i.bak -pe "s#(\s*)(?=<parameter name='IP' value='$wan_primary_ip'/>)#\$1<parameter name='IP' value='$ips[$i-$noaddip]'/>\n#\n" /etc/libvirt/qemu/$cust_id-$server_id.xml};
}
}
system("umount /mnt/$cust_id-$server_id");
system("rm -rf /mnt/$cust_id-$server_id");
system("cp /etc/libvirt/qemu/$cust_id-$server_id.xml /etc/libvirt/qemu/$cust_id-$server_id.xml-bak");
system("virsh undefine $cust_id-$server_id >/dev/null");
system("mv /etc/libvirt/qemu/$cust_id-$server_id.xml-bak /etc/libvirt/qemu/$cust_id-$server_id.xml");
system("virsh define /etc/libvirt/qemu/$cust_id-$server_id.xml >/dev/null");
system("virsh start $cust_id-$server_id >/dev/null");
return success;
}
else {
write_log(
"Something Went Wrong. Additional IP's assigmnet failed"
);
return fail;
}
}
} }
############################################################/########################
## ADD Virtual Machine
##
#####################################################################################
sub addVIRTUALMACHINE {
my $cust_id = shift;
my $server_id = shift;
my $cust_name = shift;
my $os_type = shift;
my $os_name = shift;
my $os_version = shift;
my $os_arch = shift;
my $os_image_location = shift;
my $host_name = shift;
my $ram = shift;
my $vcpu = shift;
my $storage = shift;
my $disk_path = "/var/lib/libvirt/images/$cust_id-$server_id.qcow2";
my $iso_path = "/var/lib/libvirt/images/$cust_id-$server_id.iso"
; # Cloud Init ISO, generated later.
my $admin_password = shift;
my $wan_mac_address = shift;
my $wan_primary_ip = shift;
my $wan_sec_ip_list = shift;
my $vm_host_vnc_port = shift;
my $vnc_password = shift;
my $ram = $ram * 1024;
my $dm = "G";
my $host_ip_address = "1.1.1.1";
my $resized = 0;
###########################################################################################
######## Check whether same hostname machine is not created previously#######################
if ( -e "/etc/libvirt/qemu/$cust_id-$server_id.xml" ) {
write_log("Machine name $cust_id-$server_id exist:: ");
return fail;
}
else
################### Create Virtual Machine #######################################################
{
system(
"qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/$cust_id-$server_id.qcow2 20$dm >/dev/null 2>/dev/null "
);
write_log(
"QCOW2 IMAGE HAS BEEN CREATED UNDER VAR-LIB-LIBVIRT-IMAGES-$cust_id-$server_id.qcow2"
);
write_log(
"qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/$cust_id-$server_id.qcow2 20$dm"
);
system("touch $iso_path"); # create a dummy
system("virt-install --connect qemu:///system --name $cust_id-$server_id --ram $ram --vcpus $vcpu --disk /var/lib/libvirt/images/$cust_id-$server_id.qcow2,format=qcow2 --graphics vnc,password=$vnc_password,port=$vm_host_vnc_port,listen=$host_ip_address --cdrom $iso_path --network network:phybrid,mac=$wan_mac_address >>/tmp/log 2>>/tmp/log");
write_log("virt-install --connect qemu:///system --name $cust_id-$server_id --ram $ram --vcpus $vcpu --disk /var/lib/libvirt/images/$cust_id-$server_id.qcow2,format=qcow2 --graphics vnc,password=$vnc_password,port=$vm_host_vnc_port,listen=$host_ip_address --cdrom $iso_path --network network:phybrid,mac=$wan_mac_address >>/tmp/log 2>>/tmp/log");
write_log("VIRT-INSTALL COMMAND CALLED FOR CUSTOMER ID $cust_id AND SERVER ID $server_id");
system("virsh destroy $cust_id-$server_id >>/tmp/log 2>>/tmp/log");
write_log("VIRSH DESTROY $cust_id-$server_id :: $?");
if ( $? != 0 ) {
write_log("Virtual Machine Not Created");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
write_log("REMOVE QCOW2 IMAGE FROM PATH : VAR-LIB-LIBVIRT-IMAGES-$cust_id-$server_id.qcow2 ");
return fail;
}
if (
-e "/mnt/$os_image_location/$os_name-$os_version-$os_arch.qcow2"
)
{
write_log(
"COPYING IMAGE FROM /mnt/$os_image_location/'$os_name-$os_version-$os_arch.qcow2' TO /var/lib/libvirt/images/$cust_id-$server_id.qcow2"
);
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system(
"cp /mnt/$os_image_location/'$os_name-$os_version-$os_arch.qcow2' /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null"
);
}
else {
write_log("PATH : $os_image_location \n");
write_log(
"IMAGE IS NOT EXIST AT /mnt/$os_image_location/$os_name-$os_version-$os_arch.qcow2 \n"
);
system("virsh undefine $cust_id-$server_id ");
write_log("HOST UNDEFINED :: $cust_id-$server_id\n");
return fail;
}
############ Check for HDD Size to be increased ###########################################
if ( $storage != 20 ) {
# write_log("IMAGE IS MORE THAN 20 GB : TAKING BACKUP OF EXITING IMAGE \n");
# system("cp -rp /var/lib/libvirt/images/$cust_id-$server_id.qcow2 /iso/backupimages/$cust_id-$server_id.qcow2 > /dev/null");
# write_log("cp -rp /var/lib/libvirt/images/$cust_id-$server_id.qcow2 /iso/backupimages/$cust_id-$server_id.qcow2");
system("qemu-img create -f qcow2 -o preallocation=metadata /iso/tempimages/$cust_id-$server_id.qcow2 $storage$dm > /dev/null");
write_log("qemu-img create -f qcow2 -o preallocation=metadata /iso/tempimages/$cust_id-$server_id.qcow2 $storage$dm");
if ( $os_type eq "linux" ) {
write_log("Linux VIRT-RESIZE CALLED :\n ");
system("virt-resize --expand /dev/sda1 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 /iso/tempimages/$cust_id-$server_id.qcow2 >/dev/null");
if ( $? != 0 )
{
write_log("DELETING BACKUP IMAGE");
system("rm -rf /iso/backupimages/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
write_log("DELETING TEMP IMAGE AS WELL");
system("rm -rf /iso/tempimages/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system("virsh undefine $cust_id-$server_id >/dev/null 2>/dev/null");
write_log("HOST UNDEFINED :: $cust_id-$server_id\n");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
write_log("SOMETHING WENT WRONG WHILE RESIZEING :: $?");
return fail;
}
system("mv /iso/tempimages/$cust_id-$server_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 > /dev/null");
write_log("mv /iso/tempimages/$cust_id-$server_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
}
else {
write_log("VIRT-RESIZE CALLED (os version $os_version and storage size $storage)\n ");
if($storage < 60) {
write_log("Image will be too small for Windows Server 2012. $storage GB requested, 60 GB required.\n");
return fail;
}
elsif($os_version eq "2012" && $storage > 60) {
write_log("OS version $os_version Windows 2012 image is expanding.\n");
system("virt-resize --expand /dev/sda2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 /iso/tempimages/$cust_id-$server_id.qcow2 >>/tmp/resizelog");
$resized = 1;
}
elsif($os_version eq "2008" && $storage > 60) {
write_log("Windows 2008 image is expanding.\n");
system("virt-resize --expand /dev/sda2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 /iso/tempimages/$cust_id-$server_id.qcow2 >>/tmp/resizelog");
$resized = 1;
}
else { write_log("Resize skipped. Image was already the correct size.\n"); system("true"); }
if ( $? != 0 )
{
write_log("SOMETHING WENT WRONG WHILE RESIZEING :: $?");
write_log("DELETING BACKUP IMAGE");
system("rm -rf /iso/backupimages/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
write_log("DELETING TEMP IMAGE AS WELL");
system("rm -rf /iso/tempimages/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system("virsh undefine $cust_id-$server_id >/dev/null 2>/dev/null");
write_log("HOST UNDEFINED :: $cust_id-$server_id\n");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
return fail;
}
}
if($resized == 1) {
system("mv /iso/tempimages/$cust_id-$server_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 > /dev/null");
write_log("mv /iso/tempimages/$cust_id-$server_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
}
write_log("Disk Resized Successful, Moving Ahead");
write_log("DELETING BACKUP IMAGE /iso/backupimages/$cust_id-$server_id.qcow2 ");
system("rm -rf /iso/backupimages/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
}
################# Setting Hostname and Public IP #######################
system("mkdir -p /mnt/$cust_id-$server_id > /dev/null");
system("guestmount -a /var/lib/libvirt/images/$cust_id-$server_id.qcow2 -i --rw /mnt/$cust_id-$server_id > /dev/null");
################ Calculate Gateway #############################################
my @array1 = split( /\./, $wan_primary_ip ); # Perl Code to split strings of an ip address by delimiter "."
splice @array1, 3, 4; # perl Code to get the first three strings out of the splitted array
my $wan_ip_gateway = join( ".", @array1 ); # Perl Code to join the first three spliced strings of array
####################################################################################################
####################### IP Assignment ###########################################################
if ( $os_type eq "linux" ) {
if ( ( $os_name eq "centos" ) || ( $os_name eq "redhat" ) ) {
if ( ( $os_name eq "centos" ) && ( $os_version eq "7.0" ) )
{
my $file ="/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3";
system(">/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3");
$line1 = "DEVICE=ens3";
$line2 = "ONBOOT=yes";
$line3 = "HOTPLUG=no";
$line4 = "IPADDR=$wan_primary_ip";
$line5 = "NETMASK=255.255.255.0";
$line6 = "GATEWAY=$wan_ip_gateway.1";
write_log("Adding IP $wan_primary_ip to file");
if ( $response =
Append( $file, $line1 )
&& Append( $file, $line2 )
&& Append( $file, $line3 )
&& Append( $file, $line4 )
&& Append( $file, $line5 )
&& Append( $file, $line6 ) )
{
write_log(
" IP Assignment $wan_primary_ip Successful ");
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);
if ( $ipsno != 0 ) {
for ( $i = 0 ; $i < $ipsno ; $i++ ) {
open( OUT,
"/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3:$i"
);
my $filei =
"/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3:$i";
$line7 = "DEVICE=ens3:$i";
$line8 = "ONBOOT=yes";
$line9 = "IPADDR=$ips[$i]";
$line10 = "NETMASK=255.255.255.0";
if ( $response =
Append( $filei, $line7 )
&& Append( $filei, $line8 )
&& Append( $filei, $line9 )
&& Append( $filei, $line10 ) )
{
write_log(
"Additional IP- $ips[$i] assigned successfully"
);
}
}
}
}
else {
write_log(
"Something Went Wrong. Additional IP's assigmnet failed"
);
}
system(">/mnt/$cust_id-$server_id/etc/hostname");
my $file = "/mnt/$cust_id-$server_id/etc/hostname";
$line11 = "$host_name";
Append( $file, $line11 );
write_log(" Unmounting Disk");
write_log("OS Version : $os_version \n");
}
else {
my $file =
"/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-eth0";
system(
">/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-eth0"
);
$line1 = "DEVICE=eth0";
$line2 = "ONBOOT=yes";
$line3 = "HOTPLUG=no";
$line4 = "IPADDR=$wan_primary_ip";
$line5 = "NETMASK=255.255.255.0";
$line6 = "GATEWAY=$wan_ip_gateway.1";
write_log("Adding IP $wan_primary_ip to file");
if ( $response =
Append( $file, $line1 )
&& Append( $file, $line2 )
&& Append( $file, $line3 )
&& Append( $file, $line4 )
&& Append( $file, $line5 )
&& Append( $file, $line6 ) )
{
write_log(
" IP Assignment $wan_primary_ip Successful ");
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);
if ( $ipsno != 0 ) {
for ( $i = 0 ; $i < $ipsno ; $i++ ) {
open( OUT,"/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-eth0:$i");
my $filei ="/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-eth0:$i";
$line7 = "DEVICE=eth0:$i";
$line8 = "ONBOOT=yes";
$line9 = "IPADDR=$ips[$i]";
$line10 = "NETMASK=255.255.255.0";
if ( $response =
Append( $filei, $line7 )
&& Append( $filei, $line8 )
&& Append( $filei, $line9 )
&& Append( $filei, $line10 ) )
{
write_log(
"Additional IP- $ips[$i] assigned successfully"
);
}
}
}
}
else {
write_log(
"Something Went Wrong. Additional IP's assigmnet failed"
);
}
my $file =
"/mnt/$cust_id-$server_id/etc/sysconfig/network";
$line11 = "HOSTNAME=$host_name";
Append( $file, $line11 );
write_log(" Unmounting Disk");
write_log("OS Version : $os_version \n");
}
}
elsif ( ( $os_name eq "debian" ) || ( $os_name eq "ubuntu" ) ) {
my $file =
"/mnt/$cust_id-$server_id/etc/network/interfaces";
$line6 = "auto eth0";
$line1 = "iface eth0 inet static";
$line2 = "address $wan_primary_ip";
$line3 = "gateway $wan_ip_gateway.1";
$line4 = "netmask 255.255.255.0";
$line5 = "dns-nameservers 8.8.8.8";
if ( $response =
Append( $file, $line6 )
&& Append( $file, $line1 )
&& Append( $file, $line2 )
&& Append( $file, $line3 )
&& Append( $file, $line4 )
&& Append( $file, $line5 ) )
{
write_log(" IP Assignment $wan_primary_ip Successful ");
################# Adding Additional IP's##############################################################################
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);
if ( $ipsno != 0 ) {
for ( $i = 0 ; $i < $ipsno ; $i++ ) {
$line7 = "auto eth0:$i";
$line8 = "iface eth0:$i inet static";
$line9 = "address $ips[$i]";
$line10 = "netmask 255.255.255.0";
if ( $response =
Append( $file, $line7 )
&& Append( $file, $line8 )
&& Append( $file, $line9 )
&& Append( $file, $line10 ) )
{
write_log("Additional IP- $ips[$i] assigned successfully");
}
}
}
}
else {
write_log(
"Something Went Wrong. Additional IP's assigmnet failed"
);
}
######################## Set Hostname ##################################################
$line7 = "$host_name";
my $file = "/mnt/$cust_id-$server_id/etc/hostname";
Append( $file, $line7 );
write_log(" Unmounting Disk");
}
else {
write_log("IP Assignment went wrong. Either disk was not mounted or file was not accessible");
}
######################### SET PASSWORDS ######################################################################
$password_salt = qx{openssl passwd -1 $admin_password}; ##### Generate Password Salt
$old_password_salt =qx{cat /mnt/$cust_id-$server_id/etc/shadow | grep root | cut -d: -f2};
chomp($password_salt);
chomp($old_password_salt);
##Un Commented by Abhinav
#$output=`lsattr /mnt/$cust_id-$server_id/etc/shadow`;
system(
"cp /root/cloud.cfg-$os_name /mnt/$cust_id-$server_id/etc/cloud.cfg"
);
#system("cp /mnt/$cust_id-$server_id/etc/shadow /mnt/$cust_id-$server_id/etc/shadow-bak >/dev/null"); ###### Backup Shadow File
#qx{sed -i 's|$old_password_salt|$password_salt|' /mnt/$cust_id-$server_id/etc/shadow};
#sed 's/old/new/g' input.txt > output.txt
system(
"sed 's|$old_password_salt|$password_salt|g' /mnt/$cust_id-$server_id/etc/shadow > /mnt/$cust_id-$server_id/etc/shadow.new"
);
system(
"cp -p /mnt/$cust_id-$server_id/etc/shadow /mnt/$cust_id-$server_id/etc/shadow.orig >/dev/null"
);
system(
"cp -p /mnt/$cust_id-$server_id/etc/shadow.new /mnt/$cust_id-$server_id/etc/shadow >/dev/null"
);
#system("sed 's|root|$password_salt|g' /tmp/shadow > /tmp/shadow.new");
write_log(
"sed -i 's|$old_password_salt|$password_salt|' /mnt/$cust_id-$server_id/etc/shadow"
);
## if (($os_name eq "centos") || ($os_name eq "redhat"))
## {
## system("chattr +e /mnt/$cust_id-$server_id/etc/shadow");
## }
#$output=`lsattr /mnt/$cust_id-$server_id/etc/shadow`;
#######
write_log("SALTED password : $password_salt");
write_log("OLD SALTED password : $old_password_salt");
write_log("Password Changed to $admin_password");
######################################################################################################################
#################### UnMount File system ############################################################################
#$thehostname = `cat /mnt/$cust_id-$server_id/etc/hostname`;
#chomp($thehostname);
#$thehostname =~ s/\s+//g; # Why is chomp not working?
write_log(
"/usr/local/sbin/cloudconfig $host_name $wan_primary_ip /var/lib/libvirt/images/$cust_id-$server_id.iso"
);
system(
"/usr/local/sbin/cloudconfig $host_name $wan_primary_ip /var/lib/libvirt/images/$cust_id-$server_id.iso"
);
system("umount /mnt/$cust_id-$server_id");
system("rm -rf /mnt/$cust_id-$server_id");
}
#Staring windows machine script
#
elsif ( $os_type eq "windows" ) {
system("virsh shutdown $cust_id-$server_id >/dev/null");
while($? == 0) {
write_log(" Machine still shutting down.");
system("virsh list|grep -i $cust_id-$server_id >/dev/null");
sleep(5);
}
system("mkdir -p /mnt/$cust_id-$server_id > /dev/null");
write_log("MAKING SCRIPT FOR WINDOWS IMAGE :: mkdir -p /mnt/$cust_id-$server_id");
system("guestmount -a /var/lib/libvirt/images/$cust_id-$server_id.qcow2 -i --rw /mnt/$cust_id-$server_id > /dev/null");
system("cp -p /iso/set-config.bat /iso/set-config$cust_id-$server_id.bat");
qx{sed -i 's|SVR-2K8-10|$host_name|g' /iso/set-config$cust_id-$server_id.bat};
qx{sed -i 's|password|$admin_password|g' /iso/set-config$cust_id-$server_id.bat};
qx{sed -i 's|192.168.1.10|$wan_primary_ip|g' /iso/set-config$cust_id-$server_id.bat};
qx{sed -i 's|192.168.1.1|$wan_ip_gateway|g' /iso/set-config$cust_id-$server_id.bat};
if ( $os_version eq "2012" ) { # 2012 names interfaces differently. - Ryan
qx{sed -i 's|Local Area Connection|Ethernet|g' /iso/set-config$cust_id-$server_id.bat};
}
system("head -n -2 /iso/set-config$cust_id-$server_id.bat > /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
system("cp /root/cloudbase.msi /mnt/$cust_id-$server_id/cloudbase.msi > /dev/null");
write_log("WINDOWS SERVER PASSWORD FOR $cust_id-$server_id : $admin_password IP : $wan_primary_ip");
# system("umount /mnt/$cust_id-$server_id");
# system("rm -rf /mnt/$cust_id-$server_id");
#system("virsh start $cust_id-$server_id >/dev/null");
# end cloud-init configuration
# system("sleep 10");
}
## end of windows machine script
################## Modifying XML ##################################################################################################################
qx{perl -i.bak -pe "s#(\s*)(?=<source network='phybrid'/>)#\$1<filterref filter='clean-traffic'>\n\$1 <parameter name='IP' value='$wan_primary_ip'/>\n\$1</filterref>#\n" /etc/libvirt/qemu/$cust_id-$server_id.xml};
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);
my $string2008 = 'echo netsh interface ipv4 add address \"Local Area Connection\"';
system("echo >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
if ( $ipsno != 0 ) {
for ( $i = 0 ; $i < $ipsno ; $i++ ) {
qx{perl -i.bak -pe "s#(\s*)(?=<parameter name='IP' value='$wan_primary_ip'/>)#\$1<parameter name='IP' value='$ips[$i]'/>\n#\n" /etc/libvirt/qemu/$cust_id-$server_id.xml};
write_log("Adding IP $ips[$i]\n");
if($os_version eq "2012") {
system("echo netsh interface ipv4 add address Ethernet $ips[$i] 255.255.255.0 '> C:/iplog.txt' >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
write_log("echo netsh interface ipv4 add address Ethernet $ips[$i] 255.255.255.0 >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
}
elsif($os_version eq "2008") {
system("$string2008 $ips[$i] 255.255.255.0 >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
write_log("echo netsh interface ipv4 add address \"Local Area Connection\" $ips[$i] 255.255.255.0 >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat");
}
}
}
if($os_version eq "2008" || $os_version eq "2012") {
write_log("Appending DEL %~f0\n");
qx{echo DEL %~f0 >> /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat};
}
system("cp /mnt/$cust_id-$server_id/Windows/System32/GroupPolicy/Machine/Scripts/Startup/set-config.bat /tmp/set-config-tmp.bat");
system("mv /etc/libvirt/qemu/$cust_id-$server_id.xml /etc/libvirt/qemu/$cust_id-$server_id.xml-bak > /dev/null");
system("virsh undefine $cust_id-$server_id >/dev/null");
system("mv /etc/libvirt/qemu/$cust_id-$server_id.xml-bak /etc/libvirt/qemu/$cust_id-$server_id.xml > /dev/null");
system("virsh define /etc/libvirt/qemu/$cust_id-$server_id.xml >/dev/null");
#####################################################################################################################
############################################################################################
system("virsh attach-disk --targetbus ide --type ide --live --persistent `virsh list|grep $cust_id-$server_id|cut -f2 -d' '` /var/lib/libvirt/images/$cust_id-$server_id.iso hdd >>/tmp/disklog 2>&1");
system("umount /mnt/$cust_id-$server_id; rmdir /mnt/$cust_id-$server_id");
system("virsh start $cust_id-$server_id >/dev/null");
system("sleep 10");
return success;
}
return success;
}
return success;
}
#####################################################################################################################
################
################ Create New Virtual Machine from Backup image or Custom Image
################
#####################################################################################################################
#/usr/local/sbin/qcreatevmfrombkup '29448' '1475' 'DEMO LTD' 'nfs1' '14' '2' 'linux' 'debian' '6.0' '32' 'server00023230123.com' '1' '1' '20' 'cumPGtppD0UhsxeJ0Aao' '16:00:00:00:00:68' '162.222.32.234' '' '5909' 'xDXfelvdST6BPKwxpQGX'}{ERROR_CODE = 1}
sub createVMFROMBKUP {
my $cust_id = shift;
my $server_id = shift;
my $old_cust_id = shift;
my $old_server_id = shift;
my $cust_name = shift;
my $os_image_location = shift;
my $backup_id = shift;
my $image_type = shift;
my $os_type = shift;
my $os_name = shift;
my $os_version = shift;
my $os_arch = shift;
my $hostname = shift;
my $ram = shift;
my $vcpu = shift;
my $storage = shift;
my $admin_password = shift;
my $wan_mac_address = shift;
my $wan_primary_ip = shift;
my $wan_sec_ip_list = shift;
my $vm_host_vnc_port = shift;
my $vnc_password = shift;
my $disk_path = "/var/lib/libvirt/images/$cust_id-$server_id.qcow2";
my $iso_path = "/var/lib/libvirt/images/$cust_id-$server_id.iso";
my $ram = $ram * 1024;
my $dm = "G";
my $host_ip_address = "104.152.176.194";
write_log("RESTORE VIRTUAL MACHINE FROM BACKUP SCRIPT CALLED :: \n");
################### Create Virtual Machine #######################################################
system("qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/$cust_id-$server_id.qcow2 20$dm >/dev/null 2>/dev/null ");
write_log("QCOW2 IMAGE HAS BEEN CREATED UNDER VAR-LIB-LIBVIRT-IMAGES-$cust_id-$server_id.qcow2");
write_log("qemu-img create -f qcow2 -o preallocation=metadata /var/lib/libvirt/images/$cust_id-$server_id.qcow2 20$dm");
system("touch $iso_path"); # create a dummy
system("virt-install --connect qemu:///system --name $cust_id-$server_id --ram $ram --vcpus $vcpu --disk /var/lib/libvirt/images/$cust_id-$server_id.qcow2,format=qcow2 --graphics vnc,password=$vnc_password,port=$vm_host_vnc_port,listen=$host_ip_address --cdrom $iso_path --network network:phybrid,mac=$wan_mac_address >>/tmp/log 2>>/tmp/log");
write_log("virt-install --connect qemu:///system --name $cust_id-$server_id --ram $ram --vcpus $vcpu --disk /var/lib/libvirt/images/$cust_id-$server_id.qcow2,format=qcow2 --graphics vnc,password=$vnc_password,port=$vm_host_vnc_port,listen=$host_ip_address --cdrom $iso_path --network network:phybrid,mac=$wan_mac_address");
write_log("VIRT-INSTALL COMMAND CALLED FOR CUSTOMER ID $cust_id AND SERVER ID $server_id");
system("virsh shutdown $cust_id-$server_id >/dev/null 2>/dev/null");
while($? == 0) {
write_log(" Machine still shutting down.");
system("virsh list|grep -i $cust_id-$server_id >/dev/null");
sleep(5);
}
write_log("VIRSH DESTROY $cust_id-$server_id :: $?");
if ( $? != 0 ) {
write_log("Virtual Machine Not Created");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
write_log("REMOVE QCOW2 IMAGE FROM PATH : VAR-LIB-LIBVIRT-IMAGES-$cust_id-$server_id.qcow2 ");
return fail;
}
if ( $image_type == 2 ) {
if (-e "/$os_image_location/os_backup_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2") {
write_log("cp -p /$os_image_location/os_backup_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
system("cp -p /$os_image_location/os_backup_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null ; sleep 5");
}
else {
system("virsh undefine $cust_id-$server_id");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
write_log("BACKUP ID NOT EXIST : /$os_image_location/os_backup_images/$cust_id/$cust_id-$server_id/$old_cust_id-$old_server_id-$backup_id.qcow2");
return fail;
}
}
else {
if (
-e "/$os_image_location/os_custom_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2"
)
{
write_log("cp /$os_image_location/os_custom_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
system("cp /$os_image_location/os_custom_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2 /var/lib/libvirt/images/$cust_id-$server_id.qcow2 >/dev/null 2>/dev/null");
system("sleep 5");
}
else {
system("virsh undefine $cust_id-$server_id");
system("rm -rf /var/lib/libvirt/images/$cust_id-$server_id.qcow2");
write_log("IMAGE ID NOT EXIST : /$os_image_location/os_custom_images/$old_cust_id/$old_cust_id-$old_server_id/$old_cust_id-$old_server_id-$backup_id.qcow2");
return fail;
}
}
################# Setting Hostname and Public IP #######################
system("mkdir -p /mnt/$cust_id-$server_id > /dev/null 2>&1");
system("guestmount -a /var/lib/libvirt/images/$cust_id-$server_id.qcow2 -i --rw /mnt/$cust_id-$server_id > /tmp/mountlog 2>&1");
################# Calculate Gateway #############################################
my @array1 = split( /\./, $wan_primary_ip );
splice @array1, 3,4; # perl Code to get the first three strings out of the splitted array
my $wan_ip_gateway = join( ".", @array1 ); # Perl Code to join the first three spliced strings of array
####################################################################################################
######################## IP Assignment ###########################################################
write_log("Not touching IPs at all. Leaving them exactly as they were in the image. (image restore)");
=begin comment
if ( $os_type eq "linux" ) {
if ( ( $os_name eq "centos" ) || ( $os_name eq "redhat" ) ) {
my $file ="/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3";
system(">/mnt/$cust_id-$server_id/etc/sysconfig/network-scripts/ifcfg-ens3");
$line1 = "DEVICE=ens3";
$line2 = "ONBOOT=yes";
$line3 = "HOTPLUG=no";
$line4 = "IPADDR=$wan_primary_ip";
$line5 = "NETMASK=255.255.255.0";
$line6 = "GATEWAY=$wan_ip_gateway.1";
write_log("Adding IP $wan_primary_ip to file");
if ( $response =
Append( $file, $line1 )
&& Append( $file, $line2 )
&& Append( $file, $line3 )
&& Append( $file, $line4 )
&& Append( $file, $line5 )
&& Append( $file, $line6 ) )
{
write_log(" IP Assignment $wan_primary_ip Successful ");
my @ips = split( ',', $wan_sec_ip_list );
my $ipsno = scalar(@ips);