浏览文件
vm.go
147 行 · 5.3 KB历史下载| 1 | // Package vm is what agent-vm asks of what runs its VMs. Lima does it |
| 2 | // (lima.go); the interface keeps the rest of agent-vm apart from it, so tests |
| 3 | // use a fake, and another backend could take Lima's place. |
| 4 | package vm |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "io" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/mounts/spec" |
| 12 | ) |
| 13 | |
| 14 | // ErrNotFound is returned for a VM that does not exist. |
| 15 | var ErrNotFound = errors.New("no such VM") |
| 16 | |
| 17 | // ErrNameTooLong is returned for a VM name the backend cannot have here: |
| 18 | // the paths of its sockets would be too long. |
| 19 | var ErrNameTooLong = errors.New("VM name too long") |
| 20 | |
| 21 | // ErrUnmountable is returned by Edit for a share the backend would read as |
| 22 | // another path than the one given. |
| 23 | var ErrUnmountable = errors.New("a share the backend cannot take as written") |
| 24 | |
| 25 | // ErrLimaOverrides is returned while the backend's own config would add to |
| 26 | // what agent-vm sets for every VM (Lima's _config/default.yaml and |
| 27 | // override.yaml). |
| 28 | var ErrLimaOverrides = errors.New("agent-vm does not use Lima's default.yaml or override.yaml; move it out of the way") |
| 29 | |
| 30 | // ErrUnconfined is returned by Start for a VM with a share the VM could |
| 31 | // reach the rest of the host through (see Confined). |
| 32 | var ErrUnconfined = errors.New("agent-vm starts no VM with a share not confined to its folder") |
| 33 | |
| 34 | // Status of a VM. |
| 35 | type Status string |
| 36 | |
| 37 | const ( |
| 38 | Running Status = "Running" |
| 39 | Stopped Status = "Stopped" |
| 40 | // Broken: the backend cannot tell, or the VM is half there. |
| 41 | Broken Status = "Broken" |
| 42 | ) |
| 43 | |
| 44 | // Instance is a VM as the backend has it. |
| 45 | type Instance struct { |
| 46 | Name string |
| 47 | Status Status |
| 48 | Dir string // the backend's folder for it |
| 49 | CPUs int |
| 50 | Memory, Disk int64 // bytes |
| 51 | SSHLocalPort int // the port in use |
| 52 | // SSHPortConfig is the port the config fixes, 0 when the backend |
| 53 | // picks one on each start. |
| 54 | SSHPortConfig int |
| 55 | SSHAddress string |
| 56 | // SSHConfig is the ssh_config file the backend rewrites on each start, |
| 57 | // for tools that connect to the VM. |
| 58 | SSHConfig string |
| 59 | VMType, Arch string |
| 60 | // MountType is the backend's name for how the shares reach the guest, |
| 61 | // empty for its default. |
| 62 | MountType string |
| 63 | Mounts []Mount |
| 64 | // ConfigErr is why the backend cannot read the VM's config (Broken). |
| 65 | ConfigErr error |
| 66 | // Orphaned: Broken, the VM still running without the backend's process |
| 67 | // that serves it (Lima's hostagent). Nothing reaches it; Stop ends it by |
| 68 | // force. |
| 69 | Orphaned bool |
| 70 | } |
| 71 | |
| 72 | // Mount is a host folder shared with the guest. |
| 73 | type Mount = spec.Mount |
| 74 | |
| 75 | // SSHFS options of a reverse-sshfs share. |
| 76 | type SSHFS = spec.SSHFS |
| 77 | |
| 78 | // ReverseSSHFS is the mount type whose shares the host serves over SFTP, |
| 79 | // which is where readonlyNames are enforced. |
| 80 | const ReverseSSHFS = "reverse-sshfs" |
| 81 | |
| 82 | // Settings change a VM. A zero field is left as it is. |
| 83 | type Settings struct { |
| 84 | CPUs int |
| 85 | MemoryGiB int |
| 86 | DiskGiB int |
| 87 | SSHLocalPort *int // 0 gives the port back to the backend |
| 88 | Mounts *[]Mount |
| 89 | MountType *string // "" goes back to the backend's default |
| 90 | // NoContainerd leaves out the backend's own containerd: Docker, when |
| 91 | // installed, ships its own. |
| 92 | NoContainerd bool |
| 93 | |
| 94 | // noEphemeralPorts: see ephemeralPorts. |
| 95 | noEphemeralPorts bool |
| 96 | // Refresh the cloud-init timezone on cold starts of an existing virtual Mac. |
| 97 | macOSTimezone string |
| 98 | } |
| 99 | |
| 100 | // ephemeralPorts is the guest kernel's range for the ports a program gets |
| 101 | // when it asks for any free one (Debian's ip_local_port_range). Never |
| 102 | // forwarded: nobody chose them (Chromium's DevTools port, language |
| 103 | // servers...), and each would land on the host's loopback with a |
| 104 | // notification. |
| 105 | var ephemeralPorts = [2]int{32768, 60999} |
| 106 | |
| 107 | // ShellOpts is a command run in a running VM. |
| 108 | type ShellOpts struct { |
| 109 | // Workdir is where the command runs; it fails when it cannot cd there. |
| 110 | Workdir string |
| 111 | // Args is the command, run by the user's login shell; empty for the |
| 112 | // shell itself. |
| 113 | Args []string |
| 114 | Stdin io.Reader |
| 115 | Stdout, Stderr io.Writer |
| 116 | // Interactive asks for a terminal in the guest, as a full-screen |
| 117 | // program needs, when stdin is one. Stdout on a terminal gets one |
| 118 | // anyway. |
| 119 | Interactive bool |
| 120 | } |
| 121 | |
| 122 | // Backend runs VMs. |
| 123 | type Backend interface { |
| 124 | // List is every VM, in name order. |
| 125 | List(ctx context.Context) ([]*Instance, error) |
| 126 | // Get is one VM, or ErrNotFound. |
| 127 | Get(ctx context.Context, name string) (*Instance, error) |
| 128 | // Create makes a VM from a template, with settings, and does not start it. |
| 129 | // log gets the backend's progress. |
| 130 | Create(ctx context.Context, name, template string, s Settings, log io.Writer) error |
| 131 | // Clone copies the stopped VM from as to, stopped. |
| 132 | Clone(ctx context.Context, from, to string) error |
| 133 | // Edit changes the stopped VM name. |
| 134 | Edit(ctx context.Context, name string, s Settings) error |
| 135 | // Start boots the stopped VM name, and returns once it is ready. log |
| 136 | // gets the backend's progress. It refuses a VM whose shares are not |
| 137 | // confined to their folders (see Confined). |
| 138 | Start(ctx context.Context, name string, log io.Writer) error |
| 139 | // Stop shuts down the running VM name, and kills an orphaned one. Any |
| 140 | // other VM is left as it is. |
| 141 | Stop(ctx context.Context, name string) error |
| 142 | // Delete removes the VM name, running or not. |
| 143 | Delete(ctx context.Context, name string) error |
| 144 | // Shell runs a command in the running VM name, and returns its exit |
| 145 | // status. |
| 146 | Shell(ctx context.Context, name string, o ShellOpts) (int, error) |
| 147 | } |

