vm-import-controller/pkg/qemu/qemu.go
Volker Theile ebf60d5ec9 VM Import Controller uses too much memory during QCOW2 conversion phase
- Create the volume image using RAW disk format instead of QCOW2, so no conversion is required after downloading. This will reduce memory consumption.
- Download and write the image file in chunks (32KiB by default), so the whole file doesn't need to be downloaded completely and stored in memory before it is written to disk.
- Fix a variable name shadowing issue.
- Improve logging.

Related to: https://github.com/harvester/harvester/issues/6674

Signed-off-by: Volker Theile <vtheile@suse.com>
2024-10-09 17:43:32 +08:00

52 lines
1.2 KiB
Go

package qemu
import (
"fmt"
"io"
"os/exec"
"syscall"
"github.com/sirupsen/logrus"
)
const defaultCommand = "qemu-wrapper.sh"
func ConvertVMDKtoRAW(source, target string) error {
logrus.Infof("converting image %s to %s", source, target)
args := []string{"convert", "-f", "vmdk", "-O", "raw", source, target}
return runCommand(defaultCommand, args...)
}
func createVMDK(path string, size string) error {
args := []string{"create", "-f", "vmdk", path, size}
return runCommand(defaultCommand, args...)
}
func runCommand(command string, args ...string) error {
cmd := exec.Command(command, args...)
cmd.SysProcAttr = &syscall.SysProcAttr{}
stderr, err := cmd.StderrPipe()
if err != nil {
return fmt.Errorf("error creating stderr pipe: %v", err)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Errorf("error creating stdout pipe: %v", err)
}
if err := cmd.Start(); err != nil {
return fmt.Errorf("error in command start: %v", err)
}
errOut, _ := io.ReadAll(stderr)
out, err := io.ReadAll(stdout)
err = cmd.Wait()
if err != nil {
return fmt.Errorf("error in command: %s, %s", command, errOut)
}
logrus.Debugf("image command complete: %v", string(out))
return nil
}