attachLoopback.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // FIXME: Check here if target is a block device (in C: S_ISBLK(mode))
  36. if fi.IsDir() {
  37. }
  38. // Open the targeted loopback (use OpenFile because Open sets O_CLOEXEC)
  39. loopFile, err = osOpenFile(target, osORdWr, 0644)
  40. if err != nil {
  41. utils.Errorf("Error openning loopback device: %s", err)
  42. return nil, ErrAttachLoopbackDevice
  43. }
  44. // Try to attach to the loop file
  45. if err := ioctlLoopSetFd(loopFile.Fd(), sparseFile.Fd()); err != nil {
  46. loopFile.Close()
  47. // If the error is EBUSY, then try the next loopback
  48. if err != sysEBusy {
  49. utils.Errorf("Cannot set up loopback device %s: %s", target, err)
  50. return nil, ErrAttachLoopbackDevice
  51. }
  52. // Otherwise, we keep going with the loop
  53. continue
  54. }
  55. // In case of success, we finished. Break the loop.
  56. break
  57. }
  58. // This can't happen, but let's be sure
  59. if loopFile == nil {
  60. utils.Errorf("Unreachable code reached! Error attaching %s to a loopback device.", sparseFile.Name())
  61. return nil, ErrAttachLoopbackDevice
  62. }
  63. return loopFile, nil
  64. }
  65. // attachLoopDevice attaches the given sparse file to the next
  66. // available loopback device. It returns an opened *osFile.
  67. func attachLoopDevice(sparseName string) (loop *osFile, err error) {
  68. // Try to retrieve the next available loopback device via syscall.
  69. // If it fails, we discard error and start loopking for a
  70. // loopback from index 0.
  71. startIndex, err := getNextFreeLoopbackIndex()
  72. if err != nil {
  73. utils.Debugf("Error retrieving the next available loopback: %s", err)
  74. }
  75. // Open the given sparse file (use OpenFile because Open sets O_CLOEXEC)
  76. sparseFile, err := osOpenFile(sparseName, osORdWr, 0644)
  77. if err != nil {
  78. utils.Errorf("Error openning sparse file %s: %s", sparseName, err)
  79. return nil, ErrAttachLoopbackDevice
  80. }
  81. defer sparseFile.Close()
  82. loopFile, err := openNextAvailableLoopback(startIndex, sparseFile)
  83. if err != nil {
  84. return nil, err
  85. }
  86. // Set the status of the loopback device
  87. loopInfo := &LoopInfo64{
  88. loFileName: stringToLoopName(loopFile.Name()),
  89. loOffset: 0,
  90. loFlags: LoFlagsAutoClear,
  91. }
  92. if err := ioctlLoopSetStatus64(loopFile.Fd(), loopInfo); err != nil {
  93. utils.Errorf("Cannot set up loopback device info: %s", err)
  94. // If the call failed, then free the loopback device
  95. if err := ioctlLoopClrFd(loopFile.Fd()); err != nil {
  96. utils.Errorf("Error while cleaning up the loopback device")
  97. }
  98. loopFile.Close()
  99. return nil, ErrAttachLoopbackDevice
  100. }
  101. return loopFile, nil
  102. }