|
5 | 5 | package nativeimgutil
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "errors" |
8 | 9 | "fmt"
|
9 | 10 | "io"
|
| 11 | + "io/fs" |
10 | 12 | "os"
|
11 | 13 | "path/filepath"
|
12 | 14 |
|
13 |
| - "github.com/containerd/continuity/fs" |
| 15 | + containerdfs "github.com/containerd/continuity/fs" |
14 | 16 | "github.com/docker/go-units"
|
15 | 17 | "github.com/lima-vm/go-qcow2reader"
|
16 | 18 | "github.com/lima-vm/go-qcow2reader/convert"
|
17 | 19 | "github.com/lima-vm/go-qcow2reader/image/qcow2"
|
18 | 20 | "github.com/lima-vm/go-qcow2reader/image/raw"
|
19 | 21 | "github.com/lima-vm/lima/pkg/progressbar"
|
| 22 | + "github.com/lima-vm/lima/pkg/store/filenames" |
20 | 23 | "github.com/sirupsen/logrus"
|
21 | 24 | )
|
22 | 25 |
|
| 26 | +// CreateRawDataDisk creates an empty raw data disk. |
| 27 | +func CreateRawDataDisk(dir string, size int) error { |
| 28 | + dataDisk := filepath.Join(dir, filenames.DataDisk) |
| 29 | + if _, err := os.Stat(dataDisk); err == nil || !errors.Is(err, fs.ErrNotExist) { |
| 30 | + return err |
| 31 | + } |
| 32 | + f, err := os.Create(dataDisk) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + defer f.Close() |
| 37 | + return f.Truncate(int64(size)) |
| 38 | +} |
| 39 | + |
| 40 | +// ResizeRawDataDisk resizes a raw data disk. |
| 41 | +func ResizeRawDataDisk(dir string, size int) error { |
| 42 | + dataDisk := filepath.Join(dir, filenames.DataDisk) |
| 43 | + return os.Truncate(dataDisk, int64(size)) |
| 44 | +} |
| 45 | + |
23 | 46 | // ConvertToRaw converts a source disk into a raw disk.
|
24 | 47 | // source and dest may be same.
|
25 | 48 | // ConvertToRaw is a NOP if source == dest, and no resizing is needed.
|
@@ -109,7 +132,7 @@ func ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile b
|
109 | 132 | func convertRawToRaw(source, dest string, size *int64) error {
|
110 | 133 | if source != dest {
|
111 | 134 | // continuity attempts clonefile
|
112 |
| - if err := fs.CopyFile(dest, source); err != nil { |
| 135 | + if err := containerdfs.CopyFile(dest, source); err != nil { |
113 | 136 | return fmt.Errorf("failed to copy %q into %q: %w", source, dest, err)
|
114 | 137 | }
|
115 | 138 | }
|
|
0 commit comments