Skip to content

Commit 3eb66a2

Browse files
authored
fix: changes persistent disk path to prevent collisions (#154)
Signed-off-by: Sam Berning <[email protected]> Issue #, if available: *Description of changes:* It was possible for two different finch VMs in different locations to use the same persistent disk, causing the disk to corrupt. This changes the path to a sha256 sum of the installation location. *Testing done:* Unit testing, manual testing to confirm the disk is created in the right location. - [x] I've reviewed the guidance in CONTRIBUTING.md #### License Acceptance By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. Signed-off-by: Sam Berning <[email protected]>
1 parent 8ee5763 commit 3eb66a2

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

pkg/disk/disk.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"errors"
1010
"fmt"
1111
"io/fs"
12+
"os"
1213
"path"
1314

1415
limaStore "github.com/lima-vm/lima/pkg/store"
@@ -121,7 +122,15 @@ func (m *userDataDiskManager) createLimaDisk() error {
121122
func (m *userDataDiskManager) attachPersistentDiskToLimaDisk() error {
122123
limaPath := fmt.Sprintf("%s/_disks/%s/datadisk", m.finch.LimaHomePath(), diskName)
123124
if !m.persistentDiskExists() {
124-
err := m.fs.Rename(limaPath, m.finch.UserDataDiskPath(m.homeDir))
125+
disksDir := path.Dir(m.finch.UserDataDiskPath(m.homeDir))
126+
_, err := m.fs.Stat(disksDir)
127+
if os.IsNotExist(err) {
128+
err := m.fs.MkdirAll(disksDir, 0o755)
129+
if err != nil {
130+
return fmt.Errorf("could not create persistent disk directory: %w", err)
131+
}
132+
}
133+
err = m.fs.Rename(limaPath, m.finch.UserDataDiskPath(m.homeDir))
125134
if err != nil {
126135
return err
127136
}

pkg/disk/disk_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func TestUserDataDiskManager_InitializeUserDataDisk(t *testing.T) {
5858
cmd.EXPECT().Run().Return(nil)
5959

6060
dfs.EXPECT().Stat(finch.UserDataDiskPath(homeDir)).Return(nil, fs.ErrNotExist)
61+
dfs.EXPECT().Stat(path.Dir(finch.UserDataDiskPath(homeDir))).Return(nil, nil)
6162
dfs.EXPECT().Rename(limaPath, finch.UserDataDiskPath(homeDir)).Return(nil)
6263

6364
dfs.EXPECT().Stat(limaPath).Return(nil, fs.ErrNotExist)
@@ -89,6 +90,7 @@ func TestUserDataDiskManager_InitializeUserDataDisk(t *testing.T) {
8990
dfs.EXPECT().ReadlinkIfPossible(limaPath).Return("", nil)
9091

9192
dfs.EXPECT().Stat(finch.UserDataDiskPath(homeDir)).Return(nil, fs.ErrNotExist)
93+
dfs.EXPECT().Stat(path.Dir(finch.UserDataDiskPath(homeDir))).Return(nil, nil)
9294
dfs.EXPECT().Rename(limaPath, finch.UserDataDiskPath(homeDir)).Return(nil)
9395

9496
dfs.EXPECT().Stat(limaPath).Return(nil, fs.ErrNotExist)

pkg/path/finch.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package path
66

77
import (
8+
"crypto/sha256"
89
"fmt"
910

1011
"github.com/runfinch/finch/pkg/system"
@@ -20,15 +21,20 @@ func (Finch) ConfigFilePath(homeDir string) string {
2021

2122
// UserDataDiskPath returns the path to the permanent storage location of the Finch
2223
// user data disk.
23-
func (Finch) UserDataDiskPath(homeDir string) string {
24-
return fmt.Sprintf("%s/.finch/.datadisk", homeDir)
24+
func (w Finch) UserDataDiskPath(homeDir string) string {
25+
return fmt.Sprintf("%s/.finch/.disks/%s", homeDir, w.generatePathSum())
2526
}
2627

2728
// LimaHomePath returns the path that should be set to LIMA_HOME for Finch.
2829
func (w Finch) LimaHomePath() string {
2930
return fmt.Sprintf("%s/lima/data", w)
3031
}
3132

33+
// LimaInstancePath returns the path to the Lima instance of the Finch VM.
34+
func (w Finch) LimaInstancePath() string {
35+
return fmt.Sprintf("%s/lima/data/finch", w)
36+
}
37+
3238
// LimactlPath returns the limactl path.
3339
func (w Finch) LimactlPath() string {
3440
return fmt.Sprintf("%s/lima/bin/limactl", w)
@@ -60,6 +66,11 @@ func (w Finch) LimaSSHPrivateKeyPath() string {
6066
return fmt.Sprintf("%s/lima/data/_config/user", w)
6167
}
6268

69+
func (w Finch) generatePathSum() string {
70+
sum := sha256.Sum256([]byte(w.LimaInstancePath()))
71+
return fmt.Sprintf("%x", sum[:8])
72+
}
73+
6374
// FinchFinderDeps provides all the dependencies FindFinch needs to find Finch.
6475
//
6576
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/finch_finder_deps.go -package=mocks -mock_names FinchFinderDeps=FinchFinderDeps . FinchFinderDeps

pkg/path/finch_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestFinch_UserDataDiskPath(t *testing.T) {
2727
t.Parallel()
2828

2929
res := mockFinch.UserDataDiskPath("homeDir")
30-
assert.Equal(t, res, "homeDir/.finch/.datadisk")
30+
assert.Equal(t, res, fmt.Sprintf("homeDir/.finch/.disks/%s", mockFinch.generatePathSum()))
3131
}
3232

3333
func TestFinch_LimaHomePath(t *testing.T) {
@@ -37,6 +37,13 @@ func TestFinch_LimaHomePath(t *testing.T) {
3737
assert.Equal(t, res, "mock_finch/lima/data")
3838
}
3939

40+
func TestFinch_LimaInstancePath(t *testing.T) {
41+
t.Parallel()
42+
43+
res := mockFinch.LimaInstancePath()
44+
assert.Equal(t, res, "mock_finch/lima/data/finch")
45+
}
46+
4047
func TestFinch_LimactlPath(t *testing.T) {
4148
t.Parallel()
4249

0 commit comments

Comments
 (0)