Skip to content

Commit fd713d1

Browse files
committed
Define additional network interfaces via cloud-init
Signed-off-by: Jan Dubois <[email protected]>
1 parent cc6c3ff commit fd713d1

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
ethernets:
3+
{{- range $i, $addr := .MACAddresses}}
4+
eth{{$i}}:
5+
match:
6+
macaddress: '{{$addr}}'
7+
dhcp4: true
8+
set-name: eth{{$i}}
9+
{{- end }}

pkg/cidata/cidata.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
"github.com/AkihiroSuda/lima/pkg/iso9660util"
1616
"github.com/AkihiroSuda/lima/pkg/limayaml"
1717
"github.com/AkihiroSuda/lima/pkg/localpathutil"
18+
"github.com/AkihiroSuda/lima/pkg/qemu"
1819
"github.com/AkihiroSuda/lima/pkg/sshutil"
20+
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1921
"github.com/opencontainers/go-digest"
2022
"github.com/pkg/errors"
2123
"github.com/sirupsen/logrus"
@@ -32,7 +34,7 @@ var (
3234
}
3335
)
3436

35-
func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
37+
func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
3638
if err := limayaml.ValidateRaw(*y); err != nil {
3739
return err
3840
}
@@ -70,6 +72,11 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
7072
args.Mounts = append(args.Mounts, expanded)
7173
}
7274

75+
args.MACAddresses = append(args.MACAddresses, qemu.SlirpMACAddress)
76+
if y.Network.VDE.URL != "" {
77+
args.MACAddresses = append(args.MACAddresses, qemu.MACAddress(instDir, y))
78+
}
79+
7380
if err := ValidateTemplateArgs(args); err != nil {
7481
return err
7582
}
@@ -147,7 +154,7 @@ func GenerateISO9660(isoPath, name string, y *limayaml.LimaYAML) error {
147154
})
148155
}
149156

150-
return iso9660util.Write(isoPath, "cidata", layout)
157+
return iso9660util.Write(filepath.Join(instDir, filenames.CIDataISO), "cidata", layout)
151158
}
152159

153160
func GuestAgentBinary(arch string) (io.ReadCloser, error) {

pkg/cidata/template.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ type Containerd struct {
2424
User bool
2525
}
2626
type TemplateArgs struct {
27-
Name string // instance name
28-
User string // user name
29-
UID int
30-
SSHPubKeys []string
31-
Mounts []string // abs path, accessible by the User
32-
Containerd Containerd
27+
Name string // instance name
28+
User string // user name
29+
UID int
30+
SSHPubKeys []string
31+
Mounts []string // abs path, accessible by the User
32+
Containerd Containerd
33+
MACAddresses []string
3334
}
3435

3536
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/qemu/qemu.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,14 @@ func appendArgsIfNoConflict(args []string, k, v string) []string {
133133
return append(args, k, v)
134134
}
135135

136-
func macAddress(cfg Config) string {
137-
addr := cfg.LimaYAML.Network.VDE.MACAddress
136+
const (
137+
SlirpMACAddress = "22:11:11:11:11:11"
138+
)
139+
140+
func MACAddress(instanceDir string, y *limayaml.LimaYAML) string {
141+
addr := y.Network.VDE.MACAddress
138142
if addr == "" {
139-
sha := sha256.Sum256([]byte(cfg.InstanceDir))
143+
sha := sha256.Sum256([]byte(instanceDir))
140144
// According to https://gitlab.com/wireshark/wireshark/-/blob/master/manuf
141145
// no well-known MAC addresses start with 0x22.
142146
hw := append(net.HardwareAddr{0x22}, sha[0:5]...)
@@ -209,12 +213,12 @@ func Cmdline(cfg Config) (string, []string, error) {
209213

210214
// Network
211215
if y.Network.VDE.URL != "" {
212-
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", macAddress(cfg)))
213216
args = append(args, "-netdev", fmt.Sprintf("vde,id=net0,sock=%s", y.Network.VDE.URL))
217+
args = append(args, "-device", fmt.Sprintf("virtio-net-pci,netdev=net0,mac=%s", MACAddress(cfg.InstanceDir, y)))
214218
}
215219
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
216-
args = append(args, "-device", "virtio-net-pci,netdev=net1")
217220
args = append(args, "-netdev", fmt.Sprintf("user,id=net1,net=192.168.5.0/24,hostfwd=tcp:127.0.0.1:%d-:22", y.SSH.LocalPort))
221+
args = append(args, "-device", "virtio-net-pci,netdev=net1,mac="+SlirpMACAddress)
218222

219223
// virtio-rng-pci acceralates starting up the OS, according to https://wiki.gentoo.org/wiki/QEMU/Options
220224
args = append(args, "-device", "virtio-rng-pci")

pkg/start/start.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ import (
1919
)
2020

2121
func ensureDisk(ctx context.Context, instName, instDir string, y *limayaml.LimaYAML) error {
22-
cidataISOPath := filepath.Join(instDir, filenames.CIDataISO)
23-
if err := cidata.GenerateISO9660(cidataISOPath, instName, y); err != nil {
22+
if err := cidata.GenerateISO9660(instDir, instName, y); err != nil {
2423
return err
2524
}
2625
qCfg := qemu.Config{

0 commit comments

Comments
 (0)