MCPcopy
hub / github.com/lima-vm/lima / Prepare

Function Prepare

pkg/instance/start.go:50–149  ·  view source on GitHub ↗

Prepare ensures the disk, the nerdctl archive, etc.

(ctx context.Context, inst *limatype.Instance, guestAgent string)

Source from the content-addressed store, hash-verified

48
49// Prepare ensures the disk, the nerdctl archive, etc.
50func Prepare(ctx context.Context, inst *limatype.Instance, guestAgent string) (*Prepared, error) {
51 var needsGuestAgent bool
52 switch *inst.Config.OS {
53 case limatype.DARWIN:
54 // macOS guests always need the guest agent for running fake-cloud-init
55 needsGuestAgent = true
56 case limatype.FREEBSD:
57 // guest agent is not implemented for FreeBSD yet
58 needsGuestAgent = false
59 default:
60 needsGuestAgent = !*inst.Config.Plain
61 }
62 if needsGuestAgent && guestAgent == "" {
63 var err error
64 guestAgent, err = usrlocal.GuestAgentBinary(*inst.Config.OS, *inst.Config.Arch)
65 if err != nil {
66 return nil, err
67 }
68 }
69 limaDriver, err := driverutil.CreateConfiguredDriver(ctx, inst, 0, "")
70 if err != nil {
71 return nil, fmt.Errorf("failed to create driver instance: %w", err)
72 }
73
74 if err := limaDriver.Validate(ctx); err != nil {
75 return nil, err
76 }
77
78 if err := limaDriver.Create(ctx); err != nil {
79 return nil, err
80 }
81
82 // Migrate legacy disk layout (diffdisk → disk, ISO basedisk → iso)
83 if err := driverutil.MigrateDiskLayout(inst.Dir); err != nil {
84 return nil, err
85 }
86
87 created := limayaml.IsExistingInstanceDir(inst.Dir)
88
89 imagePath := filepath.Join(inst.Dir, filenames.Image)
90 disk := filepath.Join(inst.Dir, filenames.Disk)
91 kernel := filepath.Join(inst.Dir, filenames.Kernel)
92 kernelCmdline := filepath.Join(inst.Dir, filenames.KernelCmdline)
93 initrd := filepath.Join(inst.Dir, filenames.Initrd)
94 if !osutil.FileExists(imagePath) && !osutil.FileExists(disk) {
95 var ensuredImage bool
96 errs := make([]error, len(inst.Config.Images))
97 for i, f := range inst.Config.Images {
98 if _, err := fileutils.DownloadFile(ctx, imagePath, f.File, true, "the image", *inst.Config.Arch); err != nil {
99 errs[i] = err
100 continue
101 }
102 if f.Kernel != nil {
103 // ensure decompress kernel because vz expects it to be decompressed
104 if _, err := fileutils.DownloadFile(ctx, kernel, f.Kernel.File, true, "the kernel", *inst.Config.Arch); err != nil {
105 errs[i] = err
106 continue
107 }

Callers 2

createActionFunction · 0.92
StartWithPathsFunction · 0.85

Calls 13

GuestAgentBinaryFunction · 0.92
CreateConfiguredDriverFunction · 0.92
MigrateDiskLayoutFunction · 0.92
IsExistingInstanceDirFunction · 0.92
FileExistsFunction · 0.92
DownloadFileFunction · 0.92
ErrorsFunction · 0.92
prepareDiskFunction · 0.85
WriteFileMethod · 0.80
ValidateMethod · 0.65
CreateMethod · 0.65

Tested by

no test coverage detected