attach_loopback.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // +build linux,cgo
  2. package loopback
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "syscall"
  8. "github.com/Sirupsen/logrus"
  9. )
  10. // Loopback related errors
  11. var (
  12. ErrAttachLoopbackDevice = errors.New("loopback attach failed")
  13. ErrGetLoopbackBackingFile = errors.New("Unable to get loopback backing file")
  14. ErrSetCapacity = errors.New("Unable set loopback capacity")
  15. )
  16. func stringToLoopName(src string) [LoNameSize]uint8 {
  17. var dst [LoNameSize]uint8
  18. copy(dst[:], src[:])
  19. return dst
  20. }
  21. func getNextFreeLoopbackIndex() (int, error) {
  22. f, err := os.OpenFile("/dev/loop-control", os.O_RDONLY, 0644)
  23. if err != nil {
  24. return 0, err
  25. }
  26. defer f.Close()
  27. index, err := ioctlLoopCtlGetFree(f.Fd())
  28. if index < 0 {
  29. index = 0
  30. }
  31. return index, err
  32. }
  33. func openNextAvailableLoopback(index int, sparseFile *os.File) (loopFile *os.File, err error) {
  34. // Start looking for a free /dev/loop
  35. for {
  36. target := fmt.Sprintf("/dev/loop%d", index)
  37. index++
  38. fi, err := os.Stat(target)
  39. if err != nil {
  40. if os.IsNotExist(err) {
  41. logrus.Error("There are no more loopback devices available.")
  42. }
  43. return nil, ErrAttachLoopbackDevice
  44. }
  45. if fi.Mode()&os.ModeDevice != os.ModeDevice {
  46. logrus.Errorf("Loopback device %s is not a block device.", target)
  47. continue
  48. }
  49. // OpenFile adds O_CLOEXEC
  50. loopFile, err = os.OpenFile(target, os.O_RDWR, 0644)
  51. if err != nil {
  52. logrus.Errorf("Error opening loopback device: %s", err)
  53. return nil, ErrAttachLoopbackDevice
  54. }
  55. // Try to attach to the loop file
  56. if err := ioctlLoopSetFd(loopFile.Fd(), sparseFile.Fd()); err != nil {
  57. loopFile.Close()
  58. // If the error is EBUSY, then try the next loopback
  59. if err != syscall.EBUSY {
  60. logrus.Errorf("Cannot set up loopback device %s: %s", target, err)
  61. return nil, ErrAttachLoopbackDevice
  62. }
  63. // Otherwise, we keep going with the loop
  64. continue
  65. }
  66. // In case of success, we finished. Break the loop.
  67. break
  68. }
  69. // This can't happen, but let's be sure
  70. if loopFile == nil {
  71. logrus.Errorf("Unreachable code reached! Error attaching %s to a loopback device.", sparseFile.Name())
  72. return nil, ErrAttachLoopbackDevice
  73. }
  74. return loopFile, nil
  75. }
  76. // AttachLoopDevice attaches the given sparse file to the next
  77. // available loopback device. It returns an opened *os.File.
  78. func AttachLoopDevice(sparseName string) (loop *os.File, err error) {
  79. // Try to retrieve the next available loopback device via syscall.
  80. // If it fails, we discard error and start looping for a
  81. // loopback from index 0.
  82. startIndex, err := getNextFreeLoopbackIndex()
  83. if err != nil {
  84. logrus.Debugf("Error retrieving the next available loopback: %s", err)
  85. }
  86. // OpenFile adds O_CLOEXEC
  87. sparseFile, err := os.OpenFile(sparseName, os.O_RDWR, 0644)
  88. if err != nil {
  89. logrus.Errorf("Error opening sparse file %s: %s", sparseName, err)
  90. return nil, ErrAttachLoopbackDevice
  91. }
  92. defer sparseFile.Close()
  93. loopFile, err := openNextAvailableLoopback(startIndex, sparseFile)
  94. if err != nil {
  95. return nil, err
  96. }
  97. // Set the status of the loopback device
  98. loopInfo := &loopInfo64{
  99. loFileName: stringToLoopName(loopFile.Name()),
  100. loOffset: 0,
  101. loFlags: LoFlagsAutoClear,
  102. }
  103. if err := ioctlLoopSetStatus64(loopFile.Fd(), loopInfo); err != nil {
  104. logrus.Errorf("Cannot set up loopback device info: %s", err)
  105. // If the call failed, then free the loopback device
  106. if err := ioctlLoopClrFd(loopFile.Fd()); err != nil {
  107. logrus.Error("Error while cleaning up the loopback device")
  108. }
  109. loopFile.Close()
  110. return nil, ErrAttachLoopbackDevice
  111. }
  112. return loopFile, nil
  113. }