2023-04-09 21:24:51 +08:00
|
|
|
#!/bin/bash
|
|
|
|
# from
|
|
|
|
# https://github.com/spiritLHLS/pve
|
2023-04-24 00:58:26 +08:00
|
|
|
# 2023.04.24
|
2023-04-09 21:24:51 +08:00
|
|
|
|
2023-04-09 22:43:39 +08:00
|
|
|
# ./buildvm.sh VMID 用户名 密码 CPU核数 内存 硬盘 SSH端口 80端口 443端口 外网端口起 外网端口止 系统
|
2023-04-10 10:33:41 +08:00
|
|
|
# ./buildvm.sh 102 test1 1234567 1 512 5 40001 40002 40003 50000 50025 debian11
|
2023-04-09 22:43:39 +08:00
|
|
|
|
|
|
|
cd /root >/dev/null 2>&1
|
|
|
|
# 创建容器
|
2023-04-09 23:23:21 +08:00
|
|
|
vm_num="${1:-102}"
|
2023-04-09 22:43:39 +08:00
|
|
|
user="${2:-test}"
|
|
|
|
password="${3:-123456}"
|
|
|
|
core="${4:-1}"
|
|
|
|
memory="${5:-512}"
|
|
|
|
disk="${6:-5}"
|
2023-04-09 23:23:21 +08:00
|
|
|
sshn="${7:-40001}"
|
|
|
|
web1_port="${8:-40002}"
|
|
|
|
web2_port="${9:-40003}"
|
2023-04-09 22:43:39 +08:00
|
|
|
port_first="${10:-49975}"
|
|
|
|
port_last="${11:-50000}"
|
2023-04-11 13:03:59 +08:00
|
|
|
system="${12:-debian10}"
|
2023-04-09 22:43:39 +08:00
|
|
|
# in="${12:-300}"
|
|
|
|
# out="${13:-300}"
|
|
|
|
rm -rf "vm$name"
|
|
|
|
|
2023-04-24 08:56:45 +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:53 +08:00
|
|
|
reading(){ read -rp "$(_green "$1")" "$2"; }
|
2023-04-27 11:11:07 +08:00
|
|
|
utf8_locale=$(locale -a 2>/dev/null | grep -i -m 1 -E "utf8|UTF-8")
|
2023-04-24 08:56:45 +08:00
|
|
|
if [[ -z "$utf8_locale" ]]; then
|
|
|
|
_yellow "No UTF-8 locale found"
|
|
|
|
else
|
|
|
|
export LC_ALL="$utf8_locale"
|
|
|
|
export LANG="$utf8_locale"
|
2023-04-27 11:11:07 +08:00
|
|
|
export LANGUAGE="$utf8_locale"
|
2023-04-24 08:56:45 +08:00
|
|
|
_green "Locale set to $utf8_locale"
|
|
|
|
fi
|
2023-04-24 00:57:58 +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
|
2023-04-24 08:56:45 +08:00
|
|
|
_yellow "CDN available, using CDN"
|
2023-04-24 00:57:58 +08:00
|
|
|
else
|
2023-04-24 08:56:45 +08:00
|
|
|
_yellow "No CDN available, no use CDN"
|
2023-04-24 00:57:58 +08:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
cdn_urls=("https://cdn.spiritlhl.workers.dev/" "https://shrill-pond-3e81.hunsh.workers.dev/" "https://ghproxy.com/" "http://104.168.128.181:7823/" "https://gh.api.99988866.xyz/")
|
|
|
|
check_cdn_file
|
2023-04-09 22:43:39 +08:00
|
|
|
if [ ! -d "qcow" ]; then
|
|
|
|
mkdir qcow
|
|
|
|
fi
|
2023-04-17 21:20:16 +08:00
|
|
|
# "centos9-stream" "centos8-stream" "centos7" "almalinux8" "almalinux9"
|
|
|
|
systems=("debian10" "debian11" "debian9" "ubuntu18" "ubuntu20" "ubuntu22" "archlinux")
|
2023-04-09 22:43:39 +08:00
|
|
|
for sys in ${systems[@]}; do
|
|
|
|
if [[ "$system" == "$sys" ]]; then
|
|
|
|
file_path="/root/qcow/${system}.qcow2"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [[ -z "$file_path" ]]; then
|
2023-04-17 21:20:16 +08:00
|
|
|
# centos9-stream centos8-stream centos7 almalinux8 almalinux9
|
|
|
|
echo "无法安装对应系统,仅支持 debian9 debian10 debian11 ubuntu18 ubuntu20 ubuntu22 archlinux"
|
2023-04-09 22:43:39 +08:00
|
|
|
exit 1
|
|
|
|
fi
|
2023-04-24 00:57:58 +08:00
|
|
|
url="${cdn_success_url}https://github.com/spiritLHLS/Images/releases/download/v1.0/${system}.qcow2"
|
2023-04-09 22:43:39 +08:00
|
|
|
if [ ! -f "$file_path" ]; then
|
|
|
|
curl -L -o "$file_path" "$url"
|
|
|
|
fi
|
|
|
|
|
|
|
|
first_digit=${vm_num:0:1}
|
|
|
|
second_digit=${vm_num:1:1}
|
|
|
|
third_digit=${vm_num:2:1}
|
|
|
|
if [ $first_digit -le 2 ]; then
|
|
|
|
if [ $second_digit -eq 0 ]; then
|
|
|
|
num=$third_digit
|
|
|
|
else
|
|
|
|
num=$second_digit$third_digit
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
num=$((first_digit - 2))$second_digit$third_digit
|
|
|
|
fi
|
|
|
|
|
2023-04-12 09:15:18 +08:00
|
|
|
qm create $vm_num --agent 1 --scsihw virtio-scsi-single --serial0 socket --cores $core --sockets 1 --cpu host --net0 virtio,bridge=vmbr1,firewall=0
|
2023-04-09 22:43:39 +08:00
|
|
|
qm importdisk $vm_num /root/qcow/${system}.qcow2 local
|
|
|
|
qm set $vm_num --scsihw virtio-scsi-pci --scsi0 local:${vm_num}/vm-${vm_num}-disk-0.raw
|
|
|
|
qm set $vm_num --bootdisk scsi0
|
|
|
|
qm set $vm_num --boot order=scsi0
|
|
|
|
qm set $vm_num --memory $memory
|
2023-04-12 10:05:16 +08:00
|
|
|
# --swap 256
|
2023-04-09 22:43:39 +08:00
|
|
|
qm set $vm_num --ide2 local:cloudinit
|
|
|
|
qm set $vm_num --nameserver 8.8.8.8
|
|
|
|
qm set $vm_num --searchdomain 8.8.4.4
|
|
|
|
user_ip="172.16.1.${num}"
|
|
|
|
qm set $vm_num --ipconfig0 ip=${user_ip}/24,gw=172.16.1.1
|
|
|
|
qm set $vm_num --cipassword $password --ciuser $user
|
|
|
|
qm resize $vm_num scsi0 ${disk}G
|
|
|
|
qm start $vm_num
|
|
|
|
|
2023-04-10 14:35:43 +08:00
|
|
|
iptables -t nat -A PREROUTING -p tcp --dport ${sshn} -j DNAT --to-destination ${user_ip}:22
|
|
|
|
iptables -t nat -A PREROUTING -p tcp -m tcp --dport ${web1_port} -j DNAT --to-destination ${user_ip}:80
|
|
|
|
iptables -t nat -A PREROUTING -p tcp -m tcp --dport ${web2_port} -j DNAT --to-destination ${user_ip}:443
|
2023-04-10 14:40:02 +08:00
|
|
|
iptables -t nat -A PREROUTING -p tcp -m tcp --dport ${port_first}:${port_last} -j DNAT --to-destination ${user_ip}:${port_first}-${port_last}
|
|
|
|
iptables -t nat -A PREROUTING -p udp -m udp --dport ${port_first}:${port_last} -j DNAT --to-destination ${user_ip}:${port_first}-${port_last}
|
|
|
|
if [ ! -f "/etc/iptables/rules.v4" ]; then
|
|
|
|
touch /etc/iptables/rules.v4
|
|
|
|
fi
|
2023-04-10 14:35:43 +08:00
|
|
|
iptables-save > /etc/iptables/rules.v4
|
2023-04-10 14:56:14 +08:00
|
|
|
service netfilter-persistent restart
|
2023-04-09 22:52:16 +08:00
|
|
|
echo "$vm_num $user $password $core $memory $disk $sshn $web1_port $web2_port $port_first $port_last $system" >> "vm${vm_num}"
|
2023-04-09 23:19:16 +08:00
|
|
|
cat "vm${vm_num}"
|