driver.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // +build linux
  2. package devmapper // import "github.com/docker/docker/daemon/graphdriver/devmapper"
  3. import (
  4. "fmt"
  5. "io/ioutil"
  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. "github.com/docker/docker/pkg/locker"
  14. "github.com/docker/docker/pkg/mount"
  15. "github.com/docker/go-units"
  16. "github.com/pkg/errors"
  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. if umountErr != nil {
  113. return errors.Wrapf(umountErr, "error unmounting %s", d.home)
  114. }
  115. return nil
  116. }
  117. // CreateReadWrite creates a layer that is writable for use as a container
  118. // file system.
  119. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  120. return d.Create(id, parent, opts)
  121. }
  122. // Create adds a device with a given id and the parent.
  123. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  124. var storageOpt map[string]string
  125. if opts != nil {
  126. storageOpt = opts.StorageOpt
  127. }
  128. return d.DeviceSet.AddDevice(id, parent, storageOpt)
  129. }
  130. // Remove removes a device with a given id, unmounts the filesystem, and removes the mount point.
  131. func (d *Driver) Remove(id string) error {
  132. d.locker.Lock(id)
  133. defer d.locker.Unlock(id)
  134. if !d.DeviceSet.HasDevice(id) {
  135. // Consider removing a non-existing device a no-op
  136. // This is useful to be able to progress on container removal
  137. // if the underlying device has gone away due to earlier errors
  138. return nil
  139. }
  140. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  141. if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
  142. return fmt.Errorf("failed to remove device %s: %v", id, err)
  143. }
  144. // Most probably the mount point is already removed on Put()
  145. // (see DeviceSet.UnmountDevice()), but just in case it was not
  146. // let's try to remove it here as well, ignoring errors as
  147. // an older kernel can return EBUSY if e.g. the mount was leaked
  148. // to other mount namespaces. A failure to remove the container's
  149. // mount point is not important and should not be treated
  150. // as a failure to remove the container.
  151. mp := path.Join(d.home, "mnt", id)
  152. err := unix.Rmdir(mp)
  153. if err != nil && !os.IsNotExist(err) {
  154. logrus.WithField("storage-driver", "devicemapper").Warnf("unable to remove mount point %q: %s", mp, err)
  155. }
  156. return nil
  157. }
  158. // Get mounts a device with given id into the root filesystem
  159. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  160. d.locker.Lock(id)
  161. defer d.locker.Unlock(id)
  162. mp := path.Join(d.home, "mnt", id)
  163. rootFs := path.Join(mp, "rootfs")
  164. if count := d.ctr.Increment(mp); count > 1 {
  165. return containerfs.NewLocalContainerFS(rootFs), nil
  166. }
  167. uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  168. if err != nil {
  169. d.ctr.Decrement(mp)
  170. return nil, err
  171. }
  172. // Create the target directories if they don't exist
  173. if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  174. d.ctr.Decrement(mp)
  175. return nil, err
  176. }
  177. if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
  178. d.ctr.Decrement(mp)
  179. return nil, err
  180. }
  181. // Mount the device
  182. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  183. d.ctr.Decrement(mp)
  184. return nil, err
  185. }
  186. if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  187. d.ctr.Decrement(mp)
  188. d.DeviceSet.UnmountDevice(id, mp)
  189. return nil, err
  190. }
  191. idFile := path.Join(mp, "id")
  192. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  193. // Create an "id" file with the container/image id in it to help reconstruct this in case
  194. // of later problems
  195. if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
  196. d.ctr.Decrement(mp)
  197. d.DeviceSet.UnmountDevice(id, mp)
  198. return nil, err
  199. }
  200. }
  201. return containerfs.NewLocalContainerFS(rootFs), nil
  202. }
  203. // Put unmounts a device and removes it.
  204. func (d *Driver) Put(id string) error {
  205. d.locker.Lock(id)
  206. defer d.locker.Unlock(id)
  207. mp := path.Join(d.home, "mnt", id)
  208. if count := d.ctr.Decrement(mp); count > 0 {
  209. return nil
  210. }
  211. err := d.DeviceSet.UnmountDevice(id, mp)
  212. if err != nil {
  213. logrus.WithField("storage-driver", "devicemapper").Errorf("Error unmounting device %s: %v", id, err)
  214. }
  215. return err
  216. }
  217. // Exists checks to see if the device exists.
  218. func (d *Driver) Exists(id string) bool {
  219. return d.DeviceSet.HasDevice(id)
  220. }