forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples_zfs.go
executable file
·74 lines (64 loc) · 1.98 KB
/
examples_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
package zfs
import (
"fmt"
)
/* ------------------------------------------------------------------------- */
// EXAMPLES:
// Example of creating ZFS volume
func ExampleDatasetCreate() {
// Create map to represent ZFS dataset properties. This is equivalent to
// list of properties you can get from ZFS CLI tool, and some more
// internally used by lib
props := make(map[DatasetProp]PropertyValue)
// I choose to create (block) volume 1GiB in size. Size is just ZFS dataset
// property and this is done as map of strings. So, You have to either
// specify size as base 10 number in string, or use strconv package or
// similar to convert in to string (base 10) from numeric type.
strSize := "1073741824"
props[DatasetPropVolsize] = PropertyValue{Value: strSize}
// In addition I explicitly choose some more properties to be set.
props[DatasetPropVolblocksize] = PropertyValue{Value: "4096"}
props[DatasetPropReservation] = PropertyValue{Value: strSize}
// Lets create desired volume
d, err := DatasetCreate("TESTPOOL"+"/VOLUME1", DatasetTypeVolume, props)
if err != nil {
println(err.Error())
return
}
// Dataset have to be closed for memory cleanup
defer d.Close()
println("Created zfs volume TESTPOOL/VOLUME1")
}
func ExampleDatasetOpen() {
// Open dataset and read its available space
d, err := DatasetOpen("TESTPOOL"+"/DATASET1")
if err != nil {
panic(err.Error())
}
defer d.Close()
var p PropertyValue
if p, err = d.GetProperty(DatasetPropAvailable); err != nil {
panic(err.Error())
}
println(DatasetPropertyToName(DatasetPropAvailable), " = ",
p.Value)
}
func ExampleDatasetOpenAll() {
datasets, err := DatasetOpenAll()
if err != nil {
panic(err.Error())
}
defer DatasetCloseAll(datasets)
// Print out path and type of root datasets
for _, d := range datasets {
path, err := d.Path()
if err != nil {
panic(err.Error())
}
p, err := d.GetProperty(DatasetPropType)
if err != nil {
panic(err.Error())
}
fmt.Printf("%30s | %10s\n", path, p.Value)
}
}