Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/cidata/cidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ func templateArgs(ctx context.Context, bootScripts bool, instDir, name string, i
if err != nil {
return nil, err
}
for i, f := range instConfig.Mounts {
tag := fmt.Sprintf("mount%d", i)
for _, f := range instConfig.Mounts {
tag := limayaml.MountTag(f.Location, *f.MountPoint)
options := "defaults"
switch fstype {
case "9p", "virtiofs":
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/krunkit/krunkit_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ func Cmdline(inst *limatype.Instance) (*exec.Cmd, error) {

// File sharing commands
if *inst.Config.MountType == limatype.VIRTIOFS {
for i, mount := range inst.Config.Mounts {
for _, mount := range inst.Config.Mounts {
if _, err := os.Stat(mount.Location); errors.Is(err, os.ErrNotExist) {
if err := os.MkdirAll(mount.Location, 0o750); err != nil {
return nil, err
}
}
tag := fmt.Sprintf("mount%d", i)
tag := limayaml.MountTag(mount.Location, *mount.MountPoint)
mountArg := fmt.Sprintf("virtio-fs,sharedDir=%s,mountTag=%s", mount.Location, tag)
args = append(args, "--device", mountArg)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/driver/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ func Cmdline(ctx context.Context, cfg Config) (exe string, args []string, err er

if *y.MountType == limatype.NINEP || *y.MountType == limatype.VIRTIOFS {
for i, f := range y.Mounts {
tag := fmt.Sprintf("mount%d", i)
tag := limayaml.MountTag(f.Location, *f.MountPoint)
if err := os.MkdirAll(f.Location, 0o755); err != nil {
return "", nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/driver/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func attachDisplay(inst *limatype.Instance, vmConfig *vz.VirtualMachineConfigura
func attachFolderMounts(inst *limatype.Instance, vmConfig *vz.VirtualMachineConfiguration) error {
var mounts []vz.DirectorySharingDeviceConfiguration
if *inst.Config.MountType == limatype.VIRTIOFS {
for i, mount := range inst.Config.Mounts {
for _, mount := range inst.Config.Mounts {
if _, err := os.Stat(mount.Location); errors.Is(err, os.ErrNotExist) {
err := os.MkdirAll(mount.Location, 0o750)
if err != nil {
Expand All @@ -592,7 +592,7 @@ func attachFolderMounts(inst *limatype.Instance, vmConfig *vz.VirtualMachineConf
return err
}

tag := fmt.Sprintf("mount%d", i)
tag := limayaml.MountTag(mount.Location, *mount.MountPoint)
config, err := vz.NewVirtioFileSystemDeviceConfiguration(tag)
if err != nil {
return err
Expand Down
7 changes: 7 additions & 0 deletions pkg/limayaml/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func MACAddress(uniqueID string) string {
return hw.String()
}

// MountTag generates a stable mount tag from location and mountPoint.
// Both paths are hashed to handle the same location mounted to multiple mount points.
func MountTag(location, mountPoint string) string {
sha := sha256.Sum256([]byte(location + "\x00" + mountPoint))
return fmt.Sprintf("lima-%x", sha[0:8])
}

func defaultCPUs() int {
const x = 4
if hostCPUs := runtime.NumCPU(); hostCPUs < x {
Expand Down
45 changes: 45 additions & 0 deletions pkg/limayaml/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,48 @@ func TestStaticPortForwarding(t *testing.T) {
})
}
}

func TestMountTag(t *testing.T) {
tests := []struct {
name string
location string
mountPoint string
}{
{"home directory", "/Users/testuser", "/Users/testuser"},
{"nested path", "/Users/testuser/Development/project", "/mnt/project"},
{"root path", "/", "/mnt/root"},
{"tmp directory", "/tmp/lima", "/tmp/lima"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tag := MountTag(tt.location, tt.mountPoint)
assert.Assert(t, len(tag) > 5 && tag[:5] == "lima-")
assert.Assert(t, len(tag) <= 36)
assert.Equal(t, tag, MountTag(tt.location, tt.mountPoint))
})
}
}

func TestMountTagUniqueness(t *testing.T) {
mounts := []struct{ location, mountPoint string }{
{"/Users/user1", "/Users/user1"},
{"/Users/user2", "/Users/user2"},
{"/home/user1", "/home/user1"},
{"/mnt/data", "/mnt/data"},
{"/tmp/lima", "/tmp/lima"},
}
tags := make(map[string]bool)
for _, m := range mounts {
tag := MountTag(m.location, m.mountPoint)
assert.Assert(t, !tags[tag], "tag collision: %s", tag)
tags[tag] = true
}
}

func TestMountTagDuplicateLocation(t *testing.T) {
location := "/Users/testuser"
tag1 := MountTag(location, "/home/user")
tag2 := MountTag(location, "/mnt/home-writable")
assert.Assert(t, tag1 != tag2)
}
Loading