forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.go
executable file
·1107 lines (976 loc) · 25.4 KB
/
dataset.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package zfs
// #include <stdlib.h>
// #include <libzfs.h>
// #include "common.h"
// #include "zpool.h"
// #include "zfs.h"
import "C"
import (
"encoding/json"
"errors"
"fmt"
"path"
"sort"
"strings"
"sync"
"time"
"unsafe"
)
const (
msgDatasetIsNil = "Dataset handle not initialized or its closed"
)
// DatasetProperties type is map of dataset or volume properties prop -> value
type DatasetProperties map[DatasetProp]PropertyValue
// DatasetType defines enum of dataset types
type DatasetType int32
const (
// DatasetTypeFilesystem - file system dataset
DatasetTypeFilesystem DatasetType = C.ZFS_TYPE_FILESYSTEM
// DatasetTypeSnapshot - snapshot of dataset
DatasetTypeSnapshot = C.ZFS_TYPE_SNAPSHOT
// DatasetTypeVolume - volume (virtual block device) dataset
DatasetTypeVolume = C.ZFS_TYPE_VOLUME
// DatasetTypePool - pool dataset
DatasetTypePool = C.ZFS_TYPE_POOL
// DatasetTypeBookmark - bookmark dataset
DatasetTypeBookmark = C.ZFS_TYPE_BOOKMARK
)
var stringToDatasetType = make(map[string]DatasetType)
func init() {
types := [...]DatasetType{
DatasetTypeFilesystem,
DatasetTypeSnapshot,
DatasetTypeVolume,
DatasetTypePool,
DatasetTypeBookmark,
}
for _, t := range types {
stringToDatasetType[t.String()] = t
}
}
func (t DatasetType) String() string {
return C.GoString(C.zfs_type_to_name((C.zfs_type_t)(t)))
}
func (t DatasetType) MarshalJSON() ([]byte, error) {
s := t.String()
return json.Marshal(s)
}
func (t *DatasetType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
dstype, ok := stringToDatasetType[j]
if !ok {
return fmt.Errorf("type \"%s\" not exists", j)
}
*t = dstype
return err
}
// HoldTag - user holds tags
type HoldTag struct {
Name string
Timestamp time.Time
}
// Dataset - ZFS dataset object
type Dataset struct {
Type DatasetType `json:"type,omitempty"`
Properties DatasetProperties `json:"properties,omitempty"`
Children []Dataset `json:"-"`
zpath string
zh *C.zfs_handle_t
mtx sync.Mutex
}
func NewDataset() *Dataset {
return &Dataset{
Properties: make(DatasetProperties),
}
}
func (p Dataset) String() string {
data, err := json.Marshal(&p)
if err != nil {
return ""
}
return string(data)
}
func (d *Dataset) openHdl() error {
var err error
if d.zh != nil {
return nil
} else if d.zpath == "" {
return NewError(EBadpath, "no path")
}
csPath := C.CString(d.zpath)
defer C.free(unsafe.Pointer(csPath))
var types C.zfs_type_t = C.ZFS_TYPE_FILESYSTEM | C.ZFS_TYPE_SNAPSHOT | C.ZFS_TYPE_VOLUME | C.ZFS_TYPE_POOL | C.ZFS_TYPE_BOOKMARK
d.zh = C.zfs_open(C.libzfs_get_handle(), csPath, C.int(types))
if d.zh == nil {
return LastError()
}
return err
}
func (d *Dataset) closeHdl() error {
if d.zh != nil {
C.zfs_close(d.zh)
}
d.zh = nil
return nil
}
func (d *Dataset) _load(props []DatasetProp) error {
var err error
name := C.zfs_get_name(d.zh)
if name != nil {
d.zpath = C.GoString(name)
}
d.Type = DatasetType(C.zfs_get_type(d.zh))
err = d.loadProperties(props)
if err != nil {
return err
}
err = d.openChildren()
return err
}
func (d *Dataset) Load() error {
d.mtx.Lock()
defer d.mtx.Unlock()
if err := d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
err := d._load(nil)
return err
}
func loadDatasetFromZfsHdl(zh *C.zfs_handle_t) (*Dataset, error) {
if zh == nil {
return nil, errors.New("handle invalid")
}
dt := &Dataset{
zh: zh,
}
props := []DatasetProp{
DatasetPropType,
DatasetPropName,
DatasetPropCreation,
DatasetPropAvailable,
DatasetPropUsed,
DatasetPropMountpoint,
DatasetPropGUID,
DatasetPropReceiveResumeToken,
DatasetPropUsedsnap,
DatasetPropUsedchild,
DatasetPropEncryption,
}
err := dt._load(props)
if err != nil {
return nil, err
}
dt.zh = nil
return dt, nil
}
func (d *Dataset) openChildren() error {
var err error
d.Children = make([]Dataset, 0, 5)
list := C.dataset_list_children(d.zh)
if list == nil {
//no children
return nil
}
defer C.dataset_list_free(list)
pItem := list
for pItem != nil {
dataset, err := loadDatasetFromZfsHdl(pItem.zh)
if err != nil {
return err
}
d.Children = append(d.Children, *dataset)
pItem = C.dataset_list_next(pItem)
}
return err
}
// DatasetOpenAll recursive get handles to all available datasets on system
// (file-systems, volumes or snapshots).
func DatasetOpenAll() (datasets []Dataset, err error) {
list := C.dataset_list_root()
if list == nil {
return nil, LastError()
}
defer C.dataset_list_free(list)
pItem := list
for pItem != nil {
dataset, err := loadDatasetFromZfsHdl(pItem.zh)
if err != nil {
return nil, err
}
datasets = append(datasets, *dataset)
pItem = C.dataset_list_next(pItem)
}
return
}
// DatasetCloseAll close all datasets in slice and all of its recursive
// children datasets
func DatasetCloseAll(datasets []Dataset) {
for _, d := range datasets {
d.Close()
}
}
// DatasetOpen open dataset and all of its recursive children datasets
func DatasetOpen(path string) (*Dataset, error) {
d, err := DatasetOpenSingle(path)
if err != nil {
return nil, err
}
return d, err
}
// DatasetOpenSingle open dataset without opening all of its recursive
// children datasets
func DatasetOpenSingle(path string) (*Dataset, error) {
d := &Dataset{
zpath: path,
}
err := d.openHdl()
if err != nil {
return nil, err
}
defer d.closeHdl()
err = d._load(nil)
return d, err
}
func datasetPropertiesTonvlist(props map[DatasetProp]PropertyValue) (
C.nvlist_ptr, error) {
// convert properties to nvlist C type
cprops := C.new_property_nvlist()
if cprops == nil {
return nil, NewError(ENomem, "Failed to allocate properties")
}
for prop, value := range props {
csValue := C.CString(value.Value)
r := C.property_nvlist_add(
cprops, C.zfs_prop_to_name(C.zfs_prop_t(prop)), csValue)
C.free(unsafe.Pointer(csValue))
if r != 0 {
err := LastError()
C.nvlist_free(cprops)
return nil, NewError(err.(*Error).ErrorCode(), fmt.Sprintf("failed to convert property: %s", err.Error()))
}
}
return cprops, nil
}
// DatasetCreate create a new filesystem or volume on path representing
// pool/dataset or pool/parent/dataset
func DatasetCreate(path string, dtype DatasetType,
props map[DatasetProp]PropertyValue) (d *Dataset, err error) {
var cprops C.nvlist_ptr
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
return
}
defer C.nvlist_free(cprops)
csPath := C.CString(path)
errcode := C.dataset_create(csPath, C.zfs_type_t(dtype), cprops)
C.free(unsafe.Pointer(csPath))
if errcode != 0 {
err = LastError()
return
}
d, err = DatasetOpen(path)
return
}
// Close close dataset and all its recursive children datasets (close handle
// and cleanup dataset object/s from memory)
func (d *Dataset) Close() {
d.mtx.Lock()
d.closeHdl()
d.mtx.Unlock()
for _, cd := range d.Children {
cd.Close()
}
}
// reOpen - close and open dataset. Not thread safe!
func (d *Dataset) reOpen() (err error) {
d.Close()
d, err = DatasetOpen(d.Properties[DatasetPropName].Value)
return
}
// Destroy destroys the dataset. The caller must make sure that the filesystem
// isn't mounted, and that there are no active dependents. Set Defer argument
// to true to defer destruction for when dataset is not in use. Call Close() to
// cleanup memory.
func (d *Dataset) Destroy(Defer bool) (err error) {
if len(d.Children) > 0 {
path, err := d.Path()
if err != nil {
return err
}
dsType, err := d.GetProperty(DatasetPropType)
if err != nil {
dsType.Value = err.Error() // just put error (why it didn't fetch property type)
}
err = NewError(EUndefined, fmt.Sprintf("cannot destroy dataset "+path+
": "+dsType.Value+" has children"))
return err
}
err = d.openHdl()
if err == nil {
if ec := C.dataset_destroy(d.zh, booleanT(Defer)); ec != 0 {
err = LastError()
}
d.closeHdl()
} else {
err = NewError(EUndefined, msgDatasetIsNil)
}
return
}
// IsSnapshot - retrun true if datset is snapshot
func (d *Dataset) IsSnapshot() (ok bool) {
path := d.Properties[DatasetPropName].Value
ok = (d.Type == DatasetTypeSnapshot || strings.Contains(path, "@"))
return
}
// DestroyRecursive recursively destroy children of dataset and dataset.
func (d *Dataset) DestroyRecursive() error {
var path string
var err error
if path, err = d.Path(); err != nil {
return err
}
if !strings.Contains(path, "@") { // not snapshot
if len(d.Children) > 0 {
for _, c := range d.Children {
if err = c.DestroyRecursive(); err != nil {
return err
}
// close handle to destroyed child dataset
c.Close()
}
// clear closed children array
d.Children = make([]Dataset, 0)
}
err = d.Destroy(false)
} else {
tmp := strings.Split(path, "@")
ppath, snapname := tmp[0], tmp[1]
parent, err := DatasetOpen(ppath)
if err != nil {
return err
}
defer parent.Close()
if len(parent.Children) > 0 {
for _, c := range parent.Children {
if path, err = c.Path(); err != nil {
return err
}
if strings.Contains(path, "@") {
continue // skip other snapshots
}
dt, err := DatasetOpen(path + "@" + snapname)
if err != nil {
continue
}
if err = dt.DestroyRecursive(); err != nil {
dt.Close()
return err
}
dt.Close()
}
}
err = d.Destroy(false)
}
return err
}
// Pool returns pool dataset belongs to
// func (d *Dataset) Pool() (p Pool, error) {
// err := d.openHdl()
// p.list = C.dataset_get_pool(d.zh)
// if p.list != nil && p.list.zph != nil {
// err = p.ReloadProperties(nil)
// return
// }
// err = LastError()
// return
// }
// PoolName - return name of the pool
func (d *Dataset) PoolName() string {
path := d.Properties[DatasetPropName].Value
i := strings.Index(path, "/")
if i < 0 {
return path
}
return path[0:i]
}
// ReloadProperties re-read dataset's properties
func (d *Dataset) loadProperties(props []DatasetProp) error {
if d.zh == nil {
return NewError(ENoent, "not open")
}
d.Properties = make(map[DatasetProp]PropertyValue)
Global.Mtx.Lock()
defer Global.Mtx.Unlock()
if props != nil {
for _, prop := range props {
plist := C.read_dataset_property(d.zh, C.int(prop))
if plist == nil {
continue
}
d.Properties[prop] = PropertyValue{Value: C.GoString(&(*plist).value[0]),
Source: C.GoString(&(*plist).source[0])}
C.free_properties(plist)
}
} else {
for prop := DatasetPropType; prop < zfsMaxDatasetProp; prop++ {
plist := C.read_dataset_property(d.zh, C.int(prop))
if plist == nil {
continue
}
d.Properties[prop] = PropertyValue{Value: C.GoString(&(*plist).value[0]),
Source: C.GoString(&(*plist).source[0])}
C.free_properties(plist)
}
}
return nil
}
func (d *Dataset) ReloadProperties() error {
d.mtx.Lock()
defer d.mtx.Unlock()
if err := d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
d.Type = DatasetType(C.zfs_get_type(d.zh))
return d.loadProperties(nil)
}
// GetProperty reload and return single specified property. This also reloads requested
// property in Properties map.
func (d *Dataset) GetProperty(p DatasetProp) (PropertyValue, error) {
var prop PropertyValue
d.mtx.Lock()
defer d.mtx.Unlock()
err := d.openHdl()
if err != nil {
return prop, err
}
defer d.closeHdl()
plist := C.read_dataset_property(d.zh, C.int(p))
if plist == nil {
err = LastError()
return prop, err
}
defer C.free_properties(plist)
prop = PropertyValue{Value: C.GoString(&(*plist).value[0]),
Source: C.GoString(&(*plist).source[0])}
d.Properties[p] = prop
return prop, err
}
// GetUserProperty - lookup and return user propery
func (d *Dataset) GetUserProperty(p string) (prop PropertyValue, err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return
}
defer d.closeHdl()
csp := C.CString(p)
defer C.free(unsafe.Pointer(csp))
plist := C.read_user_property(d.zh, csp)
if plist == nil {
err = LastError()
return
}
defer C.free_properties(plist)
prop = PropertyValue{Value: C.GoString(&(*plist).value[0]),
Source: C.GoString(&(*plist).source[0])}
return
}
// SetProperty set ZFS dataset property to value. Not all properties can be set,
// some can be set only at creation time and some are read only.
// Always check if returned error and its description.
func (d *Dataset) SetProperty(p DatasetProp, value string) error {
var err error
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
csValue := C.CString(value)
defer C.free(unsafe.Pointer(csValue))
errcode := C.dataset_prop_set(d.zh, C.zfs_prop_t(p), csValue)
if errcode != 0 {
return LastError()
}
prop := PropertyValue{
Value: value,
}
d.Properties[p] = prop
return err
}
// SetUserProperty -
func (d *Dataset) SetUserProperty(prop, value string) error {
var err error
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
csValue := C.CString(value)
csProp := C.CString(prop)
errcode := C.dataset_user_prop_set(d.zh, csProp, csValue)
C.free(unsafe.Pointer(csValue))
C.free(unsafe.Pointer(csProp))
if errcode != 0 {
err = LastError()
}
return err
}
// Clone - clones the dataset. The target must be of the same type as
// the source.
func (d *Dataset) Clone(target string, props map[DatasetProp]PropertyValue) (rd Dataset, err error) {
var cprops C.nvlist_ptr
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return rd, err
}
defer d.closeHdl()
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
return
}
defer C.nvlist_free(cprops)
csTarget := C.CString(target)
defer C.free(unsafe.Pointer(csTarget))
if errc := C.dataset_clone(d.zh, csTarget, cprops); errc != 0 {
err = LastError()
return
}
prd, err := DatasetOpen(target)
if err == nil {
rd = *prd
}
return
}
// DatasetSnapshot create dataset snapshot. Set recur to true to snapshot child datasets.
func DatasetSnapshot(path string, recur bool, props map[DatasetProp]PropertyValue) (rd Dataset, err error) {
var cprops C.nvlist_ptr
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
return
}
defer C.nvlist_free(cprops)
csPath := C.CString(path)
defer C.free(unsafe.Pointer(csPath))
if errc := C.dataset_snapshot(csPath, booleanT(recur), cprops); errc != 0 {
err = LastError()
return
}
prd, err := DatasetOpen(path)
if err == nil {
rd = *prd
}
return
}
// Path return zfs dataset path/name
func (d *Dataset) Path() (path string, err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
return d.zpath, nil
}
// Rollback rollabck's dataset snapshot
func (d *Dataset) Rollback(snap *Dataset, force bool) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
if errc := C.dataset_rollback(d.zh, snap.zh, booleanT(force)); errc != 0 {
err = LastError()
return
}
d.ReloadProperties()
return
}
// Promote promotes dataset clone
func (d *Dataset) Promote() (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
if errc := C.dataset_promote(d.zh); errc != 0 {
err = LastError()
return
}
d.loadProperties(nil)
return
}
// Rename dataset
func (d *Dataset) Rename(newName string, recur, forceUnmount bool) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
csNewName := C.CString(newName)
defer C.free(unsafe.Pointer(csNewName))
if errc := C.dataset_rename(d.zh, csNewName,
booleanT(recur), booleanT(forceUnmount)); errc != 0 {
err = LastError()
return
}
d.loadProperties(nil)
return
}
// IsMounted checks to see if the mount is active. If the filesystem is mounted,
// sets in 'where' argument the current mountpoint, and returns true. Otherwise,
// returns false.
func (d *Dataset) IsMounted() (mounted bool, where string) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err := d.openHdl(); err != nil {
return false, ""
}
defer d.closeHdl()
mp := C.dataset_is_mounted(d.zh)
// defer C.free(mp)
if mounted = (mp != nil); mounted {
where = C.GoString(mp)
C.free(unsafe.Pointer(mp))
}
return
}
// Mount the given filesystem.
func (d *Dataset) Mount(options string, flags int) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
csOptions := C.CString(options)
defer C.free(unsafe.Pointer(csOptions))
if ec := C.dataset_mount(d.zh, csOptions, C.int(flags)); ec != 0 {
err = LastError()
}
return
}
// Unmount the given filesystem.
func (d *Dataset) Unmount(flags int) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
if ec := C.dataset_unmount(d.zh, C.int(flags)); ec != 0 {
err = LastError()
}
return
}
// UnmountAll unmount this filesystem and any children inheriting the
// mountpoint property.
func (d *Dataset) UnmountAll(flags int) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
if err = d.openHdl(); err != nil {
return err
}
defer d.closeHdl()
// This is implemented recursive because zfs_unmountall() didn't work
if len(d.Children) > 0 {
for _, c := range d.Children {
if err = c.UnmountAll(flags); err != nil {
return
}
}
}
return d.Unmount(flags)
}
// Hold - Adds a single reference, named with the tag argument, to the snapshot.
// Each snapshot has its own tag namespace, and tags must be unique within that space.
func (d *Dataset) Hold2(snapname string, flag string) error {
d.mtx.Lock()
defer d.mtx.Unlock()
err := d.openHdl()
if err != nil {
return err
}
defer d.closeHdl()
csSnapName := C.CString(snapname)
defer C.free(unsafe.Pointer(csSnapName))
csFlag := C.CString(flag)
defer C.free(unsafe.Pointer(csFlag))
if 0 != C.zfs_hold(d.zh, csSnapName, csFlag, booleanT(false), -1) {
err = LastError()
}
return err
}
func (d *Dataset) Hold(flag string) error {
var path string
d.mtx.Lock()
defer d.mtx.Unlock()
path = d.zpath
if path == "" {
return NewError(EBadpath, "path not set")
}
err := d.openHdl()
if err != nil {
return err
}
defer d.closeHdl()
if !strings.Contains(path, "@") {
return NewError(EUndefined, fmt.Sprintf("'%s' is not a snapshot", path))
}
pd, err := DatasetOpenSingle(path[:strings.Index(path, "@")])
if err != nil {
return err
}
snapname := path[strings.Index(path, "@")+1:]
err = pd.Hold2(snapname, flag)
return err
}
// Release - Removes a single reference, named with the tag argument, from the specified snapshot.
// The tag must already exist for each snapshot. If a hold exists on a snapshot, attempts to destroy
// that snapshot by using the zfs destroy command return EBUSY.
func (d *Dataset) Release2(snapname, flag string) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
err = d.openHdl()
if err != nil {
return err
}
defer d.closeHdl()
csSnapName := C.CString(snapname)
defer C.free(unsafe.Pointer(csSnapName))
csFlag := C.CString(flag)
defer C.free(unsafe.Pointer(csFlag))
if 0 != C.zfs_release(d.zh, csSnapName, csFlag, booleanT(false)) {
err = LastError()
}
return err
}
func (d *Dataset) Release(flag string) (err error) {
d.mtx.Lock()
defer d.mtx.Unlock()
path := d.zpath
if path == "" {
return NewError(EBadpath, "path not set")
}
err = d.openHdl()
if err != nil {
return err
}
defer d.closeHdl()
if !strings.Contains(path, "@") {
err = NewError(EUndefined, fmt.Sprintf("'%s' is not a snapshot", path))
return
}
pd, err := DatasetOpenSingle(path[:strings.Index(path, "@")])
if err != nil {
return
}
snapname := path[strings.Index(path, "@")+1:]
err = pd.Release2(snapname, flag)
return err
}
// Holds - Lists all existing user references for the given snapshot
func (d *Dataset) Holds() (tags []HoldTag, err error) {
var nvl *C.nvlist_t
var nvp *C.nvpair_t
var tu64 C.uint64_t
d.mtx.Lock()
defer d.mtx.Unlock()
path := d.zpath
if path == "" {
return nil, NewError(EBadpath, "path not set")
}
err = d.openHdl()
if err != nil {
return nil, err
}
defer d.closeHdl()
if !strings.Contains(path, "@") {
err = NewError(EUndefined, fmt.Sprintf("'%s' is not a snapshot", path))
return
}
if 0 != C.zfs_get_holds(d.zh, &nvl) {
err = LastError()
return
}
defer C.nvlist_free(nvl)
tags = make([]HoldTag, 0, 5)
for nvp = C.nvlist_next_nvpair(nvl, nvp); nvp != nil; {
tag := C.nvpair_name(nvp)
C.nvpair_value_uint64(nvp, &tu64)
tags = append(tags, HoldTag{
Name: C.GoString(tag),
Timestamp: time.Unix(int64(tu64), 0),
})
nvp = C.nvlist_next_nvpair(nvl, nvp)
}
return
}
// DatasetPropertyToName convert property to name
// ( returns built in string representation of property name).
// This is optional, you can represent each property with string
// name of choice.
func DatasetPropertyToName(p DatasetProp) (name string) {
if p == zfsMaxDatasetProp {
return "numofprops"
}
prop := C.zfs_prop_t(p)
name = C.GoString(C.zfs_prop_to_name(prop))
return
}
// DestroyPromote - Same as DestroyRecursive() except it will not destroy
// any dependent clones, but promote them first.
// This function will navigate any dependency chain
// of cloned datasets using breadth first search to promote according and let
// you remove dataset regardless of its cloned dependencies.
// Note: that this function wan't work when you want to destroy snapshot this way.
// However it will destroy all snaphsot of destroyed dataset without dependencies,
// otherwise snapshot will move to promoted clone
func (d *Dataset) DestroyPromote() (err error) {
var snaps []Dataset
var clones []string
// We need to save list of child snapshots, to destroy them latter
// since they will be moved to promoted clone
var psnaps []string
if clones, err = d.Clones(); err != nil {
return
}
if len(clones) > 0 {
var cds *Dataset
// For this to always work we need to promote youngest clone
// in terms of most recent origin snapshot or creation time if
// cloned from same snapshot
if cds, err = DatasetOpen(clones[0]); err != nil {
return
}
defer cds.Close()
// since promote will move the snapshots to promoted dataset
// we need to check and resolve possible name conflicts
if snaps, err = d.Snapshots(); err != nil {
return
}
for _, s := range snaps {
spath := s.Properties[DatasetPropName].Value
sname := spath[strings.Index(spath, "@"):]
// conflict and resolve
if ok, _ := cds.FindSnapshotName(sname); ok {
// snapshot with the same name already exist
volname := path.Base(spath[:strings.Index(spath, "@")])
sname = sname + "." + volname
if err = s.Rename(spath+"."+volname, false, true); err != nil {
return
}
}
psnaps = append(psnaps, sname)
}
if err = cds.Promote(); err != nil {
return
}
}