|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ext |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "net" |
| 11 | + "slices" |
| 12 | + |
| 13 | + "github.com/lima-vm/lima/v2/pkg/driver" |
| 14 | + "github.com/lima-vm/lima/v2/pkg/limatype" |
| 15 | + "github.com/lima-vm/lima/v2/pkg/ptr" |
| 16 | +) |
| 17 | + |
| 18 | +type LimaExtDriver struct { |
| 19 | + Instance *limatype.Instance |
| 20 | +} |
| 21 | + |
| 22 | +var _ driver.Driver = (*LimaExtDriver)(nil) |
| 23 | + |
| 24 | +func New() *LimaExtDriver { |
| 25 | + return &LimaExtDriver{} |
| 26 | +} |
| 27 | + |
| 28 | +func (l *LimaExtDriver) Configure(inst *limatype.Instance) *driver.ConfiguredDriver { |
| 29 | + l.Instance = inst |
| 30 | + |
| 31 | + return &driver.ConfiguredDriver{ |
| 32 | + Driver: l, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (l *LimaExtDriver) Validate(ctx context.Context) error { |
| 37 | + return validateConfig(ctx, l.Instance.Config) |
| 38 | +} |
| 39 | + |
| 40 | +func validateConfig(_ context.Context, cfg *limatype.LimaYAML) error { |
| 41 | + if cfg == nil { |
| 42 | + return errors.New("configuration is nil") |
| 43 | + } |
| 44 | + if err := validateMountType(cfg); err != nil { |
| 45 | + return err |
| 46 | + } |
| 47 | + |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// Helper method for mount type validation. |
| 52 | +func validateMountType(cfg *limatype.LimaYAML) error { |
| 53 | + if cfg.MountTypesUnsupported != nil && cfg.MountType != nil && slices.Contains(cfg.MountTypesUnsupported, *cfg.MountType) { |
| 54 | + return fmt.Errorf("mount type %q is explicitly unsupported", *cfg.MountType) |
| 55 | + } |
| 56 | + |
| 57 | + return nil |
| 58 | +} |
| 59 | + |
| 60 | +func (l *LimaExtDriver) FillConfig(_ context.Context, cfg *limatype.LimaYAML, _ string) error { |
| 61 | + if cfg.VMType == nil { |
| 62 | + cfg.VMType = ptr.Of(limatype.EXT) |
| 63 | + } |
| 64 | + |
| 65 | + if cfg.MountType == nil || *cfg.MountType == "" || *cfg.MountType == "default" { |
| 66 | + cfg.MountType = ptr.Of(limatype.REVSSHFS) |
| 67 | + } |
| 68 | + |
| 69 | + mountTypesUnsupported := make(map[string]struct{}) |
| 70 | + mountTypesUnsupported[limatype.NINEP] = struct{}{} |
| 71 | + mountTypesUnsupported[limatype.VIRTIOFS] = struct{}{} |
| 72 | + |
| 73 | + if _, ok := mountTypesUnsupported[*cfg.MountType]; ok { |
| 74 | + return fmt.Errorf("mount type %q is explicitly unsupported", *cfg.MountType) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +func (l *LimaExtDriver) Start(_ context.Context) (chan error, error) { |
| 81 | + errCh := make(chan error) |
| 82 | + |
| 83 | + return errCh, nil |
| 84 | +} |
| 85 | + |
| 86 | +func (l *LimaExtDriver) BootScripts() (map[string][]byte, error) { |
| 87 | + return nil, nil |
| 88 | +} |
| 89 | + |
| 90 | +func (l *LimaExtDriver) RunGUI() error { |
| 91 | + return fmt.Errorf("RunGUI is not supported for the given driver '%s' and display '%s'", "ext", *l.Instance.Config.Video.Display) |
| 92 | +} |
| 93 | + |
| 94 | +func (l *LimaExtDriver) Stop(_ context.Context) error { |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +func (l *LimaExtDriver) GuestAgentConn(_ context.Context) (net.Conn, string, error) { |
| 99 | + return nil, "", nil |
| 100 | +} |
| 101 | + |
| 102 | +func (l *LimaExtDriver) Info() driver.Info { |
| 103 | + var info driver.Info |
| 104 | + info.Name = "ext" |
| 105 | + if l.Instance != nil && l.Instance.Dir != "" { |
| 106 | + info.InstanceDir = l.Instance.Dir |
| 107 | + } |
| 108 | + |
| 109 | + info.Features = driver.DriverFeatures{ |
| 110 | + DynamicSSHAddress: false, |
| 111 | + SkipSocketForwarding: false, |
| 112 | + CanRunGUI: false, |
| 113 | + } |
| 114 | + return info |
| 115 | +} |
| 116 | + |
| 117 | +func (l *LimaExtDriver) SSHAddress(_ context.Context) (string, error) { |
| 118 | + return l.Instance.SSHAddress, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (l *LimaExtDriver) InspectStatus(_ context.Context, _ *limatype.Instance) string { |
| 122 | + return "" |
| 123 | +} |
| 124 | + |
| 125 | +func (l *LimaExtDriver) Create(_ context.Context) error { |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +func (l *LimaExtDriver) Delete(_ context.Context) error { |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func (l *LimaExtDriver) CreateDisk(_ context.Context) error { |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func (l *LimaExtDriver) Register(_ context.Context) error { |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func (l *LimaExtDriver) Unregister(_ context.Context) error { |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (l *LimaExtDriver) ChangeDisplayPassword(_ context.Context, _ string) error { |
| 146 | + return nil |
| 147 | +} |
| 148 | + |
| 149 | +func (l *LimaExtDriver) DisplayConnection(_ context.Context) (string, error) { |
| 150 | + return "", nil |
| 151 | +} |
| 152 | + |
| 153 | +func (l *LimaExtDriver) CreateSnapshot(_ context.Context, _ string) error { |
| 154 | + return errUnimplemented |
| 155 | +} |
| 156 | + |
| 157 | +func (l *LimaExtDriver) ApplySnapshot(_ context.Context, _ string) error { |
| 158 | + return errUnimplemented |
| 159 | +} |
| 160 | + |
| 161 | +func (l *LimaExtDriver) DeleteSnapshot(_ context.Context, _ string) error { |
| 162 | + return errUnimplemented |
| 163 | +} |
| 164 | + |
| 165 | +func (l *LimaExtDriver) ListSnapshots(_ context.Context) (string, error) { |
| 166 | + return "", errUnimplemented |
| 167 | +} |
| 168 | + |
| 169 | +func (l *LimaExtDriver) ForwardGuestAgent() bool { |
| 170 | + // If driver is not providing, use host agent |
| 171 | + return true |
| 172 | +} |
0 commit comments