attach_loopback.go 3.2 KB

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