Skip to content

Commit

Permalink
fix: changes persistent disk path to prevent collisions (#154)
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Berning <bernings@amazon.com>

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 <bernings@amazon.com>
  • Loading branch information
sam-berning committed Jan 14, 2023
1 parent 8ee5763 commit 3eb66a2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
11 changes: 10 additions & 1 deletion pkg/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"io/fs"
"os"
"path"

limaStore "github.com/lima-vm/lima/pkg/store"
Expand Down Expand Up @@ -121,7 +122,15 @@ func (m *userDataDiskManager) createLimaDisk() error {
func (m *userDataDiskManager) attachPersistentDiskToLimaDisk() error {
limaPath := fmt.Sprintf("%s/_disks/%s/datadisk", m.finch.LimaHomePath(), diskName)
if !m.persistentDiskExists() {
err := m.fs.Rename(limaPath, m.finch.UserDataDiskPath(m.homeDir))
disksDir := path.Dir(m.finch.UserDataDiskPath(m.homeDir))
_, err := m.fs.Stat(disksDir)
if os.IsNotExist(err) {
err := m.fs.MkdirAll(disksDir, 0o755)
if err != nil {
return fmt.Errorf("could not create persistent disk directory: %w", err)
}
}
err = m.fs.Rename(limaPath, m.finch.UserDataDiskPath(m.homeDir))
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestUserDataDiskManager_InitializeUserDataDisk(t *testing.T) {
cmd.EXPECT().Run().Return(nil)

dfs.EXPECT().Stat(finch.UserDataDiskPath(homeDir)).Return(nil, fs.ErrNotExist)
dfs.EXPECT().Stat(path.Dir(finch.UserDataDiskPath(homeDir))).Return(nil, nil)
dfs.EXPECT().Rename(limaPath, finch.UserDataDiskPath(homeDir)).Return(nil)

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

dfs.EXPECT().Stat(finch.UserDataDiskPath(homeDir)).Return(nil, fs.ErrNotExist)
dfs.EXPECT().Stat(path.Dir(finch.UserDataDiskPath(homeDir))).Return(nil, nil)
dfs.EXPECT().Rename(limaPath, finch.UserDataDiskPath(homeDir)).Return(nil)

dfs.EXPECT().Stat(limaPath).Return(nil, fs.ErrNotExist)
Expand Down
15 changes: 13 additions & 2 deletions pkg/path/finch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package path

import (
"crypto/sha256"
"fmt"

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

// UserDataDiskPath returns the path to the permanent storage location of the Finch
// user data disk.
func (Finch) UserDataDiskPath(homeDir string) string {
return fmt.Sprintf("%s/.finch/.datadisk", homeDir)
func (w Finch) UserDataDiskPath(homeDir string) string {
return fmt.Sprintf("%s/.finch/.disks/%s", homeDir, w.generatePathSum())
}

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

// LimaInstancePath returns the path to the Lima instance of the Finch VM.
func (w Finch) LimaInstancePath() string {
return fmt.Sprintf("%s/lima/data/finch", w)
}

// LimactlPath returns the limactl path.
func (w Finch) LimactlPath() string {
return fmt.Sprintf("%s/lima/bin/limactl", w)
Expand Down Expand Up @@ -60,6 +66,11 @@ func (w Finch) LimaSSHPrivateKeyPath() string {
return fmt.Sprintf("%s/lima/data/_config/user", w)
}

func (w Finch) generatePathSum() string {
sum := sha256.Sum256([]byte(w.LimaInstancePath()))
return fmt.Sprintf("%x", sum[:8])
}

// FinchFinderDeps provides all the dependencies FindFinch needs to find Finch.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/finch_finder_deps.go -package=mocks -mock_names FinchFinderDeps=FinchFinderDeps . FinchFinderDeps
Expand Down
9 changes: 8 additions & 1 deletion pkg/path/finch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestFinch_UserDataDiskPath(t *testing.T) {
t.Parallel()

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

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

func TestFinch_LimaInstancePath(t *testing.T) {
t.Parallel()

res := mockFinch.LimaInstancePath()
assert.Equal(t, res, "mock_finch/lima/data/finch")
}

func TestFinch_LimactlPath(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 3eb66a2

Please sign in to comment.