Skip to content

Commit 25ebf13

Browse files
committed
clone image before creating vm
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]>
1 parent 5b7cb5d commit 25ebf13

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

cmd/macadam/init.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,13 @@ func initMachine(cmd *cobra.Command, args []string) error {
186186
return fmt.Errorf("invalid name %q: %w", machineName, ldefine.RegexError)
187187
}
188188

189+
puller, err := imagepullers.NewNoopImagePuller(machineName, provider.VMType())
190+
if err != nil {
191+
return err
192+
}
193+
189194
initOpts := macadam.DefaultInitOpts(machineName)
190-
initOpts.ImagePuller = &imagepullers.NoopImagePuller{}
195+
initOpts.ImagePuller = puller
191196
initOpts.ImagePuller.SetSourceURI(diskImage)
192197
initOpts.Name = machineName
193198
initOpts.Image = diskImage

pkg/imagepullers/noop.go

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,86 @@
11
package imagepullers
22

33
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
411
"github.com/containers/podman/v5/pkg/machine/define"
12+
13+
"github.com/containers/podman/v5/pkg/machine/env"
514
)
615

716
type NoopImagePuller struct {
8-
localPath string
17+
localPath *define.VMFile
18+
sourceURI string
19+
vmType define.VMType
20+
machineDirs *define.MachineDirs
21+
machineName string
22+
}
23+
24+
func NewNoopImagePuller(machineName string, vmType define.VMType) (*NoopImagePuller, error) {
25+
dirs, err := env.GetMachineDirs(vmType)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
puller := NoopImagePuller{
31+
machineDirs: dirs,
32+
machineName: machineName,
33+
vmType: vmType,
34+
}
35+
36+
return &puller, nil
937
}
1038

11-
var _ define.ImagePuller = &NoopImagePuller{}
39+
func (puller *NoopImagePuller) SetSourceURI(sourcePath string) {
40+
puller.sourceURI = sourcePath
41+
}
1242

13-
func (puller *NoopImagePuller) SetSourceURI(localPath string) {
14-
puller.localPath = localPath
43+
func imageExtension(sourceURI string) string {
44+
if strings.HasSuffix(sourceURI, ".tar.gz") {
45+
return "tar.gz"
46+
}
47+
return filepath.Ext(sourceURI)
1548
}
1649

1750
func (puller *NoopImagePuller) LocalPath() (*define.VMFile, error) {
18-
return define.NewMachineFile(puller.localPath, nil)
51+
vmFile, err := puller.machineDirs.DataDir.AppendToNewVMFile(fmt.Sprintf("%s-%s.%s", puller.machineName, puller.vmType.String(), imageExtension(puller.sourceURI)), nil)
52+
if err != nil {
53+
return nil, err
54+
}
55+
puller.localPath = vmFile
56+
return vmFile, nil
1957
}
2058

59+
/*
60+
The noopImageBuilder does not actually download any image as the image is already stored locally.
61+
The download func is used to make a copy of the source image so that the user image is not modified
62+
by macadam
63+
*/
2164
func (puller *NoopImagePuller) Download() error {
22-
return nil
65+
return copyFile(puller.sourceURI, puller.localPath.Path)
66+
}
67+
68+
func copyFile(src, dst string) error {
69+
in, err := os.Open(src)
70+
if err != nil {
71+
return err
72+
}
73+
defer in.Close()
74+
75+
out, err := os.Create(dst)
76+
if err != nil {
77+
return err
78+
}
79+
defer out.Close()
80+
81+
bufferedWriter := bufio.NewWriter(out)
82+
defer bufferedWriter.Flush()
83+
84+
_, err = io.Copy(bufferedWriter, in)
85+
return err
2386
}

0 commit comments

Comments
 (0)