Skip to content

Commit 5619725

Browse files
authored
Merge pull request #142 from rancher-sandbox/host-lima-internal
Add host.lima.internal to /etc/hosts
2 parents be41f25 + 2bf4fe5 commit 5619725

File tree

8 files changed

+30
-16
lines changed

8 files changed

+30
-16
lines changed

docs/internal.md

+1
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,4 @@ The volume label is "cidata", as defined by [cloud-init NoCloud](https://cloudin
107107
- `LIMA_CIDATA_MOUNTS_%d_MOUNTPOINT`: the N-th mount point of Lima mounts (N=0, 1, ...)
108108
- `LIMA_CIDATA_CONTAINERD_USER`: set to "1" if rootless containerd to be set up
109109
- `LIMA_CIDATA_CONTAINERD_SYSTEM`: set to "1" if system-wide containerd to be set up
110+
- `LIMA_CIDATA_SLIRP_GATEWAY`: set to the IP address of the host on the SLIRP network. `192.168.5.2`.

docs/network.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ By default Lima only enables the user-mode networking aka "slirp".
66

77
### Guest IP (192.168.5.15)
88

9-
The guest IP address is typically set to 192.168.5.15.
9+
The guest IP address is set to `192.168.5.15`.
1010

1111
This IP address is not accessible from the host by design.
1212

13-
Use `vde_vmnet` to allow accessing the guest IP from the host and other guests.
13+
Use [vde_vmnet](https://github.com/lima-vm/vde_vmnet) to allow accessing the guest IP from the host and other guests.
1414

1515
### Host IP (192.168.5.2)
1616

17-
The loopback addresses of the host is accessible from the guest as 192.168.5.2.
17+
The loopback addresses of the host is `192.168.5.2` and is accessible from the guest as `host.lima.internal`.
1818

1919
### DNS (192.168.5.3)
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
set -eux -o pipefail
3+
4+
sed -i '/host.lima.internal/d' /etc/hosts
5+
echo -e "${LIMA_CIDATA_SLIRP_GATEWAY}\thost.lima.internal" >>/etc/hosts

pkg/cidata/cidata.TEMPLATE.d/lima.env

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ LIMA_CIDATA_CONTAINERD_SYSTEM=1
1414
{{- else}}
1515
LIMA_CIDATA_CONTAINERD_SYSTEM=
1616
{{- end}}
17+
LIMA_CIDATA_SLIRP_GATEWAY={{ .SlirpGateway }}

pkg/cidata/cidata.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
4747
return err
4848
}
4949
args := TemplateArgs{
50-
Name: name,
51-
User: u.Username,
52-
UID: uid,
53-
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
50+
Name: name,
51+
User: u.Username,
52+
UID: uid,
53+
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
54+
SlirpGateway: qemuconst.SlirpGateway,
5455
}
5556

5657
pubKeys, err := sshutil.DefaultPubKeys(*y.SSH.LoadDotSSHPubKeys)

pkg/cidata/template.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ type Network struct {
2828
Name string
2929
}
3030
type TemplateArgs struct {
31-
Name string // instance name
32-
User string // user name
33-
UID int
34-
SSHPubKeys []string
35-
Mounts []string // abs path, accessible by the User
36-
Containerd Containerd
37-
Networks []Network
31+
Name string // instance name
32+
User string // user name
33+
UID int
34+
SSHPubKeys []string
35+
Mounts []string // abs path, accessible by the User
36+
Containerd Containerd
37+
Networks []Network
38+
SlirpGateway string
3839
}
3940

4041
func ValidateTemplateArgs(args TemplateArgs) error {

pkg/qemu/qemu.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/AkihiroSuda/lima/pkg/downloader"
1313
"github.com/AkihiroSuda/lima/pkg/iso9660util"
1414
"github.com/AkihiroSuda/lima/pkg/limayaml"
15+
"github.com/AkihiroSuda/lima/pkg/qemu/qemuconst"
1516
"github.com/AkihiroSuda/lima/pkg/store/filenames"
1617
"github.com/docker/go-units"
1718
"github.com/mattn/go-shellwords"
@@ -193,8 +194,8 @@ func Cmdline(cfg Config) (string, []string, error) {
193194
args = append(args, "-cdrom", filepath.Join(cfg.InstanceDir, filenames.CIDataISO))
194195

195196
// Network
196-
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
197-
args = append(args, "-netdev", fmt.Sprintf("user,id=net0,net=192.168.5.0/24,hostfwd=tcp:127.0.0.1:%d-:22", y.SSH.LocalPort))
197+
args = append(args, "-netdev", fmt.Sprintf("user,id=net0,net=%s,dhcpstart=%s,hostfwd=tcp:127.0.0.1:%d-:22",
198+
qemuconst.SlirpNetwork, qemuconst.SlirpIPAddress, y.SSH.LocalPort))
198199
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+limayaml.MACAddress(cfg.InstanceDir))
199200
for i, vde := range y.Network.VDE {
200201
// VDE4 accepts VNL like vde:///var/run/vde.ctl as well as file path like /var/run/vde.ctl .

pkg/qemu/qemuconst/qemuconst.go

+4
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ package qemuconst
22

33
const (
44
SlirpNICName = "eth0"
5+
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
6+
SlirpNetwork = "192.168.5.0/24"
7+
SlirpGateway = "192.168.5.2"
8+
SlirpIPAddress = "192.168.5.15"
59
)

0 commit comments

Comments
 (0)