-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.sh
More file actions
1302 lines (1282 loc) · 54.2 KB
/
manager.sh
File metadata and controls
1302 lines (1282 loc) · 54.2 KB
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
# A simple script to create, destroy, rebuild OpenVZ containers.
# Tested on CentOS 6 64bit, requires OpenVZ Kernel and tools.
# Define the default settings for the script
getdefaultvar () {
container_hostname="localhost.localdomain"
container_template="debian-7.0-x86_64-minimal"
container_nameserver1="8.8.8.8"
container_nameserver2="8.8.4.4"
container_tunstatus="ON"
container_cpucore="1"
container_ramsize="256M"
container_swapsize="256M"
container_diskquota="10G"
}
#======================================================================
# Do not edit anything past this section.
#======================================================================
# Get a random password
getrandpass () {
container_rootpass=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c${1:-10})
}
# Get next available container ID
getctid () {
ctid=11
while true; do
checkctid=$(vzlist -a -o ctid | grep -w $ctid)
if [[ -z $checkctid ]]; then
break
fi
((ctid++))
done
}
# If more than 1 CPU vCores, use plural form
getcpucoreunit () {
if [[ $container_cpucore == "1" ]]; then
cpucoreunit="vCore"
else
cpucoreunit="vCores"
fi
}
# Build container function
buildct () {
# Calculate total ram
ramnounit=$( echo $container_ramsize | cut -d 'M' -f 1 )
swapnounit=$( echo $container_swapsize | cut -d 'M' -f 1 )
container_totalram=$(expr $ramnounit + $swapnounit)"M"
# Create a new container with template chosen
vzctl create "$ctid" --ostemplate "$container_template"
# Set the container to start on boot
vzctl set "$ctid" --onboot yes --save
# Set the IP for container
vzctl set "$ctid" --ipadd "$container_ipaddr" --save
# Set the CPU cores for container
vzctl set "$ctid" --cpus "$container_cpucore" --save
# Set the nameservers for container
if [[ -z $container_nameserver2 ]]; then
vzctl set "$ctid" --nameserver "$container_nameserver1" --save
else
vzctl set "$ctid" --nameserver "$container_nameserver1" --nameserver "$container_nameserver2" --save
fi
# Set the hostname for container
vzctl set "$ctid" --hostname "$container_hostname" --save
# Set disk quota for container, softlimit:hardlimit
vzctl set "$ctid" --diskspace "$container_diskquota" --save
# Set ram size for container
vzctl set "$ctid" --ram "$container_ramsize" --swap "$container_swapsize" --save
vzctl set "$ctid" --privvmpages "unlimited" --save
vzctl set "$ctid" --vmguarpages "unlimited" --save
vzctl set "$ctid" --oomguarpages "unlimited" --save
vzctl set "$ctid" --kmemsize "$((524288 * $ramnounit))" --save
vzctl set "$ctid" --lockedpages "$((128 * $ramnounit))" --save
vzctl set "$ctid" --dcachesize "$((262144 * $ramnounit))" --save
# Set root user password for container
vzctl set "$ctid" --userpasswd root:"$container_rootpass" --save
# Set TUN/TAP/PPP if selected
if [[ $container_tunstatus == "ON" ]]; then
if [[ $(vzctl status "$ctid" | grep "running") ]]; then
vzctl stop "$ctid"
sleep 3
fi
vzctl set "$ctid" --capability net_admin:on --save
vzctl set "$ctid" --features ppp:on --save
vzctl start "$ctid"
sleep 3
vzctl set "$ctid" --devnodes net/tun:rw --save
vzctl set "$ctid" --devices c:10:200:rw --save
vzctl set "$ctid" --devices c:108:0:rw --save
vzctl exec "$ctid" mkdir -p /dev/net
vzctl exec "$ctid" chmod 600 /dev/net/tun
vzctl exec "$ctid" mknod /dev/ppp c 108 0
vzctl exec "$ctid" chmod 600 /dev/ppp
else
# Start the container
vzctl start "$ctid"
fi
# Outputting additional info for modify and rebuild functions
sed -i "17i#################################################" /etc/vz/conf/$ctid.conf
sed -i "17i# CONTAINER_ROOT_PASS:$container_rootpass" /etc/vz/conf/$ctid.conf
sed -i "17i# CONTAINER_TUN_STATUS:$container_tunstatus" /etc/vz/conf/$ctid.conf
sed -i "17i# CONTAINER_SWAP_SIZE:$container_swapsize" /etc/vz/conf/$ctid.conf
sed -i "17i# CONTAINER_RAM_SIZE:$container_ramsize" /etc/vz/conf/$ctid.conf
sed -i "17i# CONTAINER_DISK_QUOTA:$container_diskquota" /etc/vz/conf/$ctid.conf
sed -i "17i################# DO NOT DELETE #################" /etc/vz/conf/$ctid.conf
sed -i "17i############# OpenVZ Bash Manager #############" /etc/vz/conf/$ctid.conf
sed -i "17i#################################################" /etc/vz/conf/$ctid.conf
# Display container info after build
clear
if [[ $rebuild ]]; then
actionword="rebuilt"
else
actionword="built"
fi
echo "================================================================================"
echo "Container $ctid has been $actionword successfully!"
echo "Root Password: $container_rootpass"
echo "Hostname: $container_hostname"
echo "Template: $container_template"
echo "CPU Cores: $container_cpucore $cpucoreunit"
echo "Disk Space: $container_diskquota"
echo "RAM Size: $container_ramsize"
echo "vSWAP Size: $container_swapsize"
echo "IP Address: $container_ipaddr"
echo "Nameservers: $container_nameserver"
echo "TUN/TAP/PPP: $container_tunstatus"
echo "================================================================================"
read -n 1 -s -p "Press any key to continue... "
successmsg="Container $ctid $actionword!"
break
}
# New container function
newct () {
echo "Inspecting system..."
getctid
getrandpass
getdefaultvar
container_ipaddr="NOT SET"
ipaddrstatus="INVALID"
while true; do
if [[ "$ipaddrstatus" == "VALID" ]]; then
ccip=$'\e[0m'
else
ccip=$'\e[1;31m'
fi
container_nameserver="$container_nameserver1$(if [[ $container_nameserver2 ]]; then echo ",$container_nameserver2"; fi)"
getcpucoreunit
spacecount=$(expr 23 - ${#container_ramsize})
clear
echo "================================================================================"
echo " Create New Container "
echo "================================================================================"
echo "[1] Container ID: $ctid"
echo "[2] Root Password: $container_rootpass"
echo "[3] Hostname: $container_hostname"
echo "[4] Template: $container_template"
echo ""
echo "[5] CPU Cores: $container_cpucore $cpucoreunit"
echo "[6] Disk Space: $container_diskquota"
printf "%s%$(echo $spacecount)s%s\n\n" "[7] RAM Size: $container_ramsize" "" "vSWAP Size: $container_swapsize"
printf "%s${ccip}%s\e[0m\n" "[8] " "IP Address: $container_ipaddr"
echo "[9] Nameservers: $container_nameserver"
echo "[0] TUN/TAP/PPP: $container_tunstatus"
if [[ "$ipaddrstatus" == "VALID" ]]; then
printf "\n\e[1;32m%s\e[0m%15s%s\n\n" "[b] Build Container Now!" "" "[q] Exit to Main Menu"
else
printf "\n[q] Exit to Main Menu\n\n"
fi
if [[ $errormsg ]]; then
printf "\e[1;31m%s\e[0m\n\n" "ERROR: $errormsg"
unset errormsg
fi
if [[ $successmsg ]]; then
printf "\e[1;32m%s\e[0m\n\n" "SUCCESS: $successmsg"
unset successmsg
fi
read -p "Choose an option: " cfgopt
case $cfgopt in
1)
read -p "Enter a new Container ID: " newctid
checkctid=$(vzlist -a -o ctid | grep -w $newctid)
if [[ -z $checkctid ]]; then
ctid=$newctid
successmsg="Selected container $newctid"
else
errormsg="Container ID $newctid is in use!"
fi
;;
2)
read -p "Enter a new root user password: " container_rootpass
successmsg="Root user password set to $container_rootpass"
;;
3)
read -p "Enter a new hostname: " container_hostname
successmsg="Container hostname set to $container_hostname"
;;
4)
printf "\nAvailable templates -->\n"
alltemp=$(ls -1 /vz/template/cache/ | sed -e 's/\.[^.]*tar.gz//')
printf "%s\n\n" "$alltemp"
cd /vz/template/cache/
echo "Press TAB for autocompletion."
read -e -p "Enter the full template name to use: " newcttemplate
cd $currentpwd
if [[ "$newcttemplate" == *.tar.gz ]]; then
if ! [[ -f /vz/template/cache/$newcttemplate ]]; then
errormsg="The template you entered does not exist."
else
container_template=$(echo $newcttemplate | sed -e 's/\.[^.]*tar.gz//')
successmsg="Container Template set to $container_template"
fi
else
if ! [[ -f /vz/template/cache/$newcttemplate.tar.gz ]]; then
errormsg="The template you entered does not exist."
else
container_template=$newcttemplate
successmsg="Container Template set to $container_template"
fi
fi
;;
5)
maxcpucore=$(grep -c ^processor /proc/cpuinfo)
read -p "Enter the number of CPU vCores [max: $maxcpucore]: " newvcore
if [[ $newvcore =~ ^-?[0-9]+$ ]]; then
if [[ $newvcore -lt 1 ]] || [[ $newvcore -gt $maxcpucore ]]; then
errormsg="CPU vCore must be between 1 and $maxcpucore"
else
container_cpucore=$newvcore
successmsg="CPU vCore set to $newvcore"
modify_cpu=1
fi
else
errormsg="CPU vCore must be between 1 and $maxcpucore"
fi
;;
6)
read -p "Enter amount of disk space (in GB): " newdiskquota
if [[ $newdiskquota =~ ^-?[0-9]+$ ]]; then
if [[ "$newdiskquota" -lt 1 ]]; then
errormsg="Disk quota must be 1G or more."
else
container_diskquota=$newdiskquota"G"
successmsg="Disk quota set to $container_diskquota"
fi
else
errormsg="Please enter number only, without any units."
fi
;;
7)
read -p "Enter amount of RAM (in MB): " newram
if [[ $newram =~ ^-?[0-9]+$ ]]; then
container_ramsize=$newram"M"
else
errormsg="Please enter number only, without any units."
fi
if [[ -z $errormsg ]]; then
read -p "Enter amount of vSwap (in MB): " newswap
if [[ $newswap =~ ^-?[0-9]+$ ]]; then
container_swapsize=$newswap"M"
successmsg="RAM set to $container_ramsize, vSwap set to $container_swapsize"
modify_ram=1
else
errormsg="Please enter number only, without any units."
fi
fi
;;
8)
read -p "Enter the IP address: " newipaddr
if expr "$newipaddr" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newipaddr
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newipaddr is not a valid IP."
break
fi
done
if [[ -z $errormsg ]]; then
ipusestatus=$(grep "$newipaddr" /etc/vz/conf/*.conf)
if [[ -z $ipusestatus ]]; then
container_ipaddr=$newipaddr
successmsg="IP set to $newipaddr"
ipaddrstatus="VALID"
else
ipusectid=$(echo $ipusestatus | cut -d "/" -f 5 | cut -d "." -f 1)
errormsg="IP $newipaddr is used by container $ipusectid."
fi
fi
else
errormsg="$newipaddr is not a valid IP."
fi
unset IFS
;;
9)
read -p "Enter first nameserver IP: " newnsip1
if [[ -z $newnsip1 ]]; then
echo "At least one nameserver IP is required."
else
if expr "$newnsip1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newnsip1
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newnsip1 is not a valid IP for nameserver 1."
break
fi
done
if [[ -z $errormsg ]]; then
container_nameserver1=$newnsip1
successmsg="Nameserver 1 set to $newnsip1"
fi
else
errormsg="$newnsip1 is not a valid IP for nameserver 1."
fi
fi
if [[ -z $errormsg ]]; then
read -p "Enter second nameserver IP: " newnsip2
if [[ -z $newnsip2 ]]; then
unset container_nameserver2
else
if expr "$newnsip2" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newnsip2
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newnsip2 is not a valid IP for nameserver 2."
break
fi
done
if [[ -z $errormsg ]]; then
container_nameserver2=$newnsip2
successmsg="Nameservers set to $newnsip1 and $newnsip2"
fi
else
errormsg="$newnsip2 is not a valid IP for nameserver 2."
fi
fi
fi
unset IFS
;;
0)
if [[ $container_tunstatus == "ON" ]]; then
container_tunstatus="OFF"
successmsg="TUN/TAP/PPP set to OFF"
elif [[ $container_tunstatus == "OFF" ]]; then
container_tunstatus="ON"
successmsg="TUN/TAP/PPP set to ON"
fi
;;
q)
break
;;
b)
if [[ "$ipaddrstatus" == "VALID" ]]; then
buildct
break
else
errormsg="Invalid option entered, please try again..."
fi
;;
*)
if [[ "$ipaddrstatus" == "VALID" ]]; then
read -p "Build Container Now? [Y/n] " buildopt
case $buildopt in
''|y|yes|Y|YES)
buildct
break
;;
esac
else
errormsg="Invalid option entered, please try again..."
fi
;;
esac
done
}
# Rebuild container function
rebuildct () {
getctinfo="TRUE"
ctid="NOT SET"
ctidstatus="INVALID"
getrandpass
getdefaultvar
while true; do
if [[ $ctid =~ ^-?[0-9]+$ ]]; then
chkctid=$(vzlist -a -o ctid | grep -w $ctid)
if [[ -z $chkctid ]]; then
errormsg="Container ID entered doesn't exist..."
ctidstatus="INVALID"
else
ctidstatus="VALID"
fi
elif [[ $ctid == "NOT SET" ]]; then
# This line serves no purpose
ctid="NOT SET"
else
errormsg="Invalid Container ID, please try again..."
fi
container_nameserver="$container_nameserver1$(if [[ $container_nameserver2 ]]; then echo ",$container_nameserver2"; fi)"
getcpucoreunit
spacecount=$(expr 23 - ${#container_ramsize})
clear
echo "================================================================================"
echo " Rebuild Container "
echo "================================================================================"
echo "[c] List all containers [q] Exit to Main Menu"
if ! [[ "$ctidstatus" == "VALID" ]]; then
printf "\n\e[1;31m%s\e[0m\n\n" "Container ID: $ctid"
else
printf "\n%s\n\n" "[1] Container ID: $ctid"
fi
if [[ "$ctidstatus" == "VALID" ]]; then
if [[ $(vzctl status "$ctid" | grep "down") ]]; then
vzctl start "$ctid"
sleep 3
fi
if [[ $getctinfo == "TRUE" ]]; then
container_rootpass=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_ROOT_PASS' | cut -d ':' -f 2)
container_hostname=$(cat /etc/vz/conf/$ctid.conf | grep 'HOSTNAME' | cut -d '"' -f 2)
container_template=$(cat /etc/vz/conf/$ctid.conf | grep 'OSTEMPLATE' | cut -d '"' -f 2)
container_cpucore=$(cat /etc/vz/conf/$ctid.conf | grep 'CPUS' | cut -d '"' -f 2)
container_diskquota=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_DISK_QUOTA' | cut -d ':' -f 2)
container_ramsize=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_RAM_SIZE' | cut -d ':' -f 2)
container_swapsize=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_SWAP_SIZE' | cut -d ':' -f 2)
container_ipaddr=$(cat /etc/vz/conf/$ctid.conf | grep 'IP_ADDRESS' | cut -d '"' -f 2)
container_nameserver1=$(cat /etc/vz/conf/$ctid.conf | grep 'NAMESERVER' | cut -d '"' -f 2 | cut -d ' ' -f 1)
container_nameserver2=$(cat /etc/vz/conf/$ctid.conf | grep 'NAMESERVER' | cut -d '"' -f 2 | cut -d ' ' -f 2)
container_tunstatus=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_TUN_STATUS' | cut -d ':' -f 2)
getcpucoreunit
if [[ $container_nameserver1 == $container_nameserver2 ]]; then
unset container_nameserver2
fi
container_nameserver="$container_nameserver1$(if [[ $container_nameserver2 ]]; then echo ",$container_nameserver2"; fi)"
spacecount=$(expr 23 - ${#container_ramsize})
getctinfo="FALSE"
fi
echo "[2] Root Password: $container_rootpass"
echo "[3] Hostname: $container_hostname"
echo "[4] Template: $container_template"
echo ""
echo "[5] CPU Cores: $container_cpucore $cpucoreunit"
echo "[6] Disk Space: $container_diskquota"
printf "%s%$(echo $spacecount)s%s\n" "[7] RAM Size: $container_ramsize" "" "vSWAP Size: $container_swapsize"
echo ""
echo "[8] IP Address: $container_ipaddr"
echo "[9] Nameservers: $container_nameserver"
echo "[0] TUN/TAP/PPP: $container_tunstatus"
echo "[r] Rebuild Container Now!"
echo ""
fi
if [[ $errormsg ]]; then
printf "\e[1;31m%s\e[0m\n\n" "ERROR: $errormsg"
unset errormsg
fi
if [[ $successmsg ]]; then
printf "\e[1;32m%s\e[0m\n\n" "SUCCESS: $successmsg"
unset successmsg
fi
if ! [[ "$ctidstatus" == "VALID" ]]; then
read -p "Enter Container ID or Choose a Menu Option: " rbldopt
if [[ $rbldopt =~ ^-?[0-9]+$ ]]; then
checkctid=$(vzlist -a -o ctid | grep -w $rbldopt)
if [[ -z $checkctid ]]; then
errormsg="Container ID $rbldopt is invalid!"
else
ctid=$rbldopt
successmsg="Selected container $rbldopt"
getctinfo="TRUE"
fi
elif [[ $rbldopt == "c" ]]; then
getctlist
elif [[ $rbldopt == "q" ]]; then
break
else
errormsg="Invalid option entered, please try again..."
fi
else
read -p "Choose an option: " rbldopt
case $rbldopt in
1)
read -p "Enter a Container ID: " newctid
checkctid=$(vzlist -a -o ctid | grep -w $newctid)
if [[ -z $checkctid ]]; then
errormsg="Container ID $newctid is invalid!"
else
ctid=$newctid
successmsg="Selected container $newctid"
getctinfo="TRUE"
fi
;;
2)
read -p "Enter a new root user password: " container_rootpass
successmsg="Root user password set to $container_rootpass"
;;
3)
read -p "Enter a new hostname: " container_hostname
successmsg="Container hostname set to $container_hostname"
;;
4)
printf "\nAvailable templates -->\n"
alltemp=$(ls -1 /vz/template/cache/ | sed -e 's/\.[^.]*tar.gz//')
printf "%s\n\n" "$alltemp"
cd /vz/template/cache/
echo "Press TAB for autocompletion."
read -e -p "Enter the full template name to use: " newcttemplate
cd $currentpwd
if [[ "$newcttemplate" == *.tar.gz ]]; then
if ! [[ -f /vz/template/cache/$newcttemplate ]]; then
errormsg="The template you entered does not exist."
else
container_template=$(echo $newcttemplate | sed -e 's/\.[^.]*tar.gz//')
successmsg="Container Template set to $container_template"
fi
else
if ! [[ -f /vz/template/cache/$newcttemplate.tar.gz ]]; then
errormsg="The template you entered does not exist."
else
container_template=$newcttemplate
successmsg="Container Template set to $container_template"
fi
fi
;;
5)
maxcpucore=$(grep -c ^processor /proc/cpuinfo)
read -p "Enter the number of CPU vCores [max: $maxcpucore]: " newvcore
if [[ $newvcore =~ ^-?[0-9]+$ ]]; then
if [[ $newvcore -lt 1 ]] || [[ $newvcore -gt $maxcpucore ]]; then
errormsg="CPU vCore must be between 1 and $maxcpucore"
else
container_cpucore=$newvcore
successmsg="CPU vCore set to $newvcore"
modify_cpu=1
fi
else
errormsg="CPU vCore must be between 1 and $maxcpucore"
fi
;;
6)
read -p "Enter amount of disk space (in GB): " newdiskquota
if [[ $newdiskquota =~ ^-?[0-9]+$ ]]; then
if [[ "$newdiskquota" -lt 1 ]]; then
errormsg="Disk quota must be 1G or more."
else
container_diskquota=$newdiskquota"G"
successmsg="Disk quota set to $container_diskquota"
fi
else
errormsg="Please enter number only, without any units."
fi
;;
7)
read -p "Enter amount of RAM (in MB): " newram
if [[ $newram =~ ^-?[0-9]+$ ]]; then
container_ramsize=$newram"M"
else
errormsg="Please enter number only, without any units."
fi
if [[ -z $errormsg ]]; then
read -p "Enter amount of vSwap (in MB): " newswap
if [[ $newswap =~ ^-?[0-9]+$ ]]; then
container_swapsize=$newswap"M"
successmsg="RAM set to $container_ramsize, vSwap set to $container_swapsize"
modify_ram=1
else
errormsg="Please enter number only, without any units."
fi
fi
;;
8)
read -p "Enter the IP address: " newipaddr
if expr "$newipaddr" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newipaddr
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newipaddr is not a valid IP."
break
fi
done
if [[ -z $errormsg ]]; then
ipusestatus=$(grep "$newipaddr" /etc/vz/conf/*.conf)
if [[ -z $ipusestatus ]]; then
container_ipaddr=$newipaddr
successmsg="IP set to $newipaddr"
ipaddrstatus="VALID"
else
ipusectid=$(echo $ipusestatus | cut -d "/" -f 5 | cut -d "." -f 1)
errormsg="IP $newipaddr is used by container $ipusectid."
fi
fi
else
errormsg="$newipaddr is not a valid IP."
fi
unset IFS
;;
9)
read -p "Enter first nameserver IP: " newnsip1
if [[ -z $newnsip1 ]]; then
echo "At least one nameserver IP is required."
else
if expr "$newnsip1" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newnsip1
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newnsip1 is not a valid IP for nameserver 1."
break
fi
done
if [[ -z $errormsg ]]; then
container_nameserver1=$newnsip1
successmsg="Nameserver 1 set to $newnsip1"
fi
else
errormsg="$newnsip1 is not a valid IP for nameserver 1."
fi
fi
if [[ -z $errormsg ]]; then
read -p "Enter second nameserver IP: " newnsip2
if [[ -z $newnsip2 ]]; then
unset container_nameserver2
else
if expr "$newnsip2" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newnsip2
for quad in 1 2 3 4; do
if eval [ \$$quad -gt 255 ]; then
errormsg="$newnsip2 is not a valid IP for nameserver 2."
break
fi
done
if [[ -z $errormsg ]]; then
container_nameserver2=$newnsip2
successmsg="Nameservers set to $newnsip1 and $newnsip2"
fi
else
errormsg="$newnsip2 is not a valid IP for nameserver 2."
fi
fi
fi
unset IFS
;;
0)
if [[ $container_tunstatus == "ON" ]]; then
container_tunstatus="OFF"
successmsg="TUN/TAP/PPP set to OFF"
elif [[ $container_tunstatus == "OFF" ]]; then
container_tunstatus="ON"
successmsg="TUN/TAP/PPP set to ON"
fi
;;
q)
break
;;
r)
if [[ "$ctidstatus" == "VALID" ]]; then
vzctl stop $ctid
vzctl destroy $ctid
rebuild=1
buildct
break
else
errormsg="Invalid option entered, please try again..."
fi
;;
*)
if [[ "$ctidstatus" == "VALID" ]]; then
read -p "Rebuild Container Now? [Y/n] " buildopt
case $buildopt in
''|y|yes|Y|YES)
vzctl stop $ctid
vzctl destroy $ctid
rebuild=1
buildct
break
;;
*)
errormsg="Container NOT rebuilt!"
;;
esac
else
errormsg="Invalid option entered, please try again..."
fi
;;
esac
fi
done
}
# Destroy container function
destroyct () {
while true; do
clear
echo "================================================================================"
echo " Destroy Container "
echo "================================================================================"
echo "[c] List all containers [q] Exit to Main Menu"
echo ""
if [[ $errormsg ]]; then
printf "\e[1;31m%s\e[0m\n\n" "ERROR: $errormsg"
unset errormsg
fi
if [[ $successmsg ]]; then
printf "\e[1;32m%s\e[0m\n\n" "SUCCESS: $successmsg"
unset successmsg
fi
read -p "Enter Container ID or Choose a Menu Option: " destroyopt
if [[ $destroyopt =~ ^-?[0-9]+$ ]]; then
checkctid=$(vzlist -a -o ctid | grep -w $destroyopt)
if [[ -z $checkctid ]]; then
errormsg="Container ID $destroyopt is invalid!"
else
ctid=$destroyopt
printf "\n\e[1;33mWARNING: All files will be deleted immediately%c\e[0m\n" '!'
read -p "Confirm to destroy container $destroyopt [y/N]: " destroyconfirm
case $destroyconfirm in
y|yes|Y|YES)
vzctl stop $destroyopt
vzctl destroy $destroyopt
successmsg="Container $destroyopt has been destroyed!"
break
;;
*)
errormsg="Container $destroyopt has not been destroyed!"
break
;;
esac
fi
elif [[ $destroyopt == "c" ]]; then
getctlist
elif [[ $destroyopt == "q" ]]; then
break
else
chkctid=$(vzlist -a -o ctid | grep -w $destroyopt)
if [[ -z $chkctid ]]; then
errormsg="Container ID entered doesn't exist..."
fi
fi
done
}
# Power control function
powerct () {
ctidstatus="INVALID"
while true; do
clear
echo "================================================================================"
echo " Container Power Control "
echo "================================================================================"
echo "[c] List all containers [q] Exit to Main Menu"
echo ""
if [[ $ctidstatus == "VALID" ]]; then
echo "[1] Power ON Container $pwropt"
echo "[2] Restart Container $pwropt"
echo "[3] Shutdown Container $pwropt"
echo "[4] Power OFF Container $pwropt"
echo ""
fi
if [[ $errormsg ]]; then
printf "\e[1;31m%s\e[0m\n\n" "ERROR: $errormsg"
unset errormsg
fi
if [[ $successmsg ]]; then
printf "\e[1;32m%s\e[0m\n\n" "SUCCESS: $successmsg"
unset successmsg
fi
if [[ $ctidstatus == "VALID" ]]; then
read -p "Choose an option: " pwraction
case $pwraction in
1)
vzctl start $pwropt
successmsg="Container $pwropt has been Powered ON."
break
;;
2)
vzctl restart $pwropt
successmsg="Container $pwropt has been Restarted."
break
;;
3)
vzctl stop $pwropt
successmsg="Container $pwropt has been Shut Down."
break
;;
4)
vzctl stop $pwropt --fast
successmsg="Container $pwropt has been Powered OFF."
break
;;
c)
getctlist
;;
q)
break
;;
esac
else
read -p "Enter Container ID or Choose a Menu Option: " pwropt
if [[ $pwropt =~ ^-?[0-9]+$ ]]; then
checkctid=$(vzlist -a -o ctid | grep -w $pwropt)
if [[ -z $checkctid ]]; then
errormsg="Container ID $pwropt is invalid!"
else
ctid=$pwropt
successmsg="Selected container $pwropt"
ctidstatus="VALID"
fi
elif [[ $pwropt == "c" ]]; then
getctlist
elif [[ $pwropt == "q" ]]; then
break
else
errormsg="Invalid option entered, please try again..."
fi
fi
done
}
# Change container root user password function
modifyct () {
getctinfo="TRUE"
ctid="NOT SET"
ctidstatus="INVALID"
while true; do
ramnounit=$( echo $container_ramsize | cut -d 'M' -f 1 )
swapnounit=$( echo $container_swapsize | cut -d 'M' -f 1 )
container_totalram=$(expr $ramnounit + $swapnounit)"M"
if [[ $ctid =~ ^-?[0-9]+$ ]]; then
chkctid=$(vzlist -a -o ctid | grep -w $ctid)
if [[ -z $chkctid ]]; then
errormsg="Container ID entered doesn't exist..."
ctidstatus="INVALID"
else
ctidstatus="VALID"
fi
elif [[ $ctid == "NOT SET" ]]; then
# This line serves no purpose
ctid="NOT SET"
else
errormsg="Invalid Container ID, please try again..."
fi
container_nameserver="$container_nameserver1$(if [[ $container_nameserver2 ]]; then echo ",$container_nameserver2"; fi)"
getcpucoreunit
spacecount=$(expr 23 - ${#container_ramsize})
clear
echo "================================================================================"
echo " Modify Container Settings "
echo "================================================================================"
echo "[c] List all containers [q] Exit to Main Menu"
if ! [[ "$ctidstatus" == "VALID" ]]; then
printf "\n\e[1;31m%s\e[0m\n\n" "Container ID: $ctid"
else
printf "\n%s\n\n" "[1] Container ID: $ctid"
fi
if [[ "$ctidstatus" == "VALID" ]]; then
if [[ $(vzctl status "$ctid" | grep "down") ]]; then
vzctl start "$ctid"
sleep 3
fi
if [[ $getctinfo == "TRUE" ]]; then
container_rootpass=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_ROOT_PASS' | cut -d ':' -f 2)
container_hostname=$(cat /etc/vz/conf/$ctid.conf | grep 'HOSTNAME' | cut -d '"' -f 2)
container_template=$(cat /etc/vz/conf/$ctid.conf | grep 'OSTEMPLATE' | cut -d '"' -f 2)
container_cpucore=$(cat /etc/vz/conf/$ctid.conf | grep 'CPUS' | cut -d '"' -f 2)
container_diskquota=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_DISK_QUOTA' | cut -d ':' -f 2)
container_ramsize=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_RAM_SIZE' | cut -d ':' -f 2)
container_swapsize=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_SWAP_SIZE' | cut -d ':' -f 2)
container_ipaddr=$(cat /etc/vz/conf/$ctid.conf | grep 'IP_ADDRESS' | cut -d '"' -f 2)
container_nameserver1=$(cat /etc/vz/conf/$ctid.conf | grep 'NAMESERVER' | cut -d '"' -f 2 | cut -d ' ' -f 1)
container_nameserver2=$(cat /etc/vz/conf/$ctid.conf | grep 'NAMESERVER' | cut -d '"' -f 2 | cut -d ' ' -f 2)
container_tunstatus=$(cat /etc/vz/conf/$ctid.conf | grep 'CONTAINER_TUN_STATUS' | cut -d ':' -f 2)
getcpucoreunit
spacecount=$(expr 23 - ${#container_ramsize})
if [[ $container_nameserver1 == $container_nameserver2 ]]; then
unset container_nameserver2
fi
container_nameserver="$container_nameserver1$(if [[ $container_nameserver2 ]]; then echo ",$container_nameserver2"; fi)"
orig_tunstatus=$container_tunstatus
orig_ipaddr=$container_ipaddr
getctinfo="FALSE"
fi
echo "[2] CPU: $container_cpucore $cpucoreunit"
printf "%s%$(echo $spacecount)s%s\n" "[3] RAM Size: $container_ramsize" "" "vSWAP Size: $container_swapsize"
echo "[4] Disk Space: $container_diskquota"
echo "[5] Root Password: $container_rootpass"
echo ""
echo "[6] IP Address: $container_ipaddr"
echo "[7] Nameservers: $container_nameserver"
echo "[8] Hostname: $container_hostname"
echo "[9] TUN/TAP/PPP: $container_tunstatus"
echo ""
echo "[s] Save new settings to container"
echo ""
fi
if [[ $errormsg ]]; then
printf "\e[1;31m%s\e[0m\n\n" "ERROR: $errormsg"
unset errormsg
fi
if [[ $successmsg ]]; then
printf "\e[1;32m%s\e[0m\n\n" "SUCCESS: $successmsg"
unset successmsg
fi
if ! [[ "$ctidstatus" == "VALID" ]]; then
read -p "Enter Container ID or Choose a Menu Option: " mdfyopt
if [[ $mdfyopt =~ ^-?[0-9]+$ ]]; then
checkctid=$(vzlist -a -o ctid | grep -w $mdfyopt)
if [[ -z $checkctid ]]; then
errormsg="Container ID $mdfyopt is invalid!"
else
ctid=$mdfyopt
successmsg="Selected container $mdfyopt"
getctinfo="TRUE"
fi
elif [[ $mdfyopt == "c" ]]; then
getctlist
elif [[ $mdfyopt == "q" ]]; then
break
else
errormsg="Invalid option entered, please try again..."
fi
else
read -p "Choose an option: " mdfyopt
case $mdfyopt in
1)
read -p "Enter a Container ID: " newctid
checkctid=$(vzlist -a -o ctid | grep -w $newctid)
if [[ -z $checkctid ]]; then
errormsg="Container ID $newctid is invalid!"
else
ctid=$newctid
successmsg="Selected container $newctid"
getctinfo="TRUE"
fi
;;
2)
maxcpucore=$(grep -c ^processor /proc/cpuinfo)
read -p "Enter the number of CPU vCores [max: $maxcpucore]: " newvcore
if [[ $newvcore =~ ^-?[0-9]+$ ]]; then
if [[ $newvcore -lt 1 ]] || [[ $newvcore -gt $maxcpucore ]]; then
errormsg="CPU vCore must be between 1 and $maxcpucore"
else
container_cpucore=$newvcore
successmsg="CPU vCore set to $newvcore"
modify_cpu=1
fi
else
errormsg="CPU vCore must be between 1 and $maxcpucore"
fi
;;
3)
read -p "Enter amount of RAM (in MB): " newram
if [[ $newram =~ ^-?[0-9]+$ ]]; then
container_ramsize=$newram"M"
else
errormsg="Please enter number only, without any units."
fi
if [[ -z $errormsg ]]; then
read -p "Enter amount of vSwap (in MB): " newswap
if [[ $newswap =~ ^-?[0-9]+$ ]]; then
container_swapsize=$newswap"M"
successmsg="RAM set to $container_ramsize, vSwap set to $container_swapsize"
modify_ram=1
else
errormsg="Please enter number only, without any units."
fi
fi
;;
4)
read -p "Enter amount of disk space (in GB): " newdiskquota
if [[ $newdiskquota =~ ^-?[0-9]+$ ]]; then
if [[ "$newdiskquota" -lt 1 ]]; then
errormsg="Disk quota must be 1G or more."
else
container_diskquota=$newdiskquota"G"
successmsg="Disk quota set to $container_diskquota"
modify_disk=1
fi
else
errormsg="Please enter number only, without any units."
fi
;;
5)
read -p "Enter a new root user password: " container_rootpass
successmsg="Root user password set to $container_rootpass"
modify_rootpass=1
;;
6)
read -p "Enter the IP address: " newipaddr
if expr "$newipaddr" : '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*$' >/dev/null; then
IFS=.
set $newipaddr
for quad in 1 2 3 4; do