浏览文件
driver.go
161 行 · 6.2 KB历史下载| 1 | // SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | package driver |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "net" |
| 10 | |
| 11 | "github.com/lima-vm/lima/v2/pkg/hostagent/events" |
| 12 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 13 | ) |
| 14 | |
| 15 | // ErrDriverNotScreenshotter is returned when the active driver does not implement Screenshotter. |
| 16 | var ErrDriverNotScreenshotter = errors.New("driver does not support screenshots") |
| 17 | |
| 18 | // ErrNoDisplay is returned when the VM has no display configured. |
| 19 | var ErrNoDisplay = errors.New("no display configured") |
| 20 | |
| 21 | // VsockEventEmitter is an optional interface for drivers to emit vsock events. |
| 22 | type VsockEventEmitter interface { |
| 23 | SetVsockEventCallback(callback func(*events.VsockEvent)) |
| 24 | } |
| 25 | |
| 26 | // Screenshotter is an optional interface for drivers that can capture the VM display. |
| 27 | // format is "png" or "bmp"; an empty string defaults to "png". |
| 28 | type Screenshotter interface { |
| 29 | CaptureScreenshot(format string) ([]byte, error) |
| 30 | } |
| 31 | |
| 32 | // Lifecycle defines basic lifecycle operations. |
| 33 | type Lifecycle interface { |
| 34 | // Validate returns error if the current driver isn't support for given config |
| 35 | Validate(_ context.Context) error |
| 36 | |
| 37 | // Create is called on creating the instance for the first time. |
| 38 | // (e.g., creating "vz-identifier" file) |
| 39 | // |
| 40 | // Create MUST return nil when it is called against an existing instance. |
| 41 | // |
| 42 | // Create does not create the disks. |
| 43 | Create(_ context.Context) error |
| 44 | |
| 45 | // CreateDisk returns error if the current driver fails in creating disk |
| 46 | CreateDisk(_ context.Context) error |
| 47 | |
| 48 | // Start is used for booting the vm using driver instance |
| 49 | // It returns a chan error on successful boot |
| 50 | // The second argument may contain error occurred while starting driver |
| 51 | Start(_ context.Context) (chan error, error) |
| 52 | |
| 53 | // Stop will terminate the running vm instance. |
| 54 | // It returns error if there are any errors during Stop |
| 55 | Stop(_ context.Context) error |
| 56 | |
| 57 | Delete(_ context.Context) error |
| 58 | |
| 59 | InspectStatus(_ context.Context, inst *limatype.Instance) string |
| 60 | |
| 61 | // BootScripts returns the content of boot scripts to be injected into the vm. |
| 62 | // The key must be "boot.<OS>/<SCRIPT>" or "<SCRIPT>" (deprecated alias for "boot.Linux/<SCRIPT>"). |
| 63 | BootScripts(ctx context.Context) (map[string][]byte, error) |
| 64 | } |
| 65 | |
| 66 | // GUI defines GUI-related operations. |
| 67 | type GUI interface { |
| 68 | // RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates |
| 69 | // It returns error if there are any failures |
| 70 | RunGUI(context.Context) error |
| 71 | |
| 72 | ChangeDisplayPassword(ctx context.Context, password string) error |
| 73 | DisplayConnection(ctx context.Context) (string, error) |
| 74 | } |
| 75 | |
| 76 | // SnapshotManager defines operations for managing snapshots. |
| 77 | type SnapshotManager interface { |
| 78 | CreateSnapshot(ctx context.Context, tag string) error |
| 79 | ApplySnapshot(ctx context.Context, tag string) error |
| 80 | DeleteSnapshot(ctx context.Context, tag string) error |
| 81 | ListSnapshots(ctx context.Context) (string, error) |
| 82 | } |
| 83 | |
| 84 | // GuestAgent defines operations for the guest agent. |
| 85 | type GuestAgent interface { |
| 86 | // ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent. |
| 87 | ForwardGuestAgent(context.Context) bool |
| 88 | |
| 89 | // GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh). |
| 90 | GuestAgentConn(_ context.Context) (net.Conn, string, error) |
| 91 | } |
| 92 | |
| 93 | // Driver interface is used by hostagent for managing vm. |
| 94 | type Driver interface { |
| 95 | Lifecycle |
| 96 | GUI |
| 97 | SnapshotManager |
| 98 | GuestAgent |
| 99 | |
| 100 | Info(context.Context) Info |
| 101 | |
| 102 | // Configure fills driver-specific configuration defaults, validates |
| 103 | // the configuration, and sets up the driver for the given instance. |
| 104 | Configure(ctx context.Context, inst *limatype.Instance) (*ConfiguredDriver, error) |
| 105 | |
| 106 | SSHAddress(ctx context.Context) (string, error) |
| 107 | |
| 108 | // AdditionalSetupForSSH provides additional setup required for SSH connection. |
| 109 | // It is called after VM is started, before first SSH connection. |
| 110 | // It returns an error if the setup fails. |
| 111 | AdditionalSetupForSSH(ctx context.Context) error |
| 112 | } |
| 113 | |
| 114 | type ConfiguredDriver struct { |
| 115 | Driver |
| 116 | } |
| 117 | |
| 118 | // Info describes a driver instance: its identity, the guest-agent transport, |
| 119 | // the instance directory and the driver's capability flags. It is returned by |
| 120 | // Driver.Info and, for external drivers, carried as JSON in InfoResponse. |
| 121 | type Info struct { |
| 122 | // Name is the driver name, e.g. "qemu", "vz", "wsl2", "hcs" or "krunkit". |
| 123 | Name string `json:"name"` |
| 124 | // VsockPort is the vsock port used by the guest agent, or 0 when vsock is |
| 125 | // not used. |
| 126 | VsockPort int `json:"vsockPort"` |
| 127 | // VirtioPort is the virtio-serial port name used by the guest agent, or |
| 128 | // empty when virtio-serial is not used. |
| 129 | VirtioPort string `json:"virtioPort"` |
| 130 | // InstanceDir is the absolute path of the instance directory. |
| 131 | InstanceDir string `json:"instanceDir,omitempty"` |
| 132 | // Features declares the driver's optional capabilities and behaviors. |
| 133 | Features DriverFeatures `json:"features"` |
| 134 | } |
| 135 | |
| 136 | // DriverFeatures declares optional capabilities and behaviors of a driver. The |
| 137 | // host agent reads them (via Info) to adapt provisioning, SSH and forwarding. |
| 138 | type DriverFeatures struct { |
| 139 | // CanRunGUI reports that the driver can display a GUI via RunGUI. |
| 140 | CanRunGUI bool `json:"canRunGui,omitempty"` |
| 141 | // DynamicSSHAddress reports that the SSH address is not known until after |
| 142 | // Start, so the host agent re-queries SSHAddress once the VM is running. |
| 143 | DynamicSSHAddress bool `json:"dynamicSSHAddress"` |
| 144 | // StaticSSHPort reports that the driver uses a fixed SSH port; when false the |
| 145 | // host agent allocates a free local port. |
| 146 | StaticSSHPort bool `json:"staticSSHPort"` |
| 147 | // SkipSocketForwarding reports that the host agent should skip unix-socket |
| 148 | // forwarding for this driver. |
| 149 | SkipSocketForwarding bool `json:"skipSocketForwarding"` |
| 150 | // NoCloudInit reports that the driver does not use cloud-init, so the host |
| 151 | // agent skips building the cloud-init seed. |
| 152 | NoCloudInit bool `json:"noCloudInit"` |
| 153 | // RosettaEnabled reports that Rosetta is enabled for the guest. |
| 154 | RosettaEnabled bool `json:"rosettaEnabled"` |
| 155 | // RosettaBinFmt reports that Rosetta is registered as a binfmt_misc handler |
| 156 | // in the guest. |
| 157 | RosettaBinFmt bool `json:"rosettaBinFmt"` |
| 158 | // SupportedImageFormats lists the disk image formats the driver can boot |
| 159 | // (e.g. "raw", "qcow2"), used to select or convert the instance image. |
| 160 | SupportedImageFormats []string `json:"supportedImageFormats,omitempty"` |
| 161 | } |

