driver.go 7.4 KB

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