|
| 1 | +// Package cloudhypervisor implements the hypervisor.Hypervisor interface |
| 2 | +// for Cloud Hypervisor VMM. |
| 3 | +package cloudhypervisor |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/onkernel/hypeman/lib/hypervisor" |
| 10 | + "github.com/onkernel/hypeman/lib/vmm" |
| 11 | +) |
| 12 | + |
| 13 | +// CloudHypervisor implements hypervisor.Hypervisor for Cloud Hypervisor VMM. |
| 14 | +type CloudHypervisor struct { |
| 15 | + client *vmm.VMM |
| 16 | + socketPath string |
| 17 | +} |
| 18 | + |
| 19 | +// New creates a new Cloud Hypervisor client for an existing VMM socket. |
| 20 | +func New(socketPath string) (*CloudHypervisor, error) { |
| 21 | + client, err := vmm.NewVMM(socketPath) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("create vmm client: %w", err) |
| 24 | + } |
| 25 | + return &CloudHypervisor{ |
| 26 | + client: client, |
| 27 | + socketPath: socketPath, |
| 28 | + }, nil |
| 29 | +} |
| 30 | + |
| 31 | +// Capabilities returns the features supported by Cloud Hypervisor. |
| 32 | +func (c *CloudHypervisor) Capabilities() hypervisor.Capabilities { |
| 33 | + return hypervisor.Capabilities{ |
| 34 | + SupportsSnapshot: true, |
| 35 | + SupportsHotplugMemory: true, |
| 36 | + SupportsPause: true, |
| 37 | + SupportsVsock: true, |
| 38 | + SupportsGPUPassthrough: true, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// CreateVM configures the VM in Cloud Hypervisor. |
| 43 | +func (c *CloudHypervisor) CreateVM(ctx context.Context, config hypervisor.VMConfig) error { |
| 44 | + vmConfig := ToVMConfig(config) |
| 45 | + resp, err := c.client.CreateVMWithResponse(ctx, vmConfig) |
| 46 | + if err != nil { |
| 47 | + return fmt.Errorf("create vm: %w", err) |
| 48 | + } |
| 49 | + if resp.StatusCode() != 204 { |
| 50 | + return fmt.Errorf("create vm failed with status %d: %s", resp.StatusCode(), string(resp.Body)) |
| 51 | + } |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// BootVM starts the configured VM. |
| 56 | +func (c *CloudHypervisor) BootVM(ctx context.Context) error { |
| 57 | + resp, err := c.client.BootVMWithResponse(ctx) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("boot vm: %w", err) |
| 60 | + } |
| 61 | + if resp.StatusCode() != 204 { |
| 62 | + return fmt.Errorf("boot vm failed with status %d: %s", resp.StatusCode(), string(resp.Body)) |
| 63 | + } |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +// DeleteVM removes the VM configuration from Cloud Hypervisor. |
| 68 | +func (c *CloudHypervisor) DeleteVM(ctx context.Context) error { |
| 69 | + resp, err := c.client.DeleteVMWithResponse(ctx) |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("delete vm: %w", err) |
| 72 | + } |
| 73 | + if resp.StatusCode() != 204 { |
| 74 | + return fmt.Errorf("delete vm failed with status %d: %s", resp.StatusCode(), string(resp.Body)) |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Shutdown stops the VMM process gracefully. |
| 80 | +func (c *CloudHypervisor) Shutdown(ctx context.Context) error { |
| 81 | + resp, err := c.client.ShutdownVMMWithResponse(ctx) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("shutdown vmm: %w", err) |
| 84 | + } |
| 85 | + // ShutdownVMM may return various codes, 204 is success |
| 86 | + if resp.StatusCode() != 204 { |
| 87 | + return fmt.Errorf("shutdown vmm failed with status %d", resp.StatusCode()) |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// GetVMInfo returns current VM state. |
| 93 | +func (c *CloudHypervisor) GetVMInfo(ctx context.Context) (*hypervisor.VMInfo, error) { |
| 94 | + resp, err := c.client.GetVmInfoWithResponse(ctx) |
| 95 | + if err != nil { |
| 96 | + return nil, fmt.Errorf("get vm info: %w", err) |
| 97 | + } |
| 98 | + if resp.StatusCode() != 200 || resp.JSON200 == nil { |
| 99 | + return nil, fmt.Errorf("get vm info failed with status %d", resp.StatusCode()) |
| 100 | + } |
| 101 | + |
| 102 | + // Map Cloud Hypervisor state to hypervisor.VMState |
| 103 | + var state hypervisor.VMState |
| 104 | + switch resp.JSON200.State { |
| 105 | + case vmm.Created: |
| 106 | + state = hypervisor.StateCreated |
| 107 | + case vmm.Running: |
| 108 | + state = hypervisor.StateRunning |
| 109 | + case vmm.Paused: |
| 110 | + state = hypervisor.StatePaused |
| 111 | + case vmm.Shutdown: |
| 112 | + state = hypervisor.StateShutdown |
| 113 | + default: |
| 114 | + return nil, fmt.Errorf("unknown vm state: %s", resp.JSON200.State) |
| 115 | + } |
| 116 | + |
| 117 | + return &hypervisor.VMInfo{State: state}, nil |
| 118 | +} |
| 119 | + |
| 120 | +// Pause suspends VM execution. |
| 121 | +func (c *CloudHypervisor) Pause(ctx context.Context) error { |
| 122 | + resp, err := c.client.PauseVMWithResponse(ctx) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("pause vm: %w", err) |
| 125 | + } |
| 126 | + if resp.StatusCode() != 204 { |
| 127 | + return fmt.Errorf("pause vm failed with status %d", resp.StatusCode()) |
| 128 | + } |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +// Resume continues VM execution. |
| 133 | +func (c *CloudHypervisor) Resume(ctx context.Context) error { |
| 134 | + resp, err := c.client.ResumeVMWithResponse(ctx) |
| 135 | + if err != nil { |
| 136 | + return fmt.Errorf("resume vm: %w", err) |
| 137 | + } |
| 138 | + if resp.StatusCode() != 204 { |
| 139 | + return fmt.Errorf("resume vm failed with status %d", resp.StatusCode()) |
| 140 | + } |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +// Snapshot creates a VM snapshot. |
| 145 | +func (c *CloudHypervisor) Snapshot(ctx context.Context, destPath string) error { |
| 146 | + snapshotURL := "file://" + destPath |
| 147 | + snapshotConfig := vmm.VmSnapshotConfig{DestinationUrl: &snapshotURL} |
| 148 | + resp, err := c.client.PutVmSnapshotWithResponse(ctx, snapshotConfig) |
| 149 | + if err != nil { |
| 150 | + return fmt.Errorf("snapshot: %w", err) |
| 151 | + } |
| 152 | + if resp.StatusCode() != 204 { |
| 153 | + return fmt.Errorf("snapshot failed with status %d", resp.StatusCode()) |
| 154 | + } |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +// Restore loads a VM from snapshot. |
| 159 | +func (c *CloudHypervisor) Restore(ctx context.Context, sourcePath string) error { |
| 160 | + sourceURL := "file://" + sourcePath |
| 161 | + restoreConfig := vmm.RestoreConfig{ |
| 162 | + SourceUrl: sourceURL, |
| 163 | + Prefault: ptr(false), |
| 164 | + } |
| 165 | + resp, err := c.client.PutVmRestoreWithResponse(ctx, restoreConfig) |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("restore: %w", err) |
| 168 | + } |
| 169 | + if resp.StatusCode() != 204 { |
| 170 | + return fmt.Errorf("restore failed with status %d", resp.StatusCode()) |
| 171 | + } |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +// ResizeMemory changes the VM's memory allocation. |
| 176 | +func (c *CloudHypervisor) ResizeMemory(ctx context.Context, bytes int64) error { |
| 177 | + resizeConfig := vmm.VmResize{DesiredRam: &bytes} |
| 178 | + resp, err := c.client.PutVmResizeWithResponse(ctx, resizeConfig) |
| 179 | + if err != nil { |
| 180 | + return fmt.Errorf("resize memory: %w", err) |
| 181 | + } |
| 182 | + if resp.StatusCode() != 204 { |
| 183 | + return fmt.Errorf("resize memory failed with status %d", resp.StatusCode()) |
| 184 | + } |
| 185 | + return nil |
| 186 | +} |
| 187 | + |
| 188 | +func ptr[T any](v T) *T { |
| 189 | + return &v |
| 190 | +} |
0 commit comments