driver.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. {"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
  64. }
  65. if len(s.DataFile) > 0 {
  66. status = append(status, [2]string{"Data file", s.DataFile})
  67. }
  68. if len(s.MetadataFile) > 0 {
  69. status = append(status, [2]string{"Metadata file", s.MetadataFile})
  70. }
  71. if len(s.DataLoopback) > 0 {
  72. status = append(status, [2]string{"Data loop file", s.DataLoopback})
  73. }
  74. if len(s.MetadataLoopback) > 0 {
  75. status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
  76. }
  77. status = append(status, [][2]string{
  78. {"Data Space Used", units.HumanSize(float64(s.Data.Used))},
  79. {"Data Space Total", units.HumanSize(float64(s.Data.Total))},
  80. {"Data Space Available", units.HumanSize(float64(s.Data.Available))},
  81. {"Metadata Space Used", units.HumanSize(float64(s.Metadata.Used))},
  82. {"Metadata Space Total", units.HumanSize(float64(s.Metadata.Total))},
  83. {"Metadata Space Available", units.HumanSize(float64(s.Metadata.Available))},
  84. {"Thin Pool Minimum Free Space", units.HumanSize(float64(s.MinFreeSpace))},
  85. {"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
  86. {"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
  87. {"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
  88. }...)
  89. if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
  90. status = append(status, [2]string{"Library Version", vStr})
  91. }
  92. return status
  93. }
  94. // GetMetadata returns a map of information about the device.
  95. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  96. m, err := d.DeviceSet.exportDeviceMetadata(id)
  97. if err != nil {
  98. return nil, err
  99. }
  100. metadata := make(map[string]string)
  101. metadata["DeviceId"] = strconv.Itoa(m.deviceID)
  102. metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
  103. metadata["DeviceName"] = m.deviceName
  104. return metadata, nil
  105. }
  106. // Cleanup unmounts a device.
  107. func (d *Driver) Cleanup() error {
  108. err := d.DeviceSet.Shutdown(d.home)
  109. if err2 := mount.Unmount(d.home); err == nil {
  110. err = err2
  111. }
  112. return err
  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.
  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. return system.EnsureRemoveAll(path.Join(d.home, "mnt", id))
  142. }
  143. // Get mounts a device with given id into the root filesystem
  144. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  145. d.locker.Lock(id)
  146. defer d.locker.Unlock(id)
  147. mp := path.Join(d.home, "mnt", id)
  148. rootFs := path.Join(mp, "rootfs")
  149. if count := d.ctr.Increment(mp); count > 1 {
  150. return containerfs.NewLocalContainerFS(rootFs), nil
  151. }
  152. uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  153. if err != nil {
  154. d.ctr.Decrement(mp)
  155. return nil, err
  156. }
  157. // Create the target directories if they don't exist
  158. if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  159. d.ctr.Decrement(mp)
  160. return nil, err
  161. }
  162. if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
  163. d.ctr.Decrement(mp)
  164. return nil, err
  165. }
  166. // Mount the device
  167. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  168. d.ctr.Decrement(mp)
  169. return nil, err
  170. }
  171. if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
  172. d.ctr.Decrement(mp)
  173. d.DeviceSet.UnmountDevice(id, mp)
  174. return nil, err
  175. }
  176. idFile := path.Join(mp, "id")
  177. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  178. // Create an "id" file with the container/image id in it to help reconstruct this in case
  179. // of later problems
  180. if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
  181. d.ctr.Decrement(mp)
  182. d.DeviceSet.UnmountDevice(id, mp)
  183. return nil, err
  184. }
  185. }
  186. return containerfs.NewLocalContainerFS(rootFs), nil
  187. }
  188. // Put unmounts a device and removes it.
  189. func (d *Driver) Put(id string) error {
  190. d.locker.Lock(id)
  191. defer d.locker.Unlock(id)
  192. mp := path.Join(d.home, "mnt", id)
  193. if count := d.ctr.Decrement(mp); count > 0 {
  194. return nil
  195. }
  196. err := d.DeviceSet.UnmountDevice(id, mp)
  197. if err != nil {
  198. logrus.Errorf("devmapper: Error unmounting device %s: %v", id, err)
  199. }
  200. return err
  201. }
  202. // Exists checks to see if the device exists.
  203. func (d *Driver) Exists(id string) bool {
  204. return d.DeviceSet.HasDevice(id)
  205. }