-
Notifications
You must be signed in to change notification settings - Fork 0
/
newVM.sh
executable file
·1383 lines (1267 loc) · 30.6 KB
/
newVM.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
# Writted by Valentin OUVRARD, 2015
#
# This script create or clone VMs on Debian 8 or Ubuntu 14.04 using Ganeti, Xen and DRBD.
###############################################################
#################### Ganeti Configuration #####################
###############################################################
# Ganeti master hostname
GNT_MASTER="gnt-master.hostname"
# Any Ganeti Node for secondary DRBD
GNT_NODE="secondary-node.hostname"
DEFAULT_VLAN=""
VG_MASTER="vgganeti"
###############################################################
## --help function
Help () {
echo
tput bold; echo "NewVM.sh v0.1`tput sgr0` - Create a Ganeti Virtual Machine"
echo "Author: Valentin OUVRARD"
echo "Usage: newVM.sh --name <NAME> --disk <DISK> --ram <RAM> "
echo
echo "Options:"
echo
echo " --name <VM_NAME> New virtual machine hostname"
echo " --disk <DISK> Disk size in gigabytes (G|g)"
echo " --ram <RAM> Memory size in gigabytes (G|g)"
echo " --vcpu <VCPU> Virtual CPU number"
echo " --nodes <NODES> First node and second node(1st:2nd) "
echo " --clone <CLONE> Name of a VM to clone"
echo " --variant <OS> Choose between trusty, xenial and jessie (by default)"
echo " --no-confirm Disable VM creation's confirmation"
echo
echo "Networking options:"
echo
echo " --ipv4 <IPV4> Virtual machine IPV4 Address "
echo " --gw <GW> Virtual machine IPV4 Gateway"
echo " --netmask <MASK> Virtual machine IPV4 Netmask (CIDR)"
echo " --ipv6 <IPV6> Virtual machine IPV6 Address "
echo " --vlan <VLAN> Specify a VLAN for eth0 (none by default) "
echo
echo "Advanced Disk options: (in gigabytes)"
echo
echo " --root <ROOT> Give the / partition size"
echo " --boot <BOOT> Give the /boot partition size (in megabytes)"
echo " --swap <SWAP> Give the SWAP partition size"
echo " --tmp <TMP> Give the /tmp partition size (in megabytes)"
echo " --usr <USR> Give the /usr partition size"
echo " --var <VAR> Give the /var partition size"
echo " --vlog <VARLOG> Give the /var/log partition size"
echo " --plain Create the VM with plain disk (no drbd)"
echo
echo "Examples:"
echo
echo " ./newVM.sh --name vm1 --disk 15G --ram 2G"
echo " ./newVM.sh --name vm2 --disk 15G --ram 2G --var 4G --ipv4 192.168.1.42"
echo
}
## Min - Max variables #### Can be modified !!!
MIN_RAM_SIZE=1
MAX_RAM_SIZE=30
MIN_DISK_SIZE=9
MAX_DISK_SIZE=100
MIN_ROOT_SIZE=3
MAX_ROOT_SIZE=90
MIN_BOOT_SIZE=200
MAX_BOOT_SIZE=800
MIN_VAR_SIZE=1
MAX_VAR_SIZE=90
MIN_VLOG_SIZE=1
MAX_VLOG_SIZE=90
MIN_USR_SIZE=2
MAX_USR_SIZE=90
MIN_TMP_SIZE=100
MAX_TMP_SIZE=1024
MIN_SWAP_SIZE=1
MAX_SWAP_SIZE=10
MIN_VCPU=1
MAX_VCPU=2
###############################################################
## Nothing to modify downstair !! ##
###############################################################
## Is IPV4 ?
function isIPv4 {
if [ $# = 1 ]
then
printf $1 | grep -Eq '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-4]|2[0-4][0-9]|[01]?[1-9][0-9]?)$'
return $?
else
return 2
fi
}
## CIDR 2 NetMASK
cdr2mask ()
{
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[ $1 -gt 1 ] && shift $1 || shift
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
function trap_ctrlc ()
{
echo " you catch CTRL+C,"
tput bold; tput setaf 4;
read -r -p "Are you really want to leave ? [o/N] " response
tput sgr0
case $response in
[oO][eE][sS]|[oO])
echo
tput bold ; tput setaf 1
echo "You leave.." ; tput sgr0; echo ; exit 1
;;
*) echo
;;
esac
}
## Catch argz !
while [ "$1" != "" ]; do
case $1 in
# obligatoires
--name ) shift
VM_NAME=$1
;;
--disk ) shift
DISK=$1
;;
--ram ) shift
RAM=$1
;;
# facultatifs
--vcpu ) shift
VCPU=$1
;;
--nodes ) shift
NODES=$1
;;
--clone ) shift
CLONE=True ; CLONE_FROM=$1
;;
--variant ) shift
OS=$1
;;
--no-confirm )
CONFIRM=False
;;
# network
--ipv4 ) shift
IPV4=$1
;;
--netmask ) shift
NETMASK=$1
;;
--gw ) shift
GW=$1
;;
--ipv6 ) shift
IPV6=$1
;;
--vlan ) shift
DEFAULT_VLAN=$1
;;
# Disks
--root ) shift
ROOT=$1
;;
--boot ) shift
BOOT=$1
;;
--var ) shift
VAR=$1
;;
--vlog ) shift
VLOG=$1
;;
--usr ) shift
USR=$1
;;
--tmp ) shift
TMP=$1
;;
--swap ) shift
SWAP=$1
;;
--plain )
DISK_METHOD=plain
;;
* | -h | --help)
if [ "$1" != "-h" ] && [ "$1" != "--help" ]
then
tput bold ; tput setaf 1; echo ; echo $1 est incorrect ! ; tput sgr0 ; echo ; Help; exit 1
else Help ; exit 1
fi
esac
shift
done
###############################################################
## Checks argz ! ##
###############################################################
## VM NAME
if [ -z $VM_NAME ]
then
echo
echo "You don't give the VM's name (--name)"
exit 1
fi
VM_NAME=`echo "${VM_NAME,,}"`
if [ "$VM_NAME" == "$CLONE_FROM" ]
then
echo
echo "You can't make a clone with the same name !"
exit 1
fi
## OS NAME
if [ -z $OS ]
then
OS=jessie
fi
## DISK
if [ -z $DISK ]
then
echo
echo "You don't give the Disk size (--disk)"
exit 1
fi
try_disk=`echo "${DISK: -1}"`
if [ "$try_disk" == "G" ] || [ "$try_disk" == "g" ]
then :
else
echo
echo "You don't give Disk's size in gigabytes (G|g)"
exit 1
fi
DISK_SIZE=`echo $DISK | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $DISK_SIZE =~ $re ]]
then
echo
echo "You don't give a number for the disk size"
exit 1
fi
if [ "$DISK_SIZE" -gt $MAX_DISK_SIZE ]
then
echo
echo "You give a too high disk's size ! (limit "$MAX_DISK_SIZE"G)"
exit 1
fi
if [ "$DISK_SIZE" -lt $MIN_DISK_SIZE ]
then
echo
echo "You give a too small disk's size ! (limit "$MIN_DISK_SIZE"G)"
exit 1
fi
## RAM
if [ -z $RAM ]
then
echo
echo "You don't give the RAM's size ! (--ram)"
exit 1
fi
try_gram=`echo "${RAM: -1}"`
if [ "$try_gram" == "G" ] || [ "$try_gram" == "g" ]
then :
else
echo
echo "You don't give the RAM's size in gigabytes ! (G|g)"
exit 1
fi
RAM_SIZE=`echo $RAM | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $RAM_SIZE =~ $re ]]
then
echo
echo "You don't give a number for the RAM's size ! "
exit 1
fi
if [ "$RAM_SIZE" -gt $MAX_RAM_SIZE ]
then
echo
echo "You give a too high RAM's size !(limit "$MAX_RAM_SIZE"G)"
exit 1
fi
if [ "$RAM_SIZE" -lt $MIN_RAM_SIZE ]
then
echo
echo "You give a too small RAM's size ! (limit "$MIN_RAM_SIZE"G)"
exit 1
fi
###############################################################
## VCPU
if [ -z $VCPU ]
then VCPU=$MIN_VCPU
else
if [ $VCPU -gt $MAX_VCPU ]
then
echo
echo "You give too much vCPU ! (limit "$MAX_VCPU")"
exit 1
fi
if [ $VCPU -lt $MIN_VCPU ]
then
echo
echo "You give not enought vCPU ! (limit "$MIN_VCPU")"
exit 1
fi
fi
## IPV4
if [ -z $IPV4 ]
then :
else
isIPv4 $IPV4
verif_ipv4=`echo $?`
if [ $verif_ipv4 -eq 1 ]
then
echo
echo "It's not a good IPV4 Address !"
exit 1
fi
if [ -z $NETMASK ]
then NETMASK="/24"
fi
fi
if [ -z $IPV4 ] && [ ! -z $GW ]
then
echo
echo "If you give a Gateway, you have to give an IPV4 !"
exit 1
fi
if [ -z $IPV4 ] && [ ! -z $NETMASK ]
then
echo
echo "If you give a Netmask, you have to give an IPV4 !"
exit 1
fi
if [ -z $IPV4 ] && [ ! -z $DEFAULT_VLAN ]
then
echo
echo "If you give a VLAN, you have to give an IPV4 or IPV6 !"
exit 1
fi
## GW
if [ -z $GW ]
then :
else
isIPv4 $GW
verif_gw=`echo $?`
if [ $verif_gw -eq 1 ]
then
echo
echo "It's not a good Gateway ! (--gw)"
exit 1
fi
fi
## Netmask
if [ -z $NETMASK ]
then :
else
if [ "${NETMASK::1}" != "/" ]
then
echo
echo "It's not an CIDR Netmask ! (like /24)"
exit 1
fi
NETMASK_CIDR=`echo $NETMASK |cut -d '/' -f2`
if [[ $NETMASK_CIDR =~ ^[-+]?[0-9]+$ ]]
then :
else
echo
echo "It's not an CIDR Netmask ! (like /24)"
exit 1
fi
if [ $NETMASK_CIDR -gt 32 ]
then
echo
echo "It's not an CIDR Netmask ! (like /24)"
exit 1
else :
fi
NETMASK_FULL=`cdr2mask $NETMASK_CIDR`
fi
## IPV6
## VLAN
if [ ! -z $DEFAULT_VLAN ]
then
if [[ "${DEFAULT_VLAN}" =~ ^[-+]?[0-9]+$ ]]
then :
else
echo
echo "You give a bad VLAN number (not a number) !"
exit 1
fi
vlan_length=`echo ${#DEFAULT_VLAN}`
if [ $vlan_length -gt 3 ]
then
echo
echo "The max VLAN number is 999 !"
exit 1
fi
:
fi
###############################################################
## Root
if [ -z $ROOT ]
then :
else
try_root=`echo "${ROOT: -1}"`
if [ "$try_root" == "G" ] || [ "$try_root" == "g" ]
then :
else
echo
echo "You don't give the /'s size in gigabytes ! (G|g)"
exit 1
fi
ROOT_SIZE=`echo $ROOT | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $ROOT_SIZE =~ $re ]]
then
echo
echo "You don't give an number for /'s size !"
exit 1
fi
if [ "$ROOT_SIZE" -gt $MAX_ROOT_SIZE ]
then
echo
echo "The /'s size is too big ! (limit "$MAX_ROOT_SIZE"G)"
exit 1
fi
if [ "$ROOT_SIZE" -lt $MIN_ROOT_SIZE ]
then
echo
echo "The /'s size is too small ! (limit "$MIN_ROOT_SIZE"G)"
exit 1
fi
fi
## Boot
if [ -z $BOOT ]
then :
else
try_boot=`echo "${BOOT: -1}"`
if [ "$try_boot" == "m" ] || [ "$try_boot" == "M" ]
then :
else
echo
echo "You don't give the /boot's size in megabytes ! (M|m)"
exit 1
fi
BOOT_SIZE=`echo $BOOT | sed 's/[m|M]//' `
re='^[0-9]+$'
if ! [[ $BOOT_SIZE =~ $re ]]
then
echo
echo "You don't give an number for /boot's size !"
exit 1
fi
if [ "$BOOT_SIZE" -gt $MAX_BOOT_SIZE ]
then
echo
echo "The /boot's size is too big ! (limit "$MAX_BOOT_SIZE"M)"
exit 1
fi
if [ "$BOOT_SIZE" -lt $MIN_BOOT_SIZE ]
then
echo
echo "The /boot's size is too small ! (limit "$MIN_BOOT_SIZE"M)"
exit 1
fi
fi
## Usr
if [ -z $USR ]
then :
else
try_usr=`echo "${USR: -1}"`
if [ "$try_usr" == "g" ] || [ "$try_usr" == "G" ]
then :
else
echo
echo "You don't give the /usr's size in gigabytes ! (g|G)"
exit 1
fi
USR_SIZE=`echo $USR | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $USR_SIZE =~ $re ]]
then
echo
echo "You don't give an number for /usr's size !"
exit 1
fi
if [ "$USR_SIZE" -gt $MAX_USR_SIZE ]
then
echo
echo "The /usr's size is too big ! (limit "$MAX_USR_SIZE"G)"
exit 1
fi
if [ "$USR_SIZE" -lt $MIN_USR_SIZE ]
then
echo
echo "The /usr's size is too small ! (limit "$MIN_USR_SIZE"G)"
exit 1
fi
fi
## var
if [ -z $VAR ]
then :
else
try_var=`echo "${VAR: -1}"`
if [ "$try_var" == "g" ] || [ "$try_var" == "G" ]
then :
else
echo
echo "You don't give the /var's size in gigabytes ! (g|G)"
exit 1
fi
VAR_SIZE=`echo $VAR | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $VAR_SIZE =~ $re ]]
then
echo
echo "You don't give an number for /var's size !"
exit 1
fi
if [ "$VAR_SIZE" -gt $MAX_VAR_SIZE ]
then
echo
echo "The /var's size is too big ! (limit "$MAX_VAR_SIZE"G)"
exit 1
fi
if [ "$VAR_SIZE" -lt $MIN_VAR_SIZE ]
then
echo
echo "The /var's size is too small ! (limit "$MIN_VAR_SIZE"G)"
exit 1
fi
fi
## Var/Log
if [ -z $VLOG ]
then :
else
try_vlog=`echo "${VLOG: -1}"`
if [ "$try_vlog" == "g" ] || [ "$try_vlog" == "G" ]
then :
else
echo
echo "You don't give the /var/log's size in gigabytes ! (g|G)"
exit 1
fi
VLOG_SIZE=`echo $VLOG | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $VLOG_SIZE =~ $re ]]
then
echo
echo "You don't give a an number for /var/log's size ! "
exit 1
fi
if [ "$VLOG_SIZE" -gt $MAX_VLOG_SIZE ]
then
echo
echo "The /var/log's size is too big ! (limit "$MAX_VLOG_SIZE"G)"
exit 1
fi
if [ "$VLOG_SIZE" -lt $MIN_VLOG_SIZE ]
then
echo
echo "The /var/log's size is too small ! (limit "$MIN_VLOG_SIZE"G)"
exit 1
fi
fi
## TMP
if [ -z $TMP ]
then :
else
try_tmp=`echo "${TMP: -1}"`
if [ "$try_tmp" == "m" ] || [ "$try_tmp" == "M" ]
then :
else
echo
echo "You don't give the /tmp's size in megabytes ! (m|M)"
exit 1
fi
TMP_SIZE=`echo $TMP | sed 's/[m|M]//' `
re='^[0-9]+$'
if ! [[ $TMP_SIZE =~ $re ]]
then
echo
echo "You don't give a number for /tmp's size ! "
exit 1
fi
if [ "$TMP_SIZE" -gt $MAX_TMP_SIZE ]
then
echo
echo "The /tmp's size is too big ! (limit "$MAX_TMP_SIZE"M)"
exit 1
fi
if [ "$TMP_SIZE" -lt $MIN_TMP_SIZE ]
then
echo
echo "The /tmp's size is too small ! (limit "$MIN_TMP_SIZE"M)"
exit 1
fi
fi
## SWAP
if [ -z $SWAP ]
then :
else
try_swap=`echo "${SWAP: -1}"`
if [ "$try_swap" == "g" ] || [ "$try_swap" == "G" ]
then :
else
echo
echo "You don't give the Swap's size in gigabytes ! (g|G)"
exit 1
fi
SWAP_SIZE=`echo $SWAP | sed 's/[g|G]//' `
re='^[0-9]+$'
if ! [[ $SWAP_SIZE =~ $re ]]
then
echo
echo "You don't give a number for Swap's size ! "
exit 1
fi
if [ "$SWAP_SIZE" -gt $MAX_SWAP_SIZE ]
then
echo
echo "The Swap's size is too big ! (limit "$MAX_SWAP_SIZE"G)"
exit 1
fi
if [ "$SWAP_SIZE" -lt $MIN_SWAP_SIZE ]
then
echo
echo "The Swap's size is too small ! (limit "$MIN_SWAP_SIZE"G)"
exit 1
fi
fi
###############################################################
## Valeurs par défaut : ##
###############################################################
## METHOD (Drbd / Plain)
if [ -z $DISK_METHOD ]
then DISK_METHOD=drbd
fi
## Nodes
if [ -z $NODES ]
then NODES="$GNT_MASTER:$GNT_NODE"
else verif_nodes=`echo $NODES |grep ":" `
code_nodes=`echo $?`
if [ "$code_nodes" == "0" ]
then :
else
echo
echo "The nodes' names go wrong ! (hostname1:hostname2)"
exit 1
fi
NODE1=`echo $NODES |cut -d ':' -f1`
NODE2=`echo $NODES |cut -d ':' -f2`
if [ -z "$NODE1" ] || [ -z "$NODE2" ]
then echo
echo "The nodes' names go wrong ! (hostname1:hostname2)"
exit 1
fi
fi
## Default Disk Size
if [ -z $BOOT ]
then BOOT=200M
BOOT_SIZE=200
fi
if [ -z $ROOT ]
then ROOT=3G
ROOT_SIZE=3
fi
if [ -z $USR ]
then USR=2G
USR_SIZE=2
fi
if [ -z $TMP ]
then TMP=512M
TMP_SIZE=512
fi
if [ -z $VAR ]
then VAR=1G
VAR_SIZE=1
fi
if [ -z $VLOG ]
then VLOG=1G
VLOG_SIZE=1
fi
if [ -z $SWAP ]
then SWAP=1G
SWAP_SIZE=1
fi
## Let's check disks sizes.
num=$(( $ROOT_SIZE + $SWAP_SIZE + $USR_SIZE + $VAR_SIZE + $VLOG_SIZE + 1 ))
# The +1 is for tmp who isn't bigger than 1g)
if [ $num -gt $DISK_SIZE ]
then
echo
echo "All your partitions are bigger than your disk size !"
exit 1
fi
## Ecriture OS propre
if [ "$OS" == "jessie" ]
then os_clean="Debian 8.5"
elif [ "$OS" == "trusty" ]
then os_clean="Ubuntu 14.04"
elif [ "$OS" == "xenial" ]
then os_clean="Ubuntu 16.04"
else
echo
echo "The OS given is wrong, (\"jessie\" for Debian and \"trusty\" or \"xenial\" for Ubuntu)"
exit 1
fi
## Debootstrap ou NTLDEB (clonage)
if [ -z $CLONE ]
then METHOD="debootstrap"
else METHOD="clone"
fi
###############################################################
## Récapitulatif ##
###############################################################
echo
echo "---------------------------------------------------------"
tput bold
tput setaf 2
echo "You're going to create `tput setaf 4`\"$VM_NAME\""
tput sgr0
echo "---------------------------------------------------------"
echo "--- `tput bold`Memory`tput sgr0` = $RAM"
echo "--- `tput bold`Disk`tput sgr0` = $DISK"
echo "--- `tput bold`VCPU`tput sgr0` = $VCPU"
if [ "$CLONE" == "True" ]
then echo "--- `tput bold`OS`tput sgr0` = OS from $CLONE_FROM"
else
echo "--- `tput bold`OS`tput sgr0` = $os_clean"
fi
if [ -z $NODES ]
then :
else
echo "--- `tput bold`Nodes`tput sgr0` = $NODES"
fi
if [ -z $CLONE_FROM ]
then :
else
echo "--- `tput bold`Clone`tput sgr0` = $CLONE_FROM"
fi
if [ -z $ROOT ]
then :
else
echo "---------------------------------------------------------"
echo "--- `tput bold`/`tput sgr0` = $ROOT"
fi
if [ -z $BOOT ]
then :
else
echo "--- `tput bold`/boot`tput sgr0` = $BOOT"
fi
if [ -z $TMP ]
then :
else
echo "--- `tput bold`/tmp`tput sgr0` = $TMP"
fi
if [ -z $USR ]
then :
else
echo "--- `tput bold`/usr`tput sgr0` = $USR"
fi
if [ -z $VAR ]
then :
else
echo "--- `tput bold`/var`tput sgr0` = $VAR"
fi
if [ -z $VLOG ]
then :
else
echo "--- `tput bold`/v/log`tput sgr0` = $VLOG"
fi
if [ -z $SWAP ]
then :
else
echo "--- `tput bold`Swap`tput sgr0` = $SWAP"
fi
echo "--- `tput bold`Method`tput sgr0` = $DISK_METHOD"
if [ -z $IPV4 ]
then :
else
echo "---------------------------------------------------------"
echo "--- `tput bold`IPV4`tput sgr0` = $IPV4"
fi
if [ -z $GW ]
then :
else
echo "--- `tput bold`Gateway`tput sgr0` = $GW"
fi
if [ -z $NETMASK ]
then :
else
echo "--- `tput bold`Netmask`tput sgr0` = $NETMASK"
fi
if [ -z $IPV6 ]
then :
else
echo "--- `tput bold`IPV6`tput sgr0` = $IPV6"
fi
if [ -z $DEFAULT_VLAN ]
then :
else
echo "--- `tput bold`VLAN`tput sgr0` = $DEFAULT_VLAN"
fi
tput sgr0
echo "---------------------------------------------------------"
echo
## Confirmation si pas d'argument --no-confirm
if [ "$CONFIRM" = "False" ]
then :
else
tput bold; tput setaf 4;
read -r -p "Launch the creation ? [o/N] " response
tput sgr0
case $response in
[oO][eE][sS]|[oO])
echo
;;
*)
echo
tput bold ; tput setaf 1
echo "Exit.." ; tput sgr0; echo ; exit 1
;;
esac
fi
tput bold ; tput setaf 2
echo "Creation.." ; tput sgr0; echo
# Cela permet de catcher CTRL+C
trap "trap_ctrlc" 2
###############################################################
## Let's generate VM's config file ##
###############################################################
if [ -d /srv/ganeti/ ]
then :
else
echo
echo "The folder /srv/ganeti don't exist !"
exit 1
fi
# Vérification du dossier VMCREATION
if [ -d /srv/ganeti/vmcreation ]
then :
else mkdir /srv/ganeti/vmcreation
fi
# Vérification do dossier VMINITLOG
if [ -d /srv/ganeti/vminitlog ]
then :
else mkdir /srv/ganeti/vminitlog
fi
cat > /srv/ganeti/vmcreation/$VM_NAME.conf <<EOF
disk;$DISK
boot;$BOOT
root;$ROOT
usr;$USR
var;$VAR
vlog;$VLOG
swap;$SWAP
tmp;$TMP
ipv4;$IPV4
netmask;$NETMASK_FULL
gw;$GW
ipv6;$IPV6
vlan;$DEFAULT_VLAN
method;$METHOD
clonefrom;$CLONE_FROM
EOF
LOG_FILE=/srv/ganeti/vminitlog/$VM_NAME.log
# The sleep time is for the first VM boot (for update-grub) !
SLEEP=40
###############################################################
## Check Ganeti configuration ##
###############################################################
## Config du Cluster :
if [ ! -f /var/lib/ganeti/config.data ]
then echo
echo "Is Ganeti installed ? ..."
echo
tput bold ; tput setaf 1
echo "Exit.." ; tput sgr0; echo ; exit 1
exit 1
fi
## Vérification de la configuration LVM :
lvm_conf=`cat /etc/lvm/lvm.conf |grep drbd`
lvm_code=`echo $?`
if [ "$lvm_code" == "1" ]
then echo
echo "The LVM configuration is not good ! (filter drbd)"
echo
tput bold ; tput setaf 1
echo "Exit.." ; tput sgr0; echo ; exit 1
exit 1
fi
vg_conf=`vgscan |grep $VG_MASTER`
vg_code=`echo $?`
if [ "$vg_code" == "1" ]
then echo
echo "The VG $VG_MASTER don't exist !"
echo
tput bold ; tput setaf 1
echo "Exit.." ; tput sgr0; echo ; exit 1
exit 1
fi
## Fichier de conf (deboostrap / clone/ variants ...)
if [ ! -f /usr/share/ganeti/os/debootstrap/create ]
then echo