-
Notifications
You must be signed in to change notification settings - Fork 10
optimize image copy on windows #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lstocchi
wants to merge
2
commits into
crc-org:main
Choose a base branch
from
lstocchi:i69
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//go:build darwin | ||
|
||
package imagepullers | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
|
||
"github.com/lima-vm/go-qcow2reader" | ||
"github.com/lima-vm/go-qcow2reader/convert" | ||
"github.com/lima-vm/go-qcow2reader/image" | ||
"github.com/lima-vm/go-qcow2reader/image/qcow2" | ||
"github.com/lima-vm/go-qcow2reader/image/raw" | ||
) | ||
|
||
func imageExtension(vmType define.VMType, _ string) string { | ||
return "." + vmType.ImageFormat().Kind() | ||
} | ||
|
||
func doCopyFile(src *os.File, dest string) error { | ||
srcImg, err := qcow2reader.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcImg.Close() | ||
|
||
switch srcImg.Type() { | ||
case raw.Type: | ||
return copyFile(src, dest) | ||
case qcow2.Type: | ||
return convertToRaw(srcImg, dest) | ||
lstocchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
default: | ||
return fmt.Errorf("%s format not supported for conversion to raw", srcImg.Type()) | ||
} | ||
} | ||
|
||
func convertToRaw(srcImg image.Image, dest string) error { | ||
destF, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
defer destF.Close() | ||
|
||
if err := srcImg.Readable(); err != nil { | ||
return fmt.Errorf("source image is not readable: %w", err) | ||
} | ||
|
||
return convert.Convert(destF, srcImg, convert.Options{}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//go:build !windows && !darwin | ||
|
||
package imagepullers | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
) | ||
lstocchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
func imageExtension(_ define.VMType, sourceURI string) string { | ||
return filepath.Ext(sourceURI) | ||
} | ||
|
||
func doCopyFile(src *os.File, dest string) error { | ||
return copyFile(src, dest) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//go:build windows | ||
|
||
package imagepullers | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
func imageExtension(vmType define.VMType, sourceURI string) string { | ||
switch vmType { | ||
case define.WSLVirt: | ||
ext := filepath.Ext(sourceURI) | ||
if ext == ".wsl" { | ||
return ".wsl" | ||
} | ||
return ".tar.gz" | ||
default: | ||
return filepath.Ext(sourceURI) | ||
} | ||
} | ||
|
||
func doCopyFile(src *os.File, dest string) error { | ||
binary, err := exec.LookPath("robocopy") | ||
if err != nil { | ||
logrus.Debugf("warning: failed to get robocopy binary: %v. Falling back to default file copy between %s and %s\n", err, src.Name(), dest) | ||
return copyFile(src, dest) | ||
} | ||
|
||
srcDir, srcFile := filepath.Split(src.Name()) | ||
destDir := filepath.Dir(dest) | ||
|
||
lstocchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Executes the robocopy command with options optimized for a fast, single-file copy. | ||
// /J: Copies using unbuffered I/O (better for large files). | ||
// /MT: Enables multi-threaded copying for improved performance. | ||
// /R:0: Sets retries on failed copies to 0 to avoid long waits. | ||
// /IS: Includes Same files, which forces an overwrite even if the destination | ||
// file appears identical to the source. | ||
cmd := exec.Command(binary, "/J", "/MT", "/R:0", "/IS", srcDir, destDir, srcFile) | ||
if logrus.IsLevelEnabled(logrus.DebugLevel) { | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
} else { | ||
cmd.Stdout = io.Discard | ||
cmd.Stderr = io.Discard | ||
} | ||
|
||
err = cmd.Run() | ||
if err != nil { | ||
// robocopy does not use classic exit codes like linux commands, so we need to check for specific errors | ||
// Only exit code 8 is considered an error, all other exit codes are considered successful with exceptions | ||
// see https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy | ||
var exitErr *exec.ExitError | ||
if errors.As(err, &exitErr) { | ||
exitCode := exitErr.ExitCode() | ||
if exitCode >= 8 { | ||
return fmt.Errorf("failed to run robocopy: %w", err) | ||
} | ||
} else { | ||
return fmt.Errorf("failed to run robocopy: %w", err) | ||
} | ||
} | ||
|
||
if err := os.Remove(dest); err != nil && !errors.Is(err, os.ErrNotExist) { | ||
return fmt.Errorf("failed to remove existing destination file: %w", err) | ||
} | ||
|
||
err = os.Rename(filepath.Join(destDir, srcFile), dest) | ||
if err != nil { | ||
return fmt.Errorf("failed to rename file: %w", err) | ||
} | ||
lstocchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.