2023-02-23 21:42:24 +08:00
#!/bin/bash
2023-06-22 14:42:39 +08:00
# from
# https://github.com/spiritLHLS/pve
2023-06-29 21:41:08 +08:00
# 2023.06.29
2023-02-23 21:58:44 +08:00
2023-06-24 10:21:48 +08:00
2023-02-24 14:28:18 +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:20:17 +08:00
reading( ) { read -rp " $( _green " $1 " ) " " $2 " ; }
2023-04-26 16:28:12 +08:00
export DEBIAN_FRONTEND = noninteractive
2023-04-27 11:26:28 +08:00
utf8_locale = $( locale -a 2>/dev/null | grep -i -m 1 -E "UTF-8|utf8" )
2023-04-24 08:57:07 +08:00
if [ [ -z " $utf8_locale " ] ] ; then
2023-04-27 11:26:28 +08:00
echo "No UTF-8 locale found"
2023-04-24 08:57:07 +08:00
else
export LC_ALL = " $utf8_locale "
export LANG = " $utf8_locale "
2023-04-27 11:11:30 +08:00
export LANGUAGE = " $utf8_locale "
2023-04-27 11:26:28 +08:00
echo " Locale set to $utf8_locale "
2023-04-24 08:57:07 +08:00
fi
2023-02-24 14:28:18 +08:00
2023-06-28 17:27:58 +08:00
rm -rf /root/build_backend_pve.txt
2023-02-24 14:28:18 +08:00
# 查询信息
2023-06-03 14:29:56 +08:00
if ! command -v lshw > /dev/null 2>& 1; then
apt-get install -y lshw
fi
2023-06-05 09:03:53 +08:00
# 提取物理网卡名字
2023-06-23 16:47:39 +08:00
interface_1 = $( lshw -C network | awk '/logical name:/{print $3}' | head -1)
2023-06-21 11:09:56 +08:00
interface_2 = $( lshw -C network | awk '/logical name:/{print $3}' | sed -n '2p' )
2023-06-23 16:47:39 +08:00
if [ -z " $interface_1 " ] ; then
2023-04-24 00:17:08 +08:00
interface = "eth0"
fi
2023-06-23 16:47:39 +08:00
if ! grep -q " $interface_1 " "/etc/network/interfaces" ; then
if [ -f "/etc/network/interfaces.d/50-cloud-init" ] ; then
if ! grep -q " $interface_1 " "/etc/network/interfaces.d/50-cloud-init" && grep -q " $interface_2 " "/etc/network/interfaces.d/50-cloud-init" ; then
interface = ${ interface_2 }
else
interface = ${ interface_1 }
fi
else
if grep -q " $interface_2 " "/etc/network/interfaces" ; then
interface = ${ interface_2 }
else
interface = ${ interface_1 }
fi
fi
else
interface = ${ interface_1 }
fi
2023-06-05 09:03:53 +08:00
# 提取IPV4地址
ipv4_address = $( ip addr show | awk '/inet .*global/ && !/inet6/ {print $2}' )
# 提取IPV4网关
gateway = $( ip route | awk '/default/ {print $3}' )
# 获取IPV6子网前缀
2023-06-24 10:16:33 +08:00
SUBNET_PREFIX = $( ip -6 addr show | grep -E 'inet6.*global' | awk '{print $2}' | awk -F'/' '{print $1}' | head -n 1 | rev | cut -d ':' -f 2- | rev) :0
2023-06-05 09:03:53 +08:00
# 提取IPV6地址
ipv6_address = $( ip addr show | awk '/inet6.*scope global/ { print $2 }' | head -n 1)
# 检查是否存在 IPV6
2023-06-24 10:21:48 +08:00
if [ -z " $SUBNET_PREFIX " ] || [ " $SUBNET_PREFIX " = ":0" ] ; then
2023-06-23 16:32:16 +08:00
_red "No IPV6 subnet, no automatic mapping"
2023-06-29 21:41:08 +08:00
_red "无 IPV6 子网,不进行自动映射"
2023-04-24 00:17:08 +08:00
else
2023-06-24 21:23:20 +08:00
_blue " The IPV6 subnet prefix of the host is $SUBNET_PREFIX "
2023-06-29 21:41:08 +08:00
_blue " 母鸡的IPV6子网前缀为 $SUBNET_PREFIX "
2023-06-05 09:03:53 +08:00
fi
if [ -z " $ipv6_address " ] ; then
2023-06-23 16:32:16 +08:00
_red "No IPV6 address on the parent machine, no automatic mapping"
2023-06-29 21:41:08 +08:00
_red "母机无 IPV6 地址,不进行自动映射"
2023-06-05 09:03:53 +08:00
else
2023-06-23 16:32:16 +08:00
_blue " The IPV6 address of the host is $ipv6_address "
2023-06-29 21:41:08 +08:00
_blue " 母鸡的IPV6地址为 $ipv6_address "
2023-04-24 00:17:08 +08:00
fi
2023-05-30 21:59:29 +08:00
2023-02-24 14:28:18 +08:00
# 录入网关
2023-06-12 22:35:02 +08:00
if [ -f /etc/network/interfaces ] ; then
cp /etc/network/interfaces /etc/network/interfaces.bak
fi
2023-06-21 16:21:26 +08:00
# 修正部分网络设置重复的错误
if [ [ -f "/etc/network/interfaces.d/50-cloud-init" && -f "/etc/network/interfaces" ] ] ; then
if grep -q "auto lo" "/etc/network/interfaces.d/50-cloud-init" && grep -q "iface lo inet loopback" "/etc/network/interfaces.d/50-cloud-init" && grep -q "auto lo" "/etc/network/interfaces" && grep -q "iface lo inet loopback" "/etc/network/interfaces" ; then
# 从 /etc/network/interfaces.d/50-cloud-init 中删除重复的行
2023-06-22 09:45:57 +08:00
chattr -i /etc/network/interfaces.d/50-cloud-init
2023-06-21 16:21:26 +08:00
sed -i '/auto lo/d' "/etc/network/interfaces.d/50-cloud-init"
sed -i '/iface lo inet loopback/d' "/etc/network/interfaces.d/50-cloud-init"
2023-06-22 09:45:57 +08:00
chattr +i /etc/network/interfaces.d/50-cloud-init
2023-06-21 16:21:26 +08:00
fi
fi
2023-06-24 08:43:54 +08:00
if [ -f "/etc/network/interfaces.new" ] ; then
chattr -i /etc/network/interfaces.new
rm -rf /etc/network/interfaces.new
fi
2023-06-12 23:49:23 +08:00
interfaces_file = "/etc/network/interfaces"
2023-06-12 23:27:24 +08:00
chattr -i " $interfaces_file "
2023-06-14 09:10:10 +08:00
if ! grep -q "auto lo" " $interfaces_file " ; then
2023-06-14 09:14:05 +08:00
# echo "auto lo" >> "$interfaces_file"
2023-06-29 21:41:08 +08:00
_blue " Can not find 'auto lo' in ${ interfaces_file } "
2023-06-14 09:14:05 +08:00
exit 1
2023-06-14 09:10:10 +08:00
fi
if ! grep -q "iface lo inet loopback" " $interfaces_file " ; then
2023-06-14 09:14:05 +08:00
# echo "iface lo inet loopback" >> "$interfaces_file"
2023-06-29 21:41:08 +08:00
_blue " Can not find 'iface lo inet loopback' in ${ interfaces_file } "
2023-06-14 09:14:05 +08:00
exit 1
2023-06-14 09:10:10 +08:00
fi
2023-06-12 22:35:02 +08:00
if grep -q "vmbr0" " $interfaces_file " ; then
2023-06-29 21:41:08 +08:00
_blue " vmbr0 already exists in ${ interfaces_file } "
_blue " vmbr0 已存在在 ${ interfaces_file } "
2023-02-24 13:50:58 +08:00
else
2023-06-24 10:21:48 +08:00
if [ -z " $SUBNET_PREFIX " ] || [ " $SUBNET_PREFIX " = ":0" ] || [ -z " $ipv6_address " ] ; then
2023-06-12 22:35:02 +08:00
cat << EOF | sudo tee -a " $interfaces_file "
2023-06-05 09:03:53 +08:00
auto vmbr0
iface vmbr0 inet static
address $ipv4_address
gateway $gateway
bridge_ports $interface
bridge_stp off
bridge_fd 0
EOF
2023-06-26 09:17:02 +08:00
elif [ -f "/root/iface_auto.txt" ] ; then
cat << EOF | sudo tee -a " $interfaces_file "
auto vmbr0
iface vmbr0 inet static
address $ipv4_address
gateway $gateway
bridge_ports $interface
bridge_stp off
bridge_fd 0
2023-06-26 09:31:45 +08:00
iface vmbr0 inet6 auto
2023-06-26 09:53:19 +08:00
bridge_ports $interface
2023-06-26 09:17:02 +08:00
EOF
2023-06-05 09:03:53 +08:00
else
2023-06-12 22:35:02 +08:00
cat << EOF | sudo tee -a " $interfaces_file "
2023-02-24 13:53:12 +08:00
auto vmbr0
iface vmbr0 inet static
2023-06-05 09:03:53 +08:00
address $ipv4_address
2023-02-24 13:53:12 +08:00
gateway $gateway
bridge_ports $interface
bridge_stp off
bridge_fd 0
2023-06-05 09:03:53 +08:00
iface vmbr0 inet6 static
address ${ ipv6_address }
gateway ${ SUBNET_PREFIX }
2023-02-24 13:53:12 +08:00
EOF
2023-02-24 13:50:58 +08:00
fi
2023-06-05 09:03:53 +08:00
fi
2023-06-12 22:35:02 +08:00
if grep -q "vmbr1" " $interfaces_file " ; then
2023-06-29 21:41:08 +08:00
_blue " vmbr1 already exists in ${ interfaces_file } "
_blue " vmbr1 已存在在 ${ interfaces_file } "
2023-06-26 09:18:48 +08:00
elif [ -f "/root/iface_auto.txt" ] ; then
cat << EOF | sudo tee -a " $interfaces_file "
auto vmbr1
iface vmbr1 inet static
address 172.16.1.1
netmask 255.255.255.0
bridge_ports none
bridge_stp off
bridge_fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up echo 1 > /proc/sys/net/ipv4/conf/vmbr1/proxy_arp
post-up iptables -t nat -A POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
2023-06-26 10:06:50 +08:00
pre-up echo 2 > /proc/sys/net/ipv6/conf/vmbr0/accept_ra
2023-06-26 09:18:48 +08:00
EOF
2023-02-24 13:50:58 +08:00
else
2023-06-12 22:35:02 +08:00
cat << EOF | sudo tee -a " $interfaces_file "
2023-02-24 13:53:12 +08:00
auto vmbr1
iface vmbr1 inet static
address 172.16.1.1
netmask 255.255.255.0
bridge_ports none
bridge_stp off
bridge_fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up echo 1 > /proc/sys/net/ipv4/conf/vmbr1/proxy_arp
post-up iptables -t nat -A POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
EOF
2023-02-24 13:50:58 +08:00
fi
2023-06-12 23:27:24 +08:00
chattr +i " $interfaces_file "
2023-06-26 09:18:48 +08:00
rm -rf /root/iface_auto.txt
2023-02-24 09:56:53 +08:00
2023-04-10 14:34:28 +08:00
# 加载iptables并设置回源且允许NAT端口转发
2023-04-10 14:56:29 +08:00
apt-get install -y iptables iptables-persistent
2023-04-10 14:32:13 +08:00
iptables -t nat -A POSTROUTING -j MASQUERADE
2023-04-10 14:34:28 +08:00
sysctl net.ipv4.ip_forward= 1
2023-04-10 14:42:45 +08:00
sysctl_path = $( which sysctl)
2023-04-10 14:40:44 +08:00
if grep -q "^net.ipv4.ip_forward=1" /etc/sysctl.conf; then
if grep -q "^#net.ipv4.ip_forward=1" /etc/sysctl.conf; then
sed -i 's/^#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
fi
else
2023-06-26 09:17:02 +08:00
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
2023-04-10 14:40:44 +08:00
fi
2023-04-10 14:42:45 +08:00
${ sysctl_path } -p
2023-04-10 13:48:37 +08:00
2023-02-24 14:28:18 +08:00
# 重启配置
2023-02-24 09:56:53 +08:00
service networking restart
2023-02-23 22:03:19 +08:00
systemctl restart networking.service
2023-06-26 10:06:50 +08:00
sleep 3
ifreload -ad
2023-06-23 21:37:30 +08:00
# 已加载网络,删除对应缓存文件
if [ -f "/etc/network/interfaces.new" ] ; then
2023-06-23 22:07:26 +08:00
chattr -i /etc/network/interfaces.new
2023-06-23 21:37:30 +08:00
rm -rf /etc/network/interfaces.new
fi
2023-06-25 21:01:12 +08:00
systemctl start check-dns.service
2023-06-23 22:49:36 +08:00
# _green "Although the gateway has been set automatically, I am not sure if it has been applied successfully, please check in Datacenter-->pve-->System-->Network in PVE"
# _green "If vmbr0 and vmbr1 are displayed properly and the Apply Configuration button is grayed out, there is no need to reboot"
# _green "If the above scenario is different, click on the Apply Configuration button, wait a few minutes and reboot the system to ensure that the gateway has been successfully applied"
_green "you can test open a virtual machine or container to see if the actual network has been applied successfully"
# _green "虽然已自动设置网关, 但不确定是否已成功应用, 请查看PVE中的 Datacenter-->pve-->System-->Network"
# _green "如果 vmbr0 和 vmbr1 已正常显示且 Apply Configuration 这个按钮是灰色的,则不用执行 reboot 重启系统"
# _green "上述情形如果有不同的,请点击 Apply Configuration 这个按钮,等待几分钟后重启系统,确保网关已成功应用"
_green "你可以测试开一个虚拟机或者容器看看就知道是不是实际网络已应用成功了"