@@ -9,13 +9,15 @@ import (
9
9
"net"
10
10
"os"
11
11
"os/exec"
12
+ "path/filepath"
12
13
"syscall"
13
14
"time"
14
15
15
16
"github.com/containers/common/pkg/config"
16
17
"github.com/containers/common/pkg/strongunits"
17
18
gvproxy "github.com/containers/gvisor-tap-vsock/pkg/types"
18
19
"github.com/containers/podman/v5/pkg/machine"
20
+ "github.com/containers/podman/v5/pkg/machine/cloudinit"
19
21
"github.com/containers/podman/v5/pkg/machine/define"
20
22
"github.com/containers/podman/v5/pkg/machine/ignition"
21
23
"github.com/containers/podman/v5/pkg/machine/sockets"
@@ -141,10 +143,6 @@ func GenerateSystemDFilesForVirtiofsMounts(mounts []machine.VirtIoFs) ([]ignitio
141
143
142
144
// StartGenericAppleVM is wrapped by apple provider methods and starts the vm
143
145
func StartGenericAppleVM (mc * vmconfigs.MachineConfig , cmdBinary string , bootloader vfConfig.Bootloader , endpoint string ) (func () error , func () error , error ) {
144
- var (
145
- ignitionSocket * define.VMFile
146
- )
147
-
148
146
// Add networking
149
147
netDevice , err := vfConfig .VirtioNetNew (applehvMACAddress )
150
148
if err != nil {
@@ -209,11 +207,6 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload
209
207
return nil , nil , err
210
208
}
211
209
212
- machineDataDir , err := mc .DataDir ()
213
- if err != nil {
214
- return nil , nil , err
215
- }
216
-
217
210
cmd .Args = append (cmd .Args , endpointArgs ... )
218
211
219
212
firstBoot , err := mc .IsFirstBoot ()
@@ -231,31 +224,18 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload
231
224
}
232
225
233
226
if firstBoot {
234
- // If this is the first boot of the vm, we need to add the vsock
235
- // device to vfkit so we can inject the ignition file
236
- socketName := fmt .Sprintf ("%s-%s" , mc .Name , ignitionSocketName )
237
- ignitionSocket , err = machineDataDir .AppendToNewVMFile (socketName , & socketName )
238
- if err != nil {
239
- return nil , nil , err
240
- }
241
- if err := ignitionSocket .Delete (); err != nil {
242
- logrus .Errorf ("unable to delete ignition socket: %q" , err )
227
+ var firstBootCli []string
228
+ if mc .CloudInit {
229
+ firstBootCli , err = getFirstBootAppleVMCloudInit (mc )
230
+ } else {
231
+ firstBootCli , err = getFirstBootAppleVMIgnition (mc )
243
232
}
244
233
245
- ignitionVsockDeviceCLI , err := GetIgnitionVsockDeviceAsCLI (ignitionSocket .GetPath ())
246
234
if err != nil {
247
235
return nil , nil , err
248
236
}
249
- cmd .Args = append (cmd .Args , ignitionVsockDeviceCLI ... )
250
-
251
237
logrus .Debug ("first boot detected" )
252
- logrus .Debugf ("serving ignition file over %s" , ignitionSocket .GetPath ())
253
- go func () {
254
- if err := ServeIgnitionOverSock (ignitionSocket , mc ); err != nil {
255
- logrus .Error (err )
256
- }
257
- logrus .Debug ("ignition vsock server exited" )
258
- }()
238
+ cmd .Args = append (cmd .Args , firstBootCli ... )
259
239
}
260
240
261
241
logrus .Debugf ("listening for ready on: %s" , readySocket .GetPath ())
@@ -351,6 +331,76 @@ func StartGenericAppleVM(mc *vmconfigs.MachineConfig, cmdBinary string, bootload
351
331
return cmd .Process .Release , returnFunc , nil
352
332
}
353
333
334
+ func getFirstBootAppleVMIgnition (mc * vmconfigs.MachineConfig ) ([]string , error ) {
335
+ machineDataDir , err := mc .DataDir ()
336
+ if err != nil {
337
+ return nil , err
338
+ }
339
+
340
+ // If this is the first boot of the vm, we need to add the vsock
341
+ // device to vfkit so we can inject the ignition file
342
+ socketName := fmt .Sprintf ("%s-%s" , mc .Name , ignitionSocketName )
343
+ ignitionSocket , err := machineDataDir .AppendToNewVMFile (socketName , & socketName )
344
+ if err != nil {
345
+ return nil , err
346
+ }
347
+ if err := ignitionSocket .Delete (); err != nil {
348
+ logrus .Errorf ("unable to delete ignition socket: %q" , err )
349
+ }
350
+
351
+ ignitionVsockDeviceCLI , err := GetIgnitionVsockDeviceAsCLI (ignitionSocket .GetPath ())
352
+ if err != nil {
353
+ return nil , err
354
+ }
355
+
356
+ logrus .Debugf ("serving ignition file over %s" , ignitionSocket .GetPath ())
357
+ go func () {
358
+ if err := ServeIgnitionOverSock (ignitionSocket , mc ); err != nil {
359
+ logrus .Error (err )
360
+ }
361
+ logrus .Debug ("ignition vsock server exited" )
362
+ }()
363
+
364
+ return ignitionVsockDeviceCLI , nil
365
+ }
366
+
367
+ func getFirstBootAppleVMCloudInit (mc * vmconfigs.MachineConfig ) ([]string , error ) {
368
+ sshKey , err := machine .GetSSHKeys (mc .SSH .IdentityPath )
369
+ if err != nil {
370
+ return nil , err
371
+ }
372
+
373
+ machineDataDir , err := mc .DataDir ()
374
+ if err != nil {
375
+ return nil , err
376
+ }
377
+
378
+ // delete previous user-data, if any
379
+ if err := os .Remove (filepath .Join (machineDataDir .Path , "user-data" )); err != nil && ! os .IsNotExist (err ) {
380
+ return nil , err
381
+ }
382
+
383
+ // we generate the user-data file
384
+ userDataFile , err := cloudinit .GenerateUserData (machineDataDir .Path , cloudinit.UserData {
385
+ Users : []cloudinit.User {
386
+ cloudinit.User {
387
+ Name : mc .SSH .RemoteUsername ,
388
+ Sudo : "ALL=(ALL) NOPASSWD:ALL" ,
389
+ Shell : "/bin/bash" ,
390
+ Groups : "users" ,
391
+ SSHKeys : []string {
392
+ sshKey ,
393
+ },
394
+ },
395
+ },
396
+ })
397
+ if err != nil {
398
+ return nil , err
399
+ }
400
+
401
+ return []string {"--cloud-init" , userDataFile }, nil
402
+ }
403
+
354
404
// CheckProcessRunning checks non blocking if the pid exited
355
405
// returns nil if process is running otherwise an error if not
356
406
func CheckProcessRunning (processName string , pid int ) error {
0 commit comments