Skip to content

Commit 7f9f3fa

Browse files
authored
Merge pull request #1207 from AkihiroSuda/vznat2
vz: support `.[]networks.vzNAT` networking
2 parents e19022c + 8767e7b commit 7f9f3fa

File tree

8 files changed

+104
-18
lines changed

8 files changed

+104
-18
lines changed

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ jobs:
4646
run: make
4747
- name: Install
4848
run: sudo make install
49-
- name: Validate examples (except vmnet.yaml)
50-
run: find examples -name '*.yaml' | grep -v 'vmnet.yaml' | xargs limactl validate
5149
- name: Uninstall
5250
run: sudo make uninstall
5351

@@ -89,6 +87,8 @@ jobs:
8987
run: make
9088
- name: Install
9189
run: make install
90+
- name: Validate examples
91+
run: find examples -name '*.yaml' | xargs limactl validate
9292
- name: Install test dependencies
9393
# QEMU: required by Lima itself
9494
# bash: required by test-example.sh (OS version of bash is too old)

docs/network.md

+40-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,16 @@ During initial cloud-init bootstrap, `iptables` may not yet be installed. In tha
4242

4343
If `useHostResolver` is false, then DNS servers can be configured manually in `lima.yaml` via the `dns` setting. If that list is empty, then Lima will either use the slirp DNS (on Linux), or the nameservers from the first host interface in service order that has an assigned IPv4 address (on macOS).
4444

