-
Notifications
You must be signed in to change notification settings - Fork 2
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
clone image before creating vm #51
Merged
Merged
Changes from all commits
Commits
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 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 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 |
---|---|---|
@@ -1,23 +1,92 @@ | ||
package imagepullers | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/env" | ||
) | ||
|
||
type NoopImagePuller struct { | ||
localPath string | ||
localPath *define.VMFile | ||
sourceURI string | ||
vmType define.VMType | ||
machineName string | ||
} | ||
|
||
func NewNoopImagePuller(machineName string, vmType define.VMType) *NoopImagePuller { | ||
return &NoopImagePuller{ | ||
machineName: machineName, | ||
vmType: vmType, | ||
} | ||
} | ||
|
||
var _ define.ImagePuller = &NoopImagePuller{} | ||
func (puller *NoopImagePuller) SetSourceURI(sourcePath string) { | ||
puller.sourceURI = sourcePath | ||
} | ||
|
||
func (puller *NoopImagePuller) SetSourceURI(localPath string) { | ||
puller.localPath = localPath | ||
func imageExtension(sourceURI string) string { | ||
if strings.HasSuffix(sourceURI, ".tar.gz") { | ||
return "tar.gz" | ||
} | ||
return filepath.Ext(sourceURI) | ||
} | ||
|
||
func (puller *NoopImagePuller) LocalPath() (*define.VMFile, error) { | ||
return define.NewMachineFile(puller.localPath, nil) | ||
// if localPath has already been calculated returns it | ||
if puller.localPath != nil { | ||
return puller.localPath, nil | ||
} | ||
|
||
// calculate and set localPath | ||
dirs, err := env.GetMachineDirs(puller.vmType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
vmFile, err := dirs.DataDir.AppendToNewVMFile(fmt.Sprintf("%s-%s.%s", puller.machineName, puller.vmType.String(), imageExtension(puller.sourceURI)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
puller.localPath = vmFile | ||
return vmFile, nil | ||
} | ||
|
||
/* | ||
The noopImageBuilder does not actually download any image as the image is already stored locally. | ||
The download func is used to make a copy of the source image so that the user image is not modified | ||
by macadam | ||
*/ | ||
func (puller *NoopImagePuller) Download() error { | ||
return nil | ||
localPath, err := puller.LocalPath() | ||
if err != nil { | ||
return err | ||
} | ||
return copyFile(puller.sourceURI, localPath.Path) | ||
} | ||
|
||
func copyFile(src, dst string) error { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
bufferedWriter := bufio.NewWriter(out) | ||
defer bufferedWriter.Flush() | ||
|
||
_, err = io.Copy(bufferedWriter, in) | ||
return err | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We eventually want to use
CloneFile
on macOS with a fallback to https://github.com/containers/podman/blob/main/pkg/machine/compression/sparse_file_writer.go, andqemu-img create
with a backing file on linux, but this can be done as a follow up to this PR.