-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathprocess.go
More file actions
269 lines (234 loc) · 8.34 KB
/
process.go
File metadata and controls
269 lines (234 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cloudhypervisor
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"os"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/kernel/hypeman/lib/hypervisor"
"github.com/kernel/hypeman/lib/logger"
"github.com/kernel/hypeman/lib/paths"
"github.com/kernel/hypeman/lib/vmm"
"gvisor.dev/gvisor/pkg/cleanup"
)
func init() {
hypervisor.RegisterSocketName(hypervisor.TypeCloudHypervisor, "ch.sock")
hypervisor.RegisterCapabilities(hypervisor.TypeCloudHypervisor, capabilities())
hypervisor.RegisterClientFactory(hypervisor.TypeCloudHypervisor, func(socketPath string) (hypervisor.Hypervisor, error) {
return New(socketPath)
})
}
// Starter implements hypervisor.VMStarter for Cloud Hypervisor.
type Starter struct{}
// NewStarter creates a new Cloud Hypervisor starter.
func NewStarter() *Starter {
return &Starter{}
}
// Verify Starter implements the interface
var _ hypervisor.VMStarter = (*Starter)(nil)
// SocketName returns the socket filename for Cloud Hypervisor.
func (s *Starter) SocketName() string {
return "ch.sock"
}
// GetBinaryPath returns the path to the Cloud Hypervisor binary.
func (s *Starter) GetBinaryPath(p *paths.Paths, version string) (string, error) {
chVersion := vmm.CHVersion(version)
if !vmm.IsVersionSupported(chVersion) {
return "", fmt.Errorf("unsupported cloud-hypervisor version: %s", version)
}
return vmm.GetBinaryPath(p, chVersion)
}
// GetVersion returns the latest supported Cloud Hypervisor version.
// Cloud Hypervisor binaries are embedded, so we return the latest known version.
func (s *Starter) GetVersion(p *paths.Paths) (string, error) {
return string(vmm.V51_1), nil
}
// StartVM launches Cloud Hypervisor, configures the VM, and boots it.
// Returns the process ID and a Hypervisor client for subsequent operations.
func (s *Starter) StartVM(ctx context.Context, p *paths.Paths, version string, socketPath string, config hypervisor.VMConfig) (int, hypervisor.Hypervisor, error) {
log := logger.FromContext(ctx)
// Validate version
chVersion := vmm.CHVersion(version)
if !vmm.IsVersionSupported(chVersion) {
return 0, nil, fmt.Errorf("unsupported cloud-hypervisor version: %s", version)
}
// 1. Start the Cloud Hypervisor process
processCtx, processSpan := hypervisor.StartProcessSpan(ctx, hypervisor.TypeCloudHypervisor)
pid, err := vmm.StartProcess(processCtx, p, chVersion, socketPath)
hypervisor.FinishTraceSpan(processSpan, err)
if err != nil {
return 0, nil, fmt.Errorf("start process: %w", err)
}
// Setup cleanup to kill the process if subsequent steps fail
cu := cleanup.Make(func() {
syscall.Kill(pid, syscall.SIGKILL)
})
defer cu.Clean()
// 2. Create the HTTP client
hv, err := New(socketPath)
if err != nil {
return 0, nil, fmt.Errorf("create client: %w", err)
}
// 3. Configure the VM via HTTP API
vmConfig := ToVMConfig(config)
resp, err := hv.client.CreateVMWithResponse(ctx, vmConfig)
if err != nil {
logStartVMFailureDiagnostics(ctx, log, socketPath, pid, "create_vm", err, 0, "")
return 0, nil, fmt.Errorf("create vm: %w", err)
}
if resp.StatusCode() != 204 {
logStartVMFailureDiagnostics(ctx, log, socketPath, pid, "create_vm", nil, resp.StatusCode(), string(resp.Body))
return 0, nil, fmt.Errorf("create vm failed with status %d: %s", resp.StatusCode(), string(resp.Body))
}
// 4. Boot the VM via HTTP API
bootResp, err := hv.client.BootVMWithResponse(ctx)
if err != nil {
logStartVMFailureDiagnostics(ctx, log, socketPath, pid, "boot_vm", err, 0, "")
return 0, nil, fmt.Errorf("boot vm: %w", err)
}
if bootResp.StatusCode() != 204 {
logStartVMFailureDiagnostics(ctx, log, socketPath, pid, "boot_vm", nil, bootResp.StatusCode(), string(bootResp.Body))
return 0, nil, fmt.Errorf("boot vm failed with status %d: %s", bootResp.StatusCode(), string(bootResp.Body))
}
// Success - release cleanup to prevent killing the process
cu.Release()
return pid, hv, nil
}
// RestoreVM starts Cloud Hypervisor and restores VM state from a snapshot.
// The VM is in paused state after restore; caller should call Resume() to continue execution.
func (s *Starter) RestoreVM(ctx context.Context, p *paths.Paths, version string, socketPath string, snapshotPath string) (int, hypervisor.Hypervisor, error) {
log := logger.FromContext(ctx)
startTime := time.Now()
// Validate version
chVersion := vmm.CHVersion(version)
if !vmm.IsVersionSupported(chVersion) {
return 0, nil, fmt.Errorf("unsupported cloud-hypervisor version: %s", version)
}
// 1. Start the Cloud Hypervisor process
processStartTime := time.Now()
processCtx, processSpan := hypervisor.StartProcessSpan(ctx, hypervisor.TypeCloudHypervisor)
pid, err := vmm.StartProcess(processCtx, p, chVersion, socketPath)
hypervisor.FinishTraceSpan(processSpan, err)
if err != nil {
return 0, nil, fmt.Errorf("start process: %w", err)
}
log.DebugContext(ctx, "CH process started", "pid", pid, "duration_ms", time.Since(processStartTime).Milliseconds())
// Setup cleanup to kill the process if subsequent steps fail
cu := cleanup.Make(func() {
syscall.Kill(pid, syscall.SIGKILL)
})
defer cu.Clean()
// 2. Create the HTTP client
hv, err := New(socketPath)
if err != nil {
return 0, nil, fmt.Errorf("create client: %w", err)
}
// 3. Restore from snapshot via HTTP API
restoreAPIStart := time.Now()
sourceURL := "file://" + snapshotPath
restoreConfig := vmm.RestoreConfig{
SourceUrl: sourceURL,
Prefault: ptr(false),
}
resp, err := hv.client.PutVmRestoreWithResponse(ctx, restoreConfig)
if err != nil {
return 0, nil, fmt.Errorf("restore: %w", err)
}
if resp.StatusCode() != 204 {
return 0, nil, fmt.Errorf("restore failed with status %d: %s", resp.StatusCode(), string(resp.Body))
}
log.DebugContext(ctx, "CH restore API complete", "duration_ms", time.Since(restoreAPIStart).Milliseconds())
// Success - release cleanup to prevent killing the process
cu.Release()
log.DebugContext(ctx, "CH restore complete", "pid", pid, "total_duration_ms", time.Since(startTime).Milliseconds())
return pid, hv, nil
}
func ptr[T any](v T) *T {
return &v
}
func logStartVMFailureDiagnostics(ctx context.Context, log *slog.Logger, socketPath string, pid int, operation string, requestErr error, statusCode int, responseBody string) {
if log == nil {
return
}
socketExists := false
if _, err := os.Stat(socketPath); err == nil {
socketExists = true
}
socketDialable, socketDialErr := canDialUnixSocket(socketPath)
processRunning := false
if pid > 0 && syscall.Kill(pid, 0) == nil {
processRunning = true
}
ctxErr := ctx.Err()
ctxCause := context.Cause(ctx)
deadline, hasDeadline := ctx.Deadline()
attrs := []any{
"operation", operation,
"socket_path", socketPath,
"socket_exists", socketExists,
"socket_dialable", socketDialable,
"pid", pid,
"process_running", processRunning,
"ctx_err", errorString(ctxErr),
"ctx_cause", errorString(ctxCause),
"vmm_log_tail", tailFile(filepath.Join(filepath.Dir(socketPath), "logs", "vmm.log"), 4096),
}
if hasDeadline {
attrs = append(attrs,
"ctx_deadline", deadline.UTC().Format(time.RFC3339Nano),
"ctx_deadline_in_ms", time.Until(deadline).Milliseconds(),
)
}
if socketDialErr != nil {
attrs = append(attrs, "socket_dial_error", socketDialErr.Error())
}
if requestErr != nil {
attrs = append(attrs,
"request_error", requestErr.Error(),
"request_error_is_context_canceled", errors.Is(requestErr, context.Canceled),
"request_error_is_deadline_exceeded", errors.Is(requestErr, context.DeadlineExceeded),
)
}
if statusCode > 0 {
attrs = append(attrs, "response_status_code", statusCode)
}
if responseBody != "" {
attrs = append(attrs, "response_body", truncateString(responseBody, 1024))
}
log.ErrorContext(ctx, "cloud-hypervisor start_vm diagnostics", attrs...)
}
func canDialUnixSocket(socketPath string) (bool, error) {
conn, err := net.DialTimeout("unix", socketPath, 100*time.Millisecond)
if err != nil {
return false, err
}
_ = conn.Close()
return true, nil
}
func tailFile(path string, maxBytes int) string {
data, err := os.ReadFile(path)
if err != nil {
return fmt.Sprintf("unavailable: %v", err)
}
if len(data) > maxBytes {
data = data[len(data)-maxBytes:]
}
return truncateString(strings.TrimSpace(string(data)), maxBytes)
}
func truncateString(s string, max int) string {
if max <= 0 || len(s) <= max {
return s
}
return s[:max] + "...(truncated)"
}
func errorString(err error) string {
if err == nil {
return ""
}
return err.Error()
}