-
Notifications
You must be signed in to change notification settings - Fork 2
/
zfs.go
197 lines (172 loc) · 4.48 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
package zfs
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// Dataset is a zfs dataset
type Dataset struct {
Name string
}
// GetDataset returns a zfs dataset object from a string
func GetDataset(name string) (*Dataset, error) {
if !DatasetExists(name) {
return nil, fmt.Errorf("Dataset not found")
}
return &Dataset{
Name: name,
}, nil
}
func datasetList(name string) (ds []Dataset, err error) {
out, err := exec.Command("zfs", "list", "-r", "-H", "-o", "name", "-t", "filesystem", name).Output()
if err != nil {
return
}
for _, d := range bytes.Split(out, []byte("\n")) {
if strings.TrimSpace(string(d)) == name {
continue
}
if strings.TrimSpace(string(d)) == "" {
continue
}
ds = append(ds, Dataset{Name: strings.TrimSpace(string(d))})
}
return
}
// DatasetList lists all datasets
func DatasetList() ([]Dataset, error) {
return datasetList("")
}
// DatasetExists checks for the existence of a dataset
func DatasetExists(name string) bool {
err := exec.Command("zfs", "list", "-t", "filesystem", name).Run()
return err == nil
}
// DatasetList lists all child datasets
func (ds *Dataset) DatasetList() ([]Dataset, error) {
return datasetList(ds.Name)
}
// GetProperty returns a property for a dataset
func (ds *Dataset) GetProperty(property string) (string, error) {
out, err := exec.Command("zfs", "get", "-H",
"-o", "value",
property, ds.Name).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// GetExactProperty returns a property for a dataset using exact values
func (ds *Dataset) GetExactProperty(property string) (string, error) {
out, err := exec.Command("zfs", "get", "-H", "-p",
"-o", "value",
property, ds.Name).Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// GetMountpoint returns the mountpoint for a dataset
func (ds *Dataset) GetMountpoint() (string, error) {
return ds.GetProperty("mountpoint")
}
func (ds *Dataset) GetCreation() (time.Time, error) {
uts, err := ds.GetExactProperty("creation")
if err != nil {
return time.Time{}, err
}
ut, err := strconv.ParseInt(uts, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(ut, 0), nil
}
// CreateDatasetRecursive recursively creates a dataset
func CreateDatasetRecursive(name string, properties map[string]string) (*Dataset, error) {
return createDataset(name, true, properties)
}
// CreateDataset creates a dataset
func CreateDataset(name string, properties map[string]string) (*Dataset, error) {
return createDataset(name, false, properties)
}
func createDataset(name string, recursive bool, properties map[string]string) (*Dataset, error) {
args := []string{"create"}
if recursive {
args = append(args, "-p")
}
if properties != nil {
for n, v := range properties {
args = append(args, "-o", fmt.Sprintf("%v=%v", n, v))
}
}
args = append(args, name)
err := exec.Command("zfs", args...).Run()
if err != nil {
return nil, err
}
return GetDataset(name)
}
// Destroy destroys a dataset
func (ds *Dataset) Destroy() error {
if err := exec.Command("zfs", "destroy", "-R", ds.Name).Run(); err != nil {
return err
}
return nil
}
// GetSnapshot returns a snapshot from a string
func GetSnapshot(name string) (*Snapshot, error) {
if !SnapshotExists(name) {
return nil, fmt.Errorf("Snapshot not found")
}
return &Snapshot{
Name: name,
}, nil
}
// SnapshotExists checks for the existence of a dataset
func SnapshotExists(Name string) bool {
err := exec.Command("zfs", "list", "-t", "snapshot", Name).Run()
return err == nil
}
// Snapshot represents a zfs snapshot
type Snapshot struct {
Name string
}
// Snapshot creates a snapshot
func (ds *Dataset) Snapshot(name string) (*Snapshot, error) {
sn := fmt.Sprintf("%v@%v", ds.Name, name)
err := exec.Command("zfs", "snapshot", sn).Run()
if err != nil {
return nil, err
}
return GetSnapshot(sn)
}
// Clone clones a snapshot
func (sn *Snapshot) Clone(target string) (*Dataset, error) {
err := exec.Command("zfs", "clone", sn.Name, target).Run()
if err != nil {
return nil, err
}
return GetDataset(target)
}
// Promote promotes a cloned dataset
func (ds *Dataset) Promote() error {
return exec.Command("zfs", "promote", ds.Name).Run()
}
// IsEmpty returns whether or not a directory is empty
func IsEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1)
if err == io.EOF {
return true, nil
}
return false, err
}