attach_loopback.go 3.2 KB

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