// SPDX-FileCopyrightText: Copyright The Lima Authors1// SPDX-License-Identifier: Apache-2.023package driver45import (6"context"7"net"89"github.com/lima-vm/lima/v2/pkg/hostagent/events"10"github.com/lima-vm/lima/v2/pkg/limatype"11)1213// VsockEventEmitter is an optional interface for drivers to emit vsock events.14type VsockEventEmitter interface {15SetVsockEventCallback(callback func(*events.VsockEvent))16}1718// Lifecycle defines basic lifecycle operations.19type Lifecycle interface {20// Validate returns error if the current driver isn't support for given config21Validate(_ context.Context) error2223// Create is called on creating the instance for the first time.24// (e.g., creating "vz-identifier" file)25//26// Create MUST return nil when it is called against an existing instance.27//28// Create does not create the disks.29Create(_ context.Context) error3031// CreateDisk returns error if the current driver fails in creating disk32CreateDisk(_ context.Context) error3334// Start is used for booting the vm using driver instance35// It returns a chan error on successful boot36// The second argument may contain error occurred while starting driver37Start(_ context.Context) (chan error, error)3839// Stop will terminate the running vm instance.40// It returns error if there are any errors during Stop41Stop(_ context.Context) error4243Delete(_ context.Context) error4445InspectStatus(_ context.Context, inst *limatype.Instance) string4647BootScripts() (map[string][]byte, error)48}4950// GUI defines GUI-related operations.51type GUI interface {52// RunGUI is for starting GUI synchronously by hostagent. This method should be wait and return only after vm terminates53// It returns error if there are any failures54RunGUI() error5556ChangeDisplayPassword(ctx context.Context, password string) error57DisplayConnection(ctx context.Context) (string, error)58}5960// SnapshotManager defines operations for managing snapshots.61type SnapshotManager interface {62CreateSnapshot(ctx context.Context, tag string) error63ApplySnapshot(ctx context.Context, tag string) error64DeleteSnapshot(ctx context.Context, tag string) error65ListSnapshots(ctx context.Context) (string, error)66}6768// GuestAgent defines operations for the guest agent.69type GuestAgent interface {70// ForwardGuestAgent returns if the guest agent sock needs forwarding by host agent.71ForwardGuestAgent() bool7273// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).74GuestAgentConn(_ context.Context) (net.Conn, string, error)75}7677// Driver interface is used by hostagent for managing vm.78type Driver interface {79Lifecycle80GUI81SnapshotManager82GuestAgent8384Info() Info8586// Configure sets the configuration for the instance.87// TODO: merge Configure and FillConfig?88// Or come up with a better name to clarify the difference.89Configure(inst *limatype.Instance) *ConfiguredDriver9091// FillConfig fills and validates the configuration for the instance.92// The config is not set to the instance.93FillConfig(ctx context.Context, cfg *limatype.LimaYAML, filePath string) error9495SSHAddress(ctx context.Context) (string, error)9697// AdditionalSetupForSSH provides additional setup required for SSH connection.98// It is called after VM is started, before first SSH connection.99// It returns an error if the setup fails.100AdditionalSetupForSSH(ctx context.Context) error101}102103type ConfiguredDriver struct {104Driver105}106107type Info struct {108Name string `json:"name"`109VsockPort int `json:"vsockPort"`110VirtioPort string `json:"virtioPort"`111InstanceDir string `json:"instanceDir,omitempty"`112Features DriverFeatures `json:"features"`113}114115type DriverFeatures struct {116CanRunGUI bool `json:"canRunGui,omitempty"`117DynamicSSHAddress bool `json:"dynamicSSHAddress"`118StaticSSHPort bool `json:"staticSSHPort"`119SkipSocketForwarding bool `json:"skipSocketForwarding"`120NoCloudInit bool `json:"noCloudInit"`121RosettaEnabled bool `json:"rosettaEnabled"`122RosettaBinFmt bool `json:"rosettaBinFmt"`123}124125126