github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/daemon/graphdriver/devmapper/driver.go (about)

     1  // +build linux
     2  
     3  package devmapper
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path"
    10  	"strconv"
    11  
    12  	"github.com/sirupsen/logrus"
    13  
    14  	"github.com/docker/docker/daemon/graphdriver"
    15  	"github.com/docker/docker/pkg/containerfs"
    16  	"github.com/docker/docker/pkg/devicemapper"
    17  	"github.com/docker/docker/pkg/idtools"
    18  	"github.com/docker/docker/pkg/locker"
    19  	"github.com/docker/docker/pkg/mount"
    20  	"github.com/docker/docker/pkg/system"
    21  	units "github.com/docker/go-units"
    22  )
    23  
    24  func init() {
    25  	graphdriver.Register("devicemapper", Init)
    26  }
    27  
    28  // Driver contains the device set mounted and the home directory
    29  type Driver struct {
    30  	*DeviceSet
    31  	home    string
    32  	uidMaps []idtools.IDMap
    33  	gidMaps []idtools.IDMap
    34  	ctr     *graphdriver.RefCounter
    35  	locker  *locker.Locker
    36  }
    37  
    38  // Init creates a driver with the given home and the set of options.
    39  func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
    40  	deviceSet, err := NewDeviceSet(home, true, options, uidMaps, gidMaps)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	d := &Driver{
    46  		DeviceSet: deviceSet,
    47  		home:      home,
    48  		uidMaps:   uidMaps,
    49  		gidMaps:   gidMaps,
    50  		ctr:       graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
    51  		locker:    locker.New(),
    52  	}
    53  
    54  	return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
    55  }
    56  
    57  func (d *Driver) String() string {
    58  	return "devicemapper"
    59  }
    60  
    61  // Status returns the status about the driver in a printable format.
    62  // Information returned contains Pool Name, Data File, Metadata file, disk usage by
    63  // the data and metadata, etc.
    64  func (d *Driver) Status() [][2]string {
    65  	s := d.DeviceSet.Status()
    66  
    67  	status := [][2]string{
    68  		{"Pool Name", s.PoolName},
    69  		{"Pool Blocksize", units.HumanSize(float64(s.SectorSize))},
    70  		{"Base Device Size", units.HumanSize(float64(s.BaseDeviceSize))},
    71  		{"Backing Filesystem", s.BaseDeviceFS},
    72  		{"Udev Sync Supported", fmt.Sprintf("%v", s.UdevSyncSupported)},
    73  	}
    74  
    75  	if len(s.DataFile) > 0 {
    76  		status = append(status, [2]string{"Data file", s.DataFile})
    77  	}
    78  	if len(s.MetadataFile) > 0 {
    79  		status = append(status, [2]string{"Metadata file", s.MetadataFile})
    80  	}
    81  	if len(s.DataLoopback) > 0 {
    82  		status = append(status, [2]string{"Data loop file", s.DataLoopback})
    83  	}
    84  	if len(s.MetadataLoopback) > 0 {
    85  		status = append(status, [2]string{"Metadata loop file", s.MetadataLoopback})
    86  	}
    87  
    88  	status = append(status, [][2]string{
    89  		{"Data Space Used", units.HumanSize(float64(s.Data.Used))},
    90  		{"Data Space Total", units.HumanSize(float64(s.Data.Total))},
    91  		{"Data Space Available", units.HumanSize(float64(s.Data.Available))},
    92  		{"Metadata Space Used", units.HumanSize(float64(s.Metadata.Used))},
    93  		{"Metadata Space Total", units.HumanSize(float64(s.Metadata.Total))},
    94  		{"Metadata Space Available", units.HumanSize(float64(s.Metadata.Available))},
    95  		{"Thin Pool Minimum Free Space", units.HumanSize(float64(s.MinFreeSpace))},
    96  		{"Deferred Removal Enabled", fmt.Sprintf("%v", s.DeferredRemoveEnabled)},
    97  		{"Deferred Deletion Enabled", fmt.Sprintf("%v", s.DeferredDeleteEnabled)},
    98  		{"Deferred Deleted Device Count", fmt.Sprintf("%v", s.DeferredDeletedDeviceCount)},
    99  	}...)
   100  
   101  	if vStr, err := devicemapper.GetLibraryVersion(); err == nil {
   102  		status = append(status, [2]string{"Library Version", vStr})
   103  	}
   104  	return status
   105  }
   106  
   107  // GetMetadata returns a map of information about the device.
   108  func (d *Driver) GetMetadata(id string) (map[string]string, error) {
   109  	m, err := d.DeviceSet.exportDeviceMetadata(id)
   110  
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	metadata := make(map[string]string)
   116  	metadata["DeviceId"] = strconv.Itoa(m.deviceID)
   117  	metadata["DeviceSize"] = strconv.FormatUint(m.deviceSize, 10)
   118  	metadata["DeviceName"] = m.deviceName
   119  	return metadata, nil
   120  }
   121  
   122  // Cleanup unmounts a device.
   123  func (d *Driver) Cleanup() error {
   124  	err := d.DeviceSet.Shutdown(d.home)
   125  
   126  	if err2 := mount.RecursiveUnmount(d.home); err == nil {
   127  		err = err2
   128  	}
   129  
   130  	return err
   131  }
   132  
   133  // CreateReadWrite creates a layer that is writable for use as a container
   134  // file system.
   135  func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
   136  	return d.Create(id, parent, opts)
   137  }
   138  
   139  // Create adds a device with a given id and the parent.
   140  func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
   141  	var storageOpt map[string]string
   142  	if opts != nil {
   143  		storageOpt = opts.StorageOpt
   144  	}
   145  
   146  	if err := d.DeviceSet.AddDevice(id, parent, storageOpt); err != nil {
   147  		return err
   148  	}
   149  
   150  	return nil
   151  }
   152  
   153  // Remove removes a device with a given id, unmounts the filesystem.
   154  func (d *Driver) Remove(id string) error {
   155  	d.locker.Lock(id)
   156  	defer d.locker.Unlock(id)
   157  	if !d.DeviceSet.HasDevice(id) {
   158  		// Consider removing a non-existing device a no-op
   159  		// This is useful to be able to progress on container removal
   160  		// if the underlying device has gone away due to earlier errors
   161  		return nil
   162  	}
   163  
   164  	// This assumes the device has been properly Get/Put:ed and thus is unmounted
   165  	if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
   166  		return fmt.Errorf("failed to remove device %s: %v", id, err)
   167  	}
   168  	return system.EnsureRemoveAll(path.Join(d.home, "mnt", id))
   169  }
   170  
   171  // Get mounts a device with given id into the root filesystem
   172  func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
   173  	d.locker.Lock(id)
   174  	defer d.locker.Unlock(id)
   175  	mp := path.Join(d.home, "mnt", id)
   176  	rootFs := path.Join(mp, "rootfs")
   177  	if count := d.ctr.Increment(mp); count > 1 {
   178  		return containerfs.NewLocalContainerFS(rootFs), nil
   179  	}
   180  
   181  	uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
   182  	if err != nil {
   183  		d.ctr.Decrement(mp)
   184  		return nil, err
   185  	}
   186  
   187  	// Create the target directories if they don't exist
   188  	if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
   189  		d.ctr.Decrement(mp)
   190  		return nil, err
   191  	}
   192  	if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) {
   193  		d.ctr.Decrement(mp)
   194  		return nil, err
   195  	}
   196  
   197  	// Mount the device
   198  	if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
   199  		d.ctr.Decrement(mp)
   200  		return nil, err
   201  	}
   202  
   203  	if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil {
   204  		d.ctr.Decrement(mp)
   205  		d.DeviceSet.UnmountDevice(id, mp)
   206  		return nil, err
   207  	}
   208  
   209  	idFile := path.Join(mp, "id")
   210  	if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
   211  		// Create an "id" file with the container/image id in it to help reconstruct this in case
   212  		// of later problems
   213  		if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
   214  			d.ctr.Decrement(mp)
   215  			d.DeviceSet.UnmountDevice(id, mp)
   216  			return nil, err
   217  		}
   218  	}
   219  
   220  	return containerfs.NewLocalContainerFS(rootFs), nil
   221  }
   222  
   223  // Put unmounts a device and removes it.
   224  func (d *Driver) Put(id string) error {
   225  	d.locker.Lock(id)
   226  	defer d.locker.Unlock(id)
   227  	mp := path.Join(d.home, "mnt", id)
   228  	if count := d.ctr.Decrement(mp); count > 0 {
   229  		return nil
   230  	}
   231  
   232  	err := d.DeviceSet.UnmountDevice(id, mp)
   233  	if err != nil {
   234  		logrus.Errorf("devmapper: Error unmounting device %s: %v", id, err)
   235  	}
   236  
   237  	return err
   238  }
   239  
   240  // Exists checks to see if the device exists.
   241  func (d *Driver) Exists(id string) bool {
   242  	return d.DeviceSet.HasDevice(id)
   243  }