2014-08-02 01:46:15 -06:00
|
|
|
// +build linux
|
2013-11-27 19:12:51 -08:00
|
|
|
|
2013-11-01 05:06:49 +00:00
|
|
|
package devmapper
|
|
|
|
|
|
|
|
import (
|
2013-11-01 19:04:08 +00:00
|
|
|
"fmt"
|
2013-11-18 17:10:47 +01:00
|
|
|
"io/ioutil"
|
2014-02-11 09:40:13 +01:00
|
|
|
"os"
|
2013-11-01 19:04:08 +00:00
|
|
|
"path"
|
2014-04-28 14:17:31 -07:00
|
|
|
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
|
|
|
"github.com/docker/docker/pkg/mount"
|
|
|
|
"github.com/docker/docker/utils"
|
2013-11-01 05:06:49 +00:00
|
|
|
)
|
|
|
|
|
2013-11-04 15:22:34 -08: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
|
2014-04-03 06:34:57 +00:00
|
|
|
home string
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2014-06-05 10:34:20 +02:00
|
|
|
func Init(home string, options []string) (graphdriver.Driver, error) {
|
2014-03-27 17:48:32 +01:00
|
|
|
deviceSet, err := NewDeviceSet(home, true, options)
|
2013-10-24 21:04:49 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-05 12:50:53 -07:00
|
|
|
|
|
|
|
if err := graphdriver.MakePrivate(home); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2013-11-04 17:22:43 +00:00
|
|
|
d := &Driver{
|
2013-10-24 21:04:49 +02:00
|
|
|
DeviceSet: deviceSet,
|
2013-11-04 15:22:34 -08:00
|
|
|
home: home,
|
2013-11-01 19:04:08 +00:00
|
|
|
}
|
2014-06-05 12:50:53 -07:00
|
|
|
|
2013-11-04 17:22:43 +00:00
|
|
|
return d, nil
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2013-11-07 19:02:15 -08:00
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "devicemapper"
|
|
|
|
}
|
|
|
|
|
2013-11-15 11:04:02 +01:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
s := d.DeviceSet.Status()
|
|
|
|
|
|
|
|
status := [][2]string{
|
|
|
|
{"Pool Name", s.PoolName},
|
2014-06-26 12:39:16 -04:00
|
|
|
{"Pool Blocksize", fmt.Sprintf("%d Kb", s.SectorSize/1024)},
|
2013-11-15 11:04:02 +01:00
|
|
|
{"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 {
|
2014-06-05 12:50:53 -07:00
|
|
|
err := d.DeviceSet.Shutdown()
|
|
|
|
|
|
|
|
if err2 := mount.Unmount(d.home); err == nil {
|
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (d *Driver) Create(id, parent string) error {
|
2013-11-18 17:10:47 +01:00
|
|
|
if err := d.DeviceSet.AddDevice(id, parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-02-04 18:19:09 +01:00
|
|
|
|
2013-11-18 17:10:47 +01:00
|
|
|
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 {
|
2014-03-06 15:12:09 +01:00
|
|
|
if !d.DeviceSet.HasDevice(id) {
|
|
|
|
// Consider removing a non-existing device a no-op
|
|
|
|
// This is useful to be able to progress on container removal
|
|
|
|
// if the underlying device has gone away due to earlier errors
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:19:09 +01:00
|
|
|
// This assumes the device has been properly Get/Put:ed and thus is unmounted
|
2014-02-11 09:40:13 +01: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 21:25:06 +00:00
|
|
|
}
|
2013-11-01 05:06:49 +00:00
|
|
|
|
2014-04-17 23:47:27 +00:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
2013-11-07 22:37:33 +00:00
|
|
|
mp := path.Join(d.home, "mnt", id)
|
2014-04-23 13:50:53 +02:00
|
|
|
|
|
|
|
// Create the target directories if they don't exist
|
2014-05-16 14:10:02 +02:00
|
|
|
if err := os.MkdirAll(mp, 0755); err != nil && !os.IsExist(err) {
|
2014-02-04 18:19:09 +01:00
|
|
|
return "", err
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
2013-12-05 22:22:55 +01:00
|
|
|
|
2014-04-23 13:50:53 +02:00
|
|
|
// Mount the device
|
2014-04-28 14:17:31 -07:00
|
|
|
if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
|
2014-04-23 13:50:53 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2013-11-01 05:06:49 +00:00
|
|
|
|
2014-04-23 13:50:53 +02:00
|
|
|
rootFs := path.Join(mp, "rootfs")
|
2014-05-16 14:10:02 +02:00
|
|
|
if err := os.MkdirAll(rootFs, 0755); err != nil && !os.IsExist(err) {
|
2014-04-23 13:50:53 +02:00
|
|
|
d.DeviceSet.UnmountDevice(id)
|
|
|
|
return "", err
|
2013-12-05 22:22:55 +01:00
|
|
|
}
|
2014-04-23 13:50:53 +02:00
|
|
|
|
|
|
|
idFile := path.Join(mp, "id")
|
2014-05-16 14:10:02 +02:00
|
|
|
if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
|
2014-04-23 13:50:53 +02:00
|
|
|
// Create an "id" file with the container/image id in it to help reconscruct this in case
|
|
|
|
// of later problems
|
|
|
|
if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
|
|
|
|
d.DeviceSet.UnmountDevice(id)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootFs, nil
|
2013-12-05 22:18:02 +01:00
|
|
|
}
|
|
|
|
|
2014-04-23 13:50:53 +02:00
|
|
|
func (d *Driver) Put(id string) {
|
|
|
|
if err := d.DeviceSet.UnmountDevice(id); err != nil {
|
|
|
|
utils.Errorf("Warning: error unmounting device %s: %s\n", id, err)
|
2013-11-07 22:37:33 +00:00
|
|
|
}
|
2013-11-19 14:40:15 +01:00
|
|
|
}
|
2013-11-19 02:32:08 -08:00
|
|
|
|
|
|
|
func (d *Driver) Exists(id string) bool {
|
2014-04-23 13:50:53 +02:00
|
|
|
return d.DeviceSet.HasDevice(id)
|
2013-11-19 02:32:08 -08:00
|
|
|
}
|