Skip to content

Commit

Permalink
clone image before creating vm
Browse files Browse the repository at this point in the history
This PR changes the behavior of the noopImagePuller. As this puller does
not actually pull any image as it is used when an image is already in
the local system, it just clones the image in a dedicated folder so that
the original image does not get alterated when started by macadam.

Signed-off-by: lstocchi <[email protected]>
  • Loading branch information
lstocchi committed Feb 17, 2025
1 parent 5b7cb5d commit 25ebf13
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cmd/macadam/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@ func initMachine(cmd *cobra.Command, args []string) error {
return fmt.Errorf("invalid name %q: %w", machineName, ldefine.RegexError)
}

puller, err := imagepullers.NewNoopImagePuller(machineName, provider.VMType())
if err != nil {
return err
}

initOpts := macadam.DefaultInitOpts(machineName)
initOpts.ImagePuller = &imagepullers.NoopImagePuller{}
initOpts.ImagePuller = puller
initOpts.ImagePuller.SetSourceURI(diskImage)
initOpts.Name = machineName
initOpts.Image = diskImage
Expand Down
75 changes: 69 additions & 6 deletions pkg/imagepullers/noop.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,86 @@
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
machineDirs *define.MachineDirs
machineName string
}

func NewNoopImagePuller(machineName string, vmType define.VMType) (*NoopImagePuller, error) {
dirs, err := env.GetMachineDirs(vmType)
if err != nil {
return nil, err
}

puller := NoopImagePuller{
machineDirs: dirs,
machineName: machineName,
vmType: vmType,
}

return &puller, nil
}

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)
vmFile, err := puller.machineDirs.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
return copyFile(puller.sourceURI, puller.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
}

0 comments on commit 25ebf13

Please sign in to comment.