Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: yaroslavborbat <yaroslav.752@gmail.com>
  • Loading branch information
yaroslavborbat committed Sep 4, 2024
1 parent 9a78c4a commit 45875fd
Showing 1 changed file with 51 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
diff --git a/pkg/common/format.go b/pkg/common/format.go
new file mode 100644
index 000000000..06497e582
--- /dev/null
+++ b/pkg/common/format.go
@@ -0,0 +1,22 @@
+package common
+
+import "os"
+
+func GetFormat(path string) (string, error) {
+ const (
+ formatQcow2 = "qcow2"
+ formatRaw = "raw"
+ )
+ info, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return formatQcow2, nil
+ }
+ return "", err
+ }
+ mode := info.Mode()
+ if mode&os.ModeDevice != 0 {
+ return formatRaw, nil
+ }
+ return formatQcow2, nil
+}
diff --git a/pkg/image/qemu.go b/pkg/image/qemu.go
index 651fb5fc8..1151a2121 100644
index 651fb5fc8..adcfe8824 100644
--- a/pkg/image/qemu.go
+++ b/pkg/image/qemu.go
@@ -61,6 +61,7 @@ type ImgInfo struct {
Expand Down Expand Up @@ -61,8 +89,28 @@ index 651fb5fc8..1151a2121 100644
// convertQuantityToQemuSize translates a quantity string into a Qemu compatible string.
func convertQuantityToQemuSize(size resource.Quantity) string {
int64Size, asInt := size.AsInt64()
@@ -274,13 +312,17 @@ func CreateBlankImage(dest string, size resource.Quantity, preallocate bool) err

// CreateBlankImage creates a raw image with a given size
func (o *qemuOperations) CreateBlankImage(dest string, size resource.Quantity, preallocate bool) error {
+ format, err := common.GetFormat(dest)
+ if err != nil {
+ return err
+ }
klog.V(3).Infof("image size is %s", size.String())
- args := []string{"create", "-f", "raw", dest, convertQuantityToQemuSize(size)}
+ args := []string{"create", "-f", format, dest, convertQuantityToQemuSize(size)}
if preallocate {
klog.V(1).Infof("Added preallocation")
args = append(args, []string{"-o", "preallocation=falloc"}...)
}
- _, err := qemuExecFunction(nil, nil, "qemu-img", args...)
+ _, err = qemuExecFunction(nil, nil, "qemu-img", args...)
if err != nil {
os.Remove(dest)
return errors.Wrap(err, fmt.Sprintf("could not create raw image with size %s in %s", size.String(), dest))
diff --git a/pkg/importer/data-processor.go b/pkg/importer/data-processor.go
index ca7b2e853..82d6fccb9 100644
index ca7b2e853..ec24b3b64 100644
--- a/pkg/importer/data-processor.go
+++ b/pkg/importer/data-processor.go
@@ -276,8 +276,13 @@ func (dp *DataProcessor) convert(url *url.URL) (ProcessingPhase, error) {
Expand All @@ -71,7 +119,7 @@ index ca7b2e853..82d6fccb9 100644
}
- klog.V(3).Infoln("Converting to Raw")
- err = qemuOperations.ConvertToRawStream(url, dp.dataFile, dp.preallocation)
+ format, err := dp.getFormat(dp.dataFile)
+ format, err := common.GetFormat(dp.dataFile)
+ if err != nil {
+ return ProcessingPhaseError, errors.Wrap(err, "Unable to get format")
+ }
Expand All @@ -81,29 +129,3 @@ index ca7b2e853..82d6fccb9 100644
if err != nil {
return ProcessingPhaseError, errors.Wrap(err, "Conversion to Raw failed")
}
@@ -286,6 +291,25 @@ func (dp *DataProcessor) convert(url *url.URL) (ProcessingPhase, error) {
return ProcessingPhaseResize, nil
}

+func (dp *DataProcessor) getFormat(path string) (string, error) {
+ const (
+ formatQcow2 = "qcow2"
+ formatRaw = "raw"
+ )
+ info, err := os.Stat(path)
+ if err != nil {
+ if os.IsNotExist(err) {
+ return formatQcow2, nil
+ }
+ return "", err
+ }
+ mode := info.Mode()
+ if mode&os.ModeDevice != 0 {
+ return formatRaw, nil
+ }
+ return formatQcow2, nil
+}
+
func (dp *DataProcessor) resize() (ProcessingPhase, error) {
size, _ := getAvailableSpaceBlockFunc(dp.dataFile)
klog.V(3).Infof("Available space in dataFile: %d", size)

0 comments on commit 45875fd

Please sign in to comment.