45-
## Managed VMNet networks (192.168.105.0/24)
45+
## VMNet networks
46+
47+
VMNet assigns a "real" IP address that is reachable from the host.
48+
49+
The configuration steps are different across QEMU and VZ:
50+
- [QEMU](#qemu)
51+
- [VZ](#vz)
52+
53+
### QEMU
54+
#### Managed (192.168.105.0/24)
4655

4756
Either [`socket_vmnet`](https://github.com/lima-vm/socket_vmnet) (since Lima v0.12) or [`vde_vmnet`](https://github.com/lima-vm/vde_vmnet) (Deprecated)
4857
is required for adding another guest IP that is accessible from the host and other guests.
@@ -51,6 +60,11 @@ Starting with version v0.7.0 lima can manage the networking daemons automaticall
5160
`$LIMA_HOME/_config/networks.yaml`. If this file doesn't already exist, it will be created with these default
5261
settings:
5362

63+
<details>
64+
<summary>Default</summary>
65+
66+
<p>
67+
5468
```yaml
5569
# Path to socket_vmnet executable. Because socket_vmnet is invoked via sudo it should be
5670
# installed where only root can modify/replace it. This means also none of the
@@ -92,6 +106,10 @@ networks:
92106
netmask: 255.255.255.0
93107
```
94108
109+
</p>
110+
111+
</details>
112+
95113
Instances can then reference these networks from their `lima.yaml` file:
96114

97115
```yaml
@@ -120,7 +138,7 @@ be done via:
120138
limactl sudoers | sudo tee /etc/sudoers.d/lima
121139
```
122140

123-
## Unmanaged VMNet networks
141+
#### Unmanaged
124142
For Lima >= 0.12:
125143
```yaml
126144
networks:
@@ -131,7 +149,11 @@ networks:
131149
# - socket: "/var/run/socket_vmnet"
132150
```
133151

134-
For older Lima releases:
152+
<details>
153+
<summary>For older Lima releases</summary>
154+
155+
<p>
156+
135157
```yaml
136158
networks:
137159
# vnl (virtual network locator) points to the vde_switch socket directory,
@@ -147,3 +169,18 @@ networks:
147169
# # Interface name, defaults to "lima0", "lima1", etc.
148170
# interface: ""
149171
```
172+
</p>
173+
174+
</details>
175+
176+
### VZ
177+
178+
For VZ instances, the "vzNAT" network can be configured as follows:
179+
```yaml
180+
networks:
181+
- vzNAT: true
182+
```
183+
184+
The range of the IP address is not specifiable.
185+
186+
The "vzNAT" network does not need the `socket_vmnet` binary and the `sudoers` file.

docs/vmtype.md

-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ mountType: "virtiofs"
3737
3838
### Known Issues
3939
- "vz" doesn't support `legacyBoot: true` option, so guest machine like centos-stream, archlinux, oraclelinux will not work
40-
- Host to guest networking (`networks` section in lima yaml) is not supported
4140
- When running lima using "vz", `${LIMA_HOME}/<INSTANCE>/serial.log` will not contain kernel boot logs

examples/experimental/vz.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ mounts:
1616
- location: "/tmp/lima"
1717
writable: true
1818
mountType: "virtiofs"
19+
20+
networks:
21+
- vzNAT: true

examples/vmnet.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Example to enable vmnet.framework
1+
# Example to enable vmnet.framework for QEMU.
2+
# VZ users should refer to experimental/vz.yaml
3+
24
# This example requires Lima v0.7.0 or later.
35
# Older versions of Lima were using a different syntax for supporting vmnet.framework.
46
images:

pkg/limayaml/limayaml.go

+3
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ type Network struct {
184184
Lima string `yaml:"lima,omitempty" json:"lima,omitempty"`
185185
// Socket is a QEMU-compatible socket
186186
Socket string `yaml:"socket,omitempty" json:"socket,omitempty"`
187+
// VZNAT uses VZNATNetworkDeviceAttachment. Needs VZ. No root privilege is required.
188+
VZNAT *bool `yaml:"vzNAT,omitempty" json:"vzNAT,omitempty"`
189+
187190
// VNLDeprecated is a Virtual Network Locator (https://github.com/rd235/vdeplug4/commit/089984200f447abb0e825eb45548b781ba1ebccd).
188191
// On macOS, only VDE2-compatible form (optionally with vde:// prefix) is supported.
189192
// VNLDeprecated is deprecated. Use Socket.

pkg/limayaml/validate.go

+22
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ func validateNetwork(y LimaYAML, warn bool) error {
271271
if nw.Socket != "" {
272272
return fmt.Errorf("field `%s.lima` and field `%s.socket` are mutually exclusive", field, field)
273273
}
274+
if nw.VZNAT != nil && *nw.VZNAT {
275+
return fmt.Errorf("field `%s.lima` and field `%s.vzNAT` are mutually exclusive", field, field)
276+
}
274277
if nw.VNLDeprecated != "" {
275278
return fmt.Errorf("field `%s.lima` and field `%s.vnl` are mutually exclusive", field, field)
276279
}
@@ -285,6 +288,9 @@ func validateNetwork(y LimaYAML, warn bool) error {
285288
return fmt.Errorf("field `%s.lima` references network %q which is not defined in networks.yaml", field, nw.Lima)
286289
}
287290
} else if nw.Socket != "" {
291+
if nw.VZNAT != nil && *nw.VZNAT {
292+
return fmt.Errorf("field `%s.socket` and field `%s.vzNAT` are mutually exclusive", field, field)
293+
}
288294
if nw.VNLDeprecated != "" {
289295
return fmt.Errorf("field `%s.socket` and field `%s.vnl` are mutually exclusive", field, field)
290296
}
@@ -296,6 +302,22 @@ func validateNetwork(y LimaYAML, warn bool) error {
296302
} else if err == nil && fi.Mode()&os.ModeSocket == 0 {
297303
return fmt.Errorf("field `%s.socket` %q points to a non-socket file", field, nw.Socket)
298304
}
305+
} else if nw.VZNAT != nil && *nw.VZNAT {
306+
if y.VMType == nil || *y.VMType != VZ {
307+
return fmt.Errorf("field `%s.vzNAT` requires `vmType` to be %q", field, VZ)
308+
}
309+
if nw.Lima != "" {
310+
return fmt.Errorf("field `%s.vzNAT` and field `%s.lima` are mutually exclusive", field, field)
311+
}
312+
if nw.Socket != "" {
313+
return fmt.Errorf("field `%s.vzNAT` and field `%s.socket` are mutually exclusive", field, field)
314+
}
315+
if nw.VNLDeprecated != "" {
316+
return fmt.Errorf("field `%s.vzNAT` and field `%s.vnl` are mutually exclusive", field, field)
317+
}
318+
if nw.SwitchPortDeprecated != 0 {
319+
return fmt.Errorf("field `%s.switchPort` cannot be used with field `%s.vzNAT`", field, field)
320+
}
299321
} else {
300322
if nw.VNLDeprecated == "" {
301323
return fmt.Errorf("field `%s.lima`, field `%s.socket`, or field `%s.vnl` must be set", field, field, field)

pkg/vz/vm_darwin.go

+31-11
Original file line numberDiff line numberDiff line change
@@ -191,33 +191,53 @@ func attachSerialPort(driver *driver.BaseDriver, config *vz.VirtualMachineConfig
191191
return err
192192
}
193193

194-
func attachNetwork(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration, networkConn *os.File) error {
195-
//slirp network using gvisor netstack
196-
fileAttachment, err := vz.NewFileHandleNetworkDeviceAttachment(networkConn)
194+
func newVirtioNetworkDeviceConfiguration(attachment vz.NetworkDeviceAttachment, macStr string) (*vz.VirtioNetworkDeviceConfiguration, error) {
195+
networkConfig, err := vz.NewVirtioNetworkDeviceConfiguration(attachment)
197196
if err != nil {
198-
return err
197+
return nil, err
199198
}
200-
err = fileAttachment.SetMaximumTransmissionUnit(1500)
199+
mac, err := net.ParseMAC(macStr)
201200
if err != nil {
202-
return err
201+
return nil, err
203202
}
204-
networkConfig, err := vz.NewVirtioNetworkDeviceConfiguration(fileAttachment)
203+
address, err := vz.NewMACAddress(mac)
204+
if err != nil {
205+
return nil, err
206+
}
207+
networkConfig.SetMACAddress(address)
208+
return networkConfig, nil
209+
}
210+
211+
func attachNetwork(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfiguration, networkConn *os.File) error {
212+
//slirp network using gvisor netstack
213+
fileAttachment, err := vz.NewFileHandleNetworkDeviceAttachment(networkConn)
205214
if err != nil {
206215
return err
207216
}
208-
mac, err := net.ParseMAC(limayaml.MACAddress(driver.Instance.Dir))
217+
err = fileAttachment.SetMaximumTransmissionUnit(1500)
209218
if err != nil {
210219
return err
211220
}
212-
address, err := vz.NewMACAddress(mac)
221+
networkConfig, err := newVirtioNetworkDeviceConfiguration(fileAttachment, limayaml.MACAddress(driver.Instance.Dir))
213222
if err != nil {
214223
return err
215224
}
216-
networkConfig.SetMACAddress(address)
217-
218225
configurations := []*vz.VirtioNetworkDeviceConfiguration{
219226
networkConfig,
220227
}
228+
for _, nw := range driver.Instance.Networks {
229+
if nw.VZNAT != nil && *nw.VZNAT {
230+
attachment, err := vz.NewNATNetworkDeviceAttachment()
231+
if err != nil {
232+
return err
233+
}
234+
networkConfig, err = newVirtioNetworkDeviceConfiguration(attachment, nw.MACAddress)
235+
if err != nil {
236+
return err
237+
}
238+
configurations = append(configurations, networkConfig)
239+
}
240+
}
221241
vmConfig.SetNetworkDevicesVirtualMachineConfiguration(configurations)
222242
return nil
223243
}

0 commit comments

Comments
 (0)