volume.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package volume
  2. import (
  3. "errors"
  4. "github.com/libopenstorage/openstorage/api"
  5. )
  6. var (
  7. // ErrAlreadyShutdown returned when driver is shutdown
  8. ErrAlreadyShutdown = errors.New("VolumeDriverProvider already shutdown")
  9. // ErrExit returned when driver already registered
  10. ErrExist = errors.New("Already exists")
  11. // ErrDriverNotFound returned when a driver is not registered
  12. ErrDriverNotFound = errors.New("Driver implementation not found")
  13. // ErrDriverInitializing returned when a driver is initializing
  14. ErrDriverInitializing = errors.New("Driver is initializing")
  15. // ErrEnoEnt returned when volume does not exist
  16. ErrEnoEnt = errors.New("Volume does not exist.")
  17. // ErrEnomem returned when we are out of memory
  18. ErrEnomem = errors.New("Out of memory.")
  19. // ErrEinval returned when an invalid input is provided
  20. ErrEinval = errors.New("Invalid argument")
  21. // ErrVolDetached returned when volume is in detached state
  22. ErrVolDetached = errors.New("Volume is detached")
  23. // ErrVolAttached returned when volume is in attached state
  24. ErrVolAttached = errors.New("Volume is attached")
  25. // ErrVolAttachedOnRemoteNode returned when volume is in attached on different node
  26. ErrVolAttachedOnRemoteNode = errors.New("Volume is attached on another node")
  27. // ErrVolAttachedScale returned when volume is attached and can be scaled
  28. ErrVolAttachedScale = errors.New("Volume is attached on another node." +
  29. " Increase scale factor to create more instances")
  30. // ErrVolHasSnaps returned when volume has previous snapshots
  31. ErrVolHasSnaps = errors.New("Volume has snapshots associated")
  32. // ErrNotSupported returned when the operation is not supported
  33. ErrNotSupported = errors.New("Operation not supported")
  34. // ErrVolBusy returned when volume is in busy state
  35. ErrVolBusy = errors.New("Volume is busy")
  36. )
  37. // Constants used by the VolumeDriver
  38. const (
  39. // APIVersion for the volume management apis
  40. APIVersion = "v1"
  41. // PluginAPIBase where the docker unix socket resides
  42. PluginAPIBase = "/run/docker/plugins/"
  43. // DriverAPIBase where the osd unix socket resides
  44. DriverAPIBase = "/var/lib/osd/driver/"
  45. // MountBase for osd mountpoints
  46. MountBase = "/var/lib/osd/mounts/"
  47. // VolumeBase for osd volumes
  48. VolumeBase = "/var/lib/osd/"
  49. )
  50. const (
  51. // LocationConstaint is a label that specifies data location constraint.
  52. LocationConstraint = "LocationConstraint"
  53. // LocalNode is an alias for this node - similar to localhost.
  54. LocalNode = "LocalNode"
  55. )
  56. // AttachOptionsKey specifies a key type from a key-value pair
  57. // that will be passed in to the Attach api
  58. type AttachOptionsKey string
  59. const (
  60. AttachOptionsSecret = AttachOptionsKey("SECRET_KEY")
  61. )
  62. // Store defines the interface for basic volume store operations
  63. type Store interface {
  64. // Lock volume specified by volumeID.
  65. Lock(volumeID string) (interface{}, error)
  66. // Lock volume with token obtained from call to Lock.
  67. Unlock(token interface{}) error
  68. // CreateVol returns error if volume with the same ID already existe.
  69. CreateVol(vol *api.Volume) error
  70. // GetVol from volumeID.
  71. GetVol(volumeID string) (*api.Volume, error)
  72. // UpdateVol with vol
  73. UpdateVol(vol *api.Volume) error
  74. // DeleteVol. Returns error if volume does not exist.
  75. DeleteVol(volumeID string) error
  76. }
  77. // VolumeDriver is the main interface to be implemented by any storage driver.
  78. // Every driver must at minimum implement the ProtoDriver sub interface.
  79. type VolumeDriver interface {
  80. IODriver
  81. ProtoDriver
  82. BlockDriver
  83. Enumerator
  84. }
  85. // IODriver interfaces applicable to object store interfaces.
  86. type IODriver interface {
  87. // Read sz bytes from specified volume at specified offset.
  88. // Return number of bytes read and error.
  89. Read(volumeID string, buf []byte, sz uint64, offset int64) (int64, error)
  90. // Write sz bytes from specified volume at specified offset.
  91. // Return number of bytes written and error.
  92. Write(volumeID string, buf []byte, sz uint64, offset int64) (int64, error)
  93. // Flush writes to stable storage.
  94. // Return error.
  95. Flush(volumeID string) error
  96. }
  97. // SnapshotDriver interfaces provides snapshot capability
  98. type SnapshotDriver interface {
  99. // Snapshot create volume snapshot.
  100. // Errors ErrEnoEnt may be returned
  101. Snapshot(volumeID string, readonly bool, locator *api.VolumeLocator) (string, error)
  102. // Restore restores volume to specified snapshot.
  103. Restore(volumeID string, snapshotID string) error
  104. }
  105. // StatsDriver interface provides stats features
  106. type StatsDriver interface {
  107. // Stats for specified volume.
  108. // cumulative stats are /proc/diskstats style stats.
  109. // nonCumulative stats are stats for specific duration.
  110. // Errors ErrEnoEnt may be returned
  111. Stats(volumeID string, cumulative bool) (*api.Stats, error)
  112. // UsedSize returns currently used volume size.
  113. // Errors ErrEnoEnt may be returned.
  114. UsedSize(volumeID string) (uint64, error)
  115. // GetActiveRequests get active requests
  116. GetActiveRequests() (*api.ActiveRequests, error)
  117. }
  118. // ProtoDriver must be implemented by all volume drivers. It specifies the
  119. // most basic functionality, such as creating and deleting volumes.
  120. type ProtoDriver interface {
  121. SnapshotDriver
  122. StatsDriver
  123. // Name returns the name of the driver.
  124. Name() string
  125. // Type of this driver
  126. Type() api.DriverType
  127. // Create a new Vol for the specific volume spec.
  128. // It returns a system generated VolumeID that uniquely identifies the volume
  129. Create(locator *api.VolumeLocator, Source *api.Source, spec *api.VolumeSpec) (string, error)
  130. // Delete volume.
  131. // Errors ErrEnoEnt, ErrVolHasSnaps may be returned.
  132. Delete(volumeID string) error
  133. // Mount volume at specified path
  134. // Errors ErrEnoEnt, ErrVolDetached may be returned.
  135. Mount(volumeID string, mountPath string) error
  136. // MountedAt return volume mounted at specified mountpath.
  137. MountedAt(mountPath string) string
  138. // Unmount volume at specified path
  139. // Errors ErrEnoEnt, ErrVolDetached may be returned.
  140. Unmount(volumeID string, mountPath string) error
  141. // Update not all fields of the spec are supported, ErrNotSupported will be thrown for unsupported
  142. // updates.
  143. Set(volumeID string, locator *api.VolumeLocator, spec *api.VolumeSpec) error
  144. // Status returns a set of key-value pairs which give low
  145. // level diagnostic status about this driver.
  146. Status() [][2]string
  147. // Shutdown and cleanup.
  148. Shutdown()
  149. }
  150. // Enumerator provides a set of interfaces to get details on a set of volumes.
  151. type Enumerator interface {
  152. // Inspect specified volumes.
  153. // Returns slice of volumes that were found.
  154. Inspect(volumeIDs []string) ([]*api.Volume, error)
  155. // Enumerate volumes that map to the volumeLocator. Locator fields may be regexp.
  156. // If locator fields are left blank, this will return all volumes.
  157. Enumerate(locator *api.VolumeLocator, labels map[string]string) ([]*api.Volume, error)
  158. // Enumerate snaps for specified volumes
  159. SnapEnumerate(volID []string, snapLabels map[string]string) ([]*api.Volume, error)
  160. }
  161. // StoreEnumerator combines Store and Enumerator capabilities
  162. type StoreEnumerator interface {
  163. Store
  164. Enumerator
  165. }
  166. // BlockDriver needs to be implemented by block volume drivers. Filesystem volume
  167. // drivers can ignore this interface and include the builtin DefaultBlockDriver.
  168. type BlockDriver interface {
  169. // Attach map device to the host.
  170. // On success the devicePath specifies location where the device is exported
  171. // Errors ErrEnoEnt, ErrVolAttached may be returned.
  172. Attach(volumeID string, attachOptions map[string]string) (string, error)
  173. // Detach device from the host.
  174. // Errors ErrEnoEnt, ErrVolDetached may be returned.
  175. Detach(volumeID string, unmountBeforeDetach bool) error
  176. }
  177. // VolumeDriverProvider provides VolumeDrivers.
  178. type VolumeDriverProvider interface {
  179. // Get gets the VolumeDriver for the given name.
  180. // If a VolumeDriver was not created for the given name, the error ErrDriverNotFound is returned.
  181. Get(name string) (VolumeDriver, error)
  182. // Shutdown shuts down all volume drivers.
  183. Shutdown() error
  184. }
  185. // VolumeDriverRegistry registers VolumeDrivers.
  186. type VolumeDriverRegistry interface {
  187. VolumeDriverProvider
  188. // New creates the VolumeDriver for the given name.
  189. // If a VolumeDriver was already created for the given name, the error ErrExist is returned.
  190. Register(name string, params map[string]string) error
  191. // Add inserts a new VolumeDriver provider with a well known name.
  192. Add(name string, init func(map[string]string) (VolumeDriver, error)) error
  193. }
  194. // NewVolumeDriverRegistry constructs a new VolumeDriverRegistry.
  195. func NewVolumeDriverRegistry(nameToInitFunc map[string]func(map[string]string) (VolumeDriver, error)) VolumeDriverRegistry {
  196. return newVolumeDriverRegistry(nameToInitFunc)
  197. }