github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/engine/volume/local/local_unix.go (about) 1 //go:build linux || freebsd 2 // +build linux freebsd 3 4 // Package local provides the default implementation for volumes. It 5 // is used to mount data volume containers and directories local to 6 // the host server. 7 package local // import "github.com/docker/docker/volume/local" 8 9 import ( 10 "fmt" 11 "net" 12 "os" 13 "path/filepath" 14 "strings" 15 "syscall" 16 "time" 17 18 "github.com/docker/docker/errdefs" 19 "github.com/docker/docker/quota" 20 units "github.com/docker/go-units" 21 "github.com/moby/sys/mount" 22 "github.com/moby/sys/mountinfo" 23 "github.com/pkg/errors" 24 ) 25 26 var ( 27 oldVfsDir = filepath.Join("vfs", "dir") 28 29 validOpts = map[string]struct{}{ 30 "type": {}, // specify the filesystem type for mount, e.g. nfs 31 "o": {}, // generic mount options 32 "device": {}, // device to mount from 33 "size": {}, // quota size limit 34 } 35 mandatoryOpts = map[string][]string{ 36 "device": {"type"}, 37 "type": {"device"}, 38 "o": {"device", "type"}, 39 } 40 ) 41 42 type optsConfig struct { 43 MountType string 44 MountOpts string 45 MountDevice string 46 Quota quota.Quota 47 } 48 49 func (o *optsConfig) String() string { 50 return fmt.Sprintf("type='%s' device='%s' o='%s' size='%d'", o.MountType, o.MountDevice, o.MountOpts, o.Quota.Size) 51 } 52 53 // scopedPath verifies that the path where the volume is located 54 // is under Docker's root and the valid local paths. 55 func (r *Root) scopedPath(realPath string) bool { 56 // Volumes path for Docker version >= 1.7 57 if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) && realPath != filepath.Join(r.scope, volumesPathName) { 58 return true 59 } 60 61 // Volumes path for Docker version < 1.7 62 if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) { 63 return true 64 } 65 66 return false 67 } 68 69 func setOpts(v *localVolume, opts map[string]string) error { 70 if len(opts) == 0 { 71 return nil 72 } 73 err := validateOpts(opts) 74 if err != nil { 75 return err 76 } 77 v.opts = &optsConfig{ 78 MountType: opts["type"], 79 MountOpts: opts["o"], 80 MountDevice: opts["device"], 81 } 82 if val, ok := opts["size"]; ok { 83 size, err := units.RAMInBytes(val) 84 if err != nil { 85 return err 86 } 87 if size > 0 && v.quotaCtl == nil { 88 return errdefs.InvalidParameter(errors.Errorf("quota size requested but no quota support")) 89 } 90 v.opts.Quota.Size = uint64(size) 91 } 92 return nil 93 } 94 95 func validateOpts(opts map[string]string) error { 96 if len(opts) == 0 { 97 return nil 98 } 99 for opt := range opts { 100 if _, ok := validOpts[opt]; !ok { 101 return errdefs.InvalidParameter(errors.Errorf("invalid option: %q", opt)) 102 } 103 } 104 for opt, reqopts := range mandatoryOpts { 105 if _, ok := opts[opt]; ok { 106 for _, reqopt := range reqopts { 107 if _, ok := opts[reqopt]; !ok { 108 return errdefs.InvalidParameter(errors.Errorf("missing required option: %q", reqopt)) 109 } 110 } 111 } 112 } 113 return nil 114 } 115 116 func unmount(path string) { 117 _ = mount.Unmount(path) 118 } 119 120 func (v *localVolume) needsMount() bool { 121 if v.opts == nil { 122 return false 123 } 124 if v.opts.MountDevice != "" || v.opts.MountType != "" { 125 return true 126 } 127 return false 128 } 129 130 func (v *localVolume) mount() error { 131 if v.opts.MountDevice == "" { 132 return fmt.Errorf("missing device in volume options") 133 } 134 mountOpts := v.opts.MountOpts 135 switch v.opts.MountType { 136 case "nfs", "cifs": 137 if addrValue := getAddress(v.opts.MountOpts); addrValue != "" && net.ParseIP(addrValue).To4() == nil { 138 ipAddr, err := net.ResolveIPAddr("ip", addrValue) 139 if err != nil { 140 return errors.Wrapf(err, "error resolving passed in network volume address") 141 } 142 mountOpts = strings.Replace(mountOpts, "addr="+addrValue, "addr="+ipAddr.String(), 1) 143 } 144 } 145 err := mount.Mount(v.opts.MountDevice, v.path, v.opts.MountType, mountOpts) 146 return errors.Wrap(err, "failed to mount local volume") 147 } 148 149 func (v *localVolume) postMount() error { 150 if v.opts == nil { 151 return nil 152 } 153 if v.opts.Quota.Size > 0 { 154 if v.quotaCtl != nil { 155 err := v.quotaCtl.SetQuota(v.path, v.opts.Quota) 156 if err != nil { 157 return err 158 } 159 } else { 160 return fmt.Errorf("size quota requested for volume but no quota support") 161 } 162 } 163 return nil 164 } 165 166 func (v *localVolume) unmount() error { 167 if v.needsMount() { 168 if err := mount.Unmount(v.path); err != nil { 169 if mounted, mErr := mountinfo.Mounted(v.path); mounted || mErr != nil { 170 return errdefs.System(err) 171 } 172 } 173 v.active.mounted = false 174 } 175 return nil 176 } 177 178 func (v *localVolume) CreatedAt() (time.Time, error) { 179 fileInfo, err := os.Stat(v.path) 180 if err != nil { 181 return time.Time{}, err 182 } 183 sec, nsec := fileInfo.Sys().(*syscall.Stat_t).Ctim.Unix() 184 return time.Unix(sec, nsec), nil 185 }