Skip to content

Commit f4e50c2

Browse files
authored
Merge pull request #2817 from AkihiroSuda/fix-2813-alt
Allow "." and "_" in instance names, disallow them in hostnames
2 parents 6a798c3 + d22d643 commit f4e50c2

File tree

16 files changed

+65
-26
lines changed

16 files changed

+65
-26
lines changed

cmd/limactl/guessarg/guessarg.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func InstNameFromURL(urlStr string) (string, error) {
5656
func InstNameFromYAMLPath(yamlPath string) (string, error) {
5757
s := strings.ToLower(filepath.Base(yamlPath))
5858
s = strings.TrimSuffix(strings.TrimSuffix(s, ".yml"), ".yaml")
59-
s = strings.ReplaceAll(s, ".", "-")
59+
// "." is allowed in instance names, but replaced to "-" for hostnames.
60+
// e.g., yaml: "ubuntu-24.04.yaml" , instance name: "ubuntu-24.04", hostname: "lima-ubuntu-24-04"
6061
if err := identifiers.Validate(s); err != nil {
6162
return "", fmt.Errorf("filename %q is invalid: %w", yamlPath, err)
6263
}

cmd/limactl/show-ssh.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
8686
}
8787
return err
8888
}
89-
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s lima-%s`.",
90-
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Name)
89+
logrus.Warnf("`limactl show-ssh` is deprecated. Instead, use `ssh -F %s %s`.",
90+
filepath.Join(inst.Dir, filenames.SSHConfig), inst.Hostname)
9191
opts, err := sshutil.SSHOpts(
9292
inst.Dir,
9393
*inst.Config.SSH.LoadDotSSHPubKeys,

cmd/limactl/tunnel.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func tunnelAction(cmd *cobra.Command, args []string) error {
146146
default:
147147
fmt.Fprintf(stdout, "Set `ALL_PROXY=socks5h://127.0.0.1:%d`, etc.\n", port)
148148
}
149-
fmt.Fprintf(stdout, "The instance can be connected from the host as <http://lima-%s.internal> via a web browser.\n", inst.Name)
149+
fmt.Fprintf(stdout, "The instance can be connected from the host as <http://%s.internal> via a web browser.\n", inst.Hostname)
150150

151151
// TODO: show the port in `limactl list --json` ?
152152
// TODO: add `--stop` flag to shut down the tunnel
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
instance-id: {{.IID}}
2-
local-hostname: lima-{{.Name}}
2+
local-hostname: {{.Hostname}}

pkg/cidata/cidata.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717

