From d4ee80fcbf552d79c2085f05c473db1143fa6be6 Mon Sep 17 00:00:00 2001 From: Justin Alvarez Date: Thu, 6 Apr 2023 13:05:04 -0400 Subject: [PATCH] refactor: vz disk conversion Signed-off-by: Justin Alvarez --- pkg/qemu/imgutil/imgutil.go | 14 +++++++++++++- pkg/vz/vm_darwin.go | 12 ++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/qemu/imgutil/imgutil.go b/pkg/qemu/imgutil/imgutil.go index 4ad885fb9e5f..0680f3b0d91b 100644 --- a/pkg/qemu/imgutil/imgutil.go +++ b/pkg/qemu/imgutil/imgutil.go @@ -17,7 +17,19 @@ type Info struct { func QCOWToRaw(source string, dest string) error { var stdout, stderr bytes.Buffer - cmd := exec.Command("qemu-img", "convert", source, dest) + cmd := exec.Command("qemu-img", "convert", "-f", "qcow2", "-O", "raw", source, dest) + cmd.Stdout = &stdout + cmd.Stderr = &stderr + if err := cmd.Run(); err != nil { + return fmt.Errorf("failed to run %v: stdout=%q, stderr=%q: %w", + cmd.Args, stdout.String(), stderr.String(), err) + } + return nil +} + +func ConvertToRaw(source string, dest string) error { + var stdout, stderr bytes.Buffer + cmd := exec.Command("qemu-img", "convert", "-O", "raw", source, dest) cmd.Stdout = &stdout cmd.Stderr = &stderr if err := cmd.Run(); err != nil { diff --git a/pkg/vz/vm_darwin.go b/pkg/vz/vm_darwin.go index d7bc92665c1f..2da4d21561d7 100644 --- a/pkg/vz/vm_darwin.go +++ b/pkg/vz/vm_darwin.go @@ -357,19 +357,19 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura } if extraDiskFormat != "raw" { rawPath := fmt.Sprintf("%s.raw", extraDiskPath) - qcow2Path := fmt.Sprintf("%s.qcow2", extraDiskPath) - if err = imgutil.QCOWToRaw(extraDiskPath, rawPath); err != nil { - return fmt.Errorf("failed to convert qcow2 disk %q to raw for vz driver: %w", diskName, err) + oldFormatPath := fmt.Sprintf("%s.%s", extraDiskPath, extraDiskFormat) + if err = imgutil.ConvertToRaw(extraDiskPath, rawPath); err != nil { + return fmt.Errorf("failed to convert %s disk %q to raw for vz driver: %w", extraDiskFormat, diskName, err) } - if err = os.Rename(extraDiskPath, qcow2Path); err != nil { + if err = os.Rename(extraDiskPath, oldFormatPath); err != nil { return fmt.Errorf("failed to rename additional disk for vz driver: %w", err) } if err = os.Rename(rawPath, extraDiskPath); err != nil { return fmt.Errorf("failed to rename additional disk for vz driver: %w", err) } - if err = os.Remove(qcow2Path); err != nil { + if err = os.Remove(oldFormatPath); err != nil { logrus.Errorf("Failed to delete unused qcow2 additional disk %q."+ - "Disk is no longer needed by Lima and it can be removed manually.", qcow2Path) + "Disk is no longer needed by Lima and it can be removed manually.", oldFormatPath) } }