2014-08-02 07:46:15 +00:00
|
|
|
// +build linux
|
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-18 16:10:47 +00:00
|
|
|
"io/ioutil"
|
2014-02-11 08:40:13 +00:00
|
|
|
"os"
|
2013-11-01 19:04:08 +00:00
|
|
|
"path"
|
2015-06-15 18:05:10 +00:00
|
|
|
"strconv"
|
2014-04-28 21:17:31 +00:00
|
|
|
|
2015-03-26 22:22:04 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2014-10-24 22:11:48 +00:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2014-11-05 23:10:38 +00:00
|
|
|
"github.com/docker/docker/pkg/devicemapper"
|
2014-07-24 22:19:50 +00:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2014-08-20 02:33:46 +00:00
|
|
|
"github.com/docker/docker/pkg/units"
|
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.
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Driver contains the device set mounted and the home directory
|
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
|
|
|
}
|
|
|
|
|
2015-01-15 21:40:39 +00:00
|
|
|
var backingFs = "<unknown>"
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Init creates a driver with the given home and the set of options.
|
2014-06-05 08:34:20 +00:00
|
|
|
func Init(home string, options []string) (graphdriver.Driver, error) {
|
2015-01-15 21:40:39 +00:00
|
|
|
fsMagic, err := graphdriver.GetFSMagic(home)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fsName, ok := graphdriver.FsNames[fsMagic]; ok {
|
|
|
|
backingFs = fsName
|
|
|
|
}
|
|
|
|
|
2014-03-27 16:48:32 +00:00
|
|
|
deviceSet, err := NewDeviceSet(home, true, options)
|
2013-10-24 19:04:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-05 19:50:53 +00:00
|
|
|
|
2014-10-30 21:04:56 +00:00
|
|
|
if err := mount.MakePrivate(home); err != nil {
|
2014-06-05 19:50:53 +00:00
|
|
|
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-11-01 19:04:08 +00:00
|
|
|
}
|
2014-06-05 19:50:53 +00:00
|
|
|
|
2015-09-24 01:21:28 +00:00
|
|
|
return graphdriver.NewNaiveDiffDriver(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"
|
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Status returns the status about the driver in a printable format.
|
|
|
|
// Information returned contains Pool Name, Data File, Metadata file, disk usage by
|
|
|
|
// the data and metadata, etc.
|
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},
|
2014-11-25 08:02:47 +00:00
|
|
|
{"Pool Blocksize", fmt.Sprintf("%s", units.HumanSize(float64(s.SectorSize)))},
|
2015-01-15 21:40:39 +00:00
|
|
|
{"Backing Filesystem", backingFs},
|
2015-01-09 16:20:07 +00:00
|
|
|
{"Data file", s.DataFile},
|
|
|
|
{"Metadata file", s.MetadataFile},
|
2014-11-25 08:02:47 +00:00
|
|
|
{"Data Space Used", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Used)))},
|
|
|
|
{"Data Space Total", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Total)))},
|
2015-01-31 06:21:34 +00:00
|
|
|
{"Data Space Available", fmt.Sprintf("%s", units.HumanSize(float64(s.Data.Available)))},
|
2014-11-25 08:02:47 +00:00
|
|
|
{"Metadata Space Used", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Used)))},
|
|
|
|
{"Metadata Space Total", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Total)))},
|
2015-01-31 06:21:34 +00:00
|
|
|
{"Metadata Space Available", fmt.Sprintf("%s", units.HumanSize(float64(s.Metadata.Available)))},
|
2015-01-19 21:28:02 +00:00
|
|
|
{"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
|
2015-04-21 22:14:59 +00:00
|
|
|
{"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
|
2015-10-06 21:37:21 +00:00
|
|
|
{"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
|
2015-10-06 21:37:21 +00:00
|
|
|
{"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
|
2013-11-15 10:04:02 +00:00
|
|
|
}
|
2015-01-09 16:20:07 +00:00
|
|
|
if len(s.DataLoopback) > 0 {
|
|
|
|
status = append(status, [2]string{"Data loop file", s.DataLoopback})
|
|
|
|
}
|
|
|
|
if len(s.MetadataLoopback) > 0 {
|
|
|
|
status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
|
|
|
|
}
|
2014-11-05 23:10:38 +00:00
|
|
|
if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
|
2014-09-25 19:51:02 +00:00
|
|
|
status = append(status, [2]string{"Library Version", vStr})
|
|
|
|
}
|
2013-11-15 10:04:02 +00:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// GetMetadata returns a map of information about the device.
|
2015-06-15 18:05:10 +00:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
2015-07-23 00:42:28 +00:00
|
|
|
m, err := d.DeviceSet.exportDeviceMetadata(id)
|
2015-06-15 18:05:10 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := make(map[string]string)
|
2015-07-23 00:42:28 +00:00
|
|
|
metadata["DeviceId"] = strconv.Itoa(m.deviceID)
|
2015-06-15 18:05:10 +00:00
|
|
|
metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
|
|
|
|
metadata["DeviceName"] = m.deviceName
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Cleanup unmounts a device.
|
2013-11-04 17:22:43 +00:00
|
|
|
func (d *Driver) Cleanup() error {
|
2014-06-05 19:50:53 +00: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
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Create adds a device with a given id and the parent.
|
2014-04-17 23:47:27 +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
|
|
|
|
}
|
2014-02-04 17:19:09 +00:00
|
|
|
|
2013-11-18 16:10:47 +00:00
|
|
|
return nil
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Remove removes a device with a given id, unmounts the filesystem.
|
2013-11-07 22:37:33 +00:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2014-03-06 14:12:09 +00: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 17:19:09 +00:00
|
|
|
// This assumes the device has been properly Get/Put:ed and thus is unmounted
|
2015-10-06 21:37:21 +00:00
|
|
|
if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
|
2014-02-11 08:40:13 +00:00
|
|
|
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
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Get mounts a device with given id into the root filesystem
|
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 11:50:53 +00:00
|
|
|
|
|
|
|
// Create the target directories if they don't exist
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 23:49:05 +00:00
|
|
|
if err := os.MkdirAll(mp, 0755); err != nil {
|
2014-02-04 17:19:09 +00:00
|
|
|
return "", err
|
2013-11-01 05:06:49 +00:00
|
|
|
}
|
2013-12-05 21:22:55 +00:00
|
|
|
|
2014-04-23 11:50:53 +00:00
|
|
|
// Mount the device
|
2014-04-28 21:17:31 +00:00
|
|
|
if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
|
2014-04-23 11:50:53 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2013-11-01 05:06:49 +00:00
|
|
|
|
2014-04-23 11:50:53 +00:00
|
|
|
rootFs := path.Join(mp, "rootfs")
|
Simplify and fix os.MkdirAll() usage
TL;DR: check for IsExist(err) after a failed MkdirAll() is both
redundant and wrong -- so two reasons to remove it.
Quoting MkdirAll documentation:
> MkdirAll creates a directory named path, along with any necessary
> parents, and returns nil, or else returns an error. If path
> is already a directory, MkdirAll does nothing and returns nil.
This means two things:
1. If a directory to be created already exists, no error is returned.
2. If the error returned is IsExist (EEXIST), it means there exists
a non-directory with the same name as MkdirAll need to use for
directory. Example: we want to MkdirAll("a/b"), but file "a"
(or "a/b") already exists, so MkdirAll fails.
The above is a theory, based on quoted documentation and my UNIX
knowledge.
3. In practice, though, current MkdirAll implementation [1] returns
ENOTDIR in most of cases described in #2, with the exception when
there is a race between MkdirAll and someone else creating the
last component of MkdirAll argument as a file. In this very case
MkdirAll() will indeed return EEXIST.
Because of #1, IsExist check after MkdirAll is not needed.
Because of #2 and #3, ignoring IsExist error is just plain wrong,
as directory we require is not created. It's cleaner to report
the error now.
Note this error is all over the tree, I guess due to copy-paste,
or trying to follow the same usage pattern as for Mkdir(),
or some not quite correct examples on the Internet.
[v2: a separate aufs commit is merged into this one]
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kir@openvz.org>
2015-07-29 23:49:05 +00:00
|
|
|
if err := os.MkdirAll(rootFs, 0755); err != nil {
|
2014-04-23 11:50:53 +00:00
|
|
|
d.DeviceSet.UnmountDevice(id)
|
|
|
|
return "", err
|
2013-12-05 21:22:55 +00:00
|
|
|
}
|
2014-04-23 11:50:53 +00:00
|
|
|
|
|
|
|
idFile := path.Join(mp, "id")
|
2014-05-16 12:10:02 +00:00
|
|
|
if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
|
2014-04-23 11:50:53 +00: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 21:18:02 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 00:42:28 +00:00
|
|
|
// Put unmounts a device and removes it.
|
2015-01-09 22:14:52 +00:00
|
|
|
func (d *Driver) Put(id string) error {
|
|
|
|
err := d.DeviceSet.UnmountDevice(id)
|
|
|
|
if err != nil {
|
2015-03-26 22:22:04 +00:00
|
|
|
logrus.Errorf("Error unmounting device %s: %s", id, err)
|
2013-11-07 22:37:33 +00:00
|
|
|
}
|
2015-01-09 22:14:52 +00:00
|
|
|
return err
|
2013-11-19 13:40:15 +00:00
|
|
|
}
|
2013-11-19 10:32:08 +00:00
|
|
|
|
2015-09-30 19:21:22 +00:00
|
|
|
// Exists checks to see if the device exists.
|
2013-11-19 10:32:08 +00:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
2014-04-23 11:50:53 +00:00
|
|
|
return d.DeviceSet.HasDevice(id)
|
2013-11-19 10:32:08 +00:00
|
|
|
}
|