2023-04-09 21:30:17 +08:00
#!/bin/bash
# from
# https://github.com/spiritLHLS/pve
2023-06-29 21:41:08 +08:00
# 2023.06.29
2023-04-09 21:30:17 +08:00
# cd /root
2023-04-24 08:54:49 +08:00
_red( ) { echo -e " \033[31m\033[01m $@ \033[0m " ; }
_green( ) { echo -e " \033[32m\033[01m $@ \033[0m " ; }
_yellow( ) { echo -e " \033[33m\033[01m $@ \033[0m " ; }
_blue( ) { echo -e " \033[36m\033[01m $@ \033[0m " ; }
2023-04-24 09:19:32 +08:00
reading( ) { read -rp " $( _green " $1 " ) " " $2 " ; }
2023-04-27 11:27:20 +08:00
utf8_locale = $( locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8" )
2023-04-24 08:54:49 +08:00
if [ [ -z " $utf8_locale " ] ] ; then
2023-04-27 11:27:20 +08:00
echo "No UTF-8 locale found"
2023-04-24 08:54:49 +08:00
else
export LC_ALL = " $utf8_locale "
export LANG = " $utf8_locale "
2023-04-27 11:10:19 +08:00
export LANGUAGE = " $utf8_locale "
2023-04-27 11:27:20 +08:00
echo " Locale set to $utf8_locale "
2023-04-24 08:54:49 +08:00
fi
2023-04-09 21:30:17 +08:00
2023-04-24 09:55:08 +08:00
check_cdn( ) {
local o_url = $1
for cdn_url in " ${ cdn_urls [@] } " ; do
if curl -sL -k " $cdn_url $o_url " --max-time 6 | grep -q "success" > /dev/null 2>& 1; then
export cdn_success_url = " $cdn_url "
return
fi
sleep 0.5
done
export cdn_success_url = ""
}
check_cdn_file( ) {
check_cdn "https://raw.githubusercontent.com/spiritLHLS/ecs/main/back/test"
if [ -n " $cdn_success_url " ] ; then
_yellow "CDN available, using CDN"
else
_yellow "No CDN available, no use CDN"
fi
}
2023-05-13 19:09:35 +08:00
cdn_urls = ( "https://cdn.spiritlhl.workers.dev/" "https://cdn3.spiritlhl.net/" "https://cdn1.spiritlhl.net/" "https://ghproxy.com/" "https://cdn2.spiritlhl.net/" )
2023-04-24 09:55:08 +08:00
check_cdn_file
2023-04-09 21:30:17 +08:00
pre_check( ) {
home_dir = $( eval echo " ~ $( whoami) " )
if [ " $home_dir " != "/root" ] ; then
2023-06-29 21:41:08 +08:00
_red "The script will exit if the current path is not /root."
2023-04-24 08:54:49 +08:00
_red "当前路径不是/root, 脚本将退出。"
2023-04-09 21:30:17 +08:00
exit 1
fi
if ! command -v dos2unix > /dev/null 2>& 1; then
apt-get install dos2unix -y
fi
2023-04-11 12:00:05 +08:00
if [ ! -f "buildvm.sh" ] ; then
2023-04-24 09:55:08 +08:00
curl -L ${ cdn_success_url } https://raw.githubusercontent.com/spiritLHLS/pve/main/scripts/buildvm.sh -o buildvm.sh && chmod +x buildvm.sh
2023-04-11 12:04:41 +08:00
dos2unix buildvm.sh
2023-04-09 21:30:17 +08:00
fi
}
2023-04-11 12:18:05 +08:00
# files=$(find . -maxdepth 1 -name "vm*" | sort)
# if [ -n "$files" ]; then
# for file in $files
# do
# cat "$file" >> vmlog
# done
# fi
2023-04-11 12:00:05 +08:00
2023-04-12 09:58:35 +08:00
check_info( ) {
log_file = "vmlog"
if [ ! -f "vmlog" ] ; then
2023-06-29 21:41:08 +08:00
_yellow "vmlog file does not exist in the current directory"
2023-04-24 08:54:49 +08:00
_yellow "当前目录下不存在vmlog文件"
2023-04-12 09:58:35 +08:00
vm_num = 202
web2_port = 40003
port_end = 50025
else
while read line; do
last_line = " $line "
done < " $log_file "
last_line_array = ( $last_line )
vm_num = " ${ last_line_array [0] } "
user = " ${ last_line_array [1] } "
password = " ${ last_line_array [2] } "
ssh_port = " ${ last_line_array [6] } "
web1_port = " ${ last_line_array [7] } "
web2_port = " ${ last_line_array [8] } "
port_start = " ${ last_line_array [9] } "
port_end = " ${ last_line_array [10] } "
system = " ${ last_line_array [11] } "
2023-05-20 10:09:23 +08:00
storage = " ${ last_line_array [12] } "
2023-06-29 21:41:08 +08:00
_green "Current information corresponding to the last NAT server:"
2023-04-24 08:54:49 +08:00
_green "当前最后一个NAT服务器对应的信息: "
2023-06-29 21:41:08 +08:00
echo " NAT服务器(NAT Server): $vm_num "
2023-04-12 09:58:35 +08:00
# echo "用户名: $user"
# echo "密码: $password"
2023-06-29 21:41:08 +08:00
echo " 外网SSH端口(Extranet SSH port): $ssh_port "
echo " 外网80端口(Extranet port 80): $web1_port "
echo " 外网443端口(Extranet port 443): $web2_port "
echo " 外网其他端口范围(Other port ranges): $port_start - $port_end "
echo " 系统(System): $system "
echo " 存储盘(Storage Disk): $storage "
2023-04-12 09:58:35 +08:00
fi
}
2023-04-09 21:30:17 +08:00
2023-04-09 22:45:39 +08:00
build_new_vms( ) {
2023-04-09 21:30:17 +08:00
while true; do
2023-06-29 21:41:08 +08:00
_green "How many more NAT servers need to be generated? (Enter how many new NAT servers to add):"
2023-04-09 21:30:17 +08:00
reading "还需要生成几个NAT服务器? (输入新增几个NAT服务器): " new_nums
if [ [ " $new_nums " = ~ ^[ 1-9] [ 0-9] *$ ] ] ; then
break
else
2023-06-29 21:41:08 +08:00
_yellow "Invalid input, please enter a positive integer."
2023-04-24 08:54:49 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-09 21:30:17 +08:00
fi
done
2023-04-20 17:11:12 +08:00
while true; do
2023-06-29 21:41:08 +08:00
_green "How many CPUs are assigned to each virtual machine? (Enter 1 if 1 core is assigned to each virtual machine):"
2023-04-20 17:11:12 +08:00
reading "每个虚拟机分配几个CPU? (若每个虚拟机分配1核, 则输入1): " cpu_nums
if [ [ " $cpu_nums " = ~ ^[ 1-9] [ 0-9] *$ ] ] ; then
break
else
2023-06-29 21:41:08 +08:00
_yellow "Invalid input, please enter a positive integer."
2023-04-24 08:54:49 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-20 17:11:12 +08:00
fi
done
while true; do
2023-06-29 21:41:08 +08:00
_green "How much memory is allocated per virtual machine? (If 512 MB of memory is allocated per virtual machine, enter 512):"
2023-04-20 17:11:12 +08:00
reading "每个虚拟机分配多少内存?(若每个虚拟机分配512MB内存, 则输入512): " memory_nums
if [ [ " $memory_nums " = ~ ^[ 1-9] [ 0-9] *$ ] ] ; then
break
else
2023-06-29 21:41:08 +08:00
_yellow "Invalid input, please enter a positive integer."
2023-04-24 08:54:49 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-20 17:11:12 +08:00
fi
done
2023-05-20 10:09:23 +08:00
while true; do
2023-06-29 21:41:08 +08:00
_green "On which storage drive are the virtual machines opened? (Leave blank or enter 'local' if the virtual machine is to be opened on the system disk):"
2023-05-20 10:09:23 +08:00
reading "虚拟机们开设在哪个存储盘上?(若虚拟机要开设在系统盘上, 则留空或输入local): " storage
if [ -z " $storage " ] ; then
storage = "local"
fi
2023-05-30 11:25:08 +08:00
break
2023-05-20 10:09:23 +08:00
done
2023-04-20 17:11:12 +08:00
while true; do
2023-06-29 21:41:08 +08:00
_green "How many hard disks are allocated per virtual machine? (If 5G hard drives are allocated per virtual machine, enter 5):"
2023-04-20 17:11:12 +08:00
reading "每个虚拟机分配多少硬盘?(若每个虚拟机分配5G硬盘, 则输入5): " disk_nums
if [ [ " $disk_nums " = ~ ^[ 1-9] [ 0-9] *$ ] ] ; then
break
else
2023-06-29 21:41:08 +08:00
_yellow "Invalid input, please enter a positive integer."
2023-04-24 08:54:49 +08:00
_yellow "输入无效,请输入一个正整数。"
2023-04-20 17:11:12 +08:00
fi
done
2023-05-30 11:38:56 +08:00
while true; do
sys_status = "false"
2023-06-29 21:41:08 +08:00
_green "What system does each virtual machine use? (Leave blank or enter debian11 if all use debian11):"
2023-05-30 11:46:45 +08:00
reading "每个虚拟机都使用什么系统?(若都使用debian11, 则留空或输入debian11): " system
2023-05-30 11:38:56 +08:00
if [ -z " $system " ] ; then
system = "debian11"
fi
systems = ( "debian10" "debian11" "debian9" "ubuntu18" "ubuntu20" "ubuntu22" "archlinux" "centos9-stream" "centos8-stream" "almalinux8" "almalinux9" "fedora33" "fedora34" "opensuse-leap-15" )
for sys in ${ systems [@] } ; do
if [ [ " $system " = = " $sys " ] ] ; then
sys_status = "true"
break
fi
done
if [ " $sys_status " = "true" ] ; then
2023-06-29 21:41:08 +08:00
break
2023-05-30 11:38:56 +08:00
else
2023-06-29 21:41:08 +08:00
_yellow "This system is not supported, please check https://github.com/spiritLHLS/Images for the names of supported systems"
_yellow "不支持该系统,请查看 https://github.com/spiritLHLS/Images 支持的系统名字"
2023-05-30 11:38:56 +08:00
fi
done
2023-04-09 21:30:17 +08:00
for ( ( i = 1; i<= $new_nums ; i++) ) ; do
2023-04-11 12:06:38 +08:00
vm_num = $(( $vm_num + 1 ))
2023-04-14 15:46:16 +08:00
user = $( cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w 4 | head -n 1)
2023-04-09 21:30:17 +08:00
ori = $( date | md5sum)
password = ${ ori : 2 : 9 }
2023-04-09 22:20:13 +08:00
ssh_port = $(( $web2_port + 1 ))
web1_port = $(( $web2_port + 2 ))
2023-04-23 14:16:22 +08:00
web2_port = $(( $web1_port + 1 ))
2023-04-09 21:30:17 +08:00
port_start = $(( $port_end + 1 ))
port_end = $(( $port_start + 25 ))
2023-05-30 11:38:56 +08:00
./buildvm.sh $vm_num $user $password $cpu_nums $memory_nums $disk_nums $ssh_port $web1_port $web2_port $port_start $port_end $system $storage
2023-04-09 21:30:17 +08:00
cat " vm $vm_num " >> vmlog
rm -rf " vm $vm_num "
2023-04-14 15:34:53 +08:00
sleep 60
2023-04-09 21:30:17 +08:00
done
}
2023-04-11 12:04:41 +08:00
pre_check
2023-04-12 09:58:35 +08:00
check_info
2023-04-09 22:45:39 +08:00
build_new_vms
2023-04-12 09:58:35 +08:00
check_info