From 6c23c42f80c5bc2bc813e5b2393e49ceab33aedc Mon Sep 17 00:00:00 2001 From: Adphi Date: Thu, 4 Aug 2022 18:09:11 +0200 Subject: [PATCH] output defaults to raw move image using sparsecat print command in debug mode Signed-off-by: Adphi --- builder.go | 2 +- cmd/d2vm/build.go | 2 +- cmd/d2vm/convert.go | 2 +- convert.go | 7 +++++-- go.mod | 1 + go.sum | 2 ++ pkg/exec/exec.go | 1 + 7 files changed, 12 insertions(+), 5 deletions(-) diff --git a/builder.go b/builder.go index 8b599e4..b205481 100644 --- a/builder.go +++ b/builder.go @@ -153,7 +153,7 @@ func NewBuilder(ctx context.Context, workdir, imgTag, disk string, size int64, o b := &builder{ osRelease: osRelease, img: img, - diskRaw: filepath.Join(workdir, disk+".raw"), + diskRaw: filepath.Join(workdir, disk+".d2vm.raw"), diskOut: filepath.Join(workdir, disk+"."+format), format: f, size: size, diff --git a/cmd/d2vm/build.go b/cmd/d2vm/build.go index 06cd361..b44c997 100644 --- a/cmd/d2vm/build.go +++ b/cmd/d2vm/build.go @@ -65,7 +65,7 @@ func init() { buildCmd.Flags().StringVarP(&file, "file", "f", "", "Name of the Dockerfile") buildCmd.Flags().StringArrayVar(&buildArgs, "build-arg", nil, "Set build-time variables") - buildCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format. Supported formats: "+strings.Join(d2vm.OutputFormats(), " ")) + buildCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format, raw will be used if none. Supported formats: "+strings.Join(d2vm.OutputFormats(), " ")) buildCmd.Flags().StringVarP(&password, "password", "p", "root", "Root user password") buildCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size") buildCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") diff --git a/cmd/d2vm/convert.go b/cmd/d2vm/convert.go index 0500d61..9f9e057 100644 --- a/cmd/d2vm/convert.go +++ b/cmd/d2vm/convert.go @@ -93,7 +93,7 @@ func parseSize(s string) (int64, error) { func init() { convertCmd.Flags().BoolVar(&pull, "pull", false, "Always pull docker image") - convertCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format. Supported formats: "+strings.Join(d2vm.OutputFormats(), " ")) + convertCmd.Flags().StringVarP(&output, "output", "o", output, "The output image, the extension determine the image format, raw will be used if none. Supported formats: "+strings.Join(d2vm.OutputFormats(), " ")) convertCmd.Flags().StringVarP(&password, "password", "p", "root", "The Root user password") convertCmd.Flags().StringVarP(&size, "size", "s", "10G", "The output image size") convertCmd.Flags().BoolVarP(&debug, "debug", "d", false, "Enable Debug output") diff --git a/convert.go b/convert.go index d18c9c9..378a734 100644 --- a/convert.go +++ b/convert.go @@ -17,13 +17,13 @@ package d2vm import ( "context" "fmt" - "io" "os" "path/filepath" "strings" "github.com/google/uuid" "github.com/sirupsen/logrus" + "github.com/svenwiltink/sparsecat" "go.linka.cloud/d2vm/pkg/docker" ) @@ -64,6 +64,9 @@ func Convert(ctx context.Context, img string, size int64, password string, outpu logrus.Infof("creating vm image") format := strings.TrimPrefix(filepath.Ext(output), ".") + if format == "" { + format = "raw" + } b, err := NewBuilder(ctx, tmpPath, imgUUID, "", size, r, format) if err != nil { return err @@ -92,7 +95,7 @@ func MoveFile(sourcePath, destPath string) error { return fmt.Errorf("failed to open dest file: %s", err) } defer outputFile.Close() - _, err = io.Copy(outputFile, inputFile) + _, err = sparsecat.NewDecoder(sparsecat.NewEncoder(inputFile)).WriteTo(outputFile) inputFile.Close() if err != nil { return fmt.Errorf("failed to write to output file: %s", err) diff --git a/go.mod b/go.mod index e82d403..242d11c 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/svenwiltink/sparsecat v1.0.0 // indirect github.com/vbatts/tar-split v0.11.2 // indirect go.uber.org/atomic v1.7.0 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect diff --git a/go.sum b/go.sum index fc832e2..64e566d 100644 --- a/go.sum +++ b/go.sum @@ -743,6 +743,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= +github.com/svenwiltink/sparsecat v1.0.0 h1:SBDEIImxhD//8MskqodFR9VcixWKkZAPAW35nmA4vcw= +github.com/svenwiltink/sparsecat v1.0.0/go.mod h1:TdtvJbeTZcd+3cMQpttW6MJl/iPGZT0GHmckep0hoxU= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20180916011248-d98352740cb2/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= diff --git a/pkg/exec/exec.go b/pkg/exec/exec.go index 35b33fb..49506fa 100644 --- a/pkg/exec/exec.go +++ b/pkg/exec/exec.go @@ -38,6 +38,7 @@ func SetDebug(debug bool) { } func RunStdout(ctx context.Context, c string, args ...string) error { + fmt.Printf("\n$ %s %s\n", c, strings.Join(args, " ")) cmd := exec.CommandContext(ctx, c, args...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr