-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.go
More file actions
208 lines (186 loc) · 5.91 KB
/
snapshot.go
File metadata and controls
208 lines (186 loc) · 5.91 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
package knaller
import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/benben/knaller/firecracker"
)
// Snapshot holds the metadata stored alongside a Firecracker snapshot.
// Written to metadata.json in the snapshot directory.
type Snapshot struct {
ID string `json:"id"`
VMName string `json:"vm_name"`
CreatedAt time.Time `json:"created_at"`
VCPUs int `json:"vcpus"`
MemSizeMib int `json:"mem_size_mib"`
Ports []PortMapping `json:"ports,omitempty"`
}
// snapshotID generates a random snapshot ID with the "snap_" prefix.
func snapshotID() string {
b := make([]byte, 4)
rand.Read(b)
return "snap_" + hex.EncodeToString(b)
}
func snapshotBaseDir() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".local", "share", "knaller", "snapshots")
}
func snapshotDir(id string) string {
return filepath.Join(snapshotBaseDir(), id)
}
// CreateSnapshot takes a snapshot of a running VM. The VM is briefly paused
// while Firecracker writes the device state and memory dump, then resumed.
// The VM's rootfs is also copied into the snapshot directory.
//
// Before taking the snapshot, we patch the drive to point to the snapshot
// directory's rootfs. This makes the snapshot self-contained: the state file
// references a path inside the snapshot directory, not the original VM's
// rootfs. This allows restoring even after the original VM is deleted.
//
// Progress messages are written to w (pass nil for silent operation).
// Returns the snapshot ID.
func CreateSnapshot(ctx context.Context, vmName string, w io.Writer) (string, error) {
if w == nil {
w = io.Discard
}
socketPath := filepath.Join(socketDirectory(), vmName+".socket")
if _, err := os.Stat(socketPath); err != nil {
return "", fmt.Errorf("VM %q not found (no socket at %s)", vmName, socketPath)
}
client := firecracker.NewClient(socketPath)
vmCfg, err := client.GetVMConfig(ctx)
if err != nil {
return "", fmt.Errorf("get vm config: %w", err)
}
id := snapshotID()
dir := snapshotDir(id)
if err := os.MkdirAll(dir, 0o755); err != nil {
return "", fmt.Errorf("create snapshot dir: %w", err)
}
fmt.Fprintf(w, "Copying rootfs...\n")
srcRootfs := filepath.Join(vmDataDir(vmName), "rootfs.ext4")
dstRootfs := filepath.Join(dir, "rootfs.ext4")
cp := exec.CommandContext(ctx, "cp", "--reflink=auto", srcRootfs, dstRootfs)
if out, err := cp.CombinedOutput(); err != nil {
os.RemoveAll(dir)
return "", fmt.Errorf("copy rootfs: %s: %w", out, err)
}
fmt.Fprintf(w, "Pausing VM %q...\n", vmName)
if err := client.PauseVM(ctx); err != nil {
os.RemoveAll(dir)
return "", fmt.Errorf("pause vm: %w", err)
}
// Always resume the VM and restore the original drive path, even on error.
var snapErr error
defer func() {
// Restore the drive to the original rootfs path.
client.PatchDrive(ctx, "rootfs", srcRootfs)
fmt.Fprintf(w, "Resuming VM...\n")
if rerr := client.ResumeVM(ctx); rerr != nil {
if snapErr != nil {
snapErr = fmt.Errorf("%w; also failed to resume: %v", snapErr, rerr)
} else {
snapErr = fmt.Errorf("resume vm: %w", rerr)
}
}
}()
// Patch the drive to point to the snapshot's rootfs copy. This way the
// Firecracker snapshot state bakes in the snapshot directory path, making
// the snapshot fully self-contained.
if err := client.PatchDrive(ctx, "rootfs", dstRootfs); err != nil {
os.RemoveAll(dir)
snapErr = fmt.Errorf("patch drive for snapshot: %w", err)
return "", snapErr
}
fmt.Fprintf(w, "Creating snapshot...\n")
statePath := filepath.Join(dir, "state")
memPath := filepath.Join(dir, "memory")
if err := client.CreateSnapshot(ctx, &firecracker.SnapshotCreate{
SnapshotType: "Full",
SnapshotPath: statePath,
MemFilePath: memPath,
}); err != nil {
os.RemoveAll(dir)
snapErr = fmt.Errorf("create snapshot: %w", err)
return "", snapErr
}
meta := &Snapshot{
ID: id,
VMName: vmName,
CreatedAt: time.Now(),
Ports: loadVMPorts(vmName),
}
if vmCfg.MachineConfig != nil {
meta.VCPUs = vmCfg.MachineConfig.VcpuCount
meta.MemSizeMib = vmCfg.MachineConfig.MemSizeMib
}
metaData, err := json.MarshalIndent(meta, "", " ")
if err != nil {
os.RemoveAll(dir)
snapErr = fmt.Errorf("marshal metadata: %w", err)
return "", snapErr
}
if err := os.WriteFile(filepath.Join(dir, "metadata.json"), metaData, 0o644); err != nil {
os.RemoveAll(dir)
snapErr = fmt.Errorf("write metadata: %w", err)
return "", snapErr
}
return id, snapErr
}
// DeleteSnapshot removes a snapshot and all its files (state, memory, rootfs).
func DeleteSnapshot(id string) error {
dir := snapshotDir(id)
if _, err := os.Stat(dir); err != nil {
return fmt.Errorf("snapshot %q not found", id)
}
return os.RemoveAll(dir)
}
// GetSnapshot reads the metadata for a single snapshot by ID.
func GetSnapshot(id string) (*Snapshot, error) {
data, err := os.ReadFile(filepath.Join(snapshotDir(id), "metadata.json"))
if err != nil {
return nil, fmt.Errorf("snapshot %q not found", id)
}
var s Snapshot
if err := json.Unmarshal(data, &s); err != nil {
return nil, fmt.Errorf("corrupt snapshot metadata: %w", err)
}
return &s, nil
}
// ListSnapshots discovers all snapshots by scanning the snapshots directory
// and reading each metadata.json file. Corrupt or incomplete snapshots are
// silently skipped.
func ListSnapshots() ([]*Snapshot, error) {
dir := snapshotBaseDir()
entries, err := os.ReadDir(dir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
var snapshots []*Snapshot
for _, e := range entries {
if !e.IsDir() || !strings.HasPrefix(e.Name(), "snap_") {
continue
}
data, err := os.ReadFile(filepath.Join(dir, e.Name(), "metadata.json"))
if err != nil {
continue
}
var s Snapshot
if err := json.Unmarshal(data, &s); err != nil {
continue
}
snapshots = append(snapshots, &s)
}
return snapshots, nil
}