-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
driver_zfs_utils.go
471 lines (375 loc) · 11.1 KB
/
driver_zfs_utils.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package drivers
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pborman/uuid"
"github.com/lxc/lxd/lxd/migration"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/ioprogress"
"github.com/lxc/lxd/shared/units"
)
const (
// zfsBlockVolSuffix suffix used for block content type volumes.
zfsBlockVolSuffix = ".block"
// zfsMinBlockSize is a minimum value for recordsize and volblocksize properties.
zfsMinBlocksize = 512
// zfsMinBlockSize is a maximum value for recordsize and volblocksize properties.
zfsMaxBlocksize = 16 * 1024 * 1024
// zfsMaxVolBlocksize is a maximum value for volblocksize property.
zfsMaxVolBlocksize = 128 * 1024
)
func (d *zfs) dataset(vol Volume, deleted bool) string {
name, snapName, _ := api.GetParentAndSnapshotName(vol.name)
if vol.volType == VolumeTypeImage && vol.contentType == ContentTypeFS && d.isBlockBacked(vol) {
name = fmt.Sprintf("%s_%s", name, vol.ConfigBlockFilesystem())
}
if (vol.volType == VolumeTypeVM || vol.volType == VolumeTypeImage) && vol.contentType == ContentTypeBlock {
name = fmt.Sprintf("%s%s", name, zfsBlockVolSuffix)
}
if snapName != "" {
if deleted {
name = fmt.Sprintf("%s@deleted-%s", name, uuid.New())
} else {
name = fmt.Sprintf("%s@snapshot-%s", name, snapName)
}
} else if deleted {
if vol.volType != VolumeTypeImage {
name = uuid.New()
}
return filepath.Join(d.config["zfs.pool_name"], "deleted", string(vol.volType), name)
}
return filepath.Join(d.config["zfs.pool_name"], string(vol.volType), name)
}
func (d *zfs) createDataset(dataset string, options ...string) error {
args := []string{"create"}
for _, option := range options {
args = append(args, "-o")
args = append(args, option)
}
args = append(args, dataset)
_, err := shared.RunCommand("zfs", args...)
if err != nil {
return err
}
return nil
}
func (d *zfs) createVolume(dataset string, size int64, options ...string) error {
size = d.roundVolumeBlockSizeBytes(size)
args := []string{"create", "-s", "-V", fmt.Sprintf("%d", size)}
for _, option := range options {
args = append(args, "-o")
args = append(args, option)
}
args = append(args, dataset)
_, err := shared.RunCommand("zfs", args...)
if err != nil {
return err
}
return nil
}
func (d *zfs) datasetExists(dataset string) (bool, error) {
out, err := shared.RunCommand("zfs", "get", "-H", "-o", "name", "name", dataset)
if err != nil {
return false, nil
}
return strings.TrimSpace(out) == dataset, nil
}
func (d *zfs) deleteDatasetRecursive(dataset string) error {
// Locate the origin snapshot (if any).
origin, err := d.getDatasetProperty(dataset, "origin")
if err != nil {
return err
}
// Delete the dataset (and any snapshots left).
_, err = shared.TryRunCommand("zfs", "destroy", "-r", dataset)
if err != nil {
return err
}
// Check if the origin can now be deleted.
if origin != "" && origin != "-" {
if strings.HasPrefix(origin, filepath.Join(d.config["zfs.pool_name"], "deleted")) {
// Strip the snapshot name when dealing with a deleted volume.
dataset = strings.SplitN(origin, "@", 2)[0]
} else if strings.Contains(origin, "@deleted-") || strings.Contains(origin, "@copy-") {
// Handle deleted snapshots.
dataset = origin
} else {
// Origin is still active.
dataset = ""
}
if dataset != "" {
// Get all clones.
clones, err := d.getClones(dataset)
if err != nil {
return err
}
if len(clones) == 0 {
// Delete the origin.
err = d.deleteDatasetRecursive(dataset)
if err != nil {
return err
}
}
}
}
return nil
}
func (d *zfs) getClones(dataset string) ([]string, error) {
out, err := shared.RunCommand("zfs", "get", "-H", "-p", "-r", "-o", "value", "clones", dataset)
if err != nil {
return nil, err
}
clones := []string{}
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line == dataset || line == "" || line == "-" {
continue
}
line = strings.TrimPrefix(line, fmt.Sprintf("%s/", dataset))
clones = append(clones, line)
}
return clones, nil
}
func (d *zfs) getDatasets(dataset string) ([]string, error) {
out, err := shared.RunCommand("zfs", "get", "-H", "-r", "-o", "name", "name", dataset)
if err != nil {
return nil, err
}
children := []string{}
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line == dataset || line == "" {
continue
}
line = strings.TrimPrefix(line, dataset)
line = strings.TrimPrefix(line, "/")
children = append(children, line)
}
return children, nil
}
func (d *zfs) setDatasetProperties(dataset string, options ...string) error {
if len(zfsVersion) >= 3 && zfsVersion[0:3] == "0.6" {
// Slow path for ZFS 0.6
for _, option := range options {
_, err := shared.RunCommand("zfs", "set", option, dataset)
if err != nil {
return err
}
}
return nil
}
args := []string{"set"}
args = append(args, options...)
args = append(args, dataset)
_, err := shared.RunCommand("zfs", args...)
if err != nil {
return err
}
return nil
}
func (d *zfs) setBlocksizeFromConfig(vol Volume) error {
size := vol.ExpandedConfig("zfs.blocksize")
if size == "" {
return nil
}
// Convert to bytes.
sizeBytes, err := units.ParseByteSizeString(size)
if err != nil {
return err
}
return d.setBlocksize(vol, sizeBytes)
}
func (d *zfs) setBlocksize(vol Volume, size int64) error {
if vol.contentType != ContentTypeFS {
return nil
}
err := d.setDatasetProperties(d.dataset(vol, false), fmt.Sprintf("recordsize=%d", size))
if err != nil {
return err
}
return nil
}
func (d *zfs) getDatasetProperty(dataset string, key string) (string, error) {
output, err := shared.RunCommand("zfs", "get", "-H", "-p", "-o", "value", key, dataset)
if err != nil {
return "", err
}
return strings.TrimSpace(output), nil
}
func (d *zfs) getDatasetProperties(dataset string, keys ...string) (map[string]string, error) {
output, err := shared.RunCommand("zfs", "get", "-H", "-p", "-o", "property,value", strings.Join(keys, ","), dataset)
if err != nil {
return nil, err
}
props := make(map[string]string, len(keys))
for _, row := range strings.Split(output, "\n") {
prop := strings.Split(row, "\t")
if len(prop) < 2 {
continue
}
key := prop[0]
val := prop[1]
props[key] = val
}
return props, nil
}
// version returns the ZFS version based on package or kernel module version.
func (d *zfs) version() (string, error) {
// This function is only really ever relevant on Ubuntu as the only
// distro that ships out of sync tools and kernel modules
out, err := shared.RunCommand("dpkg-query", "--showformat=${Version}", "--show", "zfsutils-linux")
if err == nil {
return strings.TrimSpace(string(out)), nil
}
// Loaded kernel module version
if shared.PathExists("/sys/module/zfs/version") {
out, err := os.ReadFile("/sys/module/zfs/version")
if err == nil {
return strings.TrimSpace(string(out)), nil
}
}
// Module information version
out, err = shared.RunCommand("modinfo", "-F", "version", "zfs")
if err == nil {
return strings.TrimSpace(string(out)), nil
}
return "", fmt.Errorf("Could not determine ZFS module version")
}
// initialDatasets returns the list of all expected datasets.
func (d *zfs) initialDatasets() []string {
entries := []string{"deleted"}
// Iterate over the listed supported volume types.
for _, volType := range d.Info().VolumeTypes {
entries = append(entries, BaseDirectories[volType][0])
entries = append(entries, filepath.Join("deleted", BaseDirectories[volType][0]))
}
return entries
}
func (d *zfs) sendDataset(dataset string, parent string, volSrcArgs *migration.VolumeSourceArgs, conn io.ReadWriteCloser, tracker *ioprogress.ProgressTracker) error {
defer func() { _ = conn.Close() }()
// Assemble zfs send command.
args := []string{"send"}
if shared.StringInSlice("compress", volSrcArgs.MigrationType.Features) {
args = append(args, "-c")
args = append(args, "-L")
}
if parent != "" {
args = append(args, "-i", parent)
}
args = append(args, dataset)
cmd := exec.Command("zfs", args...)
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
// Setup progress tracker.
var stdout io.WriteCloser = conn
if tracker != nil {
stdout = &ioprogress.ProgressWriter{
WriteCloser: conn,
Tracker: tracker,
}
}
cmd.Stdout = stdout
// Run the command.
err = cmd.Start()
if err != nil {
return err
}
// Read any error.
output, _ := io.ReadAll(stderr)
// Handle errors.
err = cmd.Wait()
if err != nil {
return fmt.Errorf("zfs send failed: %w (%s)", err, string(output))
}
return nil
}
func (d *zfs) receiveDataset(vol Volume, conn io.ReadWriteCloser, writeWrapper func(io.WriteCloser) io.WriteCloser) error {
// Assemble zfs receive command.
cmd := exec.Command("zfs", "receive", "-x", "mountpoint", "-F", "-u", d.dataset(vol, false))
if vol.ContentType() == ContentTypeBlock || d.isBlockBacked(vol) {
cmd = exec.Command("zfs", "receive", "-F", "-u", d.dataset(vol, false))
}
// Prepare stdin/stderr.
stdin, err := cmd.StdinPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
// Forward input through stdin.
chCopyConn := make(chan error, 1)
go func() {
_, err = io.Copy(stdin, conn)
_ = stdin.Close()
chCopyConn <- err
}()
// Run the command.
err = cmd.Start()
if err != nil {
return err
}
// Read any error.
output, _ := io.ReadAll(stderr)
// Handle errors.
errs := []error{}
chCopyConnErr := <-chCopyConn
err = cmd.Wait()
if err != nil {
errs = append(errs, err)
if chCopyConnErr != nil {
errs = append(errs, chCopyConnErr)
}
}
if len(errs) > 0 {
return fmt.Errorf("Problem with zfs receive: (%v) %s", errs, string(output))
}
return nil
}
// ValidateZfsBlocksize validates blocksize property value on the pool.
func ValidateZfsBlocksize(value string) error {
// Convert to bytes.
sizeBytes, err := units.ParseByteSizeString(value)
if err != nil {
return err
}
if sizeBytes < zfsMinBlocksize || sizeBytes > zfsMaxBlocksize || (sizeBytes&(sizeBytes-1)) != 0 {
return fmt.Errorf("Value should be between 512 and 16MiB, and be power of 2")
}
return nil
}
// ZFSDataset is the structure used to store information about a dataset.
type ZFSDataset struct {
Name string `json:"name" yaml:"name"`
GUID string `json:"guid" yaml:"guid"`
}
// ZFSMetaDataHeader is the meta data header about the datasets being sent/stored.
type ZFSMetaDataHeader struct {
SnapshotDatasets []ZFSDataset `json:"snapshot_datasets" yaml:"snapshot_datasets"`
}
func (d *zfs) datasetHeader(vol Volume, snapshots []string) (*ZFSMetaDataHeader, error) {
migrationHeader := ZFSMetaDataHeader{
SnapshotDatasets: make([]ZFSDataset, len(snapshots)),
}
for i, snapName := range snapshots {
snapVol, _ := vol.NewSnapshot(snapName)
guid, err := d.getDatasetProperty(d.dataset(snapVol, false), "guid")
if err != nil {
return nil, err
}
migrationHeader.SnapshotDatasets[i].Name = snapName
migrationHeader.SnapshotDatasets[i].GUID = guid
}
return &migrationHeader, nil
}
func (d *zfs) randomVolumeName(vol Volume) string {
return fmt.Sprintf("%s_%s", vol.name, uuid.New())
}