Skip to content
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 1 commit into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/macadam/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ func initMachine(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid name %q: %w", machineName, ldefine.RegexError)
}

puller := imagepullers.NewNoopImagePuller(machineName, provider.VMType())

initOpts := macadam.DefaultInitOpts(machineName)
initOpts.ImagePuller = &imagepullers.NoopImagePuller{}
initOpts.ImagePuller = puller
initOpts.ImagePuller.SetSourceURI(diskImage)
initOpts.Name = machineName
initOpts.Image = diskImage
Expand Down
81 changes: 75 additions & 6 deletions pkg/imagepullers/noop.go
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)
Copy link
Collaborator

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, and qemu-img create with a backing file on linux, but this can be done as a follow up to this PR.

return err
}
Loading