2023-04-22 20:16:47 +08:00
|
|
|
#!/bin/bash
|
|
|
|
# from
|
|
|
|
# https://github.com/spiritLHLS/pve
|
2023-04-22 20:27:06 +08:00
|
|
|
# 2023.04.22
|
|
|
|
|
|
|
|
# ./buildct.sh CTID 密码 CPU核数 内存 硬盘 SSH端口 80端口 443端口 外网端口起 外网端口止 系统
|
|
|
|
# ./buildct.sh 102 1234567 1 512 5 40001 40002 40003 50000 50025 debian11
|
2023-04-22 19:33:39 +08:00
|
|
|
|
2023-04-22 20:16:47 +08:00
|
|
|
cd /root >/dev/null 2>&1
|
|
|
|
CTID="${1:-102}"
|
|
|
|
password="${2:-123456}"
|
|
|
|
core="${3:-1}"
|
|
|
|
memory="${4:-512}"
|
|
|
|
disk="${5:-5}"
|
|
|
|
sshn="${6:-40001}"
|
|
|
|
web1_port="${7:-40002}"
|
|
|
|
web2_port="${8:-40003}"
|
|
|
|
port_first="${9:-49975}"
|
|
|
|
port_last="${10:-50000}"
|
2023-04-22 20:27:06 +08:00
|
|
|
system="${12:-debian11}"
|
2023-04-22 20:16:47 +08:00
|
|
|
rm -rf "ct$name"
|
2023-04-22 20:33:22 +08:00
|
|
|
TMP_FILE="cloud-init.yaml"
|
2023-04-22 20:16:47 +08:00
|
|
|
echo "#cloud-config" > "$TMP_FILE"
|
2023-04-22 20:33:22 +08:00
|
|
|
echo "password: ${password}" >> "$TMP_FILE"
|
|
|
|
echo "chpasswd: {expire: False}" >> "$TMP_FILE"
|
|
|
|
echo "ssh_pwauth: True" >> "$TMP_FILE"
|
2023-04-22 20:27:06 +08:00
|
|
|
system="debian-11-standard_11.6-1_amd64.tar.zst"
|
|
|
|
|
|
|
|
first_digit=${CTID:0:1}
|
|
|
|
second_digit=${CTID:1:1}
|
|
|
|
third_digit=${CTID: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
|
|
|
|
user_ip="172.16.1.${num}"
|
2023-04-22 20:33:22 +08:00
|
|
|
pct create $CTID local:vztmpl/$system --cores $core --cpuunits 1024 --memory $memory --swap 128 --net0 name=eth0,ip=${user_ip}/24,bridge=vmbr1,gw=172.16.1.1 --rootfs local:${disk} --onboot 1 --userdata ./cloud-init.yaml
|
2023-04-22 20:16:47 +08:00
|
|
|
rm "$TMP_FILE"
|
2023-04-22 20:27:06 +08:00
|
|
|
pct start $CTID
|
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
iptables-save > /etc/iptables/rules.v4
|
|
|
|
service netfilter-persistent restart
|
|
|
|
echo "$CTID $password $core $memory $disk $sshn $web1_port $web2_port $port_first $port_last $system" >> "ct${vm_num}"
|
|
|
|
cat "ct${vm_num}"
|