1818
"github.com/docker/go-units"
1919
"github.com/lima-vm/lima/pkg/debugutil"
20+
"github.com/lima-vm/lima/pkg/identifierutil"
2021
"github.com/lima-vm/lima/pkg/iso9660util"
2122
"github.com/lima-vm/lima/pkg/limayaml"
2223
"github.com/lima-vm/lima/pkg/localpathutil"
@@ -128,6 +129,7 @@ func templateArgs(bootScripts bool, instDir, name string, instConfig *limayaml.L
128129
Debug: debugutil.Debug,
129130
BootScripts: bootScripts,
130131
Name: name,
132+
Hostname: identifierutil.HostnameFromInstName(name), // TODO: support customization
131133
User: u.Username,
132134
UID: uid,
133135
Home: fmt.Sprintf("/home/%s.linux", u.Username),

pkg/cidata/template.go

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type Disk struct {
5656
type TemplateArgs struct {
5757
Debug bool
5858
Name string // instance name
59+
Hostname string // instance hostname
5960
IID string // instance id
6061
User string // user name
6162
Home string // home directory

pkg/hostagent/hostagent.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
hostagentapi "github.com/lima-vm/lima/pkg/hostagent/api"
2929
"github.com/lima-vm/lima/pkg/hostagent/dns"
3030
"github.com/lima-vm/lima/pkg/hostagent/events"
31+
"github.com/lima-vm/lima/pkg/identifierutil"
3132
"github.com/lima-vm/lima/pkg/limayaml"
3233
"github.com/lima-vm/lima/pkg/networks"
3334
"github.com/lima-vm/lima/pkg/osutil"
@@ -288,7 +289,8 @@ func (a *HostAgent) Run(ctx context.Context) error {
288289
if limayaml.FirstUsernetIndex(a.instConfig) == -1 && *a.instConfig.HostResolver.Enabled {
289290
hosts := a.instConfig.HostResolver.Hosts
290291
hosts["host.lima.internal"] = networks.SlirpGateway
291-
hosts[fmt.Sprintf("lima-%s", a.instName)] = networks.SlirpIPAddress
292+
hostname := identifierutil.HostnameFromInstName(a.instName) // TODO: support customization
293+
hosts[hostname] = networks.SlirpIPAddress
292294
srvOpts := dns.ServerOptions{
293295
UDPPort: a.udpDNSLocalPort,
294296
TCPPort: a.tcpDNSLocalPort,

pkg/identifierutil/identifierutil.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package identifierutil
2+
3+
import "strings"
4+
5+
func HostnameFromInstName(instName string) string {
6+
s := strings.ReplaceAll(instName, ".", "-")
7+
s = strings.ReplaceAll(s, "_", "-")
8+
return "lima-" + s
9+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package identifierutil
2+
3+
import (
4+
"testing"
5+
6+
"gotest.tools/v3/assert"
7+
)
8+
9+
func TestHostnameFromInstName(t *testing.T) {
10+
assert.Equal(t, "lima-default", HostnameFromInstName("default"))
11+
assert.Equal(t, "lima-ubuntu-24-04", HostnameFromInstName("ubuntu-24.04"))
12+
assert.Equal(t, "lima-foo-bar-baz", HostnameFromInstName("foo_bar.baz"))
13+
}

pkg/instance/ansible.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func runAnsiblePlaybook(ctx context.Context, inst *store.Instance, playbook stri
4141
func createAnsibleInventory(inst *store.Instance) (string, error) {
4242
vars := map[string]interface{}{
4343
"ansible_connection": "ssh",
44-
"ansible_host": "lima-" + inst.Name,
44+
"ansible_host": inst.Hostname,
4545
"ansible_ssh_common_args": "-F " + inst.SSHConfigFile,
4646
}
4747
hosts := map[string]interface{}{

pkg/instance/start.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func watchHostAgentEvents(ctx context.Context, inst *store.Instance, haStdoutPat
308308
return true
309309
}
310310
if *inst.Config.Plain {
311-
logrus.Infof("READY. Run `ssh -F %q lima-%s` to open the shell.", inst.SSHConfigFile, inst.Name)
311+
logrus.Infof("READY. Run `ssh -F %q %s` to open the shell.", inst.SSHConfigFile, inst.Hostname)
312312
} else {
313313
logrus.Infof("READY. Run `%s` to open the shell.", LimactlShellCmd(inst.Name))
314314
}

pkg/limayaml/defaults.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/sirupsen/logrus"
2323
"golang.org/x/sys/cpu"
2424

25+
"github.com/lima-vm/lima/pkg/identifierutil"
2526
"github.com/lima-vm/lima/pkg/networks"
2627
"github.com/lima-vm/lima/pkg/osutil"
2728
"github.com/lima-vm/lima/pkg/ptr"
@@ -814,12 +815,14 @@ func executeGuestTemplate(format, instDir string, param map[string]string) (byte
814815
tmpl, err := template.New("").Parse(format)
815816
if err == nil {
816817
user, _ := osutil.LimaUser(false)
818+
name := filepath.Base(instDir)
817819
data := map[string]interface{}{
818-
"Home": fmt.Sprintf("/home/%s.linux", user.Username),
819-
"Name": filepath.Base(instDir),
820-
"UID": user.Uid,
821-
"User": user.Username,
822-
"Param": param,
820+
"Home": fmt.Sprintf("/home/%s.linux", user.Username),
821+
"Name": name,
822+
"Hostname": identifierutil.HostnameFromInstName(name), // TODO: support customization
823+
"UID": user.Uid,
824+
"User": user.Username,
825+
"Param": param,
823826
}
824827
var out bytes.Buffer
825828
if err := tmpl.Execute(&out, data); err == nil {
@@ -836,9 +839,10 @@ func executeHostTemplate(format, instDir string, param map[string]string) (bytes
836839
home, _ := os.UserHomeDir()
837840
limaHome, _ := dirnames.LimaDir()
838841
data := map[string]interface{}{
839-
"Dir": instDir,
840-
"Home": home,
841-
"Name": filepath.Base(instDir),
842+
"Dir": instDir,
843+
"Home": home,
844+
"Name": filepath.Base(instDir),
845+
// TODO: add hostname fields for the host and the guest
842846
"UID": user.Uid,
843847
"User": user.Username,
844848
"Param": param,

pkg/networks/usernet/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (c *Client) ConfigureDriver(ctx context.Context, driver *driver.BaseDriver)
3939
return err
4040
}
4141
hosts := driver.Instance.Config.HostResolver.Hosts
42-
hosts[fmt.Sprintf("lima-%s.internal", driver.Instance.Name)] = ipAddress
42+
hosts[fmt.Sprintf("%s.internal", driver.Instance.Hostname)] = ipAddress
4343
err = c.AddDNSHosts(hosts)
4444
return err
4545
}

pkg/sshutil/format.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
"io"
66
"strings"
7+
8+
"github.com/lima-vm/lima/pkg/identifierutil"
79
)
810

911
// FormatT specifies the format type.
@@ -57,7 +59,7 @@ func quoteOption(o string) string {
5759

5860
// Format formats the ssh options.
5961
func Format(w io.Writer, instName string, format FormatT, opts []string) error {
60-
fakeHostname := "lima-" + instName // corresponds to the default guest hostname
62+
fakeHostname := identifierutil.HostnameFromInstName(instName) // TODO: support customization
6163
switch format {
6264
case FormatCmd:
6365
args := []string{"ssh"}

pkg/store/instance.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
"github.com/docker/go-units"
2020
hostagentclient "github.com/lima-vm/lima/pkg/hostagent/api/client"
21+
"github.com/lima-vm/lima/pkg/identifierutil"
2122
"github.com/lima-vm/lima/pkg/limayaml"
2223
"github.com/lima-vm/lima/pkg/store/dirnames"
2324
"github.com/lima-vm/lima/pkg/store/filenames"
@@ -38,7 +39,9 @@ const (
3839
)
3940

4041
type Instance struct {
41-
Name string `json:"name"`
42+
Name string `json:"name"`
43+
// Hostname, not HostName (corresponds to SSH's naming convention)
44+
Hostname string `json:"hostname"`
4245
Status Status `json:"status"`
4346
Dir string `json:"dir"`
4447
VMType limayaml.VMType `json:"vmType"`
@@ -66,8 +69,10 @@ type Instance struct {
6669
// Other errors are returned as *Instance.Errors.
6770
func Inspect(instName string) (*Instance, error) {
6871
inst := &Instance{
69-
Name: instName,
70-
Status: StatusUnknown,
72+
Name: instName,
73+
// TODO: support customizing hostname
74+
Hostname: identifierutil.HostnameFromInstName(instName),
75+
Status: StatusUnknown,
7176
}
7277
// InstanceDir validates the instName but does not check whether the instance exists
7378
instDir, err := InstanceDir(instName)

templates/default.yaml

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ disk: null
5656

5757
# Expose host directories to the guest, the mount point might be accessible from all UIDs in the guest
5858
# "location" can use these template variables: {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
59-
# "mountPoint" can use these template variables: {{.Home}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
59+
# "mountPoint" can use these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
6060
# 🟢 Builtin default: [] (Mount nothing)
6161
# 🔵 This file: Mount the home as read-only, /tmp/lima as writable
6262
mounts:
@@ -205,7 +205,7 @@ containerd:
205205

206206
# Provisioning scripts need to be idempotent because they might be called
207207
# multiple times, e.g. when the host VM is being restarted.
208-
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
208+
# The scripts can use the following template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
209209
# 🟢 Builtin default: []
210210
# provision:
211211
# # `system` is executed with root privileges
@@ -248,7 +248,7 @@ containerd:
248248

249249
# Probe scripts to check readiness.
250250
# The scripts run in user mode. They must start with a '#!' line.
251-
# The scripts can use the following template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
251+
# The scripts can use the following template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}
252252
# 🟢 Builtin default: []
253253
# probes:
254254
# # Only `readiness` probes are supported right now.
@@ -407,7 +407,7 @@ networks:
407407
# - guestSocket: "/run/user/{{.UID}}/my.sock"
408408
# hostSocket: mysocket
409409
# # default: reverse: false
410-
# # "guestSocket" can include these template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
410+
# # "guestSocket" can include these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
411411
# # "hostSocket" can include {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
412412
# # "reverse" can only be used for unix sockets right now, not for tcp sockets.
413413
# # Put sockets into "{{.Dir}}/sock" to avoid collision with Lima internal sockets!
@@ -427,7 +427,7 @@ networks:
427427
# - guest: "/etc/myconfig.cfg"
428428
# host: "{{.Dir}}/copied-from-guest/myconfig"
429429
# # deleteOnStop: false
430-
# # "guest" can include these template variables: {{.Home}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
430+
# # "guest" can include these template variables: {{.Home}}, {{.Name}}, {{.Hostname}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
431431
# # "host" can include {{.Home}}, {{.Dir}}, {{.Name}}, {{.UID}}, {{.User}}, and {{.Param.Key}}.
432432
# # "deleteOnStop" will delete the file from the host when the instance is stopped.
433433

0 commit comments

Comments
 (0)