attach_loopback.go 3.8 KB

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