forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_zfs_utils.go
535 lines (454 loc) · 12.1 KB
/
storage_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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/logger"
)
// zfsIsEnabled returns whether zfs backend is supported.
func zfsIsEnabled() bool {
out, err := exec.LookPath("zfs")
if err != nil || len(out) == 0 {
return false
}
return true
}
// zfsModuleVersionGet returhs the ZFS module version
func zfsModuleVersionGet() (string, error) {
zfsVersion, err := ioutil.ReadFile("/sys/module/zfs/version")
if err != nil {
return "", fmt.Errorf("could not determine ZFS module version")
}
return strings.TrimSpace(string(zfsVersion)), nil
}
// zfsPoolVolumeCreate creates a ZFS dataset with a set of given properties.
func zfsPoolVolumeCreate(dataset string, properties ...string) (string, error) {
cmd := []string{"zfs", "create"}
for _, prop := range properties {
cmd = append(cmd, []string{"-o", prop}...)
}
cmd = append(cmd, []string{"-p", dataset}...)
return shared.RunCommand(cmd[0], cmd[1:]...)
}
func zfsPoolCheck(pool string) error {
output, err := shared.RunCommand(
"zfs", "get", "-H", "-o", "value", "type", pool)
if err != nil {
return fmt.Errorf(strings.Split(output, "\n")[0])
}
poolType := strings.Split(output, "\n")[0]
if poolType != "filesystem" {
return fmt.Errorf("Unsupported pool type: %s", poolType)
}
return nil
}
func zfsPoolCreate(pool string, vdev string) error {
var output string
var err error
if pool == "" {
output, err := shared.RunCommand(
"zfs", "create", "-p", "-o", "mountpoint=none", vdev)
if err != nil {
logger.Errorf("zfs create failed: %s.", output)
return fmt.Errorf("Failed to create ZFS filesystem: %s", output)
}
} else {
output, err = shared.RunCommand(
"zpool", "create", "-f", "-m", "none", "-O", "compression=on", pool, vdev)
if err != nil {
logger.Errorf("zfs create failed: %s.", output)
return fmt.Errorf("Failed to create the ZFS pool: %s", output)
}
}
return nil
}
func zfsPoolVolumeClone(pool string, source string, name string, dest string, mountpoint string) error {
output, err := shared.RunCommand(
"zfs",
"clone",
"-p",
"-o", fmt.Sprintf("mountpoint=%s", mountpoint),
"-o", "canmount=noauto",
fmt.Sprintf("%s/%s@%s", pool, source, name),
fmt.Sprintf("%s/%s", pool, dest))
if err != nil {
logger.Errorf("zfs clone failed: %s.", output)
return fmt.Errorf("Failed to clone the filesystem: %s", output)
}
subvols, err := zfsPoolListSubvolumes(pool, fmt.Sprintf("%s/%s", pool, source))
if err != nil {
return err
}
for _, sub := range subvols {
snaps, err := zfsPoolListSnapshots(pool, sub)
if err != nil {
return err
}
if !shared.StringInSlice(name, snaps) {
continue
}
destSubvol := dest + strings.TrimPrefix(sub, source)
snapshotMntPoint := getSnapshotMountPoint(pool, destSubvol)
output, err := shared.RunCommand(
"zfs",
"clone",
"-p",
"-o", fmt.Sprintf("mountpoint=%s", snapshotMntPoint),
"-o", "canmount=noauto",
fmt.Sprintf("%s/%s@%s", pool, sub, name),
fmt.Sprintf("%s/%s", pool, destSubvol))
if err != nil {
logger.Errorf("zfs clone failed: %s.", output)
return fmt.Errorf("Failed to clone the sub-volume: %s", output)
}
}
return nil
}
func zfsFilesystemEntityDelete(vdev string, pool string) error {
var output string
var err error
if strings.Contains(pool, "/") {
// Command to destroy a zfs dataset.
output, err = shared.RunCommand("zfs", "destroy", "-r", pool)
} else {
// Command to destroy a zfs pool.
output, err = shared.RunCommand("zpool", "destroy", "-f", pool)
}
if err != nil {
return fmt.Errorf("Failed to delete the ZFS pool: %s", output)
}
// Cleanup storage
if filepath.IsAbs(vdev) && !shared.IsBlockdevPath(vdev) {
os.RemoveAll(vdev)
}
return nil
}
func zfsPoolVolumeDestroy(pool string, path string) error {
mountpoint, err := zfsFilesystemEntityPropertyGet(pool, path, "mountpoint")
if err != nil {
return err
}
if mountpoint != "none" && shared.IsMountPoint(mountpoint) {
err := syscall.Unmount(mountpoint, syscall.MNT_DETACH)
if err != nil {
logger.Errorf("umount failed: %s.", err)
return err
}
}
// Due to open fds or kernel refs, this may fail for a bit, give it 10s
output, err := shared.TryRunCommand(
"zfs",
"destroy",
"-r",
fmt.Sprintf("%s/%s", pool, path))
if err != nil {
logger.Errorf("zfs destroy failed: %s.", output)
return fmt.Errorf("Failed to destroy ZFS filesystem: %s", output)
}
return nil
}
func zfsPoolVolumeCleanup(pool string, path string) error {
if strings.HasPrefix(path, "deleted/") {
// Cleanup of filesystems kept for refcount reason
removablePath, err := zfsPoolVolumeSnapshotRemovable(pool, path, "")
if err != nil {
return err
}
// Confirm that there are no more clones
if removablePath {
if strings.Contains(path, "@") {
// Cleanup snapshots
err = zfsPoolVolumeDestroy(pool, path)
if err != nil {
return err
}
// Check if the parent can now be deleted
subPath := strings.SplitN(path, "@", 2)[0]
snaps, err := zfsPoolListSnapshots(pool, subPath)
if err != nil {
return err
}
if len(snaps) == 0 {
err := zfsPoolVolumeCleanup(pool, subPath)
if err != nil {
return err
}
}
} else {
// Cleanup filesystems
origin, err := zfsFilesystemEntityPropertyGet(pool, path, "origin")
if err != nil {
return err
}
origin = strings.TrimPrefix(origin, fmt.Sprintf("%s/", pool))
err = zfsPoolVolumeDestroy(pool, path)
if err != nil {
return err
}
// Attempt to remove its parent
if origin != "-" {
err := zfsPoolVolumeCleanup(pool, origin)
if err != nil {
return err
}
}
}
return nil
}
} else if strings.HasPrefix(path, "containers") && strings.Contains(path, "@copy-") {
// Just remove the copy- snapshot for copies of active containers
err := zfsPoolVolumeDestroy(pool, path)
if err != nil {
return err
}
}
return nil
}
func zfsFilesystemEntityPropertyGet(pool string, path string, key string) (string, error) {
entity := pool
if path != "" {
entity = fmt.Sprintf("%s/%s", pool, path)
}
output, err := shared.RunCommand(
"zfs",
"get",
"-H",
"-p",
"-o", "value",
key,
entity)
if err != nil {
return "", fmt.Errorf("Failed to get ZFS config: %s", output)
}
return strings.TrimRight(output, "\n"), nil
}
func zfsPoolVolumeRename(pool string, source string, dest string) error {
var err error
var output string
for i := 0; i < 20; i++ {
output, err = shared.RunCommand(
"zfs",
"rename",
"-p",
fmt.Sprintf("%s/%s", pool, source),
fmt.Sprintf("%s/%s", pool, dest))
// Success
if err == nil {
return nil
}
// zfs rename can fail because of descendants, yet still manage the rename
if !zfsFilesystemEntityExists(pool, source) && zfsFilesystemEntityExists(pool, dest) {
return nil
}
time.Sleep(500 * time.Millisecond)
}
// Timeout
logger.Errorf("zfs rename failed: %s.", output)
return fmt.Errorf("Failed to rename ZFS filesystem: %s", output)
}
func zfsPoolVolumeSet(pool string, path string, key string, value string) error {
vdev := pool
if path != "" {
vdev = fmt.Sprintf("%s/%s", pool, path)
}
output, err := shared.RunCommand(
"zfs",
"set",
fmt.Sprintf("%s=%s", key, value),
vdev)
if err != nil {
logger.Errorf("zfs set failed: %s.", output)
return fmt.Errorf("Failed to set ZFS config: %s", output)
}
return nil
}
func zfsPoolVolumeSnapshotCreate(pool string, path string, name string) error {
output, err := shared.RunCommand(
"zfs",
"snapshot",
"-r",
fmt.Sprintf("%s/%s@%s", pool, path, name))
if err != nil {
logger.Errorf("zfs snapshot failed: %s.", output)
return fmt.Errorf("Failed to create ZFS snapshot: %s", output)
}
return nil
}
func zfsPoolVolumeSnapshotDestroy(pool, path string, name string) error {
output, err := shared.RunCommand(
"zfs",
"destroy",
"-r",
fmt.Sprintf("%s/%s@%s", pool, path, name))
if err != nil {
logger.Errorf("zfs destroy failed: %s.", output)
return fmt.Errorf("Failed to destroy ZFS snapshot: %s", output)
}
return nil
}
func zfsPoolVolumeSnapshotRestore(pool string, path string, name string) error {
output, err := shared.TryRunCommand(
"zfs",
"rollback",
fmt.Sprintf("%s/%s@%s", pool, path, name))
if err != nil {
logger.Errorf("zfs rollback failed: %s.", output)
return fmt.Errorf("Failed to restore ZFS snapshot: %s", output)
}
subvols, err := zfsPoolListSubvolumes(pool, fmt.Sprintf("%s/%s", pool, path))
if err != nil {
return err
}
for _, sub := range subvols {
snaps, err := zfsPoolListSnapshots(pool, sub)
if err != nil {
return err
}
if !shared.StringInSlice(name, snaps) {
continue
}
output, err := shared.TryRunCommand(
"zfs",
"rollback",
fmt.Sprintf("%s/%s@%s", pool, sub, name))
if err != nil {
logger.Errorf("zfs rollback failed: %s.", output)
return fmt.Errorf("Failed to restore ZFS sub-volume snapshot: %s", output)
}
}
return nil
}
func zfsPoolVolumeSnapshotRename(pool string, path string, oldName string, newName string) error {
output, err := shared.RunCommand(
"zfs",
"rename",
"-r",
fmt.Sprintf("%s/%s@%s", pool, path, oldName),
fmt.Sprintf("%s/%s@%s", pool, path, newName))
if err != nil {
logger.Errorf("zfs snapshot rename failed: %s.", output)
return fmt.Errorf("Failed to rename ZFS snapshot: %s", output)
}
return nil
}
func zfsMount(poolName string, path string) error {
output, err := shared.TryRunCommand(
"zfs",
"mount",
fmt.Sprintf("%s/%s", poolName, path))
if err != nil {
return fmt.Errorf("Failed to mount ZFS filesystem: %s", output)
}
return nil
}
func zfsUmount(poolName string, path string, mountpoint string) error {
output, err := shared.TryRunCommand(
"zfs",
"unmount",
fmt.Sprintf("%s/%s", poolName, path))
if err != nil {
logger.Warnf("Failed to unmount ZFS filesystem via zfs unmount: %s. Trying lazy umount (MNT_DETACH)...", output)
err := tryUnmount(mountpoint, syscall.MNT_DETACH)
if err != nil {
logger.Warnf("Failed to unmount ZFS filesystem via lazy umount (MNT_DETACH)...")
return err
}
}
return nil
}
func zfsPoolListSubvolumes(pool string, path string) ([]string, error) {
output, err := shared.RunCommand(
"zfs",
"list",
"-t", "filesystem",
"-o", "name",
"-H",
"-r", path)
if err != nil {
logger.Errorf("zfs list failed: %s.", output)
return []string{}, fmt.Errorf("Failed to list ZFS filesystems: %s", output)
}
children := []string{}
for _, entry := range strings.Split(output, "\n") {
if entry == "" {
continue
}
if entry == path {
continue
}
children = append(children, strings.TrimPrefix(entry, fmt.Sprintf("%s/", pool)))
}
return children, nil
}
func zfsPoolListSnapshots(pool string, path string) ([]string, error) {
path = strings.TrimRight(path, "/")
fullPath := pool
if path != "" {
fullPath = fmt.Sprintf("%s/%s", pool, path)
}
output, err := shared.RunCommand(
"zfs",
"list",
"-t", "snapshot",
"-o", "name",
"-H",
"-d", "1",
"-s", "creation",
"-r", fullPath)
if err != nil {
logger.Errorf("zfs list failed: %s.", output)
return []string{}, fmt.Errorf("Failed to list ZFS snapshots: %s", output)
}
children := []string{}
for _, entry := range strings.Split(output, "\n") {
if entry == "" {
continue
}
if entry == fullPath {
continue
}
children = append(children, strings.SplitN(entry, "@", 2)[1])
}
return children, nil
}
func zfsPoolVolumeSnapshotRemovable(pool string, path string, name string) (bool, error) {
var snap string
if name == "" {
snap = path
} else {
snap = fmt.Sprintf("%s@%s", path, name)
}
clones, err := zfsFilesystemEntityPropertyGet(pool, snap, "clones")
if err != nil {
return false, err
}
if clones == "-" || clones == "" {
return true, nil
}
return false, nil
}
func zfsFilesystemEntityExists(pool string, path string) bool {
vdev := pool
if path != "" {
vdev = fmt.Sprintf("%s/%s", pool, path)
}
output, err := shared.RunCommand(
"zfs",
"get",
"-H",
"-o",
"name",
"type",
vdev)
if err != nil {
return false
}
detectedName := strings.TrimSpace(output)
return detectedName == vdev
}