driver.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // +build linux
  2. package devmapper
  3. import (
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. log "github.com/Sirupsen/logrus"
  9. "github.com/docker/docker/daemon/graphdriver"
  10. "github.com/docker/docker/pkg/devicemapper"
  11. "github.com/docker/docker/pkg/mount"
  12. "github.com/docker/docker/pkg/units"
  13. )
  14. func init() {
  15. graphdriver.Register("devicemapper", Init)
  16. }
  17. // Placeholder interfaces, to be replaced
  18. // at integration.
  19. // End of placeholder interfaces.
  20. type Driver struct {
  21. *DeviceSet
  22. home string
  23. }
  24. func Init(home string, options []string) (graphdriver.Driver, error) {
  25. deviceSet, err := NewDeviceSet(home, true, options)
  26. if err != nil {
  27. return nil, err
  28. }
  29. if err := mount.MakePrivate(home); err != nil {
  30. return nil, err
  31. }
  32. d := &Driver{
  33. DeviceSet: deviceSet,
  34. home: home,
  35. }
  36. return graphdriver.NaiveDiffDriver(d), nil
  37. }
  38. func (d *Driver) String() string {
  39. return "devicemapper"
  40. }
  41. func (d *Driver) Status() [][2]string {
  42. s := d.DeviceSet.Status()
  43. status := [][2]string{
  44. {"Pool Name", s.PoolName},
  45. {"Pool Blocksize", fmt.Sprintf("%s", units.HumanSize(int64(s.SectorSize)))},
  46. {"Data file", s.DataLoopback},
  47. {"Metadata file", s.MetadataLoopback},
  48. {"Data Space Used", fmt.Sprintf("%s", units.HumanSize(int64(s.Data.Used)))},
  49. {"Data Space Total", fmt.Sprintf("%s", units.HumanSize(int64(s.Data.Total)))},
  50. {"Metadata Space Used", fmt.Sprintf("%s", units.HumanSize(int64(s.Metadata.Used)))},
  51. {"Metadata Space Total", fmt.Sprintf("%s", units.HumanSize(int64(s.Metadata.Total)))},
  52. }
  53. if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
  54. status = append(status, [2]string{"Library Version", vStr})
  55. }
  56. return status
  57. }
  58. func (d *Driver) Cleanup() error {
  59. err := d.DeviceSet.Shutdown()
  60. if err2 := mount.Unmount(d.home); err == nil {
  61. err = err2
  62. }
  63. return err
  64. }
  65. func (d *Driver) Create(id, parent string) error {
  66. if err := d.DeviceSet.AddDevice(id, parent); err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. func (d *Driver) Remove(id string) error {
  72. if !d.DeviceSet.HasDevice(id) {
  73. // Consider removing a non-existing device a no-op
  74. // This is useful to be able to progress on container removal
  75. // if the underlying device has gone away due to earlier errors
  76. return nil
  77. }
  78. // This assumes the device has been properly Get/Put:ed and thus is unmounted
  79. if err := d.DeviceSet.DeleteDevice(id); err != nil {
  80. return err
  81. }
  82. mp := path.Join(d.home, "mnt", id)
  83. if err := os.RemoveAll(mp); err != nil && !os.IsNotExist(err) {
  84. return err
  85. }
  86. return nil
  87. }
  88. func (d *Driver) Get(id, mountLabel string) (string, error) {
  89. mp := path.Join(d.home, "mnt", id)
  90. // Create the target directories if they don't exist
  91. if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
  92. return "", err
  93. }
  94. // Mount the device
  95. if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
  96. return "", err
  97. }
  98. rootFs := path.Join(mp, "rootfs")
  99. if err := os.MkdirAll(rootFs, 0755); err != nil && !os.IsExist(err) {
  100. d.DeviceSet.UnmountDevice(id)
  101. return "", err
  102. }
  103. idFile := path.Join(mp, "id")
  104. if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
  105. // Create an "id" file with the container/image id in it to help reconscruct this in case
  106. // of later problems
  107. if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
  108. d.DeviceSet.UnmountDevice(id)
  109. return "", err
  110. }
  111. }
  112. return rootFs, nil
  113. }
  114. func (d *Driver) Put(id string) {
  115. if err := d.DeviceSet.UnmountDevice(id); err != nil {
  116. log.Errorf("Warning: error unmounting device %s: %s", id, err)
  117. }
  118. }
  119. func (d *Driver) Exists(id string) bool {
  120. return d.DeviceSet.HasDevice(id)
  121. }