attach_loopback.go 3.2 KB

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