driver.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // +build linux,amd64
  2. package devmapper
  3. import (
  4. "fmt"
  5. "github.com/dotcloud/docker/graphdriver"
  6. "github.com/dotcloud/docker/utils"
  7. "io/ioutil"
  8. "path"
  9. "sync"
  10. )
  11. func init() {
  12. graphdriver.Register("devicemapper", Init)
  13. }
  14. // Placeholder interfaces, to be replaced
  15. // at integration.
  16. // End of placeholder interfaces.
  17. type Driver struct {
  18. *DeviceSet
  19. home string
  20. sync.Mutex // Protects concurrent modification to active
  21. active map[string]int
  22. }
  23. var Init = func(home string) (graphdriver.Driver, error) {
  24. deviceSet, err := NewDeviceSet(home, true)
  25. if err != nil {
  26. return nil, err
  27. }
  28. d := &Driver{
  29. DeviceSet: deviceSet,
  30. home: home,
  31. active: make(map[string]int),
  32. }
  33. return d, nil
  34. }
  35. func (d *Driver) String() string {
  36. return "devicemapper"
  37. }
  38. func (d *Driver) Status() [][2]string {
  39. s := d.DeviceSet.Status()
  40. status := [][2]string{
  41. {"Pool Name", s.PoolName},
  42. {"Data file", s.DataLoopback},
  43. {"Metadata file", s.MetadataLoopback},
  44. {"Data Space Used", fmt.Sprintf("%.1f Mb", float64(s.Data.Used)/(1024*1024))},
  45. {"Data Space Total", fmt.Sprintf("%.1f Mb", float64(s.Data.Total)/(1024*1024))},
  46. {"Metadata Space Used", fmt.Sprintf("%.1f Mb", float64(s.Metadata.Used)/(1024*1024))},
  47. {"Metadata Space Total", fmt.Sprintf("%.1f Mb", float64(s.Metadata.Total)/(1024*1024))},
  48. }
  49. return status
  50. }
  51. func (d *Driver) Cleanup() error {
  52. return d.DeviceSet.Shutdown()
  53. }
  54. func (d *Driver) Create(id, parent string) error {
  55. if err := d.DeviceSet.AddDevice(id, parent); err != nil {
  56. return err
  57. }
  58. mp := path.Join(d.home, "mnt", id)
  59. if err := d.mount(id, mp); err != nil {
  60. return err
  61. }
  62. if err := osMkdirAll(path.Join(mp, "rootfs"), 0755); err != nil && !osIsExist(err) {
  63. return err
  64. }
  65. // Create an "id" file with the container/image id in it to help reconscruct this in case
  66. // of later problems
  67. if err := ioutil.WriteFile(path.Join(mp, "id"), []byte(id), 0600); err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (d *Driver) Remove(id string) error {
  73. // Protect the d.active from concurrent access
  74. d.Lock()
  75. defer d.Unlock()
  76. if d.active[id] != 0 {
  77. utils.Errorf("Warning: removing active id %s\n", id)
  78. }
  79. mp := path.Join(d.home, "mnt", id)
  80. if err := d.unmount(id, mp); err != nil {
  81. return err
  82. }
  83. return d.DeviceSet.RemoveDevice(id)
  84. }
  85. func (d *Driver) Get(id string) (string, error) {
  86. // Protect the d.active from concurrent access
  87. d.Lock()
  88. defer d.Unlock()
  89. count := d.active[id]
  90. mp := path.Join(d.home, "mnt", id)
  91. if count == 0 {
  92. if err := d.mount(id, mp); err != nil {
  93. return "", err
  94. }
  95. }
  96. d.active[id] = count + 1
  97. return path.Join(mp, "rootfs"), nil
  98. }
  99. func (d *Driver) Put(id string) {
  100. // Protect the d.active from concurrent access
  101. d.Lock()
  102. defer d.Unlock()
  103. if count := d.active[id]; count > 1 {
  104. d.active[id] = count - 1
  105. } else {
  106. mp := path.Join(d.home, "mnt", id)
  107. d.unmount(id, mp)
  108. delete(d.active, id)
  109. }
  110. }
  111. func (d *Driver) mount(id, mountPoint string) error {
  112. // Create the target directories if they don't exist
  113. if err := osMkdirAll(mountPoint, 0755); err != nil && !osIsExist(err) {
  114. return err
  115. }
  116. // If mountpoint is already mounted, do nothing
  117. if mounted, err := Mounted(mountPoint); err != nil {
  118. return fmt.Errorf("Error checking mountpoint: %s", err)
  119. } else if mounted {
  120. return nil
  121. }
  122. // Mount the device
  123. return d.DeviceSet.MountDevice(id, mountPoint, false)
  124. }
  125. func (d *Driver) unmount(id, mountPoint string) error {
  126. // If mountpoint is not mounted, do nothing
  127. if mounted, err := Mounted(mountPoint); err != nil {
  128. return fmt.Errorf("Error checking mountpoint: %s", err)
  129. } else if !mounted {
  130. return nil
  131. }
  132. // Unmount the device
  133. return d.DeviceSet.UnmountDevice(id, mountPoint, true)
  134. }
  135. func (d *Driver) Exists(id string) bool {
  136. return d.Devices[id] != nil
  137. }