driver.go 5.8 KB

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