From fd0ab467dde6b70972b886471f7c5d7251980f75 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 | 4 ++-- pkg/vz/disk.go | 2 +- pkg/vz/vm_darwin.go | 21 +++++++++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pkg/qemu/imgutil/imgutil.go b/pkg/qemu/imgutil/imgutil.go index 4ad885fb9e5..0496618d449 100644 --- a/pkg/qemu/imgutil/imgutil.go +++ b/pkg/qemu/imgutil/imgutil.go @@ -15,9 +15,9 @@ type Info struct { VSize int64 `json:"virtual-size,omitempty"` } -func QCOWToRaw(source string, dest string) error { +func ConvertToRaw(source string, dest string) error { var stdout, stderr bytes.Buffer - cmd := exec.Command("qemu-img", "convert", source, dest) + 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/disk.go b/pkg/vz/disk.go index 1513c0558a0..9fb8fad24e7 100644 --- a/pkg/vz/disk.go +++ b/pkg/vz/disk.go @@ -63,7 +63,7 @@ func EnsureDisk(driver *driver.BaseDriver) error { if out, err := cmd.CombinedOutput(); err != nil { return fmt.Errorf("failed to run %v: %q: %w", cmd.Args, string(out), err) } - if err = imgutil.QCOWToRaw(diffDisk, diffDisk); err != nil { + if err = imgutil.ConvertToRaw(diffDisk, diffDisk); err != nil { return fmt.Errorf("cannot convert qcow2 to raw for vz driver") } return nil diff --git a/pkg/vz/vm_darwin.go b/pkg/vz/vm_darwin.go index d7bc92665c1..240a9850670 100644 --- a/pkg/vz/vm_darwin.go +++ b/pkg/vz/vm_darwin.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "syscall" "github.com/Code-Hex/vz/v3" @@ -356,20 +357,28 @@ func attachDisks(driver *driver.BaseDriver, vmConfig *vz.VirtualMachineConfigura return fmt.Errorf("failed to run detect disk format %q: %q", diskName, err) } if extraDiskFormat != "raw" { + if strings.Contains(extraDiskFormat, string(os.PathSeparator)) { + return fmt.Errorf( + "failed to convert disk %q to raw for vz driver because extraDiskFormat %q contains a path separator %q", + diskName, + extraDiskFormat, + os.PathSeparator, + ) + } 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) } }