zpool.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. }
  22. // zpool is a helper function to wrap typical calls to zpool.
  23. func zpool(arg ...string) ([][]string, error) {
  24. c := command{Command: "zpool"}
  25. return c.Run(arg...)
  26. }
  27. // GetZpool retrieves a single ZFS zpool by name.
  28. func GetZpool(name string) (*Zpool, error) {
  29. out, err := zpool("get", "all", "-p", name)
  30. if err != nil {
  31. return nil, err
  32. }
  33. // there is no -H
  34. out = out[1:]
  35. z := &Zpool{Name: name}
  36. for _, line := range out {
  37. if err := z.parseLine(line); err != nil {
  38. return nil, err
  39. }
  40. }
  41. return z, nil
  42. }
  43. // Datasets returns a slice of all ZFS datasets in a zpool.
  44. func (z *Zpool) Datasets() ([]*Dataset, error) {
  45. return Datasets(z.Name)
  46. }
  47. // Snapshots returns a slice of all ZFS snapshots in a zpool.
  48. func (z *Zpool) Snapshots() ([]*Dataset, error) {
  49. return Snapshots(z.Name)
  50. }
  51. // CreateZpool creates a new ZFS zpool with the specified name, properties,
  52. // and optional arguments.
  53. // A full list of available ZFS properties and command-line arguments may be
  54. // found here: https://www.freebsd.org/cgi/man.cgi?zfs(8).
  55. func CreateZpool(name string, properties map[string]string, args ...string) (*Zpool, error) {
  56. cli := make([]string, 1, 4)
  57. cli[0] = "create"
  58. if properties != nil {
  59. cli = append(cli, propsSlice(properties)...)
  60. }
  61. cli = append(cli, name)
  62. cli = append(cli, args...)
  63. _, err := zpool(cli...)
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &Zpool{Name: name}, nil
  68. }
  69. // Destroy destroys a ZFS zpool by name.
  70. func (z *Zpool) Destroy() error {
  71. _, err := zpool("destroy", z.Name)
  72. return err
  73. }
  74. // ListZpools list all ZFS zpools accessible on the current system.
  75. func ListZpools() ([]*Zpool, error) {
  76. args := []string{"list", "-Ho", "name"}
  77. out, err := zpool(args...)
  78. if err != nil {
  79. return nil, err
  80. }
  81. var pools []*Zpool
  82. for _, line := range out {
  83. z, err := GetZpool(line[0])
  84. if err != nil {
  85. return nil, err
  86. }
  87. pools = append(pools, z)
  88. }
  89. return pools, nil
  90. }