zpool.go 2.6 KB

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