driver.go 7.7 KB

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