@@ -19,6 +19,7 @@ import (
1919 "github.com/docker/go-units"
2020 "github.com/lima-vm/go-qcow2reader"
2121 "github.com/lima-vm/go-qcow2reader/convert"
22+ "github.com/lima-vm/go-qcow2reader/image"
2223 "github.com/lima-vm/go-qcow2reader/image/asif"
2324 "github.com/lima-vm/go-qcow2reader/image/qcow2"
2425 "github.com/lima-vm/go-qcow2reader/image/raw"
@@ -42,17 +43,10 @@ func roundUp(size int64) int64 {
4243 return sectors * sectorSize
4344}
4445
45- type targetImageType string
46-
47- const (
48- imageRaw targetImageType = "raw"
49- imageASIF targetImageType = "ASIF"
50- )
51-
5246// convertTo converts a source disk into a raw or ASIF disk.
5347// source and dest may be same.
5448// convertTo is a NOP if source == dest, and no resizing is needed.
55- func convertTo (destType targetImageType , source , dest string , size * int64 , allowSourceWithBackingFile bool ) error {
49+ func convertTo (destType image. Type , source , dest string , size * int64 , allowSourceWithBackingFile bool ) error {
5650 srcF , err := os .Open (source )
5751 if err != nil {
5852 return err
@@ -71,7 +65,7 @@ func convertTo(destType targetImageType, source, dest string, size *int64, allow
7165 if err = srcF .Close (); err != nil {
7266 return err
7367 }
74- if destType == imageRaw {
68+ if destType == raw . Type {
7569 return convertRawToRaw (source , dest , size )
7670 }
7771 case qcow2 .Type :
@@ -85,7 +79,7 @@ func convertTo(destType targetImageType, source, dest string, size *int64, allow
8579 }
8680 }
8781 case asif .Type :
88- if destType == imageASIF {
82+ if destType == asif . Type {
8983 return convertASIFToASIF (source , dest , size )
9084 }
9185 return fmt .Errorf ("conversion from ASIF to %q is not supported" , destType )
@@ -103,10 +97,10 @@ func convertTo(destType targetImageType, source, dest string, size *int64, allow
10397 attachedDevice string
10498 )
10599 switch destType {
106- case imageRaw :
100+ case raw . Type :
107101 destTmpF , err = os .CreateTemp (filepath .Dir (dest ), filepath .Base (dest )+ ".lima-*.tmp" )
108102 destTmp = destTmpF .Name ()
109- case imageASIF :
103+ case asif . Type :
110104 // destTmp != destTmpF.Name() because destTmpF is mounted ASIF device file.
111105 randomBase := fmt .Sprintf ("%s.lima-%d.tmp.asif" , filepath .Base (dest ), rand .UintN (math .MaxUint ))
112106 destTmp = filepath .Join (filepath .Dir (dest ), randomBase )
@@ -150,7 +144,7 @@ func convertTo(destType targetImageType, source, dest string, size *int64, allow
150144 return err
151145 }
152146 // Detach ASIF device
153- if destType == imageASIF {
147+ if destType == asif . Type {
154148 err := asifutil .DetachASIF (attachedDevice )
155149 if err != nil {
156150 return fmt .Errorf ("failed to detach ASIF image %q: %w" , attachedDevice , err )
@@ -230,7 +224,7 @@ func (n *NativeImageUtil) CreateDisk(_ context.Context, disk string, size int64)
230224
231225// ConvertToRaw converts a disk image to raw format.
232226func (n * NativeImageUtil ) ConvertToRaw (_ context.Context , source , dest string , size * int64 , allowSourceWithBackingFile bool ) error {
233- return convertTo (imageRaw , source , dest , size , allowSourceWithBackingFile )
227+ return convertTo (raw . Type , source , dest , size , allowSourceWithBackingFile )
234228}
235229
236230// ResizeDisk resizes an existing disk image to the specified size.
@@ -246,5 +240,5 @@ func (n *NativeImageUtil) MakeSparse(_ context.Context, f *os.File, offset int64
246240
247241// ConvertToASIF converts a disk image to ASIF format.
248242func (n * NativeImageUtil ) ConvertToASIF (_ context.Context , source , dest string , size * int64 , allowSourceWithBackingFile bool ) error {
249- return convertTo (imageASIF , source , dest , size , allowSourceWithBackingFile )
243+ return convertTo (asif . Type , source , dest , size , allowSourceWithBackingFile )
250244}
0 commit comments