Skip to content

Commit

Permalink
Add host.lima.internal to /etc/hosts
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Dubois <[email protected]>
  • Loading branch information
jandubois committed Aug 5, 2021
1 parent be41f25 commit 2bf4fe5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,4 @@ The volume label is "cidata", as defined by [cloud-init NoCloud](https://cloudin
- `LIMA_CIDATA_MOUNTS_%d_MOUNTPOINT`: the N-th mount point of Lima mounts (N=0, 1, ...)
- `LIMA_CIDATA_CONTAINERD_USER`: set to "1" if rootless containerd to be set up
- `LIMA_CIDATA_CONTAINERD_SYSTEM`: set to "1" if system-wide containerd to be set up
- `LIMA_CIDATA_SLIRP_GATEWAY`: set to the IP address of the host on the SLIRP network. `192.168.5.2`.
6 changes: 3 additions & 3 deletions docs/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ By default Lima only enables the user-mode networking aka "slirp".

### Guest IP (192.168.5.15)

The guest IP address is typically set to 192.168.5.15.
The guest IP address is set to `192.168.5.15`.

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

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

### Host IP (192.168.5.2)

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

### DNS (192.168.5.3)

Expand Down
5 changes: 5 additions & 0 deletions pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
set -eux -o pipefail

sed -i '/host.lima.internal/d' /etc/hosts
echo -e "${LIMA_CIDATA_SLIRP_GATEWAY}\thost.lima.internal" >>/etc/hosts
1 change: 1 addition & 0 deletions pkg/cidata/cidata.TEMPLATE.d/lima.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ LIMA_CIDATA_CONTAINERD_SYSTEM=1
{{- else}}
LIMA_CIDATA_CONTAINERD_SYSTEM=
{{- end}}
LIMA_CIDATA_SLIRP_GATEWAY={{ .SlirpGateway }}
9 changes: 5 additions & 4 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func GenerateISO9660(instDir, name string, y *limayaml.LimaYAML) error {
return err
}
args := TemplateArgs{
Name: name,
User: u.Username,
UID: uid,
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
Name: name,
User: u.Username,
UID: uid,
Containerd: Containerd{System: *y.Containerd.System, User: *y.Containerd.User},
SlirpGateway: qemuconst.SlirpGateway,
}

pubKeys, err := sshutil.DefaultPubKeys(*y.SSH.LoadDotSSHPubKeys)
Expand Down
15 changes: 8 additions & 7 deletions pkg/cidata/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ type Network struct {
Name string
}
type TemplateArgs struct {
Name string // instance name
User string // user name
UID int
SSHPubKeys []string
Mounts []string // abs path, accessible by the User
Containerd Containerd
Networks []Network
Name string // instance name
User string // user name
UID int
SSHPubKeys []string
Mounts []string // abs path, accessible by the User
Containerd Containerd
Networks []Network
SlirpGateway string
}

func ValidateTemplateArgs(args TemplateArgs) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/AkihiroSuda/lima/pkg/downloader"
"github.com/AkihiroSuda/lima/pkg/iso9660util"
"github.com/AkihiroSuda/lima/pkg/limayaml"
"github.com/AkihiroSuda/lima/pkg/qemu/qemuconst"
"github.com/AkihiroSuda/lima/pkg/store/filenames"
"github.com/docker/go-units"
"github.com/mattn/go-shellwords"
Expand Down Expand Up @@ -193,8 +194,8 @@ func Cmdline(cfg Config) (string, []string, error) {
args = append(args, "-cdrom", filepath.Join(cfg.InstanceDir, filenames.CIDataISO))

// Network
// CIDR is intentionally hardcoded to 192.168.5.0/24, as each of QEMU has its own independent slirp network.
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))
args = append(args, "-netdev", fmt.Sprintf("user,id=net0,net=%s,dhcpstart=%s,hostfwd=tcp:127.0.0.1:%d-:22",
qemuconst.SlirpNetwork, qemuconst.SlirpIPAddress, y.SSH.LocalPort))
args = append(args, "-device", "virtio-net-pci,netdev=net0,mac="+limayaml.MACAddress(cfg.InstanceDir))
for i, vde := range y.Network.VDE {
// VDE4 accepts VNL like vde:///var/run/vde.ctl as well as file path like /var/run/vde.ctl .
Expand Down
4 changes: 4 additions & 0 deletions pkg/qemu/qemuconst/qemuconst.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ package qemuconst

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

0 comments on commit 2bf4fe5

Please sign in to comment.