driver.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/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/docker/pkg/system"
  16. units "github.com/docker/go-units"
  17. )
  18. func init() {
  19. graphdriver.Register("devicemapper", Init)
  20. }
  21. // Driver contains the device set mounted and the home directory
  22. type Driver struct {
  23. *DeviceSet
  24. home string
  25. uidMaps []idtools.IDMap
  26. gidMaps []idtools.IDMap
  27. ctr *graphdriver.RefCounter
  28. locker *locker.Locker
  29. }
  30. // Init creates a driver with the given home and the set of options.
  31. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  32. deviceSet, err := NewDeviceSet(home, true, options, uidMaps, gidMaps)
  33. if err != nil {
  34. return nil, err
  35. }
  36. if err := mount.MakePrivate(home); err != nil {
  37. return nil, err
  38. }
  39. d := &Driver{
  40. DeviceSet: deviceSet,
  41. home: home,
  42. uidMaps: uidMaps,
  43. gidMaps: gidMaps,
  44. ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
  45. locker: locker.New(),
  46. }
  47. return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
  48. }
  49. func (d *Driver) String() string {
  50. return "devicemapper"
  51. }
  52. // Status returns the status about the driver in a printable format.
  53. // Information returned contains Pool Name, Data File, Metadata file, disk usage by
  54. // the data and metadata, etc.
  55. func (d *Driver) Status() [][2]string {
  56. s := d.DeviceSet.Status()
  57. status := [][2]string{
  58. {"Pool Name", s.PoolName},
  59. {"Pool Blocksize", fmt.Sprintf("%s", units.HumanSize(float64(s.SectorSize)))},
  60. {"Base Device Size", fmt.Sprintf("%s", units.HumanSize(float64(s.BaseDeviceSize)))},
  61. {"Backing Filesystem", s.BaseDeviceFS},
  62. {"Data file", s.DataFile},
  63. {"Metadata file", s.MetadataFile},
  64. {"Data Space Used", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Used)))},
  65. {"Data Space Total", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Total)))},
  66. {"Data Space Available", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Available)))},
  67. {"Metadata Space Used", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Used)))},
  68. {"Metadata Space Total", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Total)))},
  69. {"Metadata Space Available", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Available)))},
  70. {"Thin Pool Minimum Free Space", fmt.Sprintf("%s", units.HumanSize(float64(s.MinFreeSpace)))},
  71. {"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
  72. {"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
  73. {"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
  74. {"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
  75. }
  76. if len(s.DataLoopback) > 0 {
  77. status = append(status, [2]string{"Data loop file", s.DataLoopback})
  78. }
  79. if len(s.MetadataLoopback) > 0 {
  80. status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
  81. }
  82. if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
  83. status = append(status, [2]string{"Library Version", vStr})
  84. }
  85. return status
  86. }
  87. // GetMetadata returns a map of information about the device.
  88. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  89. m, err := d.DeviceSet.exportDeviceMetadata(id)
  90. if err != nil {
  91. return nil, err
  92. }
  93. metadata := make(map[string]string)
  94. metadata["DeviceId"] = strconv.Itoa(m.deviceID)
  95. metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
  96. metadata["DeviceName"] = m.deviceName
  97. return metadata, nil
  98. }
  99. // Cleanup unmounts a device.
  100. func (d *Driver) Cleanup() error {
  101. err := d.DeviceSet.Shutdown(d.home)
  102. if err2 := mount.Unmount(d.home); err == nil {
  103. err = err2
  104. }
  105. return err
  106. }
  107. // CreateReadWrite creates a layer that is writable for use as a container
  108. // file system.
  109. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  110. return d.Create(id, parent, opts)
  111. }
  112. // Create adds a device with a given id and the parent.
  113. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  114. var storageOpt map[string]string
  115. if opts != nil {
  116. storageOpt = opts.StorageOpt
  117. }
  118. if err := d.DeviceSet.AddDevice(id, parent, storageOpt); err != nil {
  119. return err
  120. }
  121. return nil
  122. }
  123. // Remove removes a device with a given id, unmounts the filesystem.
  124. func (d *Driver) Remove(id string) error {
  125. d.locker.Lock(id)
  126. defer d.locker.Unlock(id)
  127. if !d.DeviceSet.HasDevice(id) {
  128. // Consider removing a non-existing device a no-op
  129. // This is useful to be able to progress on container removal
  130. // if the underlying device has gone away due to earlier errors
  131. return nil
  132. }
  133. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  134. if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
  135. return fmt.Errorf("failed to remove device %s: %v", id, err)
  136. }
  137. mp := path.Join(d.home, "mnt", id)
  138. if err := system.EnsureRemoveAll(mp); err != nil {
  139. return err
  140. }
  141. return nil
  142. }
  143. // Get mounts a device with given id into the root filesystem
  144. func (d *Driver) Get(id, mountLabel string) (string, 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 rootFs, nil
  151. }
  152. uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  153. if err != nil {
  154. d.ctr.Decrement(mp)
  155. return "", err
  156. }
  157. // Create the target directories if they don't exist
  158. if err := idtools.MkdirAllAs(path.Join(d.home, "mnt"), 0755, uid, gid); err != nil && !os.IsExist(err) {
  159. d.ctr.Decrement(mp)
  160. return "", err
  161. }
  162. if err := idtools.MkdirAs(mp, 0755, uid, gid); err != nil && !os.IsExist(err) {
  163. d.ctr.Decrement(mp)
  164. return "", err
  165. }
  166. // Mount the device
  167. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  168. d.ctr.Decrement(mp)
  169. return "", err
  170. }
  171. if err := idtools.MkdirAllAs(rootFs, 0755, uid, gid); err != nil && !os.IsExist(err) {
  172. d.ctr.Decrement(mp)
  173. d.DeviceSet.UnmountDevice(id, mp)
  174. return "", 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 "", err
  184. }
  185. }
  186. return 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: %s", 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. }