|
6 | 6 | "log/slog" |
7 | 7 | "os" |
8 | 8 | "os/exec" |
| 9 | + "path/filepath" |
9 | 10 | "sync/atomic" |
10 | 11 | "time" |
11 | 12 |
|
@@ -93,6 +94,134 @@ func (vm *vm) Start(ctx context.Context) error { |
93 | 94 |
|
94 | 95 | } |
95 | 96 |
|
| 97 | +// StartFromSnapshot starts the VM by restoring from a snapshot instead of cold booting. |
| 98 | +// This enables sub-100ms cold starts for AI sandbox workloads. |
| 99 | +// globalSnapshotPath is the source path on the host (e.g., /var/lib/ravel/global-snapshots/instance-id/snap-1) |
| 100 | +// jailSnapshotPath is the jail-relative path (e.g., /snapshots/snap-1) |
| 101 | +func (vm *vm) StartFromSnapshot(ctx context.Context, globalSnapshotPath, jailSnapshotPath string) error { |
| 102 | + bootStart := time.Now() |
| 103 | + |
| 104 | + // Copy snapshot from global storage into the jail |
| 105 | + jailHostPath := getInstanceDir(vm.id) + jailSnapshotPath |
| 106 | + if err := copyDir(globalSnapshotPath, jailHostPath); err != nil { |
| 107 | + return fmt.Errorf("failed to copy snapshot to jail: %w", err) |
| 108 | + } |
| 109 | + |
| 110 | + // Chown to ravel-jailer user so CloudHypervisor can read |
| 111 | + jailerUid, jailerGid, err := setupRavelJailerUser() |
| 112 | + if err != nil { |
| 113 | + return fmt.Errorf("failed to get jailer user: %w", err) |
| 114 | + } |
| 115 | + if err := chownRecursive(jailHostPath, jailerUid, jailerGid); err != nil { |
| 116 | + return fmt.Errorf("failed to chown snapshot directory: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + slog.Debug("snapshot copied to jail", "from", globalSnapshotPath, "to", jailHostPath) |
| 120 | + |
| 121 | + err = vm.cmd.Start() |
| 122 | + if err != nil { |
| 123 | + metrics.VMBootsTotal.WithLabelValues("failure").Inc() |
| 124 | + return fmt.Errorf("failed to start vmm for machine %q: %w", vm.Id(), err) |
| 125 | + } |
| 126 | + defer func() { |
| 127 | + if err != nil { |
| 128 | + vm.vmm.ShutdownVMM(ctx) |
| 129 | + } |
| 130 | + }() |
| 131 | + |
| 132 | + err = vm.vmm.WaitReady(ctx) |
| 133 | + if err != nil { |
| 134 | + metrics.VMBootsTotal.WithLabelValues("failure").Inc() |
| 135 | + return fmt.Errorf("failed to wait for vmm to be ready for machine %q: %w", vm.Id(), err) |
| 136 | + } |
| 137 | + |
| 138 | + // Restore from snapshot instead of create+boot |
| 139 | + snapshotUrl := "file://" + jailSnapshotPath |
| 140 | + prefault := true |
| 141 | + _, err = vm.vmm.PutVmRestore(ctx, cloudhypervisor.RestoreConfig{ |
| 142 | + SourceUrl: snapshotUrl, |
| 143 | + Prefault: &prefault, |
| 144 | + }) |
| 145 | + if err != nil { |
| 146 | + metrics.VMBootsTotal.WithLabelValues("failure").Inc() |
| 147 | + return fmt.Errorf("failed to restore vm from snapshot for machine %q: %w", vm.Id(), err) |
| 148 | + } |
| 149 | + |
| 150 | + // Resume the restored VM |
| 151 | + _, err = vm.vmm.ResumeVM(ctx) |
| 152 | + if err != nil { |
| 153 | + metrics.VMBootsTotal.WithLabelValues("failure").Inc() |
| 154 | + return fmt.Errorf("failed to resume vm after restore for machine %q: %w", vm.Id(), err) |
| 155 | + } |
| 156 | + |
| 157 | + // Record successful boot metrics |
| 158 | + bootDuration := time.Since(bootStart).Seconds() |
| 159 | + metrics.VMBootDuration.Observe(bootDuration) |
| 160 | + metrics.VMBootsTotal.WithLabelValues("success").Inc() |
| 161 | + |
| 162 | + slog.Info("VM restored from snapshot", "id", vm.id, "duration_ms", bootDuration*1000) |
| 163 | + |
| 164 | + go vm.run() |
| 165 | + |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +// copyDir copies a directory from src to dst |
| 170 | +func copyDir(src, dst string) error { |
| 171 | + if err := os.MkdirAll(dst, 0755); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + entries, err := os.ReadDir(src) |
| 176 | + if err != nil { |
| 177 | + return err |
| 178 | + } |
| 179 | + |
| 180 | + for _, entry := range entries { |
| 181 | + srcPath := src + "/" + entry.Name() |
| 182 | + dstPath := dst + "/" + entry.Name() |
| 183 | + |
| 184 | + if entry.IsDir() { |
| 185 | + if err := copyDir(srcPath, dstPath); err != nil { |
| 186 | + return err |
| 187 | + } |
| 188 | + } else { |
| 189 | + if err := copyFile(srcPath, dstPath); err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + return nil |
| 195 | +} |
| 196 | + |
| 197 | +// copyFile copies a file from src to dst |
| 198 | +func copyFile(src, dst string) error { |
| 199 | + srcFile, err := os.Open(src) |
| 200 | + if err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + defer srcFile.Close() |
| 204 | + |
| 205 | + dstFile, err := os.Create(dst) |
| 206 | + if err != nil { |
| 207 | + return err |
| 208 | + } |
| 209 | + defer dstFile.Close() |
| 210 | + |
| 211 | + _, err = dstFile.ReadFrom(srcFile) |
| 212 | + return err |
| 213 | +} |
| 214 | + |
| 215 | +// chownRecursive changes ownership of a directory and all its contents |
| 216 | +func chownRecursive(path string, uid, gid int) error { |
| 217 | + return filepath.Walk(path, func(name string, info os.FileInfo, err error) error { |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + return os.Chown(name, uid, gid) |
| 222 | + }) |
| 223 | +} |
| 224 | + |
96 | 225 | func (vm *vm) Signal(ctx context.Context, signal string) error { |
97 | 226 | sig := syscallSignal(signal) |
98 | 227 |
|
|
0 commit comments