2014-01-31 05:16:42 -05:00
|
|
|
// +build linux,amd64
|
2013-11-27 22:12:51 -05:00
|
|
|
|
2013-11-01 01:06:49 -04:00
|
|
|
package devmapper
|
|
|
|
|
|
|
|
import (
|
2013-11-01 15:04:08 -04:00
|
|
|
"fmt"
|
2013-11-04 18:22:34 -05:00
|
|
|
"github.com/dotcloud/docker/graphdriver"
|
2013-12-05 16:22:55 -05:00
|
|
|
"github.com/dotcloud/docker/utils"
|
2013-11-18 11:10:47 -05:00
|
|
|
"io/ioutil"
|
2014-02-11 03:40:13 -05:00
|
|
|
"os"
|
2013-11-01 15:04:08 -04:00
|
|
|
"path"
|
2013-11-01 01:06:49 -04:00
|
|
|
)
|
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
func init() {
|
|
|
|
graphdriver.Register("devicemapper", Init)
|
|
|
|
}
|
|
|
|
|
2013-11-01 15:04:08 -04:00
|
|
|
// Placeholder interfaces, to be replaced
|
|
|
|
// at integration.
|
|
|
|
|
|
|
|
// End of placeholder interfaces.
|
|
|
|
|
2013-11-04 12:22:43 -05:00
|
|
|
type Driver struct {
|
2013-11-01 01:06:49 -04:00
|
|
|
*DeviceSet
|
2014-02-04 12:19:09 -05:00
|
|
|
home string
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:32:16 -05:00
|
|
|
var Init = func(home string) (graphdriver.Driver, error) {
|
2013-11-18 05:33:07 -05:00
|
|
|
deviceSet, err := NewDeviceSet(home, true)
|
2013-10-24 15:04:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2013-11-04 12:22:43 -05:00
|
|
|
d := &Driver{
|
2013-10-24 15:04:49 -04:00
|
|
|
DeviceSet: deviceSet,
|
2013-11-04 18:22:34 -05:00
|
|
|
home: home,
|
2013-11-01 15:04:08 -04:00
|
|
|
}
|
2013-11-04 12:22:43 -05:00
|
|
|
return d, nil
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:02:15 -05:00
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "devicemapper"
|
|
|
|
}
|
|
|
|
|
2013-11-15 05:04:02 -05: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 12:22:43 -05:00
|
|
|
func (d *Driver) Cleanup() error {
|
|
|
|
return d.DeviceSet.Shutdown()
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-21 19:32:16 -05:00
|
|
|
func (d *Driver) Create(id, parent string) error {
|
2013-11-18 11:10:47 -05: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 16:05:17 -05:00
|
|
|
if err := osMkdirAll(path.Join(mp, "rootfs"), 0755); err != nil && !osIsExist(err) {
|
2013-11-18 11:10:47 -05: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
|
|
|
|
}
|
|
|
|
|
2014-02-04 12:19:09 -05:00
|
|
|
// We float this reference so that the next Get call can
|
|
|
|
// steal it, so we don't have to unmount
|
|
|
|
if err := d.DeviceSet.UnmountDevice(id, UnmountFloat); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2013-11-18 11:10:47 -05:00
|
|
|
return nil
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 17:37:33 -05:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2014-02-04 12:19:09 -05:00
|
|
|
// Sink the float from create in case no Get() call was made
|
|
|
|
if err := d.DeviceSet.UnmountDevice(id, UnmountSink); err != nil {
|
2013-11-19 08:40:15 -05:00
|
|
|
return err
|
|
|
|
}
|
2014-02-04 12:19:09 -05:00
|
|
|
// This assumes the device has been properly Get/Put:ed and thus is unmounted
|
2014-02-11 03:40:13 -05:00
|
|
|
if err := d.DeviceSet.DeleteDevice(id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
if err := os.RemoveAll(mp); err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-11-02 17:25:06 -04:00
|
|
|
}
|
2013-11-01 01:06:49 -04:00
|
|
|
|
2013-11-07 17:37:33 -05:00
|
|
|
func (d *Driver) Get(id string) (string, error) {
|
|
|
|
mp := path.Join(d.home, "mnt", id)
|
2014-02-04 12:19:09 -05:00
|
|
|
if err := d.mount(id, mp); err != nil {
|
|
|
|
return "", err
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
2013-12-05 16:22:55 -05:00
|
|
|
|
2013-11-18 11:10:47 -05:00
|
|
|
return path.Join(mp, "rootfs"), nil
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-12-05 16:18:02 -05:00
|
|
|
func (d *Driver) Put(id string) {
|
2014-02-04 12:19:09 -05:00
|
|
|
if err := d.DeviceSet.UnmountDevice(id, UnmountRegular); err != nil {
|
|
|
|
utils.Errorf("Warning: error unmounting device %s: %s\n", id, err)
|
2013-12-05 16:22:55 -05:00
|
|
|
}
|
2013-12-05 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 17:56:26 -05:00
|
|
|
func (d *Driver) mount(id, mountPoint string) error {
|
2013-11-07 17:37:33 -05:00
|
|
|
// Create the target directories if they don't exist
|
2013-11-20 16:05:17 -05:00
|
|
|
if err := osMkdirAll(mountPoint, 0755); err != nil && !osIsExist(err) {
|
2013-11-07 17:37:33 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Mount the device
|
2014-02-04 12:19:09 -05:00
|
|
|
return d.DeviceSet.MountDevice(id, mountPoint)
|
2013-11-19 08:40:15 -05:00
|
|
|
}
|
2013-11-19 05:32:08 -05:00
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
|
|
|
return d.Devices[id] != nil
|
|
|
|
}
|