Skip to content

Commit

Permalink
Merge pull request #1463 from pendo324/refactor-vz-disk-conversion
Browse files Browse the repository at this point in the history
refactor: vz disk conversion
  • Loading branch information
AkihiroSuda authored Apr 8, 2023
2 parents f0282b2 + fd0ab46 commit f35b3f1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pkg/qemu/imgutil/imgutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/vz/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 15 additions & 6 deletions pkg/vz/vm_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/Code-Hex/vz/v3"
Expand Down Expand Up @@ -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)
}
}

Expand Down

0 comments on commit f35b3f1

Please sign in to comment.