Skip to content

Commit

Permalink
d2vm/run: hetzner: do not use sparsecat if not on linux
Browse files Browse the repository at this point in the history
Signed-off-by: Adphi <[email protected]>
  • Loading branch information
Adphi committed Sep 10, 2022
1 parent 96026b8 commit 7ee4e25
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions cmd/d2vm/run/hetzner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net"
"os"
"os/exec"
"runtime"
"time"

"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -194,7 +195,13 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
logrus.Infof("writing image to %s", vmBlockPath)
done := make(chan struct{})
defer close(done)
pr := newProgressReader(sparsecat.NewEncoder(src))
var r io.Reader
if runtime.GOOS == "linux" {
r = sparsecat.NewEncoder(src)
} else {
r = src
}
pr := newProgressReader(r)
wses.Stdin = pr
go func() {
tk := time.NewTicker(time.Second)
Expand All @@ -203,7 +210,7 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
select {
case <-tk.C:
b := pr.Progress()
logrus.Infof("%s / %d%% transfered ( %s/s)", humanize.Bytes(uint64(b)), int(float64(b)/float64(i.VirtualSize)*100), humanize.Bytes(uint64(b-last)))
logrus.Infof("%s / %d%% transfered (%s/s)", humanize.Bytes(uint64(b)), int(float64(b)/float64(i.VirtualSize)*100), humanize.Bytes(uint64(b-last)))
last = b
case <-ctx.Done():
logrus.Warnf("context cancelled")
Expand All @@ -214,7 +221,13 @@ func runHetzner(ctx context.Context, imgPath string, stdin io.Reader, stderr io.
}
}
}()
if b, err := wses.CombinedOutput(fmt.Sprintf("%s -r -disable-sparse-target -of %s", sparsecatPath, vmBlockPath)); err != nil {
var cmd string
if runtime.GOOS == "linux" {
cmd = fmt.Sprintf("%s -r -disable-sparse-target -of %s", sparsecatPath, vmBlockPath)
} else {
cmd = fmt.Sprintf("dd of=%s", vmBlockPath)
}
if b, err := wses.CombinedOutput(cmd); err != nil {
return fmt.Errorf("%v: %s", err, string(b))
}
return nil
Expand Down

0 comments on commit 7ee4e25

Please sign in to comment.