mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 03:10:32 +08:00
修复容器频繁重启的问题
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
@@ -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})
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user