-
Notifications
You must be signed in to change notification settings - Fork 0
Cloud Hypervisor client #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # Cloud Hypervisor VMM Client | ||
|
|
||
| Thin Go wrapper around Cloud Hypervisor's HTTP API with embedded binaries. | ||
|
|
||
| Supports multiple versions of the Cloud Hypervisor VMM because instances are version-locked to cloud hypervisor if they are in standby. We can switch them to the latest version if we shutdown / reboot the VM. | ||
|
|
||
| Embeds the binaries to make it easier to install, instead of managing multiple versions of the Cloud Hypervisor CLI externally + configuring it. | ||
sjmiller609 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Features | ||
|
|
||
| - Embedded Cloud Hypervisor binaries of multiple versions | ||
| - Generated HTTP client from official OpenAPI spec | ||
| - Automatic binary extraction to data directory | ||
| - Unix socket communication | ||
| - Version detection and validation | ||
|
|
||
| ## Requirements | ||
|
|
||
| **System:** | ||
| - Linux with KVM support (`/dev/kvm`) | ||
| - User must be in `kvm` group or run as root | ||
|
|
||
| **Add user to kvm group:** | ||
| ```bash | ||
| sudo usermod -aG kvm $USER | ||
| # Then log out and back in, or use: sg kvm -c "your-command" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ### Start a VMM Process | ||
|
|
||
| ```go | ||
| import "github.com/onkernel/hypeman/lib/vmm" | ||
|
|
||
| ctx := context.Background() | ||
| dataDir := "/var/lib/hypeman" | ||
| socketPath := "/tmp/vmm.sock" | ||
|
|
||
| // Start Cloud Hypervisor VMM (extracts binary if needed) | ||
| err := vmm.StartProcess(ctx, dataDir, vmm.V48_0, socketPath) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| ``` | ||
|
|
||
| ### Connect to Existing VMM | ||
|
|
||
| ```go | ||
| // Create client for existing VMM socket | ||
| client, err := vmm.NewVMM(socketPath) | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
|
|
||
| // All generated API methods available directly | ||
| resp, err := client.GetVmmPingWithResponse(ctx) | ||
| ``` | ||
|
|
||
| ### Check Binary Version | ||
|
|
||
| ```go | ||
| binaryPath, _ := vmm.GetBinaryPath(dataDir, vmm.V48_0) | ||
| version, err := vmm.ParseVersion(binaryPath) | ||
| fmt.Println(version) // "v48.0" | ||
| ``` | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| lib/vmm/ | ||
| ├── vmm.go # Generated from OpenAPI spec (DO NOT EDIT) | ||
| ├── client.go # Thin wrapper: NewVMM, StartProcess | ||
| ├── binaries.go # Binary embedding and extraction | ||
| ├── version.go # Version parsing utilities | ||
| ├── binaries/ # Embedded Cloud Hypervisor binaries | ||
| │ └── cloud-hypervisor/ | ||
| │ ├── v48.0/ | ||
| │ │ ├── x86_64/cloud-hypervisor (4.5MB) | ||
| │ │ └── aarch64/cloud-hypervisor (3.3MB) | ||
| │ └── v49.0/ | ||
| │ ├── x86_64/cloud-hypervisor (4.5MB) | ||
| │ └── aarch64/cloud-hypervisor (3.3MB) | ||
| | # There will be additional versions in the future... | ||
| └── client_test.go # Tests with real Cloud Hypervisor | ||
| ``` | ||
|
|
||
| ## Supported Versions | ||
|
|
||
| - Cloud Hypervisor v48.0 (API v0.3.0) | ||
| - Cloud Hypervisor v49.0 (API v0.3.0) | ||
|
|
||
| There may be additional versions in the future. Cloud hypervisor versions may update frequently, while the API updates less frequently. | ||
|
|
||
| ## Regenerating Client | ||
|
|
||
| ```bash | ||
| # Download latest spec (if needed) | ||
| make download-ch-spec | ||
|
|
||
| # Regenerate client from spec | ||
| make generate-vmm-client | ||
| ``` | ||
|
|
||
| ## Testing | ||
|
|
||
| Tests run against real Cloud Hypervisor binaries (not mocked). | ||
|
|
||
| ```bash | ||
| # Must be in kvm group | ||
| sg kvm -c "go test ./lib/vmm/..." | ||
| ``` | ||
|
|
||
| ## Binary Extraction | ||
|
|
||
| Binaries are automatically extracted from embedded FS to: | ||
| ``` | ||
| {dataDir}/system/binaries/{version}/{arch}/cloud-hypervisor | ||
| ``` | ||
|
|
||
| Extraction happens once per version. Subsequent calls reuse the extracted binary. | ||
|
|
||
| ## API Methods | ||
|
|
||
| All Cloud Hypervisor API methods available via embedded `*ClientWithResponses`: | ||
|
|
||
| - `GetVmmPingWithResponse()` - Check VMM health | ||
| - `ShutdownVMMWithResponse()` - Shutdown VMM | ||
| - `CreateVMWithResponse()` - Create VM configuration | ||
| - `BootVMWithResponse()` - Boot configured VM | ||
| - `PauseVMWithResponse()` - Pause running VM | ||
| - `ResumeVMWithResponse()` - Resume paused VM | ||
| - `VmSnapshotPutWithResponse()` - Create VM snapshot | ||
| - `VmRestorePutWithResponse()` - Restore from snapshot | ||
| - `VmInfoGetWithResponse()` - Get VM info | ||
| - And many more... | ||
|
|
||
| See generated `vmm.go` for full API surface. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package vmm | ||
|
|
||
| import ( | ||
| "embed" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "runtime" | ||
| ) | ||
|
|
||
| //go:embed binaries/cloud-hypervisor/v48.0/x86_64/cloud-hypervisor | ||
| //go:embed binaries/cloud-hypervisor/v48.0/aarch64/cloud-hypervisor | ||
| //go:embed binaries/cloud-hypervisor/v49.0/x86_64/cloud-hypervisor | ||
| //go:embed binaries/cloud-hypervisor/v49.0/aarch64/cloud-hypervisor | ||
| var binaryFS embed.FS | ||
|
|
||
| type CHVersion string | ||
|
|
||
| const ( | ||
| V48_0 CHVersion = "v48.0" | ||
| V49_0 CHVersion = "v49.0" | ||
| ) | ||
|
|
||
| var SupportedVersions = []CHVersion{V48_0, V49_0} | ||
|
|
||
| // ExtractBinary extracts the embedded Cloud Hypervisor binary to the data directory | ||
| func ExtractBinary(dataDir string, version CHVersion) (string, error) { | ||
| arch := runtime.GOARCH | ||
| if arch == "amd64" { | ||
| arch = "x86_64" | ||
| } | ||
|
|
||
| embeddedPath := fmt.Sprintf("binaries/cloud-hypervisor/%s/%s/cloud-hypervisor", version, arch) | ||
| extractPath := filepath.Join(dataDir, "system", "binaries", string(version), arch, "cloud-hypervisor") | ||
|
|
||
| // Check if already extracted | ||
| if _, err := os.Stat(extractPath); err == nil { | ||
| return extractPath, nil | ||
| } | ||
|
|
||
| // Create directory | ||
| if err := os.MkdirAll(filepath.Dir(extractPath), 0755); err != nil { | ||
| return "", fmt.Errorf("create binaries dir: %w", err) | ||
| } | ||
|
|
||
| // Read embedded binary | ||
| data, err := binaryFS.ReadFile(embeddedPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("read embedded binary: %w", err) | ||
| } | ||
|
|
||
| // Write to filesystem | ||
| if err := os.WriteFile(extractPath, data, 0755); err != nil { | ||
| return "", fmt.Errorf("write binary: %w", err) | ||
| } | ||
|
|
||
| return extractPath, nil | ||
| } | ||
|
|
||
| // GetBinaryPath returns path to extracted binary, extracting if needed | ||
| func GetBinaryPath(dataDir string, version CHVersion) (string, error) { | ||
| return ExtractBinary(dataDir, version) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Ignore all binaries | ||
| cloud-hypervisor | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.