Debian Codegraph @

zpool.go

raw symbols SCIP
1 package zfs
2
3 // ZFS zpool states, which can indicate if a pool is online, offline, degraded, etc.
4 //
5 // More information regarding zpool states can be found in the ZFS manual:
6 // https://openzfs.github.io/openzfs-docs/man/7/zpoolconcepts.7.html#Device_Failure_and_Recovery
7 const (
8 ZpoolOnline = "ONLINE"
9 ZpoolDegraded = "DEGRADED"
10 ZpoolFaulted = "FAULTED"
11 ZpoolOffline = "OFFLINE"
12 ZpoolUnavail = "UNAVAIL"
13 ZpoolRemoved = "REMOVED"
14 )
15
16 // Zpool is a ZFS zpool.
17 // A pool is a top-level structure in ZFS, and can contain many descendent datasets.
18 type Zpool struct {
19 Name string
20 Health string
21 Allocated uint64
22 Size uint64
23 Free uint64
24 Fragmentation uint64
25 ReadOnly bool
26 Freeing uint64
27 Leaked uint64
28 DedupRatio float64
29 }
30
31 // zpool is a helper function to wrap typical calls to zpool and ignores stdout.
32 func zpool(arg ...string) error {
33 _, err := zpoolOutput(arg...)
34 return err
35 }
36
37 // zpool is a helper function to wrap typical calls to zpool.
38 func zpoolOutput(arg ...string) ([][]string, error) {
39 c := command{Command: "zpool"}
40 return c.Run(arg...)
41 }
42
43 // GetZpool retrieves a single ZFS zpool by name.
44 func GetZpool(name string) (*Zpool, error) {
45 args := zpoolArgs
46 args = append(args, name)
47 out, err := zpoolOutput(args...)
48 if err != nil {
49 return nil, err
50 }
51
52 // there is no -H
53 out = out[1:]
54
55 z := &Zpool{Name: name}
56 for _, line := range out {
57 if err := z.parseLine(line); err != nil {
58 return nil, err
59 }
60 }
61
62 return z, nil
63 }
64
65 // Datasets returns a slice of all ZFS datasets in a zpool.
66 func (z *Zpool) Datasets() ([]*Dataset, error) {
67 return Datasets(z.Name)
68 }
69
70 // Snapshots returns a slice of all ZFS snapshots in a zpool.
71 func (z *Zpool) Snapshots() ([]*Dataset, error) {
72 return Snapshots(z.Name)
73 }
74
75 // CreateZpool creates a new ZFS zpool with the specified name, properties, and optional arguments.
76 //
77 // A full list of available ZFS properties and command-line arguments may be found in the ZFS manual:
78 // https://openzfs.github.io/openzfs-docs/man/7/zfsprops.7.html.
79 // https://openzfs.github.io/openzfs-docs/man/8/zpool-create.8.html
80 func CreateZpool(name string, properties map[string]string, args ...string) (*Zpool, error) {
81 cli := make([]string, 1, 4)
82 cli[0] = "create"
83 if properties != nil {
84 cli = append(cli, propsSlice(properties)...)
85 }
86 cli = append(cli, name)
87 cli = append(cli, args...)
88 if err := zpool(cli...); err != nil {
89 return nil, err
90 }
91
92 return &Zpool{Name: name}, nil
93 }
94
95 // Destroy destroys a ZFS zpool by name.
96 func (z *Zpool) Destroy() error {
97 err := zpool("destroy", z.Name)
98 return err
99 }
100
101 // ListZpools list all ZFS zpools accessible on the current system.
102 func ListZpools() ([]*Zpool, error) {
103 args := []string{"list", "-Ho", "name"}
104 out, err := zpoolOutput(args...)
105 if err != nil {
106 return nil, err
107 }
108
109 var pools []*Zpool
110
111 for _, line := range out {
112 z, err := GetZpool(line[0])
113 if err != nil {
114 return nil, err
115 }
116 pools = append(pools, z)
117 }
118 return pools, nil
119 }