volume_path_handler_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // +build linux
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package volumepathhandler
  15. import (
  16. "errors"
  17. "fmt"
  18. "os"
  19. "os/exec"
  20. "strings"
  21. "k8s.io/klog"
  22. )
  23. // AttachFileDevice takes a path to a regular file and makes it available as an
  24. // attached block device.
  25. func (v VolumePathHandler) AttachFileDevice(path string) (string, error) {
  26. blockDevicePath, err := v.GetLoopDevice(path)
  27. if err != nil && err.Error() != ErrDeviceNotFound {
  28. return "", err
  29. }
  30. // If no existing loop device for the path, create one
  31. if blockDevicePath == "" {
  32. klog.V(4).Infof("Creating device for path: %s", path)
  33. blockDevicePath, err = makeLoopDevice(path)
  34. if err != nil {
  35. return "", err
  36. }
  37. }
  38. return blockDevicePath, nil
  39. }
  40. // GetLoopDevice returns the full path to the loop device associated with the given path.
  41. func (v VolumePathHandler) GetLoopDevice(path string) (string, error) {
  42. _, err := os.Stat(path)
  43. if os.IsNotExist(err) {
  44. return "", errors.New(ErrDeviceNotFound)
  45. }
  46. if err != nil {
  47. return "", fmt.Errorf("not attachable: %v", err)
  48. }
  49. args := []string{"-j", path}
  50. cmd := exec.Command(losetupPath, args...)
  51. out, err := cmd.CombinedOutput()
  52. if err != nil {
  53. klog.V(2).Infof("Failed device discover command for path %s: %v %s", path, err, out)
  54. return "", err
  55. }
  56. return parseLosetupOutputForDevice(out)
  57. }
  58. func makeLoopDevice(path string) (string, error) {
  59. args := []string{"-f", "--show", path}
  60. cmd := exec.Command(losetupPath, args...)
  61. out, err := cmd.CombinedOutput()
  62. if err != nil {
  63. klog.V(2).Infof("Failed device create command for path: %s %v %s ", path, err, out)
  64. return "", err
  65. }
  66. return parseLosetupOutputForDevice(out)
  67. }
  68. // RemoveLoopDevice removes specified loopback device
  69. func (v VolumePathHandler) RemoveLoopDevice(device string) error {
  70. args := []string{"-d", device}
  71. cmd := exec.Command(losetupPath, args...)
  72. out, err := cmd.CombinedOutput()
  73. if err != nil {
  74. if _, err := os.Stat(device); os.IsNotExist(err) {
  75. return nil
  76. }
  77. klog.V(2).Infof("Failed to remove loopback device: %s: %v %s", device, err, out)
  78. return err
  79. }
  80. return nil
  81. }
  82. func parseLosetupOutputForDevice(output []byte) (string, error) {
  83. if len(output) == 0 {
  84. return "", errors.New(ErrDeviceNotFound)
  85. }
  86. // losetup returns device in the format:
  87. // /dev/loop1: [0073]:148662 (/dev/sda)
  88. device := strings.TrimSpace(strings.SplitN(string(output), ":", 2)[0])
  89. if len(device) == 0 {
  90. return "", errors.New(ErrDeviceNotFound)
  91. }
  92. return device, nil
  93. }