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