2014-08-02 03:46:15 -04:00
|
|
|
// +build linux
|
2013-11-27 22:12:51 -05:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package devmapper // import "github.com/docker/docker/daemon/graphdriver/devmapper"
|
2013-11-01 01:06:49 -04:00
|
|
|
|
|
|
|
import (
|
2013-11-01 15:04:08 -04:00
|
|
|
"fmt"
|
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"
|
2015-06-15 14:05:10 -04:00
|
|
|
"strconv"
|
2014-04-28 17:17:31 -04:00
|
|
|
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-10-08 11:51:41 -04:00
|
|
|
|
2014-10-24 18:11:48 -04:00
|
|
|
"github.com/docker/docker/daemon/graphdriver"
|
2017-08-03 20:22:00 -04:00
|
|
|
"github.com/docker/docker/pkg/containerfs"
|
2014-11-05 18:10:38 -05:00
|
|
|
"github.com/docker/docker/pkg/devicemapper"
|
2015-10-08 11:51:41 -04:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2017-02-17 18:46:19 -05:00
|
|
|
"github.com/docker/docker/pkg/locker"
|
2014-07-24 18:19:50 -04:00
|
|
|
"github.com/docker/docker/pkg/mount"
|
2017-02-14 13:35:20 -05:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2017-02-17 18:46:19 -05:00
|
|
|
units "github.com/docker/go-units"
|
2013-11-01 01:06:49 -04:00
|
|
|
)
|
|
|
|
|
2013-11-04 18:22:34 -05:00
|
|
|
func init() {
|
|
|
|
graphdriver.Register("devicemapper", Init)
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Driver contains the device set mounted and the home directory
|
2013-11-04 12:22:43 -05:00
|
|
|
type Driver struct {
|
2013-11-01 01:06:49 -04:00
|
|
|
*DeviceSet
|
2015-10-08 11:51:41 -04:00
|
|
|
home string
|
|
|
|
uidMaps []idtools.IDMap
|
|
|
|
gidMaps []idtools.IDMap
|
2016-04-18 19:41:29 -04:00
|
|
|
ctr *graphdriver.RefCounter
|
2017-02-17 18:46:19 -05:00
|
|
|
locker *locker.Locker
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Init creates a driver with the given home and the set of options.
|
2015-10-08 11:51:41 -04:00
|
|
|
func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
|
|
|
|
deviceSet, err := NewDeviceSet(home, true, options, uidMaps, gidMaps)
|
2013-10-24 15:04:49 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-06-05 15:50:53 -04:00
|
|
|
|
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,
|
2015-10-08 11:51:41 -04:00
|
|
|
uidMaps: uidMaps,
|
|
|
|
gidMaps: gidMaps,
|
2016-05-06 16:09:45 -04:00
|
|
|
ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
|
2017-02-17 18:46:19 -05:00
|
|
|
locker: locker.New(),
|
2013-11-01 15:04:08 -04:00
|
|
|
}
|
2014-06-05 15:50:53 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2013-11-07 22:02:15 -05:00
|
|
|
func (d *Driver) String() string {
|
|
|
|
return "devicemapper"
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04: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 05:04:02 -05:00
|
|
|
func (d *Driver) Status() [][2]string {
|
|
|
|
s := d.DeviceSet.Status()
|
|
|
|
|
|
|
|
status := [][2]string{
|
|
|
|
{"Pool Name", s.PoolName},
|
2017-09-11 14:55:05 -04:00
|
|
|
{"Pool Blocksize", units.HumanSize(float64(s.SectorSize))},
|
|
|
|
{"Base Device Size", units.HumanSize(float64(s.BaseDeviceSize))},
|
2015-11-13 13:56:21 -05:00
|
|
|
{"Backing Filesystem", s.BaseDeviceFS},
|
2017-10-27 03:59:09 -04:00
|
|
|
{"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.DataFile) > 0 {
|
|
|
|
status = append(status, [2]string{"Data file", s.DataFile})
|
|
|
|
}
|
|
|
|
if len(s.MetadataFile) > 0 {
|
|
|
|
status = append(status, [2]string{"Metadata file", s.MetadataFile})
|
|
|
|
}
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
|
|
|
status = append(status, [][2]string{
|
2017-09-11 14:55:05 -04:00
|
|
|
{"Data Space Used", units.HumanSize(float64(s.Data.Used))},
|
|
|
|
{"Data Space Total", units.HumanSize(float64(s.Data.Total))},
|
|
|
|
{"Data Space Available", units.HumanSize(float64(s.Data.Available))},
|
|
|
|
{"Metadata Space Used", units.HumanSize(float64(s.Metadata.Used))},
|
|
|
|
{"Metadata Space Total", units.HumanSize(float64(s.Metadata.Total))},
|
|
|
|
{"Metadata Space Available", units.HumanSize(float64(s.Metadata.Available))},
|
|
|
|
{"Thin Pool Minimum Free Space", units.HumanSize(float64(s.MinFreeSpace))},
|
2015-04-21 18:14:59 -04:00
|
|
|
{"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
|
2015-10-06 17:37:21 -04:00
|
|
|
{"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
|
2015-10-06 17:37:21 -04:00
|
|
|
{"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
|
2017-10-27 03:59:09 -04:00
|
|
|
}...)
|
|
|
|
|
2014-11-05 18:10:38 -05:00
|
|
|
if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
|
2014-09-25 15:51:02 -04:00
|
|
|
status = append(status, [2]string{"Library Version", vStr})
|
|
|
|
}
|
2013-11-15 05:04:02 -05:00
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// GetMetadata returns a map of information about the device.
|
2015-06-15 14:05:10 -04:00
|
|
|
func (d *Driver) GetMetadata(id string) (map[string]string, error) {
|
2015-07-22 20:42:28 -04:00
|
|
|
m, err := d.DeviceSet.exportDeviceMetadata(id)
|
2015-06-15 14:05:10 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := make(map[string]string)
|
2015-07-22 20:42:28 -04:00
|
|
|
metadata["DeviceId"] = strconv.Itoa(m.deviceID)
|
2015-06-15 14:05:10 -04:00
|
|
|
metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
|
|
|
|
metadata["DeviceName"] = m.deviceName
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Cleanup unmounts a device.
|
2013-11-04 12:22:43 -05:00
|
|
|
func (d *Driver) Cleanup() error {
|
2016-03-09 16:23:04 -05:00
|
|
|
err := d.DeviceSet.Shutdown(d.home)
|
2014-06-05 15:50:53 -04:00
|
|
|
|
2018-01-17 21:17:26 -05:00
|
|
|
if err2 := mount.RecursiveUnmount(d.home); err == nil {
|
2014-06-05 15:50:53 -04:00
|
|
|
err = err2
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2016-02-18 20:24:59 -05:00
|
|
|
// CreateReadWrite creates a layer that is writable for use as a container
|
|
|
|
// file system.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
return d.Create(id, parent, opts)
|
2016-02-18 20:24:59 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Create adds a device with a given id and the parent.
|
2016-11-09 15:59:58 -05:00
|
|
|
func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
|
|
|
|
var storageOpt map[string]string
|
|
|
|
if opts != nil {
|
|
|
|
storageOpt = opts.StorageOpt
|
|
|
|
}
|
2018-01-14 18:42:25 -05:00
|
|
|
return d.DeviceSet.AddDevice(id, parent, storageOpt)
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Remove removes a device with a given id, unmounts the filesystem.
|
2013-11-07 17:37:33 -05:00
|
|
|
func (d *Driver) Remove(id string) error {
|
2017-02-17 18:46:19 -05:00
|
|
|
d.locker.Lock(id)
|
|
|
|
defer d.locker.Unlock(id)
|
2014-03-06 09:12:09 -05: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 12:19:09 -05:00
|
|
|
// This assumes the device has been properly Get/Put:ed and thus is unmounted
|
2015-10-06 17:37:21 -04:00
|
|
|
if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
|
2017-01-09 10:58:09 -05:00
|
|
|
return fmt.Errorf("failed to remove device %s: %v", id, err)
|
2014-02-11 03:40:13 -05:00
|
|
|
}
|
2017-09-11 14:55:05 -04:00
|
|
|
return system.EnsureRemoveAll(path.Join(d.home, "mnt", id))
|
2013-11-02 17:25:06 -04:00
|
|
|
}
|
2013-11-01 01:06:49 -04:00
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Get mounts a device with given id into the root filesystem
|
2017-08-03 20:22:00 -04:00
|
|
|
func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
|
2017-02-17 18:46:19 -05:00
|
|
|
d.locker.Lock(id)
|
|
|
|
defer d.locker.Unlock(id)
|
2013-11-07 17:37:33 -05:00
|
|
|
mp := path.Join(d.home, "mnt", id)
|
2016-05-27 14:09:37 -04:00
|
|
|
rootFs := path.Join(mp, "rootfs")
|
2016-05-02 18:44:20 -04:00
|
|
|
if count := d.ctr.Increment(mp); count > 1 {
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(rootFs), nil
|
2016-04-18 19:41:29 -04:00
|
|
|
}
|
2014-04-23 07:50:53 -04:00
|
|
|
|
2015-10-08 11:51:41 -04:00
|
|
|
uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
|
|
|
|
if err != nil {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2016-04-18 19:41:29 -04:00
|
|
|
|
2014-04-23 07:50:53 -04:00
|
|
|
// Create the target directories if they don't exist
|
Simplify/fix MkdirAll usage
This subtle bug keeps lurking in because error checking for `Mkdir()`
and `MkdirAll()` is slightly different wrt to `EEXIST`/`IsExist`:
- for `Mkdir()`, `IsExist` error should (usually) be ignored
(unless you want to make sure directory was not there before)
as it means "the destination directory was already there"
- for `MkdirAll()`, `IsExist` error should NEVER be ignored.
Mostly, this commit just removes ignoring the IsExist error, as it
should not be ignored.
Also, there are a couple of cases then IsExist is handled as
"directory already exist" which is wrong. As a result, some code
that never worked as intended is now removed.
NOTE that `idtools.MkdirAndChown()` behaves like `os.MkdirAll()`
rather than `os.Mkdir()` -- so its description is amended accordingly,
and its usage is handled as such (i.e. IsExist error is not ignored).
For more details, a quote from my runc commit 6f82d4b (July 2015):
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.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-25 15:39:36 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2015-10-08 11:51:41 -04:00
|
|
|
}
|
2017-11-18 18:52:47 -05:00
|
|
|
if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2013-11-01 01:06:49 -04:00
|
|
|
}
|
2013-12-05 16:22:55 -05:00
|
|
|
|
2014-04-23 07:50:53 -04:00
|
|
|
// Mount the device
|
2014-04-28 17:17:31 -04:00
|
|
|
if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2014-04-23 07:50:53 -04:00
|
|
|
}
|
2013-11-01 01:06:49 -04:00
|
|
|
|
Simplify/fix MkdirAll usage
This subtle bug keeps lurking in because error checking for `Mkdir()`
and `MkdirAll()` is slightly different wrt to `EEXIST`/`IsExist`:
- for `Mkdir()`, `IsExist` error should (usually) be ignored
(unless you want to make sure directory was not there before)
as it means "the destination directory was already there"
- for `MkdirAll()`, `IsExist` error should NEVER be ignored.
Mostly, this commit just removes ignoring the IsExist error, as it
should not be ignored.
Also, there are a couple of cases then IsExist is handled as
"directory already exist" which is wrong. As a result, some code
that never worked as intended is now removed.
NOTE that `idtools.MkdirAndChown()` behaves like `os.MkdirAll()`
rather than `os.Mkdir()` -- so its description is amended accordingly,
and its usage is handled as such (i.e. IsExist error is not ignored).
For more details, a quote from my runc commit 6f82d4b (July 2015):
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.
[1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-25 15:39:36 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2015-12-03 09:22:25 -05:00
|
|
|
d.DeviceSet.UnmountDevice(id, mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2013-12-05 16:22:55 -05:00
|
|
|
}
|
2014-04-23 07:50:53 -04:00
|
|
|
|
|
|
|
idFile := path.Join(mp, "id")
|
2014-05-16 08:10:02 -04:00
|
|
|
if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
|
2015-12-13 11:00:39 -05:00
|
|
|
// Create an "id" file with the container/image id in it to help reconstruct this in case
|
2014-04-23 07:50:53 -04:00
|
|
|
// of later problems
|
|
|
|
if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
|
2016-05-02 18:44:20 -04:00
|
|
|
d.ctr.Decrement(mp)
|
2015-12-03 09:22:25 -05:00
|
|
|
d.DeviceSet.UnmountDevice(id, mp)
|
2017-08-03 20:22:00 -04:00
|
|
|
return nil, err
|
2014-04-23 07:50:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
return containerfs.NewLocalContainerFS(rootFs), nil
|
2013-12-05 16:18:02 -05:00
|
|
|
}
|
|
|
|
|
2015-07-22 20:42:28 -04:00
|
|
|
// Put unmounts a device and removes it.
|
2015-01-09 17:14:52 -05:00
|
|
|
func (d *Driver) Put(id string) error {
|
2017-02-17 18:46:19 -05:00
|
|
|
d.locker.Lock(id)
|
|
|
|
defer d.locker.Unlock(id)
|
2016-05-02 18:44:20 -04:00
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
if count := d.ctr.Decrement(mp); count > 0 {
|
2016-04-18 19:41:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
2017-08-19 23:50:52 -04:00
|
|
|
|
2015-12-03 09:22:25 -05:00
|
|
|
err := d.DeviceSet.UnmountDevice(id, mp)
|
2015-01-09 17:14:52 -05:00
|
|
|
if err != nil {
|
2017-08-19 23:50:52 -04:00
|
|
|
logrus.Errorf("devmapper: Error unmounting device %s: %v", id, err)
|
2013-11-07 17:37:33 -05:00
|
|
|
}
|
2017-08-19 23:50:52 -04:00
|
|
|
|
2015-01-09 17:14:52 -05:00
|
|
|
return err
|
2013-11-19 08:40:15 -05:00
|
|
|
}
|
2013-11-19 05:32:08 -05:00
|
|
|
|
2015-09-30 15:21:22 -04:00
|
|
|
// Exists checks to see if the device exists.
|
2013-11-19 05:32:08 -05:00
|
|
|
func (d *Driver) Exists(id string) bool {
|
2014-04-23 07:50:53 -04:00
|
|
|
return d.DeviceSet.HasDevice(id)
|
2013-11-19 05:32:08 -05:00
|
|
|
}
|