2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
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"
|
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
|
|
|
|
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"
|
2019-08-05 10:37:47 -04:00
|
|
|
units "github.com/docker/go-units"
|
2020-09-10 16:15:40 -04:00
|
|
|
"github.com/moby/locker"
|
2020-03-13 19:38:24 -04:00
|
|
|
"github.com/moby/sys/mount"
|
2018-02-27 22:02:05 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/sys/unix"
|
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
|
2022-03-14 15:24:29 -04:00
|
|
|
home string
|
|
|
|
ctr *graphdriver.RefCounter
|
|
|
|
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.
|
2022-03-14 15:24:29 -04:00
|
|
|
func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdriver.Driver, error) {
|
|
|
|
deviceSet, err := NewDeviceSet(home, true, options, idMap)
|
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,
|
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
|
|
|
|
2022-03-14 15:24:29 -04:00
|
|
|
return graphdriver.NewNaiveDiffDriver(d, d.idMap), 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)
|
2018-01-23 20:17:13 -05:00
|
|
|
umountErr := mount.RecursiveUnmount(d.home)
|
2014-06-05 15:50:53 -04:00
|
|
|
|
2018-01-23 20:17:13 -05:00
|
|
|
// in case we have two errors, prefer the one from Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-22 21:30:34 -04:00
|
|
|
return umountErr
|
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
|
|
|
}
|
|
|
|
|
2018-02-27 22:02:05 -05:00
|
|
|
// Remove removes a device with a given id, unmounts the filesystem, and removes the mount point.
|
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
|
|
|
}
|
2018-02-27 22:02:05 -05:00
|
|
|
|
|
|
|
// Most probably the mount point is already removed on Put()
|
|
|
|
// (see DeviceSet.UnmountDevice()), but just in case it was not
|
|
|
|
// let's try to remove it here as well, ignoring errors as
|
|
|
|
// an older kernel can return EBUSY if e.g. the mount was leaked
|
|
|
|
// to other mount namespaces. A failure to remove the container's
|
|
|
|
// mount point is not important and should not be treated
|
|
|
|
// as a failure to remove the container.
|
|
|
|
mp := path.Join(d.home, "mnt", id)
|
|
|
|
err := unix.Rmdir(mp)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
logrus.WithField("storage-driver", "devicemapper").Warnf("unable to remove mount point %q: %s", mp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
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
|
|
|
|
2022-03-14 15:24:29 -04:00
|
|
|
root := d.idMap.RootPair()
|
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
|
2022-03-14 15:24:29 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, root); 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
|
|
|
}
|
2022-03-14 15:24:29 -04:00
|
|
|
if err := idtools.MkdirAndChown(mp, 0755, root); 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
|
|
|
|
2022-03-14 15:24:29 -04:00
|
|
|
if err := idtools.MkdirAllAndChown(rootFs, 0755, root); 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
|
2021-08-24 06:10:50 -04:00
|
|
|
if err := os.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 {
|
2018-03-05 10:09:41 -05:00
|
|
|
logrus.WithField("storage-driver", "devicemapper").Errorf("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
|
|
|
}
|