driver.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/mount"
  13. "github.com/docker/docker/pkg/units"
  14. )
  15. func init() {
  16. graphdriver.Register("devicemapper", Init)
  17. }
  18. // Placeholder interfaces, to be replaced
  19. // at integration.
  20. // End of placeholder interfaces.
  21. // Driver contains the device set mounted and the home directory
  22. type Driver struct {
  23. *DeviceSet
  24. home string
  25. }
  26. var backingFs = "<unknown>"
  27. // Init creates a driver with the given home and the set of options.
  28. func Init(home string, options []string) (graphdriver.Driver, error) {
  29. fsMagic, err := graphdriver.GetFSMagic(home)
  30. if err != nil {
  31. return nil, err
  32. }
  33. if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
  34. backingFs = fsName
  35. }
  36. deviceSet, err := NewDeviceSet(home, true, options)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if err := mount.MakePrivate(home); err != nil {
  41. return nil, err
  42. }
  43. d := &Driver{
  44. DeviceSet: deviceSet,
  45. home: home,
  46. }
  47. return graphdriver.NaiveDiffDriver(d), 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. {"Backing Filesystem", backingFs},
  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. {"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
  70. {"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
  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()
  98. if err2 := mount.Unmount(d.home); err == nil {
  99. err = err2
  100. }
  101. return err
  102. }
  103. // Create adds a device with a given id and the parent.
  104. func (d *Driver) Create(id, parent string) error {
  105. if err := d.DeviceSet.AddDevice(id, parent); err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. // Remove removes a device with a given id, unmounts the filesystem.
  111. func (d *Driver) Remove(id string) error {
  112. if !d.DeviceSet.HasDevice(id) {
  113. // Consider removing a non-existing device a no-op
  114. // This is useful to be able to progress on container removal
  115. // if the underlying device has gone away due to earlier errors
  116. return nil
  117. }
  118. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  119. if err := d.DeviceSet.DeleteDevice(id); err != nil {
  120. return err
  121. }
  122. mp := path.Join(d.home, "mnt", id)
  123. if err := os.RemoveAll(mp); err != nil && !os.IsNotExist(err) {
  124. return err
  125. }
  126. return nil
  127. }
  128. // Get mounts a device with given id into the root filesystem
  129. func (d *Driver) Get(id, mountLabel string) (string, error) {
  130. mp := path.Join(d.home, "mnt", id)
  131. // Create the target directories if they don't exist
  132. if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
  133. return "", err
  134. }
  135. // Mount the device
  136. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  137. return "", err
  138. }
  139. rootFs := path.Join(mp, "rootfs")
  140. if err := os.MkdirAll(rootFs, 0755); err != nil && !os.IsExist(err) {
  141. d.DeviceSet.UnmountDevice(id)
  142. return "", err
  143. }
  144. idFile := path.Join(mp, "id")
  145. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  146. // Create an "id" file with the container/image id in it to help reconscruct this in case
  147. // of later problems
  148. if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
  149. d.DeviceSet.UnmountDevice(id)
  150. return "", err
  151. }
  152. }
  153. return rootFs, nil
  154. }
  155. // Put unmounts a device and removes it.
  156. func (d *Driver) Put(id string) error {
  157. err := d.DeviceSet.UnmountDevice(id)
  158. if err != nil {
  159. logrus.Errorf("Error unmounting device %s: %s", id, err)
  160. }
  161. return err
  162. }
  163. // Exists checks to see if the device is mounted.
  164. func (d *Driver) Exists(id string) bool {
  165. return d.DeviceSet.HasDevice(id)
  166. }