From 9c97a9318b2b0d9d8f0b1d7814f36a81ec1a659f Mon Sep 17 00:00:00 2001 From: "junjie.liu" Date: Tue, 3 Dec 2024 13:12:21 +0800 Subject: [PATCH] feat(bmsec): add bridge config --- .../opt/sophon/bmsec/commands/_confNetwork.sh | 214 ++++++++++++++++++ .../deb/opt/sophon/bmsec/configs/1_network.sh | 52 ++++- .../deb/opt/sophon/bmsec/configs/9_comInfo.sh | 2 + .../opt/sophon/bmsec/configs/sub/subInfo.12 | 1 + source/pbmsec/doc/00_README_zh.md | 1 - .../doc/10_UserReferenceDocumentation_zh.md | 37 ++- source/pbmsec/release.sh | 2 +- 7 files changed, 301 insertions(+), 8 deletions(-) create mode 100644 source/pbmsec/deb/opt/sophon/bmsec/commands/_confNetwork.sh diff --git a/source/pbmsec/deb/opt/sophon/bmsec/commands/_confNetwork.sh b/source/pbmsec/deb/opt/sophon/bmsec/commands/_confNetwork.sh new file mode 100644 index 0000000..6edd24f --- /dev/null +++ b/source/pbmsec/deb/opt/sophon/bmsec/commands/_confNetwork.sh @@ -0,0 +1,214 @@ +#!/bin/bash +############################################### +############junjie.liu@sophgo.com############## +############################################### + +# Some vars come from 1_network.sh +# config netplan of host +CORE_NETPLAN_FILE="$YAML_FILE" +CORE_NETPLAN_FILE_BAK="/etc/netplan/netcfg.yaml.bak" + +SUB_NETPLAN_FILE="/etc/netplan/01-netcfg.yaml" +SUB_NETPLAN_FILE_BAK="/etc/netplan/01-netcfg.yaml.bak" +iptable_setup="/root/se_ctrl/iptable_setup.sh" + +ret=$(sudo bash -c "source /root/se_ctrl/sectr.sh;sectr_get_auth") +if [ $ret -eq 0 ]; then + echo "开机初始化中,请等待几分钟再执行该命令" + exit 0 +fi + +if [[ ! -f "$CORE_NETPLAN_FILE" ]]; then + echo "error: file '$CORE_NETPLAN_FILE' does not exist" + exit 1 +fi + +usage() { + echo "usage: $1 [OPTIONS]" + echo "OPTIONS:" + echo " -h , HELP INFO" + echo " 0 , delete bridge config" + echo " 1 , config bridge mode" + exit 1 +} + +while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + usage + ;; + 0) + echo "delete bridges config..." + mode=0 + ;; + 1) + echo "config bridges..." + mode=1 + ;; + *) + echo "Error: invalid args '$1'" >&2 + usage + ;; + esac + shift +done + +function reset_network() +{ + echo "reset network config of all cores" + for ((id = 1; id <= $seNCtrl_ALL_SUB_NUM; id++)); do + file_name=$(basename "$SUB_NETPLAN_FILE_BAK") + ret=$(${seNCtrl_PWD}/bmsec run $id "ls /etc/netplan/" | grep $file_name) + # echo $ret + if [[ -n "$ret" ]]; then + ${seNCtrl_PWD}/bmsec run $id "sudo chmod 777 $SUB_NETPLAN_FILE_BAK" &> /dev/null + ${seNCtrl_PWD}/bmsec run $id "sudo mv $SUB_NETPLAN_FILE_BAK $SUB_NETPLAN_FILE" &> /dev/null + ${seNCtrl_PWD}/bmsec run $id "sudo netplan apply" &> /dev/null + else + echo "Warning: The file $SUB_NETPLAN_FILE_BAK in core $id does not exist. Please reboot core $id" + fi + done + + sudo chmod 777 ${seNCtrl_PWD}/configs/sub/subInfo.12 + sudo sed -i 's/Bridge_CONFIG_FLAG=1/Bridge_CONFIG_FLAG=0/' ${seNCtrl_PWD}/configs/sub/subInfo.12 + sudo sed -i '/^Bridge_IP_HALF=/d' ${seNCtrl_PWD}/configs/sub/subInfo.12 + + if grep -q "bridges:" "$CORE_NETPLAN_FILE"; then + echo "reset host netplan config..." + if [[ -f "$CORE_NETPLAN_FILE_BAK" ]]; then + sudo chmod 777 $CORE_NETPLAN_FILE_BAK + sudo mv $CORE_NETPLAN_FILE_BAK $CORE_NETPLAN_FILE + sudo netplan apply + else + sudo cat $CORE_NETPLAN_FILE + echo "error: $CORE_NETPLAN_FILE_BAK in host does not exist!!! Please modify your $CORE_NETPLAN_FILE of host by yourself!" + fi + fi + # reset iptable_setup.sh + sudo chmod 777 $iptable_setup + sudo sed -i '/^wanname=br0/d; s/^#wanname=/wanname=/' "$iptable_setup" + if ! [ "$product" = "SE6-CTRL" ] && [ "$product" = "SE6 CTRL" ] && [ "$product" = "SM7 CTRL" ] && [ "$product" = "SE8 CTRL" ]; then + sudo $iptable_setup > /dev/null 2>&1 + fi + echo "please reboot!" +} + +function conf_bridge() +{ + sudo chmod 777 ${seNCtrl_PWD}/configs/sub/subInfo.12 + if grep -q "Bridge_IP_HALF=" "${seNCtrl_PWD}/configs/sub/subInfo.12"; then + sudo sed -i "s/^Bridge_IP_HALF=.*/Bridge_IP_HALF=$seNCtrl_SUB_IP_HALF/" "${seNCtrl_PWD}/configs/sub/subInfo.12" + else + sudo echo "Bridge_IP_HALF=$seNCtrl_SUB_IP_HALF" >> "${seNCtrl_PWD}/configs/sub/subInfo.12" + fi + sudo sed -i 's/Bridge_CONFIG_FLAG=0/Bridge_CONFIG_FLAG=1/' ${seNCtrl_PWD}/configs/sub/subInfo.12 + + # set all cores as DHCP mode while still retain original ip addr for bmsec + for ((id = 1; id <= $seNCtrl_ALL_SUB_NUM; id++)); do + file_name=$(basename "$SUB_NETPLAN_FILE_BAK") + ret=$(${seNCtrl_PWD}/bmsec run $id "ls /etc/netplan/" | grep $file_name) + if [[ -n "$ret" ]]; then + echo "The file $SUB_NETPLAN_FILE_BAK in core $id exists." + else + ${seNCtrl_PWD}/bmsec run $id "sudo cp $SUB_NETPLAN_FILE $SUB_NETPLAN_FILE_BAK" &> /dev/null + fi + done + + # ${seNCtrl_PWD}/bmsec getbi + echo "set all cores as DHCP MODE!" + ${seNCtrl_PWD}/bmsec run all "sudo chmod 777 $SUB_NETPLAN_FILE" #&> /dev/null + ${seNCtrl_PWD}/bmsec run all "sudo sed -i 's/dhcp4: no/dhcp4: yes/' $SUB_NETPLAN_FILE" &> /dev/null + ${seNCtrl_PWD}/bmsec run all "sudo sed -i '/gateway4:/d' $SUB_NETPLAN_FILE" &> /dev/null + ${seNCtrl_PWD}/bmsec run all "sudo netplan apply" &> /dev/null + # ${seNCtrl_PWD}/bmsec run all "sudo cat $SUB_NETPLAN_FILE" + + # config host netplan + if ! grep -q "bridges:" "$CORE_NETPLAN_FILE"; then + echo "setting bridges in netplan yaml file..." + if [[ ! -f "$CORE_NETPLAN_FILE_BAK" ]]; then + echo "backup $CORE_NETPLAN_FILE" + sudo cp $CORE_NETPLAN_FILE $CORE_NETPLAN_FILE_BAK + fi + sudo chmod 777 $CORE_NETPLAN_FILE + if [[ "$WAN" == "eno5" ]]; then + #se8-288 + BRIDGE_BLOCK=$(cat << EOF + bridges: + br0: + interfaces: [$WAN, $INTERFACE_0, $INTERFACE_1] + dhcp4: yes + addresses: [172.26.166.141/24, $seNCtrl_SUB_IP_0/24, $seNCtrl_SUB_IP_1/24] + nameservers: + addresses: [8.8.8.8] +EOF +) + else + BRIDGE_BLOCK=$(cat << EOF + bridges: + br0: + interfaces: [$WAN, $INTERFACE_0, $INTERFACE_1] + dhcp4: yes + addresses: [172.26.166.141/24, $seNCtrl_SUB_IP_0/24, $seNCtrl_SUB_IP_1/24] + nameservers: + addresses: [8.8.8.8] +EOF +) + fi + TEMP_FILE=$(mktemp /tmp/01-netcfg.yaml.tmp.XXXXXX) || { echo "can not create temp file"; exit 1; } + + while IFS= read -r line; do + echo "$line" >> "$TEMP_FILE" + # insert bridges config after renderer + if [[ $line =~ renderer:[[:space:]]* ]]; then + # insert bridges config block + echo "$BRIDGE_BLOCK" >> "$TEMP_FILE" + fi + done < "$CORE_NETPLAN_FILE" + sudo mv "$TEMP_FILE" "$CORE_NETPLAN_FILE" || { echo "can not modify netplan yaml file"; rm -f "$TEMP_FILE"; exit 1; } + + # set ip addr of eth0/1 as NULL + sudo sed -i "/$INTERFACE_0:/,/optional: yes/ { + s/addresses: \[[^]]*\]/addresses: []/ + }" $CORE_NETPLAN_FILE + + sudo sed -i "/$INTERFACE_1:/,/optional: yes/ { + s/addresses: \[[^]]*\]/addresses: []/ + }" $CORE_NETPLAN_FILE + + echo "config netplan..." + sudo netplan apply + if [ $? -ne 0 ]; then + sudo cat $CORE_NETPLAN_FILE + echo "Error: netplan apply failed. Please check your netplan yaml file!" + exit 1 + fi + fi + + # update wanname in iptable_setup.sh + # iptable_setup="/root/se_ctrl/iptable_setup.sh" + if ! sudo grep -q '^wanname=br0' "$iptable_setup"; then + sudo chmod 777 $iptable_setup + sudo sed -i '/^wanname=/ s/^/#/' "$iptable_setup" + sudo sed -i '/^#wanname=/a wanname=br0' "$iptable_setup" + echo "Update completed: 'wanname=br0' added." + else + echo "wanname is br0." + fi + + PID=$(ps -ef | grep "[b]ash $iptable_setup" | awk '{print $2}') + if [ -n "$PID" ]; then + sudo kill "$PID" + # echo "kill iptable_setup.sh" + fi + sudo iptables -P FORWARD ACCEPT + +} + + +if [ "$mode" -eq 0 ]; then + echo "Mode is set to 0" + reset_network +elif [ "$mode" -eq 1 ]; then + echo "Mode is set to 1" + conf_bridge +fi \ No newline at end of file diff --git a/source/pbmsec/deb/opt/sophon/bmsec/configs/1_network.sh b/source/pbmsec/deb/opt/sophon/bmsec/configs/1_network.sh index e7cb5d5..16f0867 100644 --- a/source/pbmsec/deb/opt/sophon/bmsec/configs/1_network.sh +++ b/source/pbmsec/deb/opt/sophon/bmsec/configs/1_network.sh @@ -12,15 +12,57 @@ seNCtrl_HOST_SUB_ETHS=() if [ "$product" = "SE6-CTRL" ] || [ "$product" = "SE6 CTRL" ] || [ "$product" = "SM7 CTRL" ] || [ "$product" = "SE8 CTRL" ]; then seNCtrl_HOST_SUB_ETHS+=('eth0' 'eth1') seNCtrl_DEBUG_UART=/dev/ttyS2 - + YAML_FILE="/etc/netplan/01-netcfg.yaml" + INTERFACE_0="eth0" + INTERFACE_1="eth1" + WAN="enp4s0" else #se8 x86 seNCtrl_HOST_SUB_ETHS+=('eno1' 'enp2s0f0' 'bond0') seNCtrl_DEBUG_UART=/dev/ttyS1 + YAML_FILE="/etc/netplan/01-network-manager-all.yaml" + INTERFACE_0="eno1" + INTERFACE_1="eno3" + WAN="eno5" +fi + +source ${seNCtrl_PWD}/configs/sub/subInfo.12 +if ifconfig | grep "^br" > /dev/null 2>&1 && [ "$Bridge_CONFIG_FLAG" == "1" ]; then + seNCtrl_SUB_IP_HALF=$Bridge_IP_HALF + # echo $seNCtrl_SUB_IP_HALF + + #check if netplan yaml of eth0/1 is null + ETH0_ADDRESSES=$(sudo grep -A 2 "$INTERFACE_0:" "$YAML_FILE" | grep "addresses:" | awk '{print $2}' | tr -d '[]') + ETH1_ADDRESSES=$(sudo grep -A 2 "$INTERFACE_1:" "$YAML_FILE" | grep "addresses:" | awk '{print $2}' | tr -d '[]') + # check eth0 addresses + if [ -n "$ETH0_ADDRESSES" ]; then + sudo sed -i "/$INTERFACE_0:/,/optional: yes/ { + s/addresses: \[[^]]*\]/addresses: []/ + }" $YAML_FILE + fi + + # check eth1 addresses + if [ -n "$ETH1_ADDRESSES" ]; then + sudo sed -i "/$INTERFACE_1:/,/optional: yes/ { + s/addresses: \[[^]]*\]/addresses: []/ + }" $YAML_FILE + fi + + if [ -n "$ETH0_ADDRESSES" ] || [ -n "$ETH1_ADDRESSES" ]; then + echo "net config..." + sudo netplan apply + sleep 5 + fi + +else + #seNCtrl_SUB_IP_0 and seNCtrl_SUB_IP_1 are for bridges config + seNCtrl_SUB_IP_0=$(ip addr show "$INTERFACE_0" | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1) + seNCtrl_SUB_IP_1=$(ip addr show "$INTERFACE_1" | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1) + for eth in "${seNCtrl_HOST_SUB_ETHS[@]}"; do + seNCtrl_SUB_IP_HALF=$(ifconfig "$eth" 2> /dev/null | grep "inet "|awk '{print $2}'|awk -F . '{printf("%d.%d\n", $1,$2)}') + if [ -n "$seNCtrl_SUB_IP_HALF" ]; then break; fi + done fi -for eth in "${seNCtrl_HOST_SUB_ETHS[@]}"; do - seNCtrl_SUB_IP_HALF=$(ifconfig "$eth" 2> /dev/null | grep "inet "|awk '{print $2}'|awk -F . '{printf("%d.%d\n", $1,$2)}') - if [ -n "$seNCtrl_SUB_IP_HALF" ]; then break; fi -done + if [ "$seNCtrl_SUB_IP_HALF" = "" ]; then echo "cannot get ip to core, exit"; ifconfig; exit -1; fi source <(sed 's/172\.16/${seNCtrl_SUB_IP_HALF}/g' ${seNCtrl_PWD}/configs/sub/subInfo.12) sudo chmod 777 ${seNCtrl_DEBUG_UART} diff --git a/source/pbmsec/deb/opt/sophon/bmsec/configs/9_comInfo.sh b/source/pbmsec/deb/opt/sophon/bmsec/configs/9_comInfo.sh index f518919..ce860e7 100644 --- a/source/pbmsec/deb/opt/sophon/bmsec/configs/9_comInfo.sh +++ b/source/pbmsec/deb/opt/sophon/bmsec/configs/9_comInfo.sh @@ -101,3 +101,5 @@ sudo chmod +x ${seNCtrl_PWD}/commands/* seNCtrl_OPTIONS_BY_NAME["_reset_all"]="_reset_all.sh" #对只能进入uboot状态的算力节点进行强制刷机(需要确保/recovery/tftp下刷机包的正确性) seNCtrl_OPTIONS_BY_NAME["_update_sub"]="_update_sub.sh" +#配置网络模式 +seNCtrl_OPTIONS_BY_NAME["netconf"]="_confNetwork.sh" \ No newline at end of file diff --git a/source/pbmsec/deb/opt/sophon/bmsec/configs/sub/subInfo.12 b/source/pbmsec/deb/opt/sophon/bmsec/configs/sub/subInfo.12 index 0be81fd..30e9547 100755 --- a/source/pbmsec/deb/opt/sophon/bmsec/configs/sub/subInfo.12 +++ b/source/pbmsec/deb/opt/sophon/bmsec/configs/sub/subInfo.12 @@ -3,6 +3,7 @@ # 当前所有子设备数量,用于校验该文件是否配置正确 seNCtrl_ALL_SUB_NUM=12 seNCtrl_CONFIG_FLAG="OK" +Bridge_CONFIG_FLAG=0 unset seNCtrl_ALL_SUB_IP_ID declare -A seNCtrl_ALL_SUB_IP_ID diff --git a/source/pbmsec/doc/00_README_zh.md b/source/pbmsec/doc/00_README_zh.md index 0ae69b9..f75e92b 100644 --- a/source/pbmsec/doc/00_README_zh.md +++ b/source/pbmsec/doc/00_README_zh.md @@ -47,7 +47,6 @@ 15. 重新生成算力核心配置信息 [rconf] 16. 将指定算力核心的环境进行打包,可选生成tftp刷机包和仅打半成品包 [sysbak \ \] 17. 通过此功能,用户可以编辑端口映射 [pt \ [\ \ \ \ \]] - [onlyBak]] ## 注意事项 diff --git a/source/pbmsec/doc/10_UserReferenceDocumentation_zh.md b/source/pbmsec/doc/10_UserReferenceDocumentation_zh.md index 1c82aa4..705c173 100644 --- a/source/pbmsec/doc/10_UserReferenceDocumentation_zh.md +++ b/source/pbmsec/doc/10_UserReferenceDocumentation_zh.md @@ -1154,4 +1154,39 @@ config info: udp 172.26.166.158 16081:16082 (172.16.140.16 6) 16081-16082 udp 172.26.166.158 8554:8555 (172.16.140.11 1) 8554-8555 tcp 172.26.166.158 8022 (172.16.150.12 8) 22 - ``` \ No newline at end of file + ``` +18. **设置网桥模式 [netconf \]** + - 注:该功能可以在控制板netplan增加网桥配置,同时设置算力节点为DHCP4模式;如果从网桥模式切换至其他模式,除了执行`bmsec netconf `外,您还需重启整个设备。当设备处于网桥模式时,如果重启了设备,您需执行`bmsec netconf 1`,以保证网桥模式可用。 + - 参数: + - ``:选项 + - 0 :DHCP4端口转发模式 + - 1 :网桥模式 + + - 说明:通过此功能,用户可以配置网络模式。 + - 命令行模式示例: + - `bmsec netconf 0` DHCP4端口转发模式 + - `bmsec netconf 1` 网桥模式 + - 输出示例: + + 网桥模式 + + ```bash + se6@se6-desktop:~$ bmsec netconf 1 + config bridges... + Mode is set to 1 + set all cores as DHCP MODE! + setting bridges in netplan yaml file... + backup /etc/netplan/01-network-manager-all.yaml + config netplan... + Update completed: 'wanname=br0' added. + ``` + DHCP4端口转发模式 + + ```bash + se6@se6-desktop:~$ bmsec netconf 0 + delete bridges config... + Mode is set to 0 + reset network config of all cores + reset host netplan config... + please reboot! + ``` diff --git a/source/pbmsec/release.sh b/source/pbmsec/release.sh index 3b07c79..13bf0e1 100755 --- a/source/pbmsec/release.sh +++ b/source/pbmsec/release.sh @@ -4,7 +4,7 @@ BUILD_RET=0 echo "build bmsec ..." -BMSEC_PACKAGE_VERSION="1.5.1" +BMSEC_PACKAGE_VERSION="1.6.0" export CMD_PANDOC=$(command -v pandoc) export CMD_DPKG_DEB=$(command -v dpkg-deb)