github.com/vieux/docker@v0.6.3-0.20161004191708-e097c2a938c7/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, mountLabel string, storageOpt map[string]string) error {
   126  	return d.Create(id, parent, mountLabel, storageOpt)
   127  }
   128  
   129  // Create adds a device with a given id and the parent.
   130  func (d *Driver) Create(id, parent, mountLabel string, storageOpt map[string]string) error {
   131  	if err := d.DeviceSet.AddDevice(id, parent, storageOpt); err != nil {
   132  		return err
   133  	}
   134  
   135  	return nil
   136  }
   137  
   138  // Remove removes a device with a given id, unmounts the filesystem.
   139  func (d *Driver) Remove(id string) error {
   140  	if !d.DeviceSet.HasDevice(id) {
   141  		// Consider removing a non-existing device a no-op
   142  		// This is useful to be able to progress on container removal
   143  		// if the underlying device has gone away due to earlier errors
   144  		return nil
   145  	}
   146  
   147  	// This assumes the device has been properly Get/Put:ed and thus is unmounted
   148  	if err := d.DeviceSet.DeleteDevice(id, false); err != nil {
   149  		return err
   150  	}
   151  
   152  	mp := path.Join(d.home, "mnt", id)
   153  	if err := os.RemoveAll(mp); err != nil && !os.IsNotExist(err) {
   154  		return err
   155  	}
   156  
   157  	return nil
   158  }
   159  
   160  // Get mounts a device with given id into the root filesystem
   161  func (d *Driver) Get(id, mountLabel string) (string, error) {
   162  	mp := path.Join(d.home, "mnt", id)
   163  	rootFs := path.Join(mp, "rootfs")
   164  	if count := d.ctr.Increment(mp); count > 1 {
   165  		return rootFs, nil
   166  	}
   167  
   168  	uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
   169  	if err != nil {
   170  		d.ctr.Decrement(mp)
   171  		return "", err
   172  	}
   173  
   174  	// Create the target directories if they don't exist
   175  	if err := idtools.MkdirAllAs(path.Join(d.home, "mnt"), 0755, uid, gid); err != nil && !os.IsExist(err) {
   176  		d.ctr.Decrement(mp)
   177  		return "", err
   178  	}
   179  	if err := idtools.MkdirAs(mp, 0755, uid, gid); err != nil && !os.IsExist(err) {
   180  		d.ctr.Decrement(mp)
   181  		return "", err
   182  	}
   183  
   184  	// Mount the device
   185  	if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil {
   186  		d.ctr.Decrement(mp)
   187  		return "", err
   188  	}
   189  
   190  	if err := idtools.MkdirAllAs(rootFs, 0755, uid, gid); err != nil && !os.IsExist(err) {
   191  		d.ctr.Decrement(mp)
   192  		d.DeviceSet.UnmountDevice(id, mp)
   193  		return "", err
   194  	}
   195  
   196  	idFile := path.Join(mp, "id")
   197  	if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) {
   198  		// Create an "id" file with the container/image id in it to help reconstruct this in case
   199  		// of later problems
   200  		if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil {
   201  			d.ctr.Decrement(mp)
   202  			d.DeviceSet.UnmountDevice(id, mp)
   203  			return "", err
   204  		}
   205  	}
   206  
   207  	return rootFs, nil
   208  }
   209  
   210  // Put unmounts a device and removes it.
   211  func (d *Driver) Put(id string) error {
   212  	mp := path.Join(d.home, "mnt", id)
   213  	if count := d.ctr.Decrement(mp); count > 0 {
   214  		return nil
   215  	}
   216  	err := d.DeviceSet.UnmountDevice(id, mp)
   217  	if err != nil {
   218  		logrus.Errorf("devmapper: Error unmounting device %s: %s", id, err)
   219  	}
   220  	return err
   221  }
   222  
   223  // Exists checks to see if the device exists.
   224  func (d *Driver) Exists(id string) bool {
   225  	return d.DeviceSet.HasDevice(id)
   226  }