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