Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/driver/driver.go
2611 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package driver
5
6
import (
7
"context"
8
"net"
9
10
"github.com/lima-vm/lima/v2/pkg/hostagent/events"
11
"github.com/lima-vm/lima/v2/pkg/limatype"
12
)
13
14
// VsockEventEmitter is an optional interface for drivers to emit vsock events.
15
type VsockEventEmitter interface {
16
SetVsockEventCallback(callback func(*events.VsockEvent))
17
}
18
19
// Lifecycle defines basic lifecycle operations.
20
type Lifecycle interface {
21
// Validate returns error if the current driver isn't support for given config
22
Validate(_ context.Context) error
23
24
// Create is called on creating the instance for the first time.
25
// (e.g., creating "vz-identifier" file)
26
//
27
// Create MUST return nil when it is called against an existing instance.
28
//
29
// Create does not create the disks.
30
Create(_ context.Context) error
31
32
// CreateDisk returns error if the current driver fails in creating disk
33
CreateDisk(_ context.Context) error
34
35
// Start is used for booting the vm using driver instance
36
// It returns a chan error on successful boot
37
// The second argument may contain error occurred while starting driver
38
Start(_ context.Context) (chan error, error)
39
40
// Stop will terminate the running vm instance.
41
// It returns error if there are any errors during Stop
42
Stop(_ context.Context) error
43
44
Delete(_ context.Context) error
45
46
InspectStatus(_ context.Context, inst *limatype.Instance) string
47
48
BootScripts() (map[string][]byte, error)
49
}
50
51
// GUI defines GUI-related operations.
52
type GUI interface {
53
// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates
54
// It returns error if there are any failures
55
RunGUI() error
56
57
ChangeDisplayPassword(ctx context.Context, password string) error
58
DisplayConnection(ctx context.Context) (string, error)
59
}
60
61
// SnapshotManager defines operations for managing snapshots.
62
type SnapshotManager interface {
63
CreateSnapshot(ctx context.Context, tag string) error
64
ApplySnapshot(ctx context.Context, tag string) error
65
DeleteSnapshot(ctx context.Context, tag string) error
66
ListSnapshots(ctx context.Context) (string, error)
67
}
68
69
// GuestAgent defines operations for the guest agent.
70
type GuestAgent interface {
71
// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.
72
ForwardGuestAgent() bool
73
74
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
75
GuestAgentConn(_ context.Context) (net.Conn, string, error)
76
}
77
78
// Driver interface is used by hostagent for managing vm.
79
type Driver interface {
80
Lifecycle
81
GUI
82
SnapshotManager
83
GuestAgent
84
85
Info() Info
86
87
// Configure sets the configuration for the instance.
88
// TODO: merge Configure and FillConfig?
89
// Or come up with a better name to clarify the difference.
90
Configure(inst *limatype.Instance) *ConfiguredDriver
91
92
// FillConfig fills and validates the configuration for the instance.
93
// The config is not set to the instance.
94
FillConfig(ctx context.Context, cfg *limatype.LimaYAML, filePath string) error
95
96
SSHAddress(ctx context.Context) (string, error)
97
98
// AdditionalSetupForSSH provides additional setup required for SSH connection.
99
// It is called after VM is started, before first SSH connection.
100
// It returns an error if the setup fails.
101
AdditionalSetupForSSH(ctx context.Context) error
102
}
103
104
type ConfiguredDriver struct {
105
Driver
106
}
107
108
type Info struct {
109
Name string `json:"name"`
110
VsockPort int `json:"vsockPort"`
111
VirtioPort string `json:"virtioPort"`
112
InstanceDir string `json:"instanceDir,omitempty"`
113
Features DriverFeatures `json:"features"`
114
}
115
116
type DriverFeatures struct {
117
CanRunGUI bool `json:"canRunGui,omitempty"`
118
DynamicSSHAddress bool `json:"dynamicSSHAddress"`
119
StaticSSHPort bool `json:"staticSSHPort"`
120
SkipSocketForwarding bool `json:"skipSocketForwarding"`
121
NoCloudInit bool `json:"noCloudInit"`
122
RosettaEnabled bool `json:"rosettaEnabled"`
123
RosettaBinFmt bool `json:"rosettaBinFmt"`
124
}
125
126