driver.go 7.7 KB

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