Skip to content

Commit

Permalink
Merge pull request #199 from rancher-sandbox/dns
Browse files Browse the repository at this point in the history
Pass DNS addresses from en0 interface to guest instance
  • Loading branch information
AkihiroSuda authored Sep 4, 2021
2 parents deccdaf + fd4346f commit f8f3659
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/network-config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ ethernets:
macaddress: '{{$nw.MACAddress}}'
dhcp4: true
set-name: {{$nw.Name}}
{{- if and (eq $nw.Name $.SlirpNICName) (gt (len $.DNSAddresses) 0) }}
nameservers:
addresses:
{{- range $ns := $.DNSAddresses }}
- {{$ns}}
{{- end }}
{{- end }}
{{- end }}
7 changes: 7 additions & 0 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lima-vm/lima/pkg/iso9660util"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/qemu/qemuconst"
"github.com/lima-vm/lima/pkg/sshutil"
"github.com/lima-vm/lima/pkg/store/filenames"
Expand Down Expand Up @@ -51,6 +52,7 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
User: u.Username,
UID: uid,
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
SlirpNICName: qemuconst.SlirpNICName,
SlirpGateway: qemuconst.SlirpGateway,
Env: y.Env,
}
Expand Down Expand Up @@ -80,6 +82,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
args.Networks = append(args.Networks, Network{MACAddress: vde.MACAddress, Name: vde.Name})
}

args.DNSAddresses, err = osutil.DNSAddresses()
if err != nil {
return err
}

if err := ValidateTemplateArgs(args); err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ type TemplateArgs struct {
Mounts []string // abs path, accessible by the User
Containerd Containerd
Networks []Network
SlirpNICName string
SlirpGateway string
Env map[string]*string
DNSAddresses []string
}

func ValidateTemplateArgs(args TemplateArgs) error {
Expand Down
25 changes: 25 additions & 0 deletions pkg/osutil/dns_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package osutil

import "github.com/lima-vm/lima/pkg/sysprof"

func DNSAddresses() ([]string, error) {
nwData, err := sysprof.NetworkData()
if err != nil {
return nil, err
}
var addresses []string
if len(nwData) > 0 {
// Return DNS addresses from en0 interface
for _, nw := range nwData {
if nw.Interface == "en0" {
addresses = nw.DNS.ServerAddresses
break
}
}
// In case "en0" is not found, use the addresses of the first interface
if len(addresses) == 0 {
addresses = nwData[0].DNS.ServerAddresses
}
}
return addresses, nil
}
9 changes: 9 additions & 0 deletions pkg/osutil/dns_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build !darwin
// +build !darwin

package osutil

func DNSAddresses() ([]string, error) {
// TODO: parse /etc/resolv.conf?
return []string{}, nil
}
1 change: 1 addition & 0 deletions pkg/osutil/osutil_others.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !linux
// +build !linux

package osutil
Expand Down
14 changes: 14 additions & 0 deletions pkg/sysprof/network_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sysprof

type SPNetworkDataType struct {
SPNetworkDataType []NetworkDataType `json:"SPNetworkDataType"`
}

type NetworkDataType struct {
DNS DNS `json:"DNS"`
Interface string `json:"interface"`
}

type DNS struct {
ServerAddresses []string `json:"ServerAddresses"`
}
37 changes: 37 additions & 0 deletions pkg/sysprof/sysprof_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sysprof

import (
"bytes"
"encoding/json"
"fmt"
"os/exec"
"sync"
)

var (
networkDataOnce sync.Once
networkDataCached SPNetworkDataType
networkDataError error
)

func NetworkData() ([]NetworkDataType, error) {
networkDataOnce.Do(func() {
jsonBytes, networkDataError := SystemProfiler("SPNetworkDataType")
if networkDataError == nil {
networkDataError = json.Unmarshal(jsonBytes, &networkDataCached)
}
})
return networkDataCached.SPNetworkDataType, networkDataError
}

func SystemProfiler(dataType string) ([]byte, error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command("system_profiler", dataType, "-json")
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q: %w",
cmd.Args, stdout.String(), stderr.String(), err)
}
return stdout.Bytes(), nil
}

0 comments on commit f8f3659

Please sign in to comment.