2014-01-31 10:16:42 +00:00
|
|
|
// +build linux,amd64
|
2013-11-28 03:12:51 +00:00
|
|
|
|
2013-11-01 05:06:49 +00:00
|
|
|
package devmapper
|
|
|
|
|
|
|
|
import (
|
2013-11-01 19:04:08 +00:00
|
|
|
"fmt"
|
2013-11-04 23:22:34 +00:00
|
|
|
"github.com/dotcloud/docker/graphdriver"
|
2013-12-05 21:22:55 +00:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-11-18 16:10:47 +00:00
|
|
|
"io/ioutil"
|
2013-11-01 19:04:08 +00:00
|
|
|
"path"
|
2013-12-05 21:22:55 +00:00
|
|
|
"sync"
|
2013-11-01 05:06:49 +00:00
|
|
|
)
|
|
|
|
|
2013-11-04 23:22:34 +00:00
|
|
|
func init() {
|
|
|
|
graphdriver.Register("devicemapper", Init)
|
|
|
|
}
|
|
|
|
|
2013-11-01 19:04:08 +00:00
|
|
|
// Placeholder interfaces, to be replaced
|
|
|
|
// at integration.
|
|
|
|
|
|
|
|
// End of placeholder interfaces.
|
|
|
|
|
2013-11-04 17:22:43 +00:00
|
|
|
type Driver struct {
|
2013-11-01 05:06:49 +00:00
|
|
|
*DeviceSet
|
2013-12-05 21:22:55 +00:00
|
|
|
home string
|
|
|
|
sync.Mutex // Protects concurrent modification to active
|
|
|
|
active map[string]int
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:32:16 +00:00
|
|
|
var Init = func(home string) (graphdriver.Driver, error) {
|
2013-11-18 10:33:07 +00:00
|
|
|
deviceSet, err := NewDeviceSet(home, true)
|
2013-10-24 19:04:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-04 17:22:43 +00:00
|
|
|
d := &Driver{
|
2013-10-24 19:04:49 +00:00
|
|
|
DeviceSet: deviceSet,
|
2013-11-04 23:22:34 +00:00
|
|
|
home: home,
|
2013-12-05 21:22:55 +00:00
|
|
|
active: make(map[string]int),
|
2013-11-01 19:04:08 +00:00
|
|
|
}
|
2013-11-04 17:22:43 +00:00
|
|
|
return d, nil
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 03:02:15 +00:00
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "devicemapper"
|
|
|
|
}
|
|
|
|
|
2013-11-15 10:04:02 +00:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
s := d.DeviceSet.Status()
|
|
|
|
|
|
|
|
status := [][2]string{
|
|
|
|
{"Pool Name", s.PoolName},
|
|
|
|
{"Data file", s.DataLoopback},
|
|
|
|
{"Metadata file", s.MetadataLoopback},
|
|
|
|
{"Data Space Used", fmt.Sprintf("%.1f Mb", float64(s.Data.Used)/(1024*1024))},
|
|
|
|
{"Data Space Total", fmt.Sprintf("%.1f Mb", float64(s.Data.Total)/(1024*1024))},
|
|
|
|
{"Metadata Space Used", fmt.Sprintf("%.1f Mb", float64(s.Metadata.Used)/(1024*1024))},
|
|
|
|
{"Metadata Space Total", fmt.Sprintf("%.1f Mb", float64(s.Metadata.Total)/(1024*1024))},
|
|
|
|
}
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2013-11-04 17:22:43 +00:00
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return d.DeviceSet.Shutdown()
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-22 00:32:16 +00:00
|
|
|
func (d *Driver) Create(id, parent string) error {
|
2013-11-18 16:10:47 +00:00
|
|
|
if err := d.DeviceSet.AddDevice(id, parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
if err := d.mount(id, mp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-20 21:05:17 +00:00
|
|
|
if err := osMkdirAll(path.Join(mp, "rootfs"), 0755); err != nil && !osIsExist(err) {
|
2013-11-18 16:10:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an "id" file with the container/image id in it to help reconscruct this in case
|
|
|
|
// of later problems
|
|
|
|
if err := ioutil.WriteFile(path.Join(mp, "id"), []byte(id), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:37:33 +00:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2013-12-05 21:22:55 +00:00
|
|
|
// Protect the d.active from concurrent access
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
if d.active[id] != 0 {
|
|
|
|
utils.Errorf("Warning: removing active id %s\n", id)
|
|
|
|
}
|
|
|
|
|
2013-11-19 13:40:15 +00:00
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
if err := d.unmount(id, mp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-11-07 22:37:33 +00:00
|
|
|
return d.DeviceSet.RemoveDevice(id)
|
2013-11-02 21:25:06 +00:00
|
|
|
}
|
2013-11-01 05:06:49 +00:00
|
|
|
|
2013-11-07 22:37:33 +00:00
|
|
|
func (d *Driver) Get(id string) (string, error) {
|
2013-12-05 21:22:55 +00:00
|
|
|
// Protect the d.active from concurrent access
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
count := d.active[id]
|
|
|
|
|
2013-11-07 22:37:33 +00:00
|
|
|
mp := path.Join(d.home, "mnt", id)
|
2013-12-05 21:22:55 +00:00
|
|
|
if count == 0 {
|
|
|
|
if err := d.mount(id, mp); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
2013-12-05 21:22:55 +00:00
|
|
|
|
|
|
|
d.active[id] = count + 1
|
|
|
|
|
2013-11-18 16:10:47 +00:00
|
|
|
return path.Join(mp, "rootfs"), nil
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 21:18:02 +00:00
|
|
|
func (d *Driver) Put(id string) {
|
2013-12-05 21:22:55 +00:00
|
|
|
// Protect the d.active from concurrent access
|
|
|
|
d.Lock()
|
|
|
|
defer d.Unlock()
|
|
|
|
|
|
|
|
if count := d.active[id]; count > 1 {
|
|
|
|
d.active[id] = count - 1
|
|
|
|
} else {
|
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
d.unmount(id, mp)
|
|
|
|
delete(d.active, id)
|
|
|
|
}
|
2013-12-05 21:18:02 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 22:56:26 +00:00
|
|
|
func (d *Driver) mount(id, mountPoint string) error {
|
2013-11-07 22:37:33 +00:00
|
|
|
// Create the target directories if they don't exist
|
2013-11-20 21:05:17 +00:00
|
|
|
if err := osMkdirAll(mountPoint, 0755); err != nil && !osIsExist(err) {
|
2013-11-07 22:37:33 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// If mountpoint is already mounted, do nothing
|
2013-11-13 22:56:26 +00:00
|
|
|
if mounted, err := Mounted(mountPoint); err != nil {
|
2013-11-07 22:37:33 +00:00
|
|
|
return fmt.Errorf("Error checking mountpoint: %s", err)
|
|
|
|
} else if mounted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Mount the device
|
2013-11-13 22:56:26 +00:00
|
|
|
return d.DeviceSet.MountDevice(id, mountPoint, false)
|
2013-11-07 22:37:33 +00:00
|
|
|
}
|
2013-11-19 13:40:15 +00:00
|
|
|
|
|
|
|
func (d *Driver) unmount(id, mountPoint string) error {
|
|
|
|
// If mountpoint is not mounted, do nothing
|
|
|
|
if mounted, err := Mounted(mountPoint); err != nil {
|
|
|
|
return fmt.Errorf("Error checking mountpoint: %s", err)
|
|
|
|
} else if !mounted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Unmount the device
|
|
|
|
return d.DeviceSet.UnmountDevice(id, mountPoint, true)
|
|
|
|
}
|
2013-11-19 10:32:08 +00:00
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
return d.Devices[id] != nil
|
|
|
|
}
|