修复容器频繁重启的问题

This commit is contained in:
bjdgy
2023-12-26 11:24:01 +08:00
parent dfb25718f7
commit 17492d8172
6 changed files with 42 additions and 22 deletions

View File

@@ -12,19 +12,20 @@ import (
const (
procModulesPath = "/proc/modules"
inContainerKey = "ANYLINK_IN_CONTAINER"
tunPath = "/dev/net/tun"
)
var (
inContainer = false
InContainer = false
modMap = map[string]struct{}{}
)
func initMod() {
container := os.Getenv(inContainerKey)
if container == "true" {
inContainer = true
InContainer = true
}
log.Println("inContainer", inContainer)
log.Println("InContainer", InContainer)
file, err := os.Open(procModulesPath)
if err != nil {
@@ -49,8 +50,19 @@ func CheckModOrLoad(mod string) {
return
}
if inContainer {
err := fmt.Errorf("Linux modules %s is not loaded, please run `modprobe %s`", mod, mod)
if mod == "tun" || mod == "tap" {
_, err := os.Stat(tunPath)
if err == nil {
// 文件存在
return
}
panic("Linux tunFile is null " + tunPath)
}
if InContainer {
err := fmt.Errorf("Linux module %s is not loaded, please run `modprobe %s`", mod, mod)
// log.Println(err)
// return
panic(err)
}

View File

@@ -74,8 +74,11 @@ func LinkTun(cSess *sessdata.ConnSession) error {
// 通过 ip link show 查看 alias 信息
cmdstr1 := fmt.Sprintf("ip link set dev %s up mtu %d multicast off alias %s.%s", ifce.Name(), cSess.Mtu,
cSess.Group.Name, cSess.Username)
cmdstr1 := fmt.Sprintf("ip link set dev %s up mtu %d multicast off", ifce.Name(), cSess.Mtu)
if !base.InContainer {
// 容器默认 iproute 不支持 alias
cmdstr1 += fmt.Sprintf(" alias %s.%s", cSess.Group.Name, cSess.Username)
}
cmdstr2 := fmt.Sprintf("ip addr add dev %s local %s peer %s/32",
ifce.Name(), base.Cfg.Ipv4Gateway, cSess.IpAddr)
err = execCmd([]string{cmdstr1, cmdstr2})

View File

@@ -55,8 +55,11 @@ func LinkMacvtap(cSess *sessdata.ConnSession) error {
cSess.SetIfName(ifName)
cmdstr1 := fmt.Sprintf("ip link add link %s name %s type macvtap mode bridge", base.Cfg.Ipv4Master, ifName)
cmdstr2 := fmt.Sprintf("ip link set dev %s up mtu %d address %s alias %s.%s", ifName, cSess.Mtu, cSess.MacHw,
cSess.Group.Name, cSess.Username)
cmdstr2 := fmt.Sprintf("ip link set dev %s up mtu %d address %s", ifName, cSess.Mtu, cSess.MacHw)
if !base.InContainer {
// 容器默认 iproute 不支持 alias
cmdstr2 += fmt.Sprintf(" alias %s.%s", cSess.Group.Name, cSess.Username)
}
err := execCmd([]string{cmdstr1, cmdstr2})
if err != nil {
base.Error(err)