github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/daemon/graphdriver/devmapper/driver.go (about) 1 // +build linux 2 3 package devmapper // import "github.com/docker/docker/daemon/graphdriver/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 return d.DeviceSet.AddDevice(id, parent, storageOpt) 146 } 147 148 // Remove removes a device with a given id, unmounts the filesystem. 149 func (d *Driver) Remove(id string) error { 150 d.locker.Lock(id) 151 defer d.locker.Unlock(id) 152 if !d.DeviceSet.HasDevice(id) { 153 // Consider removing a non-existing device a no-op 154 // This is useful to be able to progress on container removal 155 // if the underlying device has gone away due to earlier errors 156 return nil 157 } 158 159 // This assumes the device has been properly Get/Put:ed and thus is unmounted 160 if err := d.DeviceSet.DeleteDevice(id, false); err != nil { 161 return fmt.Errorf("failed to remove device %s: %v", id, err) 162 } 163 return system.EnsureRemoveAll(path.Join(d.home, "mnt", id)) 164 } 165 166 // Get mounts a device with given id into the root filesystem 167 func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) { 168 d.locker.Lock(id) 169 defer d.locker.Unlock(id) 170 mp := path.Join(d.home, "mnt", id) 171 rootFs := path.Join(mp, "rootfs") 172 if count := d.ctr.Increment(mp); count > 1 { 173 return containerfs.NewLocalContainerFS(rootFs), nil 174 } 175 176 uid, gid, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) 177 if err != nil { 178 d.ctr.Decrement(mp) 179 return nil, err 180 } 181 182 // Create the target directories if they don't exist 183 if err := idtools.MkdirAllAndChown(path.Join(d.home, "mnt"), 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil { 184 d.ctr.Decrement(mp) 185 return nil, err 186 } 187 if err := idtools.MkdirAndChown(mp, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil && !os.IsExist(err) { 188 d.ctr.Decrement(mp) 189 return nil, err 190 } 191 192 // Mount the device 193 if err := d.DeviceSet.MountDevice(id, mp, mountLabel); err != nil { 194 d.ctr.Decrement(mp) 195 return nil, err 196 } 197 198 if err := idtools.MkdirAllAndChown(rootFs, 0755, idtools.IDPair{UID: uid, GID: gid}); err != nil { 199 d.ctr.Decrement(mp) 200 d.DeviceSet.UnmountDevice(id, mp) 201 return nil, err 202 } 203 204 idFile := path.Join(mp, "id") 205 if _, err := os.Stat(idFile); err != nil && os.IsNotExist(err) { 206 // Create an "id" file with the container/image id in it to help reconstruct this in case 207 // of later problems 208 if err := ioutil.WriteFile(idFile, []byte(id), 0600); err != nil { 209 d.ctr.Decrement(mp) 210 d.DeviceSet.UnmountDevice(id, mp) 211 return nil, err 212 } 213 } 214 215 return containerfs.NewLocalContainerFS(rootFs), nil 216 } 217 218 // Put unmounts a device and removes it. 219 func (d *Driver) Put(id string) error { 220 d.locker.Lock(id) 221 defer d.locker.Unlock(id) 222 mp := path.Join(d.home, "mnt", id) 223 if count := d.ctr.Decrement(mp); count > 0 { 224 return nil 225 } 226 227 err := d.DeviceSet.UnmountDevice(id, mp) 228 if err != nil { 229 logrus.Errorf("devmapper: Error unmounting device %s: %v", id, err) 230 } 231 232 return err 233 } 234 235 // Exists checks to see if the device exists. 236 func (d *Driver) Exists(id string) bool { 237 return d.DeviceSet.HasDevice(id) 238 }