storageos_util.go 11 KB

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