-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathdisk.go
68 lines (56 loc) · 1.36 KB
/
disk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package store
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/lima-vm/lima/pkg/qemu/imgutil"
"github.com/lima-vm/lima/pkg/store/filenames"
)
type Disk struct {
Name string `json:"name"`
Size int64 `json:"size"`
Dir string `json:"dir"`
Instance string `json:"instance"`
InstanceDir string `json:"instanceDir"`
MountPoint string `json:"mountPoint"`
}
func InspectDisk(diskName string) (*Disk, error) {
disk := &Disk{
Name: diskName,
}
diskDir, err := DiskDir(diskName)
if err != nil {
return nil, err
}
disk.Dir = diskDir
dataDisk := filepath.Join(diskDir, filenames.DataDisk)
if _, err := os.Stat(dataDisk); err != nil {
return nil, err
}
info, err := imgutil.GetInfo(dataDisk)
if err != nil {
return nil, err
}
disk.Size = info.VSize
instDir, err := os.Readlink(filepath.Join(diskDir, filenames.InUseBy))
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
} else {
disk.Instance = filepath.Base(instDir)
disk.InstanceDir = instDir
}
disk.MountPoint = fmt.Sprintf("/mnt/lima-%s", diskName)
return disk, nil
}
func (d *Disk) Lock(instanceDir string) error {
inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
return os.Symlink(instanceDir, inUseBy)
}
func (d *Disk) Unlock() error {
inUseBy := filepath.Join(d.Dir, filenames.InUseBy)
return os.Remove(inUseBy)
}