Skip to content

Commit ab4b4b5

Browse files
authored
Merge pull request #2798 from nirs/go-qcow2reader-convert
Replace copySparse with go-qcow2reader/convert
2 parents d750162 + 0221c11 commit ab4b4b5

File tree

4 files changed

+23
-45
lines changed

4 files changed

+23
-45
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/google/go-cmp v0.6.0
2626
github.com/google/yamlfmt v0.13.0
2727
github.com/invopop/jsonschema v0.12.0
28-
github.com/lima-vm/go-qcow2reader v0.2.1
28+
github.com/lima-vm/go-qcow2reader v0.3.0
2929
github.com/lima-vm/sshocker v0.3.4
3030
github.com/mattn/go-isatty v0.0.20
3131
github.com/mattn/go-shellwords v1.0.12

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
175175
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
176176
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
177177
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
178-
github.com/lima-vm/go-qcow2reader v0.2.1 h1:aeusQHn4m+uNhf3Z4oBUaE+xpMsNHjNCMkKUbhEgbXU=
179-
github.com/lima-vm/go-qcow2reader v0.2.1/go.mod h1:e3p29BzLT8hNh4jbLezdFAHU4eBijf0bm5GilStCRKE=
178+
github.com/lima-vm/go-qcow2reader v0.3.0 h1:7nDU29oXPNymm/UMMfqN8bYXejlhxEYIHO76RRUzbbc=
179+
github.com/lima-vm/go-qcow2reader v0.3.0/go.mod h1:e3p29BzLT8hNh4jbLezdFAHU4eBijf0bm5GilStCRKE=
180180
github.com/lima-vm/sshocker v0.3.4 h1:5rn6vMkfqwZSZiBW+Udo505OIRhPB4xbLUDdEnFgWwI=
181181
github.com/lima-vm/sshocker v0.3.4/go.mod h1:QT4c7XNmeQTv79h5/8EgiS7U51B9BLenlXV7idCY0tE=
182182
github.com/linuxkit/virtsock v0.0.0-20220523201153-1a23e78aa7a2 h1:DZMFueDbfz6PNc1GwDRA8+6lBx1TB9UnxDQliCqR73Y=

pkg/nativeimgutil/nativeimgutil.go

+8-42
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
package nativeimgutil
33

44
import (
5-
"bytes"
6-
"errors"
75
"fmt"
86
"io"
97
"os"
@@ -12,6 +10,7 @@ import (
1210
"github.com/containerd/continuity/fs"
1311
"github.com/docker/go-units"
1412
"github.com/lima-vm/go-qcow2reader"
13+
"github.com/lima-vm/go-qcow2reader/convert"
1514
"github.com/lima-vm/go-qcow2reader/image/qcow2"
1615
"github.com/lima-vm/go-qcow2reader/image/raw"
1716
"github.com/lima-vm/lima/pkg/progressbar"
@@ -75,17 +74,20 @@ func ConvertToRaw(source, dest string, size *int64, allowSourceWithBackingFile b
7574
}
7675

7776
// Copy
78-
srcImgR := io.NewSectionReader(srcImg, 0, srcImg.Size())
7977
bar, err := progressbar.New(srcImg.Size())
8078
if err != nil {
8179
return err
8280
}
83-
const bufSize = 1024 * 1024
81+
conv, err := convert.New(convert.Options{})
82+
if err != nil {
83+
return err
84+
}
8485
bar.Start()
85-
copied, err := copySparse(destTmpF, bar.NewProxyReader(srcImgR), bufSize)
86+
pra := progressbar.ProxyReaderAt{ReaderAt: srcImg, Bar: bar}
87+
err = conv.Convert(destTmpF, &pra, srcImg.Size())
8688
bar.Finish()
8789
if err != nil {
88-
return fmt.Errorf("failed to call copySparse(), bufSize=%d, copied=%d: %w", bufSize, copied, err)
90+
return fmt.Errorf("failed to convert image: %w", err)
8991
}
9092

9193
// Resize
@@ -128,42 +130,6 @@ func convertRawToRaw(source, dest string, size *int64) error {
128130
return nil
129131
}
130132

131-
func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
132-
var (
133-
n int64
134-
eof bool
135-
)
136-
137-
zeroBuf := make([]byte, bufSize)
138-
buf := make([]byte, bufSize)
139-
for !eof {
140-
rN, rErr := r.Read(buf)
141-
if rErr != nil {
142-
eof = errors.Is(rErr, io.EOF)
143-
if !eof {
144-
return n, fmt.Errorf("failed to read: %w", rErr)
145-
}
146-
}
147-
// TODO: qcow2reader should have a method to notify whether buf is zero
148-
if bytes.Equal(buf[:rN], zeroBuf[:rN]) {
149-
n += int64(rN)
150-
} else {
151-
wN, wErr := w.WriteAt(buf[:rN], n)
152-
if wN > 0 {
153-
n += int64(wN)
154-
}
155-
if wErr != nil {
156-
return n, fmt.Errorf("failed to write: %w", wErr)
157-
}
158-
if wN != rN {
159-
return n, fmt.Errorf("read %d, but wrote %d bytes", rN, wN)
160-
}
161-
}
162-
}
163-
164-
return n, nil
165-
}
166-
167133
func MakeSparse(f *os.File, n int64) error {
168134
if _, err := f.Seek(n, io.SeekStart); err != nil {
169135
return err

pkg/progressbar/progressbar.go

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package progressbar
22

33
import (
4+
"io"
45
"os"
56
"time"
67

@@ -9,6 +10,17 @@ import (
910
"github.com/sirupsen/logrus"
1011
)
1112

13+
type ProxyReaderAt struct {
14+
io.ReaderAt
15+
Bar *pb.ProgressBar
16+
}
17+
18+
func (r *ProxyReaderAt) ReadAt(p []byte, off int64) (int, error) {
19+
n, err := r.ReaderAt.ReadAt(p, off)
20+
r.Bar.Add(n)
21+
return n, err
22+
}
23+
1224
func New(size int64) (*pb.ProgressBar, error) {
1325
bar := pb.New64(size)
1426

0 commit comments

Comments
 (0)