forked from bicomsystems/go-libzfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples_zpool.go
executable file
·201 lines (175 loc) · 4.68 KB
/
examples_zpool.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
package zfs
import (
"fmt"
)
func ExamplePoolProp() {
if pool, err := PoolOpen("SSD"); err == nil {
print("Pool size is: ", pool.Properties[PoolPropSize].Value)
// Turn on snapshot listing for pool
pool.SetProperty(PoolPropListsnaps, "on")
println("Changed property",
PoolPropertyToName(PoolPropListsnaps), "to value:",
pool.Properties[PoolPropListsnaps].Value)
prop, err := pool.GetProperty(PoolPropHealth)
if err != nil {
panic(err)
}
println("Update and print out pool health:", prop.Value)
} else {
print("Error: ", err)
}
}
// Open and list all pools on system with them properties
func ExamplePoolOpenAll() {
// Lets open handles to all active pools on system
pools, err := PoolOpenAll()
if err != nil {
println(err)
}
// Print each pool name and properties
for _, p := range pools {
// Print fancy header
fmt.Printf("\n -----------------------------------------------------------\n")
fmt.Printf(" POOL: %49s \n", p.Properties[PoolPropName].Value)
fmt.Printf("|-----------------------------------------------------------|\n")
fmt.Printf("| PROPERTY | VALUE | SOURCE |\n")
fmt.Printf("|-----------------------------------------------------------|\n")
// Iterate pool properties and print name, value and source
for key, prop := range p.Properties {
pkey := PoolProp(key)
if pkey == PoolPropName {
continue // Skip name its already printed above
}
fmt.Printf("|%14s | %20s | %15s |\n",
PoolPropertyToName(pkey),
prop.Value, prop.Source)
println("")
}
println("")
// Close pool handle and free memory, since it will not be used anymore
p.Close()
}
}
func ExamplePoolCreate() {
disks := [2]string{"/dev/disk/by-id/ATA-123", "/dev/disk/by-id/ATA-456"}
var vdev VDevTree
var vdevs, mdevs, sdevs []VDevTree
// build mirror devices specs
for _, d := range disks {
mdevs = append(mdevs,
VDevTree{Type: VDevTypeDisk, Path: d})
}
// spare device specs
sdevs = []VDevTree{
{Type: VDevTypeDisk, Path: "/dev/disk/by-id/ATA-789"}}
// pool specs
vdevs = []VDevTree{
VDevTree{Type: VDevTypeMirror, Devices: mdevs},
}
vdev.Devices = vdevs
vdev.Spares = sdevs
// pool properties
props := make(map[PoolProp]PropertyValue)
// root dataset filesystem properties
fsprops := make(map[DatasetProp]PropertyValue)
// pool features
features := make(map[string]string)
// Turn off auto mounting by ZFS
fsprops[DatasetPropMountpoint] = PropertyValue{Value: "none"}
// Enable some features
features["async_destroy"] = "enabled"
features["empty_bpobj"] = "enabled"
features["lz4_compress"] = "enabled"
// Based on specs formed above create test pool as 2 disk mirror and
// one spare disk
pool, err := PoolCreate("TESTPOOL", vdev, features, props, fsprops)
if err != nil {
println("Error: ", err.Error())
return
}
defer pool.Close()
}
func ExamplePool_Destroy() {
pname := "TESTPOOL"
// Need handle to pool at first place
p, err := PoolOpen(pname)
if err != nil {
println("Error: ", err.Error())
return
}
// Make sure pool handle is free after we are done here
defer p.Close()
if err = p.Destroy("Example of pool destroy (TESTPOOL)"); err != nil {
println("Error: ", err.Error())
return
}
}
func ExamplePoolImport() {
p, err := PoolImport("TESTPOOL", []string{"/dev/disk/by-id"})
if err != nil {
panic(err)
}
p.Close()
}
func ExamplePool_Export() {
p, err := PoolOpen("TESTPOOL")
if err != nil {
panic(err)
}
defer p.Close()
if err = p.Export(false, "Example exporting pool"); err != nil {
panic(err)
}
}
func ExamplePool_ExportForce() {
p, err := PoolOpen("TESTPOOL")
if err != nil {
panic(err)
}
defer p.Close()
if err = p.ExportForce("Example exporting pool"); err != nil {
panic(err)
}
}
func ExamplePool_State() {
p, err := PoolOpen("TESTPOOL")
if err != nil {
panic(err)
}
defer p.Close()
pstate, err := p.State()
if err != nil {
panic(err)
}
println("POOL TESTPOOL state:", PoolStateToName(pstate))
}
// func TestPool_VDevTree(t *testing.T) {
// type fields struct {
// poolName string
// }
// tests := []struct {
// name string
// fields fields
// wantErr bool
// }{
// // TODO: Add test cases.
// {
// name: "test1",
// fields: fields{"TESTPOOL"},
// wantErr: false,
// },
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// pool, _ := PoolOpen(tt.fields.poolName)
// defer pool.Close()
// gotVdevs, err := pool.VDevTree()
// if (err != nil) != tt.wantErr {
// t.Errorf("Pool.VDevTree() error = %v, wantErr %v", err, tt.wantErr)
// return
// }
// jsonData, _ := json.MarshalIndent(gotVdevs, "", "\t")
// t.Logf("gotVdevs: %s", string(jsonData))
// })
// }
// }