forked from MXCCO/lxdpro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxdpro.sh
2354 lines (2086 loc) · 65.3 KB
/
lxdpro.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
#!/usr/bin/env bash
Green="\033[32m"
yellow="\033[33m"
Font="\033[0m"
Red="\033[31m"
Cyan='\033[0;36m'
Pe="\033[0;35m"
# blue=$(tput setaf 4)
# normal=$(tput sgr0)
lxd_install_mod=("wget" "curl" "sudo" "jq" "ipcalc")
for lxd_install_mod in "${lxd_install_mod[@]}"
do
command -V $lxd_install_mod >/dev/null 2>&1
if [ $? -ne 0 ];
then
apt -y install $lxd_install_mod
fi
done
#报错检测
snap_detect(){
lxd_lxc_detect=$(lxc list -f csv)
if [ $? -ne 0 ]
then
echo "lxd已安装但是无法使用,请尝试重启"
exit 0
elif [[ "$lxd_lxc_detect" =~ "snap-update-ns failed with code1".* ]]
then
systemctl restart apparmor
snap restart lxd
else
echo "环境没问题"
exit 0
fi
}
# 安装snap
snap_install(){
lxd_snap=`dpkg -l |awk '/^[hi]i/{print $2}' | grep -ow snap`
lxd_snapd=`dpkg -l |awk '/^[hi]i/{print $2}' | grep -ow snapd`
if [[ "$lxd_snap" =~ ^snap.* ]]&&[[ "$lxd_snapd" =~ ^snapd.* ]]
then
echo "snap已安装"
lxd_install
else
echo "开始安装snap"
apt -y install sudo
sudo apt -y update
sudo apt -y install snap
sudo apt -y install snapd
lxd_install
fi
}
lxc_list_ipv6_1()
{
ipv6_network_name=$(ls /sys/class/net/ | grep -v "`ls /sys/devices/virtual/net/`")
ipv6_name=$(curl -s -6 ip.sb -m 2 2> /dev/null )
# ifconfig ${ipv6_network_name} | awk '/inet6/{print $2}'
ip_network_gam=$(ip -6 addr show ${ipv6_network_name} | grep -E "${ipv6_name}/64|${ipv6_name}/80|${ipv6_name}/96|${ipv6_name}/112" | grep global | awk '{print $2}' 2> /dev/null)
if [ -n "$ip_network_gam" ];
then
read -p "检测到你有一个公网ipv6的网段可以开ipv6实例,是否开启?(如果服务商上游禁止路由那ipv6可能无法使用,默认关闭) [Y/n] :" ipv6_lxc
[ -z "${ipv6_lxc}" ] && ipv6_lxc="n"
fi
}
lxc_list_ipv6_2()
{
if [[ $ipv6_lxc == [Yy] ]]; then
if ! grep "net.ipv6.conf.${ipv6_network_name}.proxy_ndp = 1" /etc/sysctl.conf >/dev/null
then
echo "net.ipv6.conf.${ipv6_network_name}.proxy_ndp = 1">>/etc/sysctl.conf
sysctl -p
fi
if ! grep "net.ipv6.conf.all.forwarding = 1" /etc/sysctl.conf >/dev/null
then
echo "net.ipv6.conf.all.forwarding = 1">>/etc/sysctl.conf
sysctl -p
fi
if ! grep "net.ipv6.conf.all.proxy_ndp=1" /etc/sysctl.conf >/dev/null
then
echo "net.ipv6.conf.all.proxy_ndp=1">>/etc/sysctl.conf
sysctl -p
fi
ipv6_lala=$(ipcalc ${ip_network_gam} | grep "Prefix:" | awk '{print $2}')
randbits=$(od -An -N2 -t x1 /dev/urandom | tr -d ' ')
lxc_ipv6="${ipv6_lala%/*}${randbits}"
lxc config device add $lxc_name eth1 nic nictype=routed parent=${ipv6_network_name} ipv6.address=${lxc_ipv6}
fi
}
# 安装LXD
lxd_install(){
test -s /etc/sysctl.d/99-lxd.conf || cat << EOF >/etc/sysctl.d/99-lxd.conf
fs.aio-max-nr = 524288
fs.inotify.max_queued_events = 1048576
fs.inotify.max_user_instances = 1048576
fs.inotify.max_user_watches = 1048576
kernel.dmesg_restrict = 1
kernel.keys.maxbytes = 2000000
kernel.keys.maxkeys = 2000
net.core.bpf_jit_limit = 3000000000
net.ipv4.neigh.default.gc_thresh3 = 8192
net.ipv6.neigh.default.gc_thresh3 = 8192
vm.max_map_count = 262144
EOF
snap_core22=`snap list core22`
snap_lxd=`snap list lxd`
if [[ "$snap_core22" =~ core22.* ]]&&[[ "$snap_lxd" =~ lxd.* ]]
then
echo "lxd已安装"
snap_detect
exit 0
else
echo "开始安装LXD"
sudo snap install core
sudo snap install lxd
snap set lxd lxcfs.pidfd=true
snap set lxd lxcfs.loadavg=true
snap set lxd lxcfs.cfs=true
echo "LXD安装完成"
echo "需要重启才能使用后续脚本"
echo "重启后请再次执行步骤1确认问题"
stty erase '^H' && read -p "需要重启VPS后,才能开启LXD,是否现在重启 ? [Y/n] :" yn
[ -z "${yn}" ] && yn="y"
if [[ $yn == [Yy] ]]; then
echo -e " ${Green}[提示]${Font} VPS 重启中..."
reboot
fi
exit 0
fi
}
#容器名称
lxd_name(){
read -p "输入容器名称(只能英文数字且必须输入):" lxc_name
if [ -z "$lxc_name" ];
then
echo "名称不能为空"
exit 0
fi
}
#网络选择
lxd_network()
{
read -p "输入网卡名称(默认与容器名相同):" network_lxc
read -p "请输入ipv4网关(默认10.10.10.1):" network_ipv4_gateway
read -p "请输入ipv4地址范围(默认10.10.10.1/24):" network_ipv4_address
read -p "请输入ipv6地址范围(如ipv6/64 默认随机):" network_ipv6_address
}
#为空
lxd_default()
{
[ -z "$network_lxc" ] && network_lxc="${lxc_name}"
[ -z "$network_ipv4_gateway" ] && network_ipv4_gateway="10.10.10.1"
[ -z "$network_ipv4_address" ] && network_ipv4_address="10.10.10.1/24"
[ -z "$network_ipv6_address" ] && network_ipv6_address="auto"
[ -z "$lxc_disk" ] && lxc_disk="btrfs"
[ -z "$lxc_disk_size" ] && lxc_disk_size="2048"
[ -z "$lxc_cpu" ] && lxc_cpu="1"
[ -z "$lxc_ram" ] && lxc_ram="1024"
[ -z "$lxc_cpu_performance" ] && lxc_cpu_performance="100"
[ -z "$lxc_rate" ] && lxc_rate="1000"
}
# 创建网卡
lxd_network_create()
{
echo -e "正在创建网卡 ${yellow}[warnning]${Font}"
echo -n `lxc network create $network_lxc>/dev/null 2>&1`
cat <<EOF | lxc network edit $network_lxc
{
"config": {
"ipv4.address": "${network_ipv4_address}",
"ipv4.dhcp": "true",
"ipv4.dhcp.gateway": "${network_ipv4_gateway}",
"ipv4.firewall": "true",
"ipv4.nat": "true",
"ipv4.routing": "true",
"ipv6.address": "${network_ipv6_address}",
"ipv6.dhcp": "true",
"ipv6.dhcp.stateful": "true",
"ipv6.firewall": "false",
"ipv6.nat": "true",
"ipv6.routing": "true"
},
"description": "",
"name": "lxcbr",
"type": "bridge",
"used_by": null,
"managed": true,
"status": "Created",
"locations": [
"none"
]}
EOF
echo -e "网卡创建完成 ${Green}[success]${Font}"
}
#物理卷选择界面
lxd_disk(){
read -p "请输入磁盘类型(可选btrfs,lvm,zfs 默认btrfs):" lxc_disk
read -p "请输入纯数字磁盘空间大小(默认2048,单位MB):" lxc_disk_size
}
#开始创建磁盘
lxd_disk_cerat()
{
echo -e "开始创建磁盘 ${yellow}[warnning]${Font}"
echo -n `lxc storage create ${lxc_name} ${lxc_disk} size=${lxc_disk_size}MB>/dev/null 2>&1`
echo -e "磁盘创建完成 ${Green}[success]${Font}"
}
#Profiles
lxd_limits()
{
read -p "请输入容器配置的模板名称:" lxc_name_profile
read -p "指定的ipv4地址(必须输入与网关同一网段的ip): " lxc_network_ipv4
read -p "输入指定的ipv6地址(必须输入与ipv6范围同网段的ip): " lxc_network_ipv6
read -p "请输入cpu的核心数限制(默认1核,请输入纯数字):" lxc_cpu
read -p "请输入运行内存限制(请输入纯数字,单位MB):" lxc_ram
read -p "请输入cpu占用百分比限制(请输入纯数字,不限制为100 默认100 单位%):" lxc_cpu_performance
read -p "请输入网速限制(请输入纯数字,默认1000 单位mbps):" lxc_rate
}
lxd_limits_profile()
{
echo -e "开始写入配置模板 ${yellow}[warnning]${Font}"
echo `lxc profile create $lxc_name_profile >/dev/null 2>&1`
cat <<EOF | lxc profile edit ${lxc_name_profile}
{
"config": {
"limits.cpu": "${lxc_cpu}",
"limits.memory": "${lxc_ram}MB",
"limits.cpu.allowance": "${lxc_cpu_performance}ms/100ms",
"limits.memory.swap": "false"
},
"description": "Default LXD profile",
"devices": {
"eth0": {
"name": "eth0",
"ipv4.address": "${lxc_network_ipv4}",
"ipv6.address": "${lxc_network_ipv6}",
"limits.max": "${lxc_rate}Mbit",
"network": "${network_lxc}",
"type": "nic"
},
"root": {
"path": "/",
"pool": "${lxc_name}",
"type": "disk"
}
},
"name": "${lxc_name}",
"used_by": [
]
}
EOF
echo -e "写入配置模板完成 ${Green}[success]${Font}"
}
#开始创建容器
lxd_lxc_create()
{
echo -e "开始创建容器 ${yellow}[warnning]${Font}"
echo `lxc init tuna-images:${lxc_os} ${lxc_name} -p ${lxc_name}`
echo -e "创建容器完成 ${Green}[success]${Font}"
}
#独立模板创建
alone_lxc_Profiles()
{
clear
read -p "请输入要创建的模板名称: " lxc_name
read -p "请输入关联此模板的网卡: " lxc_name_network
read -p "请输入关联此模板的物理卷: " lxc_name_disk
if [ -z "$lxc_name" -o -z "$lxc_name_network" -o -z "$lxc_name_disk" ];then
echo "不能为空请重新输入"
sleep 3s
alone_lxc_Profiles
fi
lxd_limits
echo -e "开始写入配置模板 ${yellow}[warnning]${Font}"
echo `lxc profile create $lxc_name >/dev/null 2>&1`
cat <<EOF | lxc profile edit ${lxc_name}
{
"config": {
"limits.cpu": "${lxc_cpu}",
"limits.memory": "${lxc_ram}MB",
"limits.cpu.allowance": "${lxc_cpu_performance}ms/100ms",
"limits.memory.swap": "false"
},
"description": "Default LXD profile",
"devices": {
"eth0": {
"name": "eth0",
"limits.max": "${lxc_rate}Mbit",
"network": "${lxc_name_network}",
"type": "nic"
},
"root": {
"path": "/",
"pool": "${lxc_name_disk}",
"type": "disk"
}
},
"name": "${lxc_name}",
"used_by": [
]
}
EOF
echo -e "写入配置模板完成 ${Green}[success]${Font}"
}
#独立网卡创建
alone_lxc_network()
{
clear
read -p "输入网卡名称(默认lxdpro):" network_lxc
read -p "请输入ipv4网关(默认10.10.10.1):" network_ipv4_gateway
read -p "请输入ipv4地址范围(默认10.10.10.1/24):" network_ipv4_address
read -p "请输入ipv6地址范围(如ipv6/64 默认随机):" network_ipv6_address
[ -z "$network_lxc" ] && network_lxc="lxdpro"
[ -z "$network_ipv4_gateway" ] && network_ipv4_gateway="10.10.10.1"
[ -z "$network_ipv4_address" ] && network_ipv4_address="10.10.10.1/24"
[ -z "$network_ipv6_address" ] && network_ipv6_address="auto"
lxd_network_create
}
#独立物理卷创建
alone_lxc_disk()
{
clear
read -p "请输入要创建的物理卷名称: " lxc_name
if [ -z "$lxc_name" ];then
echo "不能为空请重新输入!!"
sleep 3s
alone_lxc_disk
fi
lxd_disk
[ -z "$lxc_disk" ] && lxc_disk="btrfs"
[ -z "$lxc_disk_size" ] && lxc_disk_size="2048"
lxd_disk_cerat
}
#单独创建容器
alone_lxc()
{
clear
read -p "请输入要创建的容器名称:" lxc_name
read -p "输入需要与此容器关联的模板名称:" lxc_Profiles
if [ -z "$lxc_name" -o -z "$lxc_Profiles" ];then
echo "不能为空请重新输入!"
sleep 3s
alone_lxc
fi
echo -e "开始创建容器 ${yellow}[warnning]${Font}"
echo `lxc init images:debian/bullseye ${lxc_name} -p ${lxc_Profiles}`
echo -e "创建容器完成 ${Green}[success]${Font}"
}
#创建容器
lxc_user_lxc()
{
echo "开始创建容器"
lxc init tuna-images:${lxc_os} ${lxc_name} -n ${lxc_name} -s ${lxc_name}>/dev/null 2>&1
if [ $? -ne 0 ];then
lxc network delete ${lxc_name}
lxc storage delete ${lxc_name}
echo -e "${Green}创建失败了!请尝试重新创建${Font}"
fi
}
#创建简单硬盘
lxc_user_storage_create()
{
echo "开始创建物理卷 物理卷名: ${lxc_name}"
lxc storage create ${lxc_name} btrfs size=${lxc_disk}MB >/dev/null 2>&1
}
#创建简单硬盘---kvm
lxc_user_storage_create_kvm()
{
echo "开始创建物理卷 物理卷名: ${lxc_name}"
lxc storage create ${lxc_name} lvm size=${lxc_disk}MB >/dev/null 2>&1
}
#创建简单网卡
lxc_user_network_create()
{
echo "开始创建网卡 网卡名: ${lxc_name}"
lxc network create ${lxc_name} -t bridge>/dev/null 2>&1
}
#CPU核心数限制
lxc_user_cpu()
{
lxc config set ${lxc_name} limits.cpu ${lxc_cpu}
}
#CPU限制
lxc_user_cpu_allowance()
{
lxc config set ${lxc_name} limits.cpu.allowance ${lxc_cpu_allowance}
}
#CPU优先级
lxc_user_cpu_priority()
{
lxc config set ${lxc_name} limits.cpu.priority ${lxc_cpu_priority}
}
#运行内存限制
lxc_user_memory()
{
lxc config set ${lxc_name} limits.memory ${lxc_memory}MB
}
#硬盘限制
lxc_user_disk()
{
lxc config device set ${lxc_name} root size=${lxc_disk}MB
}
#网速限制
lxc_user_network_rate()
{
lxc config device set ${lxc_name} eth0 limits.max ${lxc_rate}Mbit
}
#下载流量
lxc_user_network_ingress()
{
lxc config device set ${lxc_name} eth0 limits.ingress ${lxc_rate}Mbit
}
#上传限制
lxc_user_network_egress()
{
lxc config device set ${lxc_name} eth0 limits.egress ${lxc_rate}Mbit
}
#网速优先级
lxc_user_network_priority()
{
lxc config device set ${lxc_name} eth0 ${network_priority}
}
#硬盘优先级
lxc_user_disk.priority()
{
lxc config device set ${lxc_name} root limits.disk.priority ${disk.priority}
}
#硬盘IO限制
lxc_user_disk_io()
{
lxc config device set ${lxc_name} root limits.max ${lxc_io}iops
}
#CPU软限制
lxc_user_cpu_allowance()
{
lxc config set ${lxc_name} limits.cpu.allowance ${lxc_cpu_allowance}%
}
#查询实例是否存在
lxd_jq_cunzai()
{
jq_lxc_ls=$(curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket lxd/1.0/instances | jq .metadata | jq -r .[])
if echo "${jq_lxc_ls}" | grep -w "/1.0/instances/${lxc_name}" &>/dev/null;
then
i=0
else
echo -e "${Red}未查找到当前实例,请重新输入!${Font}"
exit 0
fi
}
#jq实例列表
lxd_jq_ls()
{
clear
jq_list_name=$(curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket lxd/1.0/instances | jq .metadata | jq -r .[] | sed 's/\/1.0\/instances\///g')
jq_list_name=(${jq_list_name})
jq_statuscode=([103]=${Green}运行${Font} [102]=${yellow}停止${Font} [112]=${Red}异常${Font})
i=0
if [ -z "${jq_list_name[${i}]}" ];
then
i=0
else
echo "实例列表:"
fi
while :
do
lxc_jq_cpu=$(curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket lxd/1.0/instances/${jq_list_name[${i}]} | jq .metadata | jq .expanded_config | jq -r .'["limits.cpu"]')
lxc_jq_memory=$(curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket lxd/1.0/instances/${jq_list_name[${i}]} | jq .metadata | jq .expanded_config | jq -r .'["limits.memory"]')
lxc_jq_statuscode=$(curl -s --unix-socket /var/snap/lxd/common/lxd/unix.socket lxd/1.0/instances/${jq_list_name[${i}]} | jq .metadata | jq -r .'["status_code"]')
if [[ $lxc_jq_cpu = "null" ]];
then
lxc_jq_cpu="未限制"
fi
if [[ $lxc_jq_memory = "null" ]];
then
lxc_jq_cpu="未限制"
fi
if [ -z "${jq_list_name[${i}]}" ];
then
break
else
echo -e "容器名: ${Green}${jq_list_name[${i}]}${Font} CPU: ${Green}${lxc_jq_cpu}${Font} 核心 内存: ${Green}${lxc_jq_memory}${Font} 状态: ${jq_statuscode[${lxc_jq_statuscode}]} "
((i++))
fi
done
}
#dhclient
#停止容器
lxc_stop()
{
echo -n `lxc stop ${lxc_name} -f>/dev/null 2>&1`
echo -e "容器已停止 ${Green}[success]${Font}"
}
#删除容器
lxc_delete()
{
echo -e "正在删除容器 ${yellow}[warnning]${Font}"
echo -n `lxc delete ${lxc_name}>/dev/null 2>&1`
sleep 10s
echo -e "容器已删除 ${Green}[success]${Font}"
}
#删除模板
lxc_yaf()
{
echo -n `lxc profile delete ${profile_delete}>/dev/null 2>&1`
echo -e "配置模板已删除 ${Green}[success]${Font}"
}
#删除网卡
lxc_delete_network()
{
echo -n `lxc network delete ${network_lxc}>/dev/null 2>&1`
echo -e "网卡已删除 ${Green}[success]${Font}"
}
#删除磁盘
lxc_delete_storage()
{
echo -n `lxc storage delete ${storage_delete}>/dev/null 2>&1`
echo -e "磁盘已删除 ${Green}[success]${Font}"
}
#启动容器
lxc_start()
{
echo -e "正在启动容器 ${yellow}[warnning]${Font}"
echo -n `lxc start ${lxc_name}>/dev/null 2>&1`
echo -e "容器启动成功 ${Green}[success]${Font}"
}
#一键删除容器
lxd_delete_lxc()
{
lxd_jq_ls
lxd_name
lxd_jq_cunzai
read -p "请输入要删除的容器网卡名称(直接回车键,默认与容器名相同):" network_lxc
read -p "请输入要删除的磁盘名称(直接回车键,默认与容器名相同):" storage_delete
read -p "请输入要删除的模板名称(直接回车键,默认与容器名相同):" profile_delete
[ -z "$network_lxc" ] && network_lxc="${lxc_name}"
[ -z "$storage_delete" ] && storage_delete="${lxc_name}"
[ -z "$profile_delete" ] && profile_delete="${lxc_name}"
}
#进入容器
lxd_exec_lxc()
{
lxd_jq_ls
read -p "请输入你要进去的容器名(exit可退出容器):" lxc_exec
lxc exec ${lxc_exec} /bin/bash
}
#容器列表
lxd_list_lxc()
{
clear
lxc ls -c npc
}
#磁盘列表
lxd_list_disk()
{
clear
lxc storage list
}
#网卡列表
lxd_list_network()
{
clear
lxc network list
}
#详细创建容器
#启动容器
lxd_lxc_start()
{
clear
echo -e "1.启动指定容器"
echo -e "2.启动所有容器"
while :; do echo
read -p "请输入数字选择: " choice
if [[ ! $choice =~ ^[1-2]$ ]]
then
echo -ne " ${Red}输入错误, 请输入正确的数字!${Font}"
else
break
fi
done
case $choice in
1) lxd_name
echo -n `lxc start ${lxc_name}>/dev/null 2>&1`
[[ $? != 0 ]] && echo "容器器正在运行中,无需启动" || echo "容器启动成功"
;;
2) read -p "是否启动所有(y/n): " judge
[[ $judge = y ]] && echo `lxc start --all` || exit 0
echo "成功启动所有容器,如有报错请注意提示个别容器无法启动"
;;
esac
}
#停止容器
lxd_lxc_stop()
{
clear
echo -e "1.停止指定容器"
echo -e "2.停止所有容器"
echo -e "3.强制停止指定容器"
echo -e "4.强制停止所有容器"
while :; do echo
read -p "请输入数字选择: " choice
if [[ ! $choice =~ ^[1-2]$ ]]
then
echo -ne " ${Red}输入错误, 请输入正确的数字!${Font}"
else
break
fi
done
case $choice in
1) lxd_jq_ls
read -p "请输入容器名: " lxc_name
echo -n `lxc stop ${lxc_name}>/dev/null 2>&1`
[[ $? != 0 ]] && echo "容器已经是停止状态" || echo "容器停止成功"
;;
2) read -p "是否停止所有容器(y/n): " judge
[[ $judge = y ]] && echo `lxc stop --all` || exit 0
echo "成功停止所有容器,如有报错请注意提示个别容器无法启动"
;;
3) lxd_jq_ls
read -p "请输入容器名: " lxc_name
echo -n `lxc stop -f ${lxc_name}>/dev/null 2>&1`
[[ $? != 0 ]] && echo "容器已经是停止状态" || echo "容器停止成功"
;;
4) read -p "是否强制停止所有容器(y/n): " judge
[[ $judge = y ]] && echo `lxc stop -f --all` || exit 0
echo "成功停止所有容器,如有报错请注意提示个别容器无法启动"
;;
esac
}
#重启容器
lxd_lxc_restart()
{
clear
echo -e "1.重启指定容器"
echo -e "2.重启所有容器"
echo -e "3.强制重启指定容器"
echo -e "4.强制重启所有容器"
while :; do echo
read -p "请输入数字选择: " choice
if [[ ! $choice =~ ^[1-2]$ ]]
then
echo -ne " ${Red}输入错误, 请输入正确的数字!${Font}"
else
break
fi
done
case $choice in
1) lxd_jq_ls
lxd_name
echo -n `lxc restart ${lxc_name}>/dev/null 2>&1`
[[ $? != 0 ]] && echo "容器重启失败" || echo "容器重启成功"
;;
2) read -p "是否停止所有容器(y/n): " judge
[[ $judge = y ]] && echo `lxc restart --all` || exit 0
echo "成功停止所有容器,如有报错请注意提示个别容器无法启动"
;;
3) lxd_name
echo -n `lxc restart -f ${lxc_name}>/dev/null 2>&1`
[[ $? != 0 ]] && echo "容器重启失败" || echo "容器重启成功"
;;
4) read -p "是否强制停止所有容器(y/n): " judge
[[ $judge = y ]] && echo `lxc restart -f --all` || exit 0
echo "成功重启所有容器,如有报错请注意提示个别容器无法启动"
;;
esac
}
#对容器进行限制
lxc_limit()
{
clear
echo -e "1.限制CPU数量"
echo -e "2.软限制CPU性能"
echo -e "3.硬限制CPU性能"
echo -e "4.运行内存限制"
echo -e "5.限制网速"
echo -e "6.限制硬盘IO"
echo -e "7.设置CPU优先级"
echo -e "8.设置网络优先级"
echo -e "9.设置硬盘优先级"
echo -e "0.返回首页"
while :; do echo
read -p "请输入数字选择: " choice
if [[ ! $choice =~ ^[0-9]$ ]]
then
echo -ne " ${Red}输入错误, 请输入正确的数字!${Font}"
else
break
fi
done
case $choice in
0) front_page
;;
1) read -p "请输入要限制的容器: " lxc_name
read -p "请输入要限制的CPU内核数: " lxc_cpu
lxc_user_cpu
;;
2) read -p "请输入要限制的容器: " lxc_name
read -p "请输入你要限制CPU的性能百分比(例如50%):" lxc_cpu_allowance
lxc_user_cpu_allowance
;;
3) read -p "请输入要限制的容器: " lxc_name
read -p "请输入你要限制CPU运行时间比(例如25ms/100ms)" lxc_cpu_allowance
lxc_user_cpu_allowance
;;
4) read -p "请输入要限制的容器: " lxc_name
read -p "请输入你要限制的运行内存(单位:MB或者GB,不能有小数点,例如512MB,5GB):" lxc_memory
lxc config set ${lxc_name} limits.memory ${lxc_memory}
;;
5) read -p "请输入要限制的容器: " lxc_name
read -p "请选择要限制的选项(1.上传 2.下载 3.全部): " xuanxiang
if [[ ${xuanxiang} == 1 ]];then
read -p "限制多少速度(单位:mbps不需要再填写): " lxc_rate
lxc_user_network_egress
exit 0
fi
if [[ ${xuanxiang} == 2 ]];then
read -p "限制多少速度(单位:mbps不需要再填写): " lxc_rate
lxc_user_network_ingress
exit 0
fi
if [[ ${xuanxiang} == 3 ]];then
read -p "限制多少速度(单位:mbps不需要再填写): " lxc_rate
lxc_user_network_rate
exit 0
fi
;;
6) read -p "请输入要限制的容器: " lxc_name
read -p "输入你要限制的IO(单位iops,无需再输入): "
lxc_user_disk_io
;;
7) read -p "请输入要限制的容器: " lxc_name
read -p "请输入该容器的的CPU优先级(数字0-10之间): " lxc_cpu_priority
lxc_user_cpu_priority
;;
8) read -p "请输入要限制的容器: " lxc_name
read -p "请输入该容器的的网速优先级(数字0-10之间): " network_priority
lxc_user_network_priority
;;
9) read -p "请输入要限制的容器: " lxc_name
read -p "请输入该容器的的硬盘优先级(数字0-10之间): " disk.priority
lxc_user_disk.priority
;;
esac
}
#容器信息
lxd_information()
{
lxc_current=`lxc info ${lxc_name} | grep current | awk '{print $3}'`
lxc_peak=`lxc info ${lxc_name} | grep 'Memory (peak):' | awk '{print $3}'`
lxc_received=`lxc info ${lxc_name} | grep -A 13 eth0 | grep 'Bytes received:' | awk '{print $3}'`
lxc_sent=`lxc info ${lxc_name} | grep -A 13 eth0 | grep 'Bytes sent:' | awk '{print $3}'`
lxc_PID=`lxc info ${lxc_name} | grep PID: | awk '{print $2}'`
lxc_Created=`lxc info ${lxc_name} | grep Created: | awk '{print ($2,$3)}'`
lxc_Status=`lxc info ${lxc_name} | grep Status: | awk '{print $2}'`
lxc_disk=`lxc info ${lxc_name} | grep root: | awk '{print $2}'`
lxc_inet6=`lxc info ${lxc_name} | grep inet6 | awk '{print $2}'`
lxc_inet4=`lxc info ${lxc_name} | grep inet | grep -v inet6 | awk '{print $2}'`
lxc_cpu=`lxc config show ${lxc_name} | grep 'limits.cpu:' | awk '{print $2}' | tr -cd "[0-9]"`
lxc_memory=`lxc config show ${lxc_name} | grep 'limits.memory:' | awk '{print $2}'`
lxc_image_os=`lxc config show ${lxc_name} | grep 'image.description:' | awk '{ $1=""; print $0 }'`
lxc_architecture=`lxc config show ${lxc_name} | grep 'architecture:' | grep -v 'image.architecture:' | awk '{print $2}'`
lxc_architecture_a=`lxc config show ${lxc_name} | grep 'image.architecture:' | awk '{print $2}'`
lxc_profiles=`lxc config show ${lxc_name} | grep -A 1 'profiles:' | grep '-' | awk '{print $2}'`
if [ -z "$lxc_cpu" ];then
lxc_cpu=`lxc profile show ${lxc_name} | grep 'limits.cpu:' | awk '{print $2}' | tr -cd "[0-9]"`
fi
if [ -z "$lxc_memory" ];then
lxc_memory=`lxc profile show ${lxc_name} | grep 'limits.memory:' | awk '{print $2}'`
fi
clear
echo -e "${Cyan}容器名: ${Font}${yellow}${lxc_name}${Font}"
echo -e "${Cyan}创建时间: ${Font}${yellow}${lxc_Created}${Font}"
echo -e "${Cyan}进程PID: ${Font}${yellow}${lxc_PID}${Font}"
echo -e "${Cyan}容器状态: ${Font}${yellow}${lxc_Status}${Font}"
echo -e "${Cyan}系统名称: ${Font}${yellow}${lxc_image_os}${Font}"
echo -e "${Cyan}系统架构: ${Font}${yellow}${lxc_architecture} ${lxc_architecture_a}${Font}"
echo -e "${Cyan}所属模板: ${Font}${yellow}${lxc_profiles}${Font}"
echo -e "${Cyan}CPU核数: ${Font}${yellow}${lxc_cpu}${Font}"
echo -e "${Cyan}总内存限制: ${Font}${yellow}${lxc_memory}${Font}"
echo -e "${Cyan}正在使用内存: ${Font}${yellow}${lxc_current}${Font}"
echo -e "${Cyan}巅峰使用内存: ${Font}${yellow}${lxc_peak}${Font}"
echo -e "${Cyan}硬盘使用情况: ${Font}${yellow}${lxc_disk}${Font}"
echo -e "${Cyan}流量传入: ${Font}${yellow}${lxc_received}${Font}"
echo -e "${Cyan}流量传出: ${Font}${yellow}${lxc_sent}${Font}"
echo -e "${Cyan}IPV4地址: ${Font}\n${yellow}${lxc_inet4}${Font}"
echo -e "${Cyan}IPV6地址: ${Font}\n${yellow}${lxc_inet6}${Font}"
}
#echo " 容器名 使用内存 ipv4地址 ipv6地址"
#echo `lxc ls -c nm46 | grep ${lxc_name}`
# lxc ls -c nm46 | grep nl6 | awk '{print $9}'
# lxc ls -c npmMD46CNs
# ps -ef | grep 3346539
#详细创建容器
lxc_detailed_creation()
{
lxd_name
lxd_network
lxd_disk
lxd_limits
lxd_default
lxd_network_create
lxd_disk_cerat
lxd_limits_profile
lxd_lxc_create
lxc_start
lxd_information
}
#快速创建
lxc_system()
{
echo `lxc remote add tuna-images https://mirrors.tuna.tsinghua.edu.cn/lxc-images/ --protocol=simplestreams --public>/dev/null 2>&1`
echo "输入你需要创建的容器镜像"
echo -e "1.Centos7"
echo -e "2.Debian10"
echo -e "3.Debian11"
echo -e "4.Debian12"
echo -e "5.Ubuntu 16.04"
echo -e "6.Ubuntu 18.04"
echo -e "7.Ubuntu 20.04"
echo -e "8.Ubuntu 22.04"
echo -e "9.alpine 3.17"
echo -e "10.Archlinxe"
echo -e "11.OpenWrt 21.02"
echo -e "12.Fedora 38"
echo -e "13.Kali"
while :; do echo
read -p "请输入数字选择: " choice
if [ $choice -ge 1 -a $choice -le 13 ]
then
break
else
echo -ne " ${Red}输入错误, 请输入正确的数字!${Font}"
fi
done
if [[ ${choice} == 1 ]]; then
lxc_os="centos/7"
fi
if [[ ${choice} == 2 ]]; then
lxc_os="debian/10"
fi
if [[ ${choice} == 3 ]]; then
lxc_os="debian/11"
fi
if [[ ${choice} == 4 ]]; then
lxc_os="debian/12"
fi
if [[ ${choice} == 5 ]]; then
lxc_os="ubuntu/16.04"
fi
if [[ ${choice} == 6 ]]; then
lxc_os="ubuntu/18.04"
fi
if [[ ${choice} == 7 ]]; then
lxc_os="ubuntu/20.04"
fi
if [[ ${choice} == 8 ]]; then
lxc_os="ubuntu/22.04"
fi
if [[ ${choice} == 9 ]]; then
lxc_os="alpine/3.17"
fi
if [[ ${choice} == 10 ]]; then
lxc_os="archlinux"