| 1 |
package zfs |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
const ( |
| 8 |
ZpoolOnline = "ONLINE" |
| 9 |
ZpoolDegraded = "DEGRADED" |
| 10 |
ZpoolFaulted = "FAULTED" |
| 11 |
ZpoolOffline = "OFFLINE" |
| 12 |
ZpoolUnavail = "UNAVAIL" |
| 13 |
ZpoolRemoved = "REMOVED" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 32 |
func zpool(arg ...string) error { |
| 33 |
_, err := zpoolOutput(arg...) |
| 34 |
return err |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func zpoolOutput(arg ...string) ([][]string, error) { |
| 39 |
c := command{Command: "zpool"} |
| 40 |
return c.Run(arg...) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 66 |
func (z *Zpool) Datasets() ([]*Dataset, error) { |
| 67 |
return Datasets(z.Name) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (z *Zpool) Snapshots() ([]*Dataset, error) { |
| 72 |
return Snapshots(z.Name) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 96 |
func (z *Zpool) Destroy() error { |
| 97 |
err := zpool("destroy", z.Name) |
| 98 |
return err |
| 99 |
} |
| 100 |
|
| 101 |
|
| 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 |
} |