-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The qemuimg package provides now only qemuimg.Convert(). I plan to add qemuimg.Create(), qemuimg.Info(), qemuimg.Map(), and qemuimg.Compare(). This makes the code nicer to work with, but adds a test only dependency. The qcow2reader tests use now qemu2reader_test package, so the dependency should be built only for tests. The qemuimg test package will also be useful for other project using this library, since testing code using the library typically requires creating, converting and comparing qcow2 images. Signed-off-by: Nir Soffer <[email protected]>
- Loading branch information
Showing
2 changed files
with
55 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package qemuimg | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"os/exec" | ||
) | ||
|
||
type CompressionType string | ||
type Format string | ||
|
||
const ( | ||
// Compression types. | ||
CompressionNone = CompressionType("") | ||
CompressionZlib = CompressionType("zlib") | ||
CompressionZstd = CompressionType("zstd") | ||
|
||
// Image formats. | ||
FormatQcow2 = Format("qcow2") | ||
FormatRaw = Format("raw") | ||
) | ||
|
||
func Convert(src, dst string, dstFormat Format, compressionType CompressionType) error { | ||
args := []string{"convert", "-O", string(dstFormat)} | ||
if compressionType != CompressionNone { | ||
args = append(args, "-c", "-o", "compression_type="+string(compressionType)) | ||
} | ||
args = append(args, src, dst) | ||
cmd := exec.Command("qemu-img", args...) | ||
|
||
var stderr bytes.Buffer | ||
cmd.Stderr = &stderr | ||
|
||
if err := cmd.Run(); err != nil { | ||
// Return qemu-img stderr instead of the unhelpful default error (exited | ||
// with status 1). | ||
if _, ok := err.(*exec.ExitError); ok { | ||
return errors.New(stderr.String()) | ||
} | ||
return err | ||
} | ||
return nil | ||
} |