storageos_util.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package storageos
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "strings"
  20. "k8s.io/kubernetes/pkg/util/mount"
  21. storageosapi "github.com/storageos/go-api"
  22. storageostypes "github.com/storageos/go-api/types"
  23. "k8s.io/klog"
  24. )
  25. const (
  26. losetupPath = "losetup"
  27. modeBlock deviceType = iota
  28. modeFile
  29. modeUnsupported
  30. ErrDeviceNotFound = "device not found"
  31. ErrDeviceNotSupported = "device not supported"
  32. ErrNotAvailable = "not available"
  33. )
  34. type deviceType int
  35. // storageosVolume describes a provisioned volume
  36. type storageosVolume struct {
  37. ID string
  38. Name string
  39. Namespace string
  40. Description string
  41. Pool string
  42. SizeGB int
  43. Labels map[string]string
  44. FSType string
  45. }
  46. type storageosAPIConfig struct {
  47. apiAddr string
  48. apiUser string
  49. apiPass string
  50. apiVersion string
  51. }
  52. type apiImplementer interface {
  53. Volume(namespace string, ref string) (*storageostypes.Volume, error)
  54. VolumeCreate(opts storageostypes.VolumeCreateOptions) (*storageostypes.Volume, error)
  55. VolumeMount(opts storageostypes.VolumeMountOptions) error
  56. VolumeUnmount(opts storageostypes.VolumeUnmountOptions) error
  57. VolumeDelete(opt storageostypes.DeleteOptions) error
  58. Node(ref string) (*storageostypes.Node, error)
  59. }
  60. // storageosUtil is the utility structure to interact with the StorageOS API.
  61. type storageosUtil struct {
  62. api apiImplementer
  63. }
  64. func (u *storageosUtil) NewAPI(apiCfg *storageosAPIConfig) error {
  65. if u.api != nil {
  66. return nil
  67. }
  68. if apiCfg == nil {
  69. apiCfg = &storageosAPIConfig{
  70. apiAddr: defaultAPIAddress,
  71. apiUser: defaultAPIUser,
  72. apiPass: defaultAPIPassword,
  73. apiVersion: defaultAPIVersion,
  74. }
  75. klog.V(4).Infof("using default StorageOS API settings: addr %s, version: %s", apiCfg.apiAddr, defaultAPIVersion)
  76. }
  77. api, err := storageosapi.NewVersionedClient(apiCfg.apiAddr, defaultAPIVersion)
  78. if err != nil {
  79. return err
  80. }
  81. api.SetAuth(apiCfg.apiUser, apiCfg.apiPass)
  82. u.api = api
  83. return nil
  84. }
  85. // Creates a new StorageOS volume and makes it available as a device within
  86. // /var/lib/storageos/volumes.
  87. func (u *storageosUtil) CreateVolume(p *storageosProvisioner) (*storageosVolume, error) {
  88. klog.V(4).Infof("creating StorageOS volume %q with namespace %q", p.volName, p.volNamespace)
  89. if err := u.NewAPI(p.apiCfg); err != nil {
  90. return nil, err
  91. }
  92. if p.labels == nil {
  93. p.labels = make(map[string]string)
  94. }
  95. opts := storageostypes.VolumeCreateOptions{
  96. Name: p.volName,
  97. Size: p.sizeGB,
  98. Description: p.description,
  99. Pool: p.pool,
  100. FSType: p.fsType,
  101. Namespace: p.volNamespace,
  102. Labels: p.labels,
  103. }
  104. vol, err := u.api.VolumeCreate(opts)
  105. if err != nil {
  106. klog.Errorf("volume create failed for volume %q (%v)", opts.Name, err)
  107. return nil, err
  108. }
  109. return &storageosVolume{
  110. ID: vol.ID,
  111. Name: vol.Name,
  112. Namespace: vol.Namespace,
  113. Description: vol.Description,
  114. Pool: vol.Pool,
  115. FSType: vol.FSType,
  116. SizeGB: int(vol.Size),
  117. Labels: vol.Labels,
  118. }, nil
  119. }
  120. // Attach exposes a volume on the host as a block device. StorageOS uses a
  121. // global namespace, so if the volume exists, it should already be available as
  122. // a device within `/var/lib/storageos/volumes/<id>`.
  123. //
  124. // Depending on the host capabilities, the device may be either a block device
  125. // or a file device. Block devices can be used directly, but file devices must
  126. // be made accessible as a block device before using.
  127. func (u *storageosUtil) AttachVolume(b *storageosMounter) (string, error) {
  128. klog.V(4).Infof("attaching StorageOS volume %q with namespace %q", b.volName, b.volNamespace)
  129. if err := u.NewAPI(b.apiCfg); err != nil {
  130. return "", err
  131. }
  132. // Get the node's device path from the API, falling back to the default if
  133. // not set on the node.
  134. if b.deviceDir == "" {
  135. b.deviceDir = u.DeviceDir(b)
  136. }
  137. vol, err := u.api.Volume(b.volNamespace, b.volName)
  138. if err != nil {
  139. klog.Warningf("volume retrieve failed for volume %q with namespace %q (%v)", b.volName, b.volNamespace, err)
  140. return "", err
  141. }
  142. srcPath := filepath.Join(b.deviceDir, vol.ID)
  143. dt, err := pathDeviceType(srcPath)
  144. if err != nil {
  145. klog.Warningf("volume source path %q for volume %q not ready (%v)", srcPath, b.volName, err)
  146. return "", err
  147. }
  148. switch dt {
  149. case modeBlock:
  150. return srcPath, nil
  151. case modeFile:
  152. return attachFileDevice(srcPath, b.exec)
  153. default:
  154. return "", fmt.Errorf(ErrDeviceNotSupported)
  155. }
  156. }
  157. // Detach detaches a volume from the host. This is only needed when NBD is not
  158. // enabled and loop devices are used to simulate a block device.
  159. func (u *storageosUtil) DetachVolume(b *storageosUnmounter, devicePath string) error {
  160. klog.V(4).Infof("detaching StorageOS volume %q with namespace %q", b.volName, b.volNamespace)
  161. if !isLoopDevice(devicePath) {
  162. return nil
  163. }
  164. if _, err := os.Stat(devicePath); os.IsNotExist(err) {
  165. return nil
  166. }
  167. return removeLoopDevice(devicePath, b.exec)
  168. }
  169. // AttachDevice attaches the volume device to the host at a given mount path.
  170. func (u *storageosUtil) AttachDevice(b *storageosMounter, deviceMountPath string) error {
  171. klog.V(4).Infof("attaching StorageOS device for volume %q with namespace %q", b.volName, b.volNamespace)
  172. if err := u.NewAPI(b.apiCfg); err != nil {
  173. return err
  174. }
  175. opts := storageostypes.VolumeMountOptions{
  176. Name: b.volName,
  177. Namespace: b.volNamespace,
  178. FsType: b.fsType,
  179. Mountpoint: deviceMountPath,
  180. Client: b.plugin.host.GetHostName(),
  181. }
  182. if err := u.api.VolumeMount(opts); err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. // Mount mounts the volume on the host.
  188. func (u *storageosUtil) MountVolume(b *storageosMounter, mntDevice, deviceMountPath string) error {
  189. klog.V(4).Infof("mounting StorageOS volume %q with namespace %q", b.volName, b.volNamespace)
  190. notMnt, err := b.mounter.IsLikelyNotMountPoint(deviceMountPath)
  191. if err != nil {
  192. if os.IsNotExist(err) {
  193. if err = os.MkdirAll(deviceMountPath, 0750); err != nil {
  194. return err
  195. }
  196. notMnt = true
  197. } else {
  198. return err
  199. }
  200. }
  201. if err = os.MkdirAll(deviceMountPath, 0750); err != nil {
  202. klog.Errorf("mkdir failed on disk %s (%v)", deviceMountPath, err)
  203. return err
  204. }
  205. options := []string{}
  206. if b.readOnly {
  207. options = append(options, "ro")
  208. }
  209. if notMnt {
  210. err = b.diskMounter.FormatAndMount(mntDevice, deviceMountPath, b.fsType, options)
  211. if err != nil {
  212. os.Remove(deviceMountPath)
  213. return err
  214. }
  215. }
  216. return err
  217. }
  218. // Unmount removes the mount reference from the volume allowing it to be
  219. // re-mounted elsewhere.
  220. func (u *storageosUtil) UnmountVolume(b *storageosUnmounter) error {
  221. klog.V(4).Infof("clearing StorageOS mount reference for volume %q with namespace %q", b.volName, b.volNamespace)
  222. if err := u.NewAPI(b.apiCfg); err != nil {
  223. // We can't always get the config we need, so allow the unmount to
  224. // succeed even if we can't remove the mount reference from the API.
  225. klog.Warningf("could not remove mount reference in the StorageOS API as no credentials available to the unmount operation")
  226. return nil
  227. }
  228. opts := storageostypes.VolumeUnmountOptions{
  229. Name: b.volName,
  230. Namespace: b.volNamespace,
  231. Client: b.plugin.host.GetHostName(),
  232. }
  233. return u.api.VolumeUnmount(opts)
  234. }
  235. // Deletes a StorageOS volume. Assumes it has already been unmounted and detached.
  236. func (u *storageosUtil) DeleteVolume(d *storageosDeleter) error {
  237. if err := u.NewAPI(d.apiCfg); err != nil {
  238. return err
  239. }
  240. // Deletes must be forced as the StorageOS API will not normally delete
  241. // volumes that it thinks are mounted. We can't be sure the unmount was
  242. // registered via the API so we trust k8s to only delete volumes it knows
  243. // are unmounted.
  244. opts := storageostypes.DeleteOptions{
  245. Name: d.volName,
  246. Namespace: d.volNamespace,
  247. Force: true,
  248. }
  249. return u.api.VolumeDelete(opts)
  250. }
  251. // Get the node's device path from the API, falling back to the default if not
  252. // specified.
  253. func (u *storageosUtil) DeviceDir(b *storageosMounter) string {
  254. ctrl, err := u.api.Node(b.plugin.host.GetHostName())
  255. if err != nil {
  256. klog.Warningf("node device path lookup failed: %v", err)
  257. return defaultDeviceDir
  258. }
  259. if ctrl == nil || ctrl.DeviceDir == "" {
  260. klog.Warningf("node device path not set, using default: %s", defaultDeviceDir)
  261. return defaultDeviceDir
  262. }
  263. return ctrl.DeviceDir
  264. }
  265. // pathMode returns the FileMode for a path.
  266. func pathDeviceType(path string) (deviceType, error) {
  267. fi, err := os.Stat(path)
  268. if err != nil {
  269. return modeUnsupported, err
  270. }
  271. switch mode := fi.Mode(); {
  272. case mode&os.ModeDevice != 0:
  273. return modeBlock, nil
  274. case mode.IsRegular():
  275. return modeFile, nil
  276. default:
  277. return modeUnsupported, nil
  278. }
  279. }
  280. // attachFileDevice takes a path to a regular file and makes it available as an
  281. // attached block device.
  282. func attachFileDevice(path string, exec mount.Exec) (string, error) {
  283. blockDevicePath, err := getLoopDevice(path, exec)
  284. if err != nil && err.Error() != ErrDeviceNotFound {
  285. return "", err
  286. }
  287. // If no existing loop device for the path, create one
  288. if blockDevicePath == "" {
  289. klog.V(4).Infof("Creating device for path: %s", path)
  290. blockDevicePath, err = makeLoopDevice(path, exec)
  291. if err != nil {
  292. return "", err
  293. }
  294. }
  295. return blockDevicePath, nil
  296. }
  297. // Returns the full path to the loop device associated with the given path.
  298. func getLoopDevice(path string, exec mount.Exec) (string, error) {
  299. _, err := os.Stat(path)
  300. if os.IsNotExist(err) {
  301. return "", errors.New(ErrNotAvailable)
  302. }
  303. if err != nil {
  304. return "", fmt.Errorf("not attachable: %v", err)
  305. }
  306. args := []string{"-j", path}
  307. out, err := exec.Run(losetupPath, args...)
  308. if err != nil {
  309. klog.V(2).Infof("Failed device discover command for path %s: %v", path, err)
  310. return "", err
  311. }
  312. return parseLosetupOutputForDevice(out)
  313. }
  314. func makeLoopDevice(path string, exec mount.Exec) (string, error) {
  315. args := []string{"-f", "-P", "--show", path}
  316. out, err := exec.Run(losetupPath, args...)
  317. if err != nil {
  318. klog.V(2).Infof("Failed device create command for path %s: %v", path, err)
  319. return "", err
  320. }
  321. return parseLosetupOutputForDevice(out)
  322. }
  323. func removeLoopDevice(device string, exec mount.Exec) error {
  324. args := []string{"-d", device}
  325. out, err := exec.Run(losetupPath, args...)
  326. if err != nil {
  327. if !strings.Contains(string(out), "No such device or address") {
  328. return err
  329. }
  330. }
  331. return nil
  332. }
  333. func isLoopDevice(device string) bool {
  334. return strings.HasPrefix(device, "/dev/loop")
  335. }
  336. func parseLosetupOutputForDevice(output []byte) (string, error) {
  337. if len(output) == 0 {
  338. return "", errors.New(ErrDeviceNotFound)
  339. }
  340. // losetup returns device in the format:
  341. // /dev/loop1: [0073]:148662 (/var/lib/storageos/volumes/308f14af-cf0a-08ff-c9c3-b48104318e05)
  342. device := strings.TrimSpace(strings.SplitN(string(output), ":", 2)[0])
  343. if len(device) == 0 {
  344. return "", errors.New(ErrDeviceNotFound)
  345. }
  346. return device, nil
  347. }