/
zfs.go
68 lines (56 loc) · 1.42 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
package backup
import (
"fmt"
"os/exec"
"strings"
"github.com/vegaprotocol/devopstools/tools"
)
type Pool struct {
Name string
MountPoint string
}
func CheckZfsCommand() error {
if _, err := exec.LookPath("zfs"); err != nil {
return fmt.Errorf("zfs command not found: %w", err)
}
return nil
}
func ListZfsPools(parent string) ([]Pool, error) {
args := []string{
"list",
"-o", "name,mountpoint",
}
out, err := tools.ExecuteBinary("zfs", args, nil)
if err != nil {
return nil, fmt.Errorf("failed to list zfs pools: %w", err)
}
result := []Pool{}
tokens := strings.Fields(string(out))
if len(tokens)%2 != 0 {
return nil, fmt.Errorf("failed to parse output: got invalid number of tokens")
}
// Skip first 2 tokens because it is header from the example output:
// NAME MOUNTPOINT
// vega_pool /vega_pool
// vega_pool/home /home/vega
for i := 2; i < len(tokens); i += 2 {
if !strings.HasPrefix(tokens[i], parent) {
continue
}
result = append(result, Pool{
Name: tokens[i],
MountPoint: tokens[i+1],
})
}
return result, nil
}
func CreateRecursiveZfsSnapshot(pool string, ID string) error {
args := []string{
"snapshot",
"-r", fmt.Sprintf("%s@%s", pool, ID),
}
if out, err := tools.ExecuteBinary("zfs", args, nil); err != nil {
return fmt.Errorf("failed to create snapshot, out: %s: %w", out, err)
}
return nil
}