Skip to content

Commit

Permalink
refactor: vz disk conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Alvarez <[email protected]>
  • Loading branch information
pendo324 committed Apr 6, 2023
1 parent f0282b2 commit d4ee80f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 13 additions & 1 deletion pkg/qemu/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit d4ee80f

Please sign in to comment.