driver.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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. if err2 := mount.RecursiveUnmount(d.home); err == nil {
  107. err = err2
  108. }
  109. return err
  110. }
  111. // CreateReadWrite creates a layer that is writable for use as a container
  112. // file system.
  113. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  114. return d.Create(id, parent, opts)
  115. }
  116. // Create adds a device with a given id and the parent.
  117. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  118. var storageOpt map[string]string
  119. if opts != nil {
  120. storageOpt = opts.StorageOpt
  121. }
  122. return d.DeviceSet.AddDevice(id, parent, storageOpt)
  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.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  156. d.ctr.Decrement(mp)
  157. return nil, err
  158. }
  159. if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: 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.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  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: %v", 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. }