driver.go 6.6 KB

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