mirror of
https://github.com/bjdgyc/anylink.git
synced 2025-08-08 05:41:10 +08:00
修复 modprobe 报错
This commit is contained in:
@@ -3,5 +3,5 @@ package base
|
||||
const (
|
||||
APP_NAME = "AnyLink"
|
||||
// app版本号
|
||||
APP_VER = "0.9.4"
|
||||
APP_VER = "0.10.1"
|
||||
)
|
||||
|
65
server/base/mod.go
Normal file
65
server/base/mod.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
procModulesPath = "/proc/modules"
|
||||
inContainerKey = "ANYLINK_IN_CONTAINER"
|
||||
)
|
||||
|
||||
var (
|
||||
inContainer = false
|
||||
modMap = map[string]struct{}{}
|
||||
)
|
||||
|
||||
func initMod() {
|
||||
container := os.Getenv(inContainerKey)
|
||||
if container == "true" {
|
||||
inContainer = true
|
||||
}
|
||||
log.Println("inContainer", inContainer)
|
||||
|
||||
file, err := os.Open(procModulesPath)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("[ERROR] Problem with open file: %s", err)
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
for scanner.Scan() {
|
||||
splited := strings.Split(scanner.Text(), " ")
|
||||
if len(splited[0]) > 0 {
|
||||
modMap[splited[0]] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CheckModOrLoad(mod string) {
|
||||
log.Println("CheckModOrLoad", mod)
|
||||
|
||||
if _, ok := modMap[mod]; ok {
|
||||
return
|
||||
}
|
||||
|
||||
if inContainer {
|
||||
err := fmt.Errorf("Linux modules %s is not loaded, please run `modprobe %s`", mod, mod)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cmdstr := fmt.Sprintln("modprobe", mod)
|
||||
|
||||
cmd := exec.Command("sh", "-c", cmdstr)
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println(string(b))
|
||||
panic(err)
|
||||
}
|
||||
}
|
@@ -4,6 +4,7 @@ func Start() {
|
||||
execute()
|
||||
initCfg()
|
||||
initLog()
|
||||
initMod()
|
||||
}
|
||||
|
||||
func Test() {
|
||||
|
Reference in New Issue
Block a user