2
0
mirror of https://github.com/linka-cloud/d2vm.git synced 2024-11-29 19:16:24 +00:00

d2vm/run: hetzner: convert image to raw if needed

Signed-off-by: Adphi <philippe.adrien.nousse@gmail.com>
This commit is contained in:
Adphi 2022-08-09 20:17:50 +02:00
parent 5049361f2f
commit 3f7b5e2805
Signed by: adphi
GPG Key ID: 46BE4062DB2397FF

View File

@ -22,6 +22,7 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"path/filepath"
"runtime" "runtime"
"time" "time"
@ -31,6 +32,8 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/svenwiltink/sparsecat" "github.com/svenwiltink/sparsecat"
exec2 "go.linka.cloud/d2vm/pkg/exec"
) )
const ( const (
@ -80,7 +83,21 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
return err return err
} }
if i.Format != "raw" { if i.Format != "raw" {
return fmt.Errorf("image format must be raw") logrus.Warnf("image format is %s, expected raw", i.Format)
rawPath := filepath.Join(os.TempDir(), "d2vm", "run", filepath.Base(imgPath)+".raw")
if err := os.MkdirAll(filepath.Dir(rawPath), 0755); err != nil {
return err
}
defer os.RemoveAll(rawPath)
logrus.Infof("converting image to raw: %s", rawPath)
if err := exec2.Run(ctx, "qemu-img", "convert", "-O", "raw", imgPath, rawPath); err != nil {
return err
}
imgPath = rawPath
i, err = ImgInfo(ctx, imgPath)
if err != nil {
return err
}
} }
src, err := os.Open(imgPath) src, err := os.Open(imgPath)
if err != nil { if err != nil {