diff --git a/pkg/hostman/guestfs/linux.go b/pkg/hostman/guestfs/linux.go index 5743c076fc..672123d8a8 100644 --- a/pkg/hostman/guestfs/linux.go +++ b/pkg/hostman/guestfs/linux.go @@ -385,8 +385,8 @@ func (d *SDebianLikeRootFs) DeployNetworkingScripts(nics []jsonutils.JSONObject) gateway, _ := nic.GetString("gateway") cmds += fmt.Sprintf(" gateway %s\n", gateway) } - var routes = make([]string, 0) - hostman.AddNicRoutes(routes, nic, mainIp, len(nics)) + var routes = make([][]string, 0) + hostman.AddNicRoutes(&routes, nic, mainIp, len(nics)) } } } diff --git a/pkg/hostman/utils.go b/pkg/hostman/utils.go index 66931a1246..a78f9e0012 100644 --- a/pkg/hostman/utils.go +++ b/pkg/hostman/utils.go @@ -303,13 +303,46 @@ func Netlen2Mask(netmasklen int64) string { return mask } -func addRoute(routes []string, net, gw string) { - for _, rt := range routes { - // if rt???? 有问题 +func addRoute(routes *[][]string, net, gw string) { + for _, rt := range *routes { + if rt[0] == net { + return + } } + *routes = append(*routes, []string{net, gw}) } -func extendRoutes(routes []string, nicRoutes []jsonutils.JSONObject) error { + + // if len(nic.Routes) != 0 { + // for _, nicRoute := range nic.Routes { + // tRoute, err := parseNicRoute(nicRoute) + // if err != nil { + // log.Errorf("Parse route %v error: %v", nicRoute, err) + // continue + // } + // routes = append(routes, tRoute) + // } + // } + + +func parseNicRoute(route Route) (*types.Route, error) { + if len(route) != 2 { + return nil, fmt.Errorf("Invalid route format: %v", route) + } + _, dstNet, err := net.ParseCIDR(route[0]) + if err != nil { + return nil, err + } + gwIP := net.ParseIP(route[1]) + return &types.Route{ + Dst: *dstNet, + GW: gwIP, + }, nil +} + + + +func extendRoutes(routes *[][]string, nicRoutes []jsonutils.JSONObject) error { for i := 0; i < len(nicRoutes); i++ { rt, ok := nicRoutes[i].(*jsonutils.JSONArray) if !ok { @@ -322,15 +355,18 @@ func extendRoutes(routes []string, nicRoutes []jsonutils.JSONObject) error { } addRoute(routes, rts[0], rts[1]) } + return nil } -func AddNicRoutes(routes []string, nic jsonutils.JSONObject, mainIp string, nicCnt int) []string { +func AddNicRoutes(routes *[][]string, nic jsonutils.JSONObject, mainIp string, nicCnt int) { + var nicDesc = new(SNic) + nic.Unmarshal(nicDesc) ip, _ := nic.GetString("ip") if mainIp == ip { - return routes + return } if nic.Contains("routes") { nicRoutes, _ := nic.GetArray("routes") extendRoutes(routes, nicRoutes) - } + } else if nic.Contains("gateway") && }