-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs.go
804 lines (689 loc) · 26 KB
/
zfs.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Package zfs provides wrappers around the ZFS command line tools.
package zfs
import (
"context"
"fmt"
"io"
"slices"
"strconv"
"strings"
"github.com/klauspost/compress/zstd"
)
const (
Binary = "zfs"
)
// ListOptions are options you can specify to customize the ListDatasets and other List commands
type ListOptions struct {
// ParentDataset filters by parent dataset, empty lists all
ParentDataset string
// DatasetType filters the results by type
DatasetType DatasetType
// ExtraProperties lists the properties to retrieve besides the ones in the Dataset struct (in the ExtraProps key)
ExtraProperties []string
// Recursive, if true will list all under the parent dataset
Recursive bool
// Depth specifies the depth to go below the parent dataset (or root if no parent)
// Recursively display any children of the dataset, limiting the recursion to depth.
// A depth of 1 will display only the dataset and its direct children.
Depth int
// FilterSelf: When true, it will filter out the parent dataset itself from the results
FilterSelf bool
}
// ListDatasets lists the datasets by type and allows you to fetch extra custom fields
func ListDatasets(ctx context.Context, options ListOptions) ([]Dataset, error) {
args := make([]string, 0, 16)
args = append(args, "get", "-Hp", "-o", "name,property,value")
if options.DatasetType != "" {
args = append(args, "-t", string(options.DatasetType))
}
if options.Recursive {
args = append(args, "-r")
}
if options.Depth > 0 {
args = append(args, "-d", strconv.Itoa(options.Depth))
}
allFields := append(dsPropList, options.ExtraProperties...) // nolint: gocritic
args = append(args, strings.Join(allFields, ","))
if options.ParentDataset != "" {
args = append(args, options.ParentDataset)
}
out, err := zfsOutput(ctx, args...)
if err != nil {
return nil, err
}
ds, err := readDatasets(out, options.ExtraProperties)
if err != nil {
return nil, err
}
// Filter out the parent dataset:
if options.FilterSelf {
ds = slices.DeleteFunc(ds, func(dataset Dataset) bool {
return dataset.Name == options.ParentDataset
})
}
return ds, nil
}
// ListVolumes returns a slice of ZFS volumes.
// A filter argument may be passed to select a volume with the matching name, or empty string ("") may be used to select all volumes.
func ListVolumes(ctx context.Context, options ListOptions) ([]Dataset, error) {
options.DatasetType = DatasetVolume
options.Recursive = true
return ListDatasets(ctx, options)
}
// ListFilesystems returns a slice of ZFS filesystems.
// A filter argument may be passed to select a filesystem with the matching name, or empty string ("") may be used to select all filesystems.
func ListFilesystems(ctx context.Context, options ListOptions) ([]Dataset, error) {
options.DatasetType = DatasetFilesystem
options.Recursive = true
return ListDatasets(ctx, options)
}
// ListSnapshots returns a slice of ZFS snapshots.
// A filter argument may be passed to select a snapshot with the matching name, or empty string ("") may be used to select all snapshots.
func ListSnapshots(ctx context.Context, options ListOptions) ([]Dataset, error) {
options.DatasetType = DatasetSnapshot
options.Recursive = true
return ListDatasets(ctx, options)
}
// ListWithPropertyOptions are options you can specify to customize the ListWithProperty command
type ListWithPropertyOptions struct {
// ParentDataset filters by parent dataset, empty lists all
ParentDataset string
// DatasetType filters the results by type
DatasetType DatasetType
// PropertySources determines the source(s) of the property
PropertySources PropertySources
}
// ListWithProperty returns a map of dataset names mapped to the properties value for datasets which have the given ZFS property.
func ListWithProperty(ctx context.Context, property string, options ListWithPropertyOptions) (map[string]string, error) {
c := command{
cmd: Binary,
ctx: ctx,
}
args := make([]string, 0, 16)
args = append(args, "get")
if options.ParentDataset != "" {
args = append(args, "-t", string(options.DatasetType))
}
args = append(args, "-Hp", "-o", "name,value", "-r")
// If we have none specified, always assume we want local properties _only_
if len(options.PropertySources) == 0 {
options.PropertySources = []PropertySource{PropertySourceLocal}
}
args = append(args, "-s", strings.Join(options.PropertySources.StringSlice(), ","))
// The prop we are querying:
args = append(args, property)
if options.ParentDataset != "" {
args = append(args, options.ParentDataset)
}
lines, err := c.Run(args...)
if err != nil {
return nil, err
}
result := make(map[string]string, len(lines))
for _, line := range lines {
switch len(line) {
case 2:
result[line[0]] = line[1]
case 1:
result[line[0]] = ""
}
}
return result, nil
}
// GetDataset retrieves a single ZFS dataset by name.
// This dataset could be any valid ZFS dataset type, such as a clone, filesystem, snapshot, or volume.
func GetDataset(ctx context.Context, name string, extraProperties ...string) (*Dataset, error) {
ds, err := ListDatasets(ctx, ListOptions{
ParentDataset: name,
Recursive: false,
FilterSelf: false,
ExtraProperties: extraProperties,
})
if err != nil {
return nil, err
}
if len(ds) > 1 {
return nil, fmt.Errorf("more datasets than expected: %d", len(ds))
}
return &ds[0], nil
}
// CloneOptions are options you can specify to customize the clone command
type CloneOptions struct {
// Properties to be applied to the new dataset
Properties map[string]string
// Creates all the non-existing parent datasets. Datasets created in this manner are automatically mounted according
// to the mountpoint property inherited from their parent. If the target filesystem or volume already exists,
// the operation completes successfully.
CreateParents bool
}
// Clone clones a ZFS snapshot and returns a clone dataset.
// An error will be returned if the input dataset is not of snapshot type.
func (d *Dataset) Clone(ctx context.Context, dest string, options CloneOptions) (*Dataset, error) {
if d.Type != DatasetSnapshot {
return nil, ErrOnlySnapshotsSupported
}
args := make([]string, 1, 8)
args[0] = "clone"
if options.CreateParents {
args = append(args, "-p")
}
if options.Properties != nil {
args = append(args, propsSlice(options.Properties)...)
}
args = append(args, []string{d.Name, dest}...)
err := zfs(ctx, args...)
if err != nil {
return nil, err
}
return GetDataset(ctx, dest)
}
// Promote promotes a cloned dataset to no longer depend on origin snapshot.
// An error will be returned if the input dataset is not of snapshot type.
// The
func (d *Dataset) Promote(ctx context.Context) error {
args := make([]string, 1, 4)
args[0] = "promote"
args = append(args, []string{d.Name}...)
return zfs(ctx, args...)
}
// UnmountOptions are options you can specify to customize the unmount command
type UnmountOptions struct {
// Forcefully unmount the file system, even if it is currently in use.
Force bool
// Unload keys for any encryption roots unmounted by this command.
UnloadKeys bool
}
// Unmount unmounts currently mounted ZFS file systems.
func (d *Dataset) Unmount(ctx context.Context, options UnmountOptions) error {
if d.Type == DatasetSnapshot {
return ErrSnapshotsNotSupported
}
args := make([]string, 1, 5)
args[0] = "umount"
if options.Force {
args = append(args, "-f")
}
if options.UnloadKeys {
args = append(args, "-u")
}
args = append(args, d.Name)
return zfs(ctx, args...)
}
// LoadKeyOptions are options you can specify to customize the load-key command
type LoadKeyOptions struct {
// Recursively loads the keys for the specified filesystem and all descendent encryption roots.
Recursive bool
// Do a dry-run (no-op) load-key. This will cause zfs to simply check that the provided key is correct.
// This command may be run even if the key is already loaded.
DryRun bool
// When the key is in a file, load it using this keylocation.
// This is optional when the ZFS dataset already has this property set.
KeyLocation string
// Provide a reader to read the key from stdin
KeyReader io.Reader
}
// LoadKey loads the encryption key for this and optionally children datasets.
// See: https://openzfs.github.io/openzfs-docs/man/8/zfs-load-key.8.html
func (d *Dataset) LoadKey(ctx context.Context, options LoadKeyOptions) error {
args := make([]string, 1, 5)
args[0] = "load-key"
if options.Recursive {
args = append(args, "-r")
}
if options.DryRun {
args = append(args, "-n")
}
if options.KeyLocation != "" {
args = append(args, "-L", options.KeyLocation)
}
args = append(args, d.Name)
cmd := command{
cmd: Binary,
ctx: ctx,
stdin: options.KeyReader,
}
_, err := cmd.Run(args...)
return err
}
// UnloadKeyOptions are options you can specify to customize the unload-key command
type UnloadKeyOptions struct {
// Recursively loads the keys for the specified filesystem and all descendent encryption roots.
Recursive bool
}
// UnloadKey unloads the encryption key for this dataset and optionally for child datasets as well.
// See: https://openzfs.github.io/openzfs-docs/man/8/zfs-unload-key.8.html
func (d *Dataset) UnloadKey(ctx context.Context, options UnloadKeyOptions) error {
args := make([]string, 1, 3)
args[0] = "unload-key"
if options.Recursive {
args = append(args, "-r")
}
args = append(args, d.Name)
return zfs(ctx, args...)
}
// MountOptions are options you can specify to customize the mount command
type MountOptions struct {
// Perform an overlay mount. Allows mounting in non-empty mountpoint.
OverlayMount bool
// An optional, comma-separated list of mount options to use temporarily for the duration of the mount.
Options []string
// Load keys for encrypted filesystems as they are being mounted. This is equivalent to executing zfs load-key
// on each encryption root before mounting it. Note that if a filesystem has keylocation=prompt, this will cause
// the terminal to interactively block after asking for the key.
LoadKeys bool
}
// Mount mounts ZFS file systems.
func (d *Dataset) Mount(ctx context.Context, options MountOptions) error {
if d.Type == DatasetSnapshot {
return ErrSnapshotsNotSupported
}
args := make([]string, 1, 5)
args[0] = "mount"
if options.OverlayMount {
args = append(args, "-O")
}
if options.LoadKeys {
args = append(args, "-l")
}
if len(options.Options) > 0 {
args = append(args, "-o")
args = append(args, strings.Join(options.Options, ","))
}
args = append(args, d.Name)
return zfs(ctx, args...)
}
// ReceiveOptions are options you can specify to customize the receive command
type ReceiveOptions struct {
// Whether the received snapshot should be resumable on interrupions, or be thrown away
Resumable bool
// Properties to be applied to the dataset
Properties map[string]string
// EnableCompression enables zstd decompression
EnableDecompression bool
// Force a rollback of the file system to the most recent snapshot before performing the receive operation.
ForceRollback bool
}
// ReceiveSnapshot receives a ZFS stream from the input io.Reader.
// A new snapshot is created with the specified name, and streams the input data into the newly-created snapshot.
func ReceiveSnapshot(ctx context.Context, input io.Reader, name string, options ReceiveOptions) (*Dataset, error) {
if options.EnableDecompression {
decoder, err := zstd.NewReader(input)
if err != nil {
return nil, fmt.Errorf("error creating zstd reader: %w", err)
}
defer decoder.Close()
input = decoder
}
c := command{
cmd: Binary,
ctx: ctx,
stdin: input,
}
args := make([]string, 1, 4)
args[0] = "receive"
if options.ForceRollback {
args = append(args, "-F")
}
if options.Resumable {
args = append(args, "-s")
}
args = append(args, propsSlice(options.Properties)...)
args = append(args, name)
_, err := c.Run(args...)
if err != nil {
return nil, err
}
return GetDataset(ctx, name)
}
// SendOptions are options you can specify to customize the send command
type SendOptions struct {
// For encrypted datasets, send data exactly as it exists on disk. This allows backups to
// be taken even if encryption keys are not currently loaded. The backup may then be
// received on an untrusted machine since that machine will not have the encryption keys
// to read the protected data or alter it without being detected. Upon being received,
// the dataset will have the same encryption keys as it did on the send side, although
// the keylocation property will be defaulted to prompt if not otherwise provided. For
// unencrypted datasets, this flag will be equivalent to -Lec. Note that if you do not
// use this flag for sending encrypted datasets, data will be sent unencrypted and may be
// re-encrypted with a different encryption key on the receiving system, which will
// disable the ability to do a raw send to that system for incrementals.
Raw bool
// Include the dataset's properties in the stream. This flag is implicit when -R is
// specified. The receiving system must also support this feature. Sends of encrypted
// datasets must use -w when using this flag.
IncludeProperties bool
// Generate an incremental stream from the first snapshot (the incremental source) to the
// second snapshot (the incremental target). The incremental source can be specified as
// the last component of the snapshot name (the @ character and following) and it is
// assumed to be from the same file system as the incremental target.
//
// If the destination is a clone, the source may be the origin snapshot, which must be
// fully specified (for example, pool/fs@origin, not just @origin).
IncrementalBase *Dataset
// When set, uses a rate-limiter to limit the flow to this amount of bytes per second
BytesPerSecond int64
// CompressionLevel is the level of zstd compression, 0 for off
CompressionLevel zstd.EncoderLevel
}
// SendSnapshot sends a ZFS stream of a snapshot to the input io.Writer.
// An error will be returned if the input dataset is not of snapshot type.
func (d *Dataset) SendSnapshot(ctx context.Context, output io.Writer, options SendOptions) error {
if d.Type != DatasetSnapshot {
return ErrOnlySnapshotsSupported
}
args := make([]string, 1, 8)
args[0] = "send"
if options.Raw {
args = append(args, "-w")
}
if options.IncludeProperties {
args = append(args, "-p")
}
if options.IncrementalBase != nil {
if options.IncrementalBase.Type != DatasetSnapshot {
return fmt.Errorf("send base %s: %w", options.IncrementalBase.Name, ErrOnlySnapshotsSupported)
}
args = append(args, "-i", options.IncrementalBase.Name)
}
output = rateLimitWriter(output, options.BytesPerSecond)
output, closer, err := zstdWriter(output, options.CompressionLevel)
if err != nil {
return err
}
defer closer()
c := command{
cmd: Binary,
ctx: ctx,
stdout: output,
}
args = append(args, d.Name)
_, err = c.Run(args...)
return err
}
// ResumeSendOptions are options you can specify to customize the send resume command
type ResumeSendOptions struct {
// When set, uses a rate-limiter to limit the flow to this amount of bytes per second
BytesPerSecond int64
// CompressionLevel is the level of zstd compression, zero for off
CompressionLevel zstd.EncoderLevel
}
// ResumeSend resumes an interrupted ZFS stream of a snapshot to the input io.Writer using the receive_resume_token.
// An error will be returned if the input dataset is not of snapshot type.
func ResumeSend(ctx context.Context, output io.Writer, resumeToken string, options ResumeSendOptions) error {
output = rateLimitWriter(output, options.BytesPerSecond)
output, closer, err := zstdWriter(output, options.CompressionLevel)
if err != nil {
return err
}
defer closer()
c := command{
cmd: Binary,
ctx: ctx,
stdout: output,
}
args := append([]string{"send"}, "-t", resumeToken)
_, err = c.Run(args...)
return err
}
// CreateVolumeOptions are options you can specify to customize the create volume command
type CreateVolumeOptions struct {
// Sets the specified properties as if the command zfs set property=value was invoked at the same time the dataset was created.
Properties map[string]string
// Creates all the non-existing parent datasets. Datasets created in this manner are automatically mounted according
// to the mountpoint property inherited from their parent. Any property specified on the command line using the -o option
// is ignored. If the target filesystem already exists, the operation completes successfully.
CreateParents bool
// Creates a sparse volume with no reservation.
Sparse bool
// Do a dry-run creation. No datasets will be created. This is useful in conjunction with the -v or -P flags
// to validate properties that are passed via -o options and those implied by other options. The actual dataset creation
// can still fail due to insufficient privileges or available capacity.
DryRun bool
// Provide input to stdin, for instance for loading keys
Stdin io.Reader
}
// CreateVolume creates a new ZFS volume with the specified name, size, and properties.
//
// A full list of available ZFS properties may be found in the ZFS manual:
// https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html.
func CreateVolume(ctx context.Context, name string, size uint64, options CreateVolumeOptions) (*Dataset, error) {
args := make([]string, 3, 10)
args[0] = "create"
args[1] = "-V"
args[2] = strconv.FormatUint(size, 10)
if options.Properties != nil {
args = append(args, propsSlice(options.Properties)...)
}
if options.CreateParents {
args = append(args, "-p")
}
if options.Sparse {
args = append(args, "-s")
}
if options.DryRun {
args = append(args, "-n")
}
args = append(args, name)
cmd := command{
cmd: Binary,
ctx: ctx,
stdin: options.Stdin,
}
_, err := cmd.Run(args...)
if err != nil {
return nil, err
}
return GetDataset(ctx, name)
}
// DestroyOptions are options you can specify to customize the destroy command
type DestroyOptions struct {
// Recursively destroy all children.
Recursive bool
// Recursively destroy all dependents, including cloned file systems outside the target hierarchy.
RecursiveClones bool
// Forcibly unmount file systems. This option has no effect on non-file systems or unmounted file systems.
Force bool
// Do a dry-run (no-op) deletion. No data will be deleted.
DryRun bool
// Only for snapshots. Destroy immediately. If a snapshot cannot be destroyed now, mark it for deferred destruction.
Defer bool
}
// Destroy destroys a ZFS dataset.
// If the destroy bit flag is set, any descendents of the dataset will be recursively destroyed, including snapshots.
// If the deferred bit flag is set, the snapshot is marked for deferred deletion.
func (d *Dataset) Destroy(ctx context.Context, options DestroyOptions) error {
args := make([]string, 1, 6)
args[0] = "destroy"
if options.Recursive {
args = append(args, "-r")
}
if options.RecursiveClones {
args = append(args, "-R")
}
if options.Defer {
args = append(args, "-d")
}
if options.Force {
args = append(args, "-f")
}
args = append(args, d.Name)
return zfs(ctx, args...)
}
// SetProperty sets a ZFS property on the receiving dataset.
//
// A full list of available ZFS properties may be found in the ZFS manual:
// https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html.
func (d *Dataset) SetProperty(ctx context.Context, key, val string) error {
prop := strings.Join([]string{key, val}, "=")
return zfs(ctx, "set", prop, d.Name)
}
// GetProperty returns the current value of a ZFS property from the receiving dataset.
//
// A full list of available ZFS properties may be found in the ZFS manual:
// https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html.
func (d *Dataset) GetProperty(ctx context.Context, key string) (string, error) {
out, err := zfsOutput(ctx, "get", "-Hp", "-o", "value", key, d.Name)
if err != nil {
return "", err
}
return out[0][0], nil
}
// InheritProperty clears a property from the receiving dataset, making it use its parent datasets value.
func (d *Dataset) InheritProperty(ctx context.Context, key string) error {
return zfs(ctx, "inherit", key, d.Name)
}
// RenameOptions are options you can specify to customize the rename command
type RenameOptions struct {
// Creates all the nonexistent parent datasets. Datasets created in this manner are automatically mounted
// according to the mountpoint property inherited from their parent.
CreateParent bool
// Recursively rename the snapshots of all descendent datasets. Snapshots are the only dataset that can
// be renamed recursively.
Recursive bool
// Do not remount file systems during rename. If a file system's mountpoint property is set to legacy or none,
// the file system is not unmounted even if this option is not given.
NoMount bool
// Force unmount any file systems that need to be unmounted in the process. This flag has no effect if used together
// with the no mount flag.
Force bool
}
// Rename renames a dataset.
func (d *Dataset) Rename(ctx context.Context, name string, options RenameOptions) error {
args := make([]string, 1, 6)
args[0] = "rename"
if options.CreateParent {
args = append(args, "-p")
}
if options.Recursive {
args = append(args, "-r")
}
if options.NoMount {
args = append(args, "-u")
}
if options.Force {
args = append(args, "-f")
}
args = append(args, d.Name)
args = append(args, name)
return zfs(ctx, args...)
}
// Snapshots returns a slice of all ZFS snapshots of a given dataset.
func (d *Dataset) Snapshots(ctx context.Context, options ListOptions) ([]Dataset, error) {
options.ParentDataset = d.Name
options.DatasetType = DatasetSnapshot
options.Recursive = true
return ListDatasets(ctx, options)
}
// CreateFilesystemOptions are options you can specify to customize the create filesystem command
type CreateFilesystemOptions struct {
// Sets the specified properties as if the command zfs set property=value was invoked at the same time the dataset was created.
Properties map[string]string
// Creates all the non-existing parent datasets. Datasets created in this manner are automatically mounted according
// to the mountpoint property inherited from their parent. Any property specified on the command line using the -o option
// is ignored. If the target filesystem already exists, the operation completes successfully.
CreateParents bool
// Do a dry-run creation. No datasets will be created. This is useful in conjunction with the -v or -P flags
// to validate properties that are passed via -o options and those implied by other options. The actual dataset creation
// can still fail due to insufficient privileges or available capacity.
DryRun bool
// Do not mount the newly created file system.
NoMount bool
// Provide input to stdin, for instance for loading keys
Stdin io.Reader
}
// CreateFilesystem creates a new ZFS filesystem with the specified name and properties.
//
// A full list of available ZFS properties may be found in the ZFS manual:
// https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html.
func CreateFilesystem(ctx context.Context, name string, options CreateFilesystemOptions) (*Dataset, error) {
args := make([]string, 1, 10)
args[0] = "create"
if options.Properties != nil {
args = append(args, propsSlice(options.Properties)...)
}
if options.CreateParents {
args = append(args, "-p")
}
if options.DryRun {
args = append(args, "-n")
}
if options.NoMount {
args = append(args, "-u")
}
args = append(args, name)
cmd := command{
cmd: Binary,
ctx: ctx,
stdin: options.Stdin,
}
_, err := cmd.Run(args...)
if err != nil {
return nil, err
}
return GetDataset(ctx, name)
}
// SnapshotOptions are options you can specify to customize the snapshot command
type SnapshotOptions struct {
// Sets the specified properties on the snapshot.
Properties map[string]string
// Recursively create snapshots of all descendent datasets.
Recursive bool
}
// Snapshot creates a new ZFS snapshot of the receiving dataset, using the specified name.
// Optionally, the snapshot can be taken recursively, creating snapshots of all descendent filesystems in a single, atomic operation.
func (d *Dataset) Snapshot(ctx context.Context, name string, options SnapshotOptions) (*Dataset, error) {
args := make([]string, 1, 10)
args[0] = "snapshot"
if options.Recursive {
args = append(args, "-r")
}
if options.Properties != nil {
args = append(args, propsSlice(options.Properties)...)
}
snapName := fmt.Sprintf("%s@%s", d.Name, name)
args = append(args, snapName)
err := zfs(ctx, args...)
if err != nil {
return nil, err
}
return GetDataset(ctx, snapName)
}
// RollbackOptions are options you can specify to customize the rollback command
type RollbackOptions struct {
// Destroy any snapshots and bookmarks more recent than the one specified.
DestroyMoreRecent bool
// Destroy any more recent snapshots and bookmarks, as well as any clones of those snapshots.
DestroyMoreRecentClones bool
// Used with the DestroyMoreRecentClones option to force an unmount of any clone file systems that are to be destroyed.
Force bool
}
// Rollback rolls back the ZFS dataset to a previous snapshot.
// Optionally, intermediate snapshots can be destroyed.
// A ZFS snapshot rollback cannot be completed without the option DestroyMoreRecent, if more recent snapshots exist.
// An error will be returned if the input dataset is not of snapshot type.
func (d *Dataset) Rollback(ctx context.Context, options RollbackOptions) error {
if d.Type != DatasetSnapshot {
return ErrOnlySnapshotsSupported
}
args := make([]string, 1, 5)
args[0] = "rollback"
if options.DestroyMoreRecent {
args = append(args, "-r")
}
if options.DestroyMoreRecentClones {
args = append(args, "-R")
}
if options.Force {
args = append(args, "-f")
}
args = append(args, d.Name)
return zfs(ctx, args...)
}
// Children returns a slice of children of the receiving ZFS dataset.
// A recursion depth may be specified, or a depth of 0 allows unlimited recursion.
func (d *Dataset) Children(ctx context.Context, options ListOptions) ([]Dataset, error) {
options.ParentDataset = d.Name
options.Recursive = true
options.FilterSelf = true
return ListDatasets(ctx, options)
}