-
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.
Merge pull request #37 from nirs/qemuimg-package
Extract qemuimg test package
- 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 | ||
} |