Skip to content

Commit 27d46e8

Browse files
lstocchicfergeau
authored andcommitted
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 23945e2 commit 27d46e8

File tree

2 files changed

+78
-7
lines changed

2 files changed

+78
-7
lines changed

cmd/macadam/init.go

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

189+
puller := imagepullers.NewNoopImagePuller(machineName, provider.VMType())
190+
189191
initOpts := macadam.DefaultInitOpts(machineName)
190-
initOpts.ImagePuller = &imagepullers.NoopImagePuller{}
192+
initOpts.ImagePuller = puller
191193
initOpts.ImagePuller.SetSourceURI(diskImage)
192194
initOpts.Name = machineName
193195
initOpts.Image = diskImage

pkg/imagepullers/noop.go

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,92 @@
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+
machineName string
21+
}
22+
23+
func NewNoopImagePuller(machineName string, vmType define.VMType) *NoopImagePuller {
24+
return &NoopImagePuller{
25+
machineName: machineName,
26+
vmType: vmType,
27+
}
928
}
1029

11-
var _ define.ImagePuller = &NoopImagePuller{}
30+
func (puller *NoopImagePuller) SetSourceURI(sourcePath string) {
31+
puller.sourceURI = sourcePath
32+
}
1233

13-
func (puller *NoopImagePuller) SetSourceURI(localPath string) {
14-
puller.localPath = localPath
34+
func imageExtension(sourceURI string) string {
35+
if strings.HasSuffix(sourceURI, ".tar.gz") {
36+
return "tar.gz"
37+
}
38+
return filepath.Ext(sourceURI)
1539
}
1640

1741
func (puller *NoopImagePuller) LocalPath() (*define.VMFile, error) {
18-
return define.NewMachineFile(puller.localPath, nil)
42+
// if localPath has already been calculated returns it
43+
if puller.localPath != nil {
44+
return puller.localPath, nil
45+
}
46+
47+
// calculate and set localPath
48+
dirs, err := env.GetMachineDirs(puller.vmType)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
vmFile, err := dirs.DataDir.AppendToNewVMFile(fmt.Sprintf("%s-%s.%s", puller.machineName, puller.vmType.String(), imageExtension(puller.sourceURI)), nil)
54+
if err != nil {
55+
return nil, err
56+
}
57+
puller.localPath = vmFile
58+
return vmFile, nil
1959
}
2060

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

0 commit comments

Comments
 (0)