-
Notifications
You must be signed in to change notification settings - Fork 0
/
volumes.go
348 lines (289 loc) · 8.02 KB
/
volumes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package daemon
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/docker/docker/archive"
"github.com/docker/docker/daemon/execdriver"
"github.com/docker/docker/pkg/symlink"
)
type Volume struct {
HostPath string
VolPath string
Mode string
isBindMount bool
}
func (v *Volume) isRw() bool {
return v.Mode == "" || strings.ToLower(v.Mode) == "rw"
}
func (v *Volume) isDir() (bool, error) {
stat, err := os.Stat(v.HostPath)
if err != nil {
return false, err
}
return stat.IsDir(), nil
}
func prepareVolumesForContainer(container *Container) error {
if container.Volumes == nil || len(container.Volumes) == 0 {
container.Volumes = make(map[string]string)
container.VolumesRW = make(map[string]bool)
if err := applyVolumesFrom(container); err != nil {
return err
}
}
if err := createVolumes(container); err != nil {
return err
}
return nil
}
func setupMountsForContainer(container *Container) error {
mounts := []execdriver.Mount{
{container.ResolvConfPath, "/etc/resolv.conf", true, true},
}
if container.HostnamePath != "" {
mounts = append(mounts, execdriver.Mount{container.HostnamePath, "/etc/hostname", true, true})
}
if container.HostsPath != "" {
mounts = append(mounts, execdriver.Mount{container.HostsPath, "/etc/hosts", true, true})
}
// Mount user specified volumes
// Note, these are not private because you may want propagation of (un)mounts from host
// volumes. For instance if you use -v /usr:/usr and the host later mounts /usr/share you
// want this new mount in the container
for r, v := range container.Volumes {
mounts = append(mounts, execdriver.Mount{v, r, container.VolumesRW[r], false})
}
container.command.Mounts = mounts
return nil
}
func applyVolumesFrom(container *Container) error {
volumesFrom := container.hostConfig.VolumesFrom
if len(volumesFrom) > 0 {
for _, containerSpec := range volumesFrom {
var (
mountRW = true
specParts = strings.SplitN(containerSpec, ":", 2)
)
switch len(specParts) {
case 0:
return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
case 2:
switch specParts[1] {
case "ro":
mountRW = false
case "rw": // mountRW is already true
default:
return fmt.Errorf("Malformed volumes-from specification: %s", containerSpec)
}
}
c := container.daemon.Get(specParts[0])
if c == nil {
return fmt.Errorf("Container %s not found. Impossible to mount its volumes", specParts[0])
}
if err := c.Mount(); err != nil {
return fmt.Errorf("Container %s failed to mount. Impossible to mount its volumes", specParts[0])
}
defer c.Unmount()
for volPath, id := range c.Volumes {
if _, exists := container.Volumes[volPath]; exists {
continue
}
pth, err := c.getResourcePath(volPath)
if err != nil {
return err
}
stat, err := os.Stat(pth)
if err != nil {
return err
}
if err := createIfNotExists(pth, stat.IsDir()); err != nil {
return err
}
container.Volumes[volPath] = id
if isRW, exists := c.VolumesRW[volPath]; exists {
container.VolumesRW[volPath] = isRW && mountRW
}
}
}
}
return nil
}
func parseBindVolumeSpec(spec string) (Volume, error) {
var (
arr = strings.Split(spec, ":")
vol Volume
)
vol.isBindMount = true
switch len(arr) {
case 1:
vol.VolPath = spec
vol.Mode = "rw"
case 2:
vol.HostPath = arr[0]
vol.VolPath = arr[1]
vol.Mode = "rw"
case 3:
vol.HostPath = arr[0]
vol.VolPath = arr[1]
vol.Mode = arr[2]
default:
return vol, fmt.Errorf("Invalid volume specification: %s", spec)
}
if !filepath.IsAbs(vol.HostPath) {
return vol, fmt.Errorf("cannot bind mount volume: %s volume paths must be absolute.", vol.HostPath)
}
return vol, nil
}
func getBindMap(container *Container) (map[string]Volume, error) {
var (
// Create the requested bind mounts
volumes = map[string]Volume{}
// Define illegal container destinations
illegalDsts = []string{"/", "."}
)
for _, bind := range container.hostConfig.Binds {
vol, err := parseBindVolumeSpec(bind)
if err != nil {
return volumes, err
}
// Bail if trying to mount to an illegal destination
for _, illegal := range illegalDsts {
if vol.VolPath == illegal {
return nil, fmt.Errorf("Illegal bind destination: %s", vol.VolPath)
}
}
volumes[filepath.Clean(vol.VolPath)] = vol
}
return volumes, nil
}
func createVolumes(container *Container) error {
// Get all the bindmounts
volumes, err := getBindMap(container)
if err != nil {
return err
}
// Get all the rest of the volumes
for volPath := range container.Config.Volumes {
// Make sure the the volume isn't already specified as a bindmount
if _, exists := volumes[volPath]; !exists {
volumes[volPath] = Volume{
VolPath: volPath,
Mode: "rw",
isBindMount: false,
}
}
}
for _, vol := range volumes {
if err = vol.initialize(container); err != nil {
return err
}
}
return nil
}
func createVolumeHostPath(container *Container) (string, error) {
volumesDriver := container.daemon.volumes.Driver()
// Do not pass a container as the parameter for the volume creation.
// The graph driver using the container's information ( Image ) to
// create the parent.
c, err := container.daemon.volumes.Create(nil, "", "", "", "", nil, nil)
if err != nil {
return "", err
}
hostPath, err := volumesDriver.Get(c.ID, "")
if err != nil {
return hostPath, fmt.Errorf("Driver %s failed to get volume rootfs %s: %s", volumesDriver, c.ID, err)
}
return hostPath, nil
}
func (v *Volume) initialize(container *Container) error {
var err error
v.VolPath = filepath.Clean(v.VolPath)
// Do not initialize an existing volume
if _, exists := container.Volumes[v.VolPath]; exists {
return nil
}
// If it's not a bindmount we need to create the dir on the host
if !v.isBindMount {
v.HostPath, err = createVolumeHostPath(container)
if err != nil {
return err
}
}
hostPath, err := filepath.EvalSymlinks(v.HostPath)
if err != nil {
return err
}
// Create the mountpoint
// This is the path to the volume within the container FS
// This differs from `hostPath` in that `hostPath` refers to the place where
// the volume data is actually stored on the host
fullVolPath, err := symlink.FollowSymlinkInScope(filepath.Join(container.basefs, v.VolPath), container.basefs)
if err != nil {
return err
}
container.Volumes[v.VolPath] = hostPath
container.VolumesRW[v.VolPath] = v.isRw()
volIsDir, err := v.isDir()
if err != nil {
return err
}
if err := createIfNotExists(fullVolPath, volIsDir); err != nil {
return err
}
// Do not copy or change permissions if we are mounting from the host
if v.isRw() && !v.isBindMount {
return copyExistingContents(fullVolPath, hostPath)
}
return nil
}
func createIfNotExists(destination string, isDir bool) error {
if _, err := os.Stat(destination); err == nil || !os.IsNotExist(err) {
return nil
}
if isDir {
return os.MkdirAll(destination, 0755)
}
if err := os.MkdirAll(filepath.Dir(destination), 0755); err != nil {
return err
}
f, err := os.OpenFile(destination, os.O_CREATE, 0755)
if err != nil {
return err
}
f.Close()
return nil
}
func copyExistingContents(source, destination string) error {
volList, err := ioutil.ReadDir(source)
if err != nil {
return err
}
if len(volList) > 0 {
srcList, err := ioutil.ReadDir(destination)
if err != nil {
return err
}
if len(srcList) == 0 {
// If the source volume is empty copy files from the root into the volume
if err := archive.CopyWithTar(source, destination); err != nil {
return err
}
}
}
return copyOwnership(source, destination)
}
// copyOwnership copies the permissions and uid:gid of the source file
// into the destination file
func copyOwnership(source, destination string) error {
var stat syscall.Stat_t
if err := syscall.Stat(source, &stat); err != nil {
return err
}
if err := os.Chown(destination, int(stat.Uid), int(stat.Gid)); err != nil {
return err
}
return os.Chmod(destination, os.FileMode(stat.Mode))
}