Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix copySparse for empty qcow2 disk #2255

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ jobs:
# For non-Homebrew we have to support an old release of Go
go-version: ["1.21.x", "1.22.x"]
steps:
- name: Install test dependencies
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends qemu-utils
- uses: actions/checkout@v4
with:
fetch-depth: 1
Expand Down
13 changes: 11 additions & 2 deletions pkg/nativeimgutil/nativeimgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,13 @@ func convertRawToRaw(source, dest string, size *int64) error {
}

func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
var n int64
var (
n int64
eof, hasWrites bool
)

zeroBuf := make([]byte, bufSize)
buf := make([]byte, bufSize)
var eof bool
for !eof {
rN, rErr := r.Read(buf)
if rErr != nil {
Expand All @@ -143,6 +146,7 @@ func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
// no need to ftruncate here
n += int64(rN)
} else {
hasWrites = true
wN, wErr := w.Write(buf)
if wN > 0 {
n += int64(wN)
Expand All @@ -155,6 +159,11 @@ func copySparse(w *os.File, r io.Reader, bufSize int64) (int64, error) {
}
}
}

// Ftruncate must be run if the file contains only zeros
if !hasWrites {
return n, MakeSparse(w, n)
}
return n, nil
}

Expand Down
85 changes: 85 additions & 0 deletions pkg/nativeimgutil/nativeimgutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nativeimgutil

import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"

"gotest.tools/v3/assert"
)

func createImg(name, format, size string) error {
return exec.Command("qemu-img", "create", name, "-f", format, size).Run()
}

func TestConvertToRaw(t *testing.T) {
vasileknik76 marked this conversation as resolved.
Show resolved Hide resolved
_, err := exec.LookPath("qemu-img")
if err != nil {
t.Skipf("qemu-img does not seem installed: %v", err)
}
tmpDir := t.TempDir()

qcowImage, err := os.Create(filepath.Join(tmpDir, "qcow.img"))
assert.NilError(t, err)
defer qcowImage.Close()
err = createImg(qcowImage.Name(), "qcow2", "1M")
assert.NilError(t, err)

rawImage, err := os.Create(filepath.Join(tmpDir, "raw.img"))
assert.NilError(t, err)
defer rawImage.Close()
err = createImg(rawImage.Name(), "raw", "1M")
assert.NilError(t, err)

rawImageExtended, err := os.Create(filepath.Join(tmpDir, "raw_extended.img"))
assert.NilError(t, err)
defer rawImageExtended.Close()
err = createImg(rawImageExtended.Name(), "raw", "2M")
assert.NilError(t, err)

t.Run("qcow without backing file", func(t *testing.T) {
resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_"))
assert.NilError(t, err)

err = ConvertToRaw(qcowImage.Name(), resultImage, nil, false)
assert.NilError(t, err)
assertFileEquals(t, rawImage.Name(), resultImage)
})

t.Run("qcow with backing file", func(t *testing.T) {
resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_"))
assert.NilError(t, err)

err = ConvertToRaw(qcowImage.Name(), resultImage, nil, true)
assert.NilError(t, err)
assertFileEquals(t, rawImage.Name(), resultImage)
})

t.Run("qcow with extra size", func(t *testing.T) {
resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_"))
assert.NilError(t, err)
size := int64(2_097_152) // 2mb
err = ConvertToRaw(qcowImage.Name(), resultImage, &size, false)
assert.NilError(t, err)
assertFileEquals(t, rawImageExtended.Name(), resultImage)
})

t.Run("raw", func(t *testing.T) {
resultImage := filepath.Join(tmpDir, strings.ReplaceAll(t.Name(), string(os.PathSeparator), "_"))
assert.NilError(t, err)

err = ConvertToRaw(rawImage.Name(), resultImage, nil, false)
assert.NilError(t, err)
assertFileEquals(t, rawImage.Name(), resultImage)
})
}

func assertFileEquals(t *testing.T, expected, actual string) {
expectedContent, err := os.ReadFile(expected)
assert.NilError(t, err)
actualContent, err := os.ReadFile(actual)
assert.NilError(t, err)
assert.DeepEqual(t, expectedContent, actualContent)
}
Loading