From 2bf4fe5a195546ceae13d29d588bef8542de90ec Mon Sep 17 00:00:00 2001 From: Jan Dubois Date: Wed, 4 Aug 2021 22:37:51 -0700 Subject: [PATCH] Add host.lima.internal to /etc/hosts Signed-off-by: Jan Dubois --- docs/internal.md | 1 + docs/network.md | 6 +++--- pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh | 5 +++++ pkg/cidata/cidata.TEMPLATE.d/lima.env | 1 + pkg/cidata/cidata.go | 9 +++++---- pkg/cidata/template.go | 15 ++++++++------- pkg/qemu/qemu.go | 5 +++-- pkg/qemu/qemuconst/qemuconst.go | 4 ++++ 8 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh diff --git a/docs/internal.md b/docs/internal.md index 275a6aa6945..7d370b72e89 100644 --- a/docs/internal.md +++ b/docs/internal.md @@ -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`. \ No newline at end of file diff --git a/docs/network.md b/docs/network.md index 5a38b1bc13f..e4ca6f9f948 100644 --- a/docs/network.md +++ b/docs/network.md @@ -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) diff --git a/pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh b/pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh new file mode 100644 index 00000000000..19feaf46346 --- /dev/null +++ b/pkg/cidata/cidata.TEMPLATE.d/boot/03-etc-hosts.sh @@ -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 diff --git a/pkg/cidata/cidata.TEMPLATE.d/lima.env b/pkg/cidata/cidata.TEMPLATE.d/lima.env index 22d14bc3a06..d9d30eac67c 100644 --- a/pkg/cidata/cidata.TEMPLATE.d/lima.env +++ b/pkg/cidata/cidata.TEMPLATE.d/lima.env @@ -14,3 +14,4 @@ LIMA_CIDATA_CONTAINERD_SYSTEM=1 {{- else}} LIMA_CIDATA_CONTAINERD_SYSTEM= {{- end}} +LIMA_CIDATA_SLIRP_GATEWAY={{ .SlirpGateway }} diff --git a/pkg/cidata/cidata.go b/pkg/cidata/cidata.go index 2b4af9f7316..e4450640b6f 100644 --- a/pkg/cidata/cidata.go +++ b/pkg/cidata/cidata.go @@ -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) diff --git a/pkg/cidata/template.go b/pkg/cidata/template.go index ceb227427ef..ee2c5970dec 100644 --- a/pkg/cidata/template.go +++ b/pkg/cidata/template.go @@ -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 { diff --git a/pkg/qemu/qemu.go b/pkg/qemu/qemu.go index 01340210dd4..d82d798a7a7 100644 --- a/pkg/qemu/qemu.go +++ b/pkg/qemu/qemu.go @@ -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" @@ -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 . diff --git a/pkg/qemu/qemuconst/qemuconst.go b/pkg/qemu/qemuconst/qemuconst.go index 0ec84b960d7..38a0df3bcc0 100644 --- a/pkg/qemu/qemuconst/qemuconst.go +++ b/pkg/qemu/qemuconst/qemuconst.go @@ -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" )