driver.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //go:build linux
  2. // +build linux
  3. package devmapper // import "github.com/docker/docker/daemon/graphdriver/devmapper"
  4. import (
  5. "fmt"
  6. "os"
  7. "path"
  8. "strconv"
  9. "github.com/docker/docker/daemon/graphdriver"
  10. "github.com/docker/docker/pkg/devicemapper"
  11. "github.com/docker/docker/pkg/idtools"
  12. units "github.com/docker/go-units"
  13. "github.com/moby/locker"
  14. "github.com/moby/sys/mount"
  15. "github.com/sirupsen/logrus"
  16. "golang.org/x/sys/unix"
  17. )
  18. func init() {
  19. graphdriver.Register("devicemapper", Init)
  20. }
  21. // Driver contains the device set mounted and the home directory
  22. type Driver struct {
  23. *DeviceSet
  24. home string
  25. ctr *graphdriver.RefCounter
  26. locker *locker.Locker
  27. }
  28. // Init creates a driver with the given home and the set of options.
  29. func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdriver.Driver, error) {
  30. deviceSet, err := NewDeviceSet(home, true, options, idMap)
  31. if err != nil {
  32. return nil, err
  33. }
  34. d := &Driver{
  35. DeviceSet: deviceSet,
  36. home: home,
  37. ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
  38. locker: locker.New(),
  39. }
  40. return graphdriver.NewNaiveDiffDriver(d, d.idMap), nil
  41. }
  42. func (d *Driver) String() string {
  43. return "devicemapper"
  44. }
  45. // Status returns the status about the driver in a printable format.
  46. // Information returned contains Pool Name, Data File, Metadata file, disk usage by
  47. // the data and metadata, etc.
  48. func (d *Driver) Status() [][2]string {
  49. s := d.DeviceSet.Status()
  50. status := [][2]string{
  51. {"Pool Name", s.PoolName},
  52. {"Pool Blocksize", units.HumanSize(float64(s.SectorSize))},
  53. {"Base Device Size", units.HumanSize(float64(s.BaseDeviceSize))},
  54. {"Backing Filesystem", s.BaseDeviceFS},
  55. {"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
  56. }
  57. if len(s.DataFile) > 0 {
  58. status = append(status, [2]string{"Data file", s.DataFile})
  59. }
  60. if len(s.MetadataFile) > 0 {
  61. status = append(status, [2]string{"Metadata file", s.MetadataFile})
  62. }
  63. if len(s.DataLoopback) > 0 {
  64. status = append(status, [2]string{"Data loop file", s.DataLoopback})
  65. }
  66. if len(s.MetadataLoopback) > 0 {
  67. status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
  68. }
  69. status = append(status, [][2]string{
  70. {"Data Space Used", units.HumanSize(float64(s.Data.Used))},
  71. {"Data Space Total", units.HumanSize(float64(s.Data.Total))},
  72. {"Data Space Available", units.HumanSize(float64(s.Data.Available))},
  73. {"Metadata Space Used", units.HumanSize(float64(s.Metadata.Used))},
  74. {"Metadata Space Total", units.HumanSize(float64(s.Metadata.Total))},
  75. {"Metadata Space Available", units.HumanSize(float64(s.Metadata.Available))},
  76. {"Thin Pool Minimum Free Space", units.HumanSize(float64(s.MinFreeSpace))},
  77. {"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
  78. {"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
  79. {"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
  80. }...)
  81. if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
  82. status = append(status, [2]string{"Library Version", vStr})
  83. }
  84. return status
  85. }
  86. // GetMetadata returns a map of information about the device.
  87. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  88. m, err := d.DeviceSet.exportDeviceMetadata(id)
  89. if err != nil {
  90. return nil, err
  91. }
  92. metadata := make(map[string]string)
  93. metadata["DeviceId"] = strconv.Itoa(m.deviceID)
  94. metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
  95. metadata["DeviceName"] = m.deviceName
  96. return metadata, nil
  97. }
  98. // Cleanup unmounts a device.
  99. func (d *Driver) Cleanup() error {
  100. err := d.DeviceSet.Shutdown(d.home)
  101. umountErr := mount.RecursiveUnmount(d.home)
  102. // in case we have two errors, prefer the one from Shutdown()
  103. if err != nil {
  104. return err
  105. }
  106. return umountErr
  107. }
  108. // CreateReadWrite creates a layer that is writable for use as a container
  109. // file system.
  110. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  111. return d.Create(id, parent, opts)
  112. }
  113. // Create adds a device with a given id and the parent.
  114. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  115. var storageOpt map[string]string
  116. if opts != nil {
  117. storageOpt = opts.StorageOpt
  118. }
  119. return d.DeviceSet.AddDevice(id, parent, storageOpt)
  120. }
  121. // Remove removes a device with a given id, unmounts the filesystem, and removes the mount point.
  122. func (d *Driver) Remove(id string) error {
  123. d.locker.Lock(id)
  124. defer d.locker.Unlock(id)
  125. if !d.DeviceSet.HasDevice(id) {
  126. // Consider removing a non-existing device a no-op
  127. // This is useful to be able to progress on container removal
  128. // if the underlying device has gone away due to earlier errors
  129. return nil
  130. }
  131. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  132. if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
  133. return fmt.Errorf("failed to remove device %s: %v", id, err)
  134. }
  135. // Most probably the mount point is already removed on Put()
  136. // (see DeviceSet.UnmountDevice()), but just in case it was not
  137. // let's try to remove it here as well, ignoring errors as
  138. // an older kernel can return EBUSY if e.g. the mount was leaked
  139. // to other mount namespaces. A failure to remove the container's
  140. // mount point is not important and should not be treated
  141. // as a failure to remove the container.
  142. mp := path.Join(d.home, "mnt", id)
  143. err := unix.Rmdir(mp)
  144. if err != nil && !os.IsNotExist(err) {
  145. logrus.WithField("storage-driver", "devicemapper").Warnf("unable to remove mount point %q: %s", mp, err)
  146. }
  147. return nil
  148. }
  149. // Get mounts a device with given id into the root filesystem
  150. func (d *Driver) Get(id, mountLabel string) (string, error) {
  151. d.locker.Lock(id)
  152. defer d.locker.Unlock(id)
  153. mp := path.Join(d.home, "mnt", id)
  154. rootFs := path.Join(mp, "rootfs")
  155. if count := d.ctr.Increment(mp); count > 1 {
  156. return rootFs, nil
  157. }
  158. root := d.idMap.RootPair()
  159. // Create the target directories if they don't exist
  160. if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, root); err != nil {
  161. d.ctr.Decrement(mp)
  162. return "", err
  163. }
  164. if err := idtools.MkdirAndChown(mp, 0755, root); err != nil && !os.IsExist(err) {
  165. d.ctr.Decrement(mp)
  166. return "", err
  167. }
  168. // Mount the device
  169. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  170. d.ctr.Decrement(mp)
  171. return "", err
  172. }
  173. if err := idtools.MkdirAllAndChown(rootFs, 0755, root); err != nil {
  174. d.ctr.Decrement(mp)
  175. d.DeviceSet.UnmountDevice(id, mp)
  176. return "", err
  177. }
  178. idFile := path.Join(mp, "id")
  179. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  180. // Create an "id" file with the container/image id in it to help reconstruct this in case
  181. // of later problems
  182. if err := os.WriteFile(idFile, []byte(id), 0600); err != nil {
  183. d.ctr.Decrement(mp)
  184. d.DeviceSet.UnmountDevice(id, mp)
  185. return "", err
  186. }
  187. }
  188. return rootFs, nil
  189. }
  190. // Put unmounts a device and removes it.
  191. func (d *Driver) Put(id string) error {
  192. d.locker.Lock(id)
  193. defer d.locker.Unlock(id)
  194. mp := path.Join(d.home, "mnt", id)
  195. if count := d.ctr.Decrement(mp); count > 0 {
  196. return nil
  197. }
  198. err := d.DeviceSet.UnmountDevice(id, mp)
  199. if err != nil {
  200. logrus.WithField("storage-driver", "devicemapper").Errorf("Error unmounting device %s: %v", id, err)
  201. }
  202. return err
  203. }
  204. // Exists checks to see if the device exists.
  205. func (d *Driver) Exists(id string) bool {
  206. return d.DeviceSet.HasDevice(id)
  207. }