@@ -2,24 +2,13 @@ package imagepullers
2
2
3
3
import (
4
4
"bufio"
5
- "errors"
6
5
"fmt"
7
6
"io"
8
7
"os"
9
- "os/exec"
10
- "path/filepath"
11
-
12
- "github.com/sirupsen/logrus"
13
8
14
9
"github.com/containers/podman/v5/pkg/machine/define"
15
10
16
11
"github.com/containers/podman/v5/pkg/machine/env"
17
-
18
- "github.com/lima-vm/go-qcow2reader"
19
- "github.com/lima-vm/go-qcow2reader/convert"
20
- "github.com/lima-vm/go-qcow2reader/image"
21
- "github.com/lima-vm/go-qcow2reader/image/qcow2"
22
- "github.com/lima-vm/go-qcow2reader/image/raw"
23
12
)
24
13
25
14
type NoopImagePuller struct {
@@ -40,21 +29,6 @@ func (puller *NoopImagePuller) SetSourceURI(sourcePath string) {
40
29
puller .sourceURI = sourcePath
41
30
}
42
31
43
- func imageExtension (vmType define.VMType , sourceURI string ) string {
44
- switch vmType {
45
- case define .WSLVirt :
46
- ext := filepath .Ext (sourceURI )
47
- if ext == ".wsl" {
48
- return ".wsl"
49
- }
50
- return ".tar.gz"
51
- case define .QemuVirt , define .HyperVVirt :
52
- return filepath .Ext (sourceURI )
53
- default :
54
- return "." + vmType .ImageFormat ().Kind ()
55
- }
56
- }
57
-
58
32
func (puller * NoopImagePuller ) LocalPath () (* define.VMFile , error ) {
59
33
// if localPath has already been calculated returns it
60
34
if puller .localPath != nil {
@@ -85,97 +59,14 @@ func (puller *NoopImagePuller) Download() error {
85
59
if err != nil {
86
60
return err
87
61
}
88
- return doCopyFile (puller .sourceURI , localPath .Path , puller .vmType )
89
- }
90
-
91
- func doCopyFile (src , dest string , vmType define.VMType ) error {
92
- srcF , err := os .Open (src )
93
- if err != nil {
94
- return err
95
- }
96
- defer srcF .Close ()
97
-
98
- switch vmType {
99
- case define .AppleHvVirt , define .LibKrun :
100
- return copyFileMac (srcF , dest )
101
- case define .WSLVirt , define .HyperVVirt :
102
- return copyFileWin (srcF , dest )
103
- default :
104
- return copyFile (srcF , dest )
105
- }
106
- }
107
-
108
- func copyFileMac (src * os.File , dest string ) error {
109
- srcImg , err := qcow2reader .Open (src )
110
- if err != nil {
111
- return err
112
- }
113
- defer srcImg .Close ()
114
-
115
- switch srcImg .Type () {
116
- case raw .Type :
117
- // if the image is raw it performs a simple copy
118
- return copyFile (src , dest )
119
- case qcow2 .Type :
120
- // if the image is qcow2 it performs a conversion to raw
121
- return convertToRaw (srcImg , dest )
122
- default :
123
- return fmt .Errorf ("%s format not supported for conversion to raw" , srcImg .Type ())
124
- }
125
- }
126
-
127
- func copyFileWin (srcF * os.File , dest string ) error {
128
- binary , err := exec .LookPath ("robocopy" )
129
- if err != nil {
130
- logrus .Debugf ("warning: failed to get robocopy binary: %v. Falling back to default file copy between %s and %s\n " , err , srcF .Name (), dest )
131
- return copyFile (srcF , dest )
132
- }
133
-
134
- srcDir , srcFile := filepath .Split (srcF .Name ())
135
- destDir := filepath .Dir (dest )
136
-
137
- cmd := exec .Command (binary , "/J" , "/MT" , "/R:0" , srcDir , destDir , srcFile )
138
- if logrus .IsLevelEnabled (logrus .DebugLevel ) {
139
- cmd .Stdout = os .Stdout
140
- cmd .Stderr = os .Stderr
141
- }
142
-
143
- err = cmd .Run ()
144
- if err != nil {
145
- // robocopy does not use classic exit codes like linux commands, so we need to check for specific errors
146
- // Only exit code 8 is considered an error, all other exit codes are considered successful with exceptions
147
- // see https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
148
- var exitErr * exec.ExitError
149
- if errors .As (err , & exitErr ) {
150
- exitCode := exitErr .ExitCode ()
151
- if exitCode >= 8 {
152
- return fmt .Errorf ("failed to run robocopy: %w" , err )
153
- }
154
- } else {
155
- return fmt .Errorf ("failed to run robocopy: %w" , err )
156
- }
157
- }
158
62
159
- err = os .Rename (filepath .Join (destDir , srcFile ), dest )
160
- if err != nil {
161
- return fmt .Errorf ("failed to rename file: %w" , err )
162
- }
163
-
164
- return nil
165
- }
166
-
167
- func convertToRaw (srcImg image.Image , dest string ) error {
168
- destF , err := os .Create (dest )
63
+ src , err := os .Open (puller .sourceURI )
169
64
if err != nil {
170
65
return err
171
66
}
172
- defer destF .Close ()
173
-
174
- if err := srcImg .Readable (); err != nil {
175
- return fmt .Errorf ("source image is not readable: %w" , err )
176
- }
67
+ defer src .Close ()
177
68
178
- return convert . Convert ( destF , srcImg , convert. Options {} )
69
+ return doCopyFile ( src , localPath . Path )
179
70
}
180
71
181
72
func copyFile (src * os.File , dest string ) error {
0 commit comments