attach_loopback.go 3.8 KB

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