driver.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // +build linux
  2. package devmapper
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strconv"
  9. "github.com/sirupsen/logrus"
  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. "github.com/docker/docker/pkg/locker"
  15. "github.com/docker/docker/pkg/mount"
  16. "github.com/docker/docker/pkg/system"
  17. units "github.com/docker/go-units"
  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. if err := mount.MakePrivate(home); err != nil {
  38. return nil, err
  39. }
  40. d := &Driver{
  41. DeviceSet: deviceSet,
  42. home: home,
  43. uidMaps: uidMaps,
  44. gidMaps: gidMaps,
  45. ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
  46. locker: locker.New(),
  47. }
  48. return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
  49. }
  50. func (d *Driver) String() string {
  51. return "devicemapper"
  52. }
  53. // Status returns the status about the driver in a printable format.
  54. // Information returned contains Pool Name, Data File, Metadata file, disk usage by
  55. // the data and metadata, etc.
  56. func (d *Driver) Status() [][2]string {
  57. s := d.DeviceSet.Status()
  58. status := [][2]string{
  59. {"Pool Name", s.PoolName},
  60. {"Pool Blocksize", units.HumanSize(float64(s.SectorSize))},
  61. {"Base Device Size", units.HumanSize(float64(s.BaseDeviceSize))},
  62. {"Backing Filesystem", s.BaseDeviceFS},
  63. {"Data file", s.DataFile},
  64. {"Metadata file", s.MetadataFile},
  65. {"Data Space Used", units.HumanSize(float64(s.Data.Used))},
  66. {"Data Space Total", units.HumanSize(float64(s.Data.Total))},
  67. {"Data Space Available", units.HumanSize(float64(s.Data.Available))},
  68. {"Metadata Space Used", units.HumanSize(float64(s.Metadata.Used))},
  69. {"Metadata Space Total", units.HumanSize(float64(s.Metadata.Total))},
  70. {"Metadata Space Available", units.HumanSize(float64(s.Metadata.Available))},
  71. {"Thin Pool Minimum Free Space", units.HumanSize(float64(s.MinFreeSpace))},
  72. {"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
  73. {"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
  74. {"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
  75. {"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
  76. }
  77. if len(s.DataLoopback) > 0 {
  78. status = append(status, [2]string{"Data loop file", s.DataLoopback})
  79. }
  80. if len(s.MetadataLoopback) > 0 {
  81. status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
  82. }
  83. if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
  84. status = append(status, [2]string{"Library Version", vStr})
  85. }
  86. return status
  87. }
  88. // GetMetadata returns a map of information about the device.
  89. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  90. m, err := d.DeviceSet.exportDeviceMetadata(id)
  91. if err != nil {
  92. return nil, err
  93. }
  94. metadata := make(map[string]string)
  95. metadata["DeviceId"] = strconv.Itoa(m.deviceID)
  96. metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
  97. metadata["DeviceName"] = m.deviceName
  98. return metadata, nil
  99. }
  100. // Cleanup unmounts a device.
  101. func (d *Driver) Cleanup() error {
  102. err := d.DeviceSet.Shutdown(d.home)
  103. if err2 := mount.Unmount(d.home); err == nil {
  104. err = err2
  105. }
  106. return err
  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. if err := d.DeviceSet.AddDevice(id, parent, storageOpt); err != nil {
  120. return err
  121. }
  122. return nil
  123. }
  124. // Remove removes a device with a given id, unmounts the filesystem.
  125. func (d *Driver) Remove(id string) error {
  126. d.locker.Lock(id)
  127. defer d.locker.Unlock(id)
  128. if !d.DeviceSet.HasDevice(id) {
  129. // Consider removing a non-existing device a no-op
  130. // This is useful to be able to progress on container removal
  131. // if the underlying device has gone away due to earlier errors
  132. return nil
  133. }
  134. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  135. if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
  136. return fmt.Errorf("failed to remove device %s: %v", id, err)
  137. }
  138. return system.EnsureRemoveAll(path.Join(d.home, "mnt", id))
  139. }
  140. // Get mounts a device with given id into the root filesystem
  141. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  142. d.locker.Lock(id)
  143. defer d.locker.Unlock(id)
  144. mp := path.Join(d.home, "mnt", id)
  145. rootFs := path.Join(mp, "rootfs")
  146. if count := d.ctr.Increment(mp); count > 1 {
  147. return containerfs.NewLocalContainerFS(rootFs), nil
  148. }
  149. uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  150. if err != nil {
  151. d.ctr.Decrement(mp)
  152. return nil, err
  153. }
  154. // Create the target directories if they don't exist
  155. if err := idtools.MkdirAllAs(path.Join(d.home, "mnt"), 0755, uid, gid); err != nil && !os.IsExist(err) {
  156. d.ctr.Decrement(mp)
  157. return nil, err
  158. }
  159. if err := idtools.MkdirAs(mp, 0755, uid, gid); err != nil && !os.IsExist(err) {
  160. d.ctr.Decrement(mp)
  161. return nil, err
  162. }
  163. // Mount the device
  164. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  165. d.ctr.Decrement(mp)
  166. return nil, err
  167. }
  168. if err := idtools.MkdirAllAs(rootFs, 0755, uid, gid); err != nil && !os.IsExist(err) {
  169. d.ctr.Decrement(mp)
  170. d.DeviceSet.UnmountDevice(id, mp)
  171. return nil, err
  172. }
  173. idFile := path.Join(mp, "id")
  174. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  175. // Create an "id" file with the container/image id in it to help reconstruct this in case
  176. // of later problems
  177. if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
  178. d.ctr.Decrement(mp)
  179. d.DeviceSet.UnmountDevice(id, mp)
  180. return nil, err
  181. }
  182. }
  183. return containerfs.NewLocalContainerFS(rootFs), nil
  184. }
  185. // Put unmounts a device and removes it.
  186. func (d *Driver) Put(id string) error {
  187. d.locker.Lock(id)
  188. defer d.locker.Unlock(id)
  189. mp := path.Join(d.home, "mnt", id)
  190. if count := d.ctr.Decrement(mp); count > 0 {
  191. return nil
  192. }
  193. err := d.DeviceSet.UnmountDevice(id, mp)
  194. if err != nil {
  195. logrus.Errorf("devmapper: Error unmounting device %s: %s", id, err)
  196. }
  197. return err
  198. }
  199. // Exists checks to see if the device exists.
  200. func (d *Driver) Exists(id string) bool {
  201. return d.DeviceSet.HasDevice(id)
  202. }