driver.go 6.9 KB

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