driver.go 3.5 KB

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