forked from AliyunContainerService/pouch
/
spec_mount.go
247 lines (210 loc) · 6.07 KB
/
spec_mount.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
package mgr
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/log"
"github.com/pkg/errors"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
const (
// RPrivatePropagationMode represents mount propagation rprivate.
RPrivatePropagationMode = "rprivate"
// PrivatePropagationMode represents mount propagation private.
PrivatePropagationMode = "private"
// RSharedPropagationMode represents mount propagation rshared.
RSharedPropagationMode = "rshared"
// SharedPropagationMode represents mount propagation shared.
SharedPropagationMode = "shared"
// RSlavePropagationMode represents mount propagation rslave.
RSlavePropagationMode = "rslave"
// SlavePropagationMode represents mount propagation slave.
SlavePropagationMode = "slave"
)
func clearReadonly(m *specs.Mount) {
var opts []string
for _, o := range m.Options {
if o != "ro" {
opts = append(opts, o)
}
}
m.Options = opts
}
func overrideDefaultMount(mounts []specs.Mount, c *Container, s *specs.Spec) ([]specs.Mount, error) {
for _, sm := range s.Mounts {
dup := false
for _, cm := range c.Mounts {
if sm.Destination == cm.Destination {
dup = true
break
}
}
if dup {
continue
}
mounts = append(mounts, sm)
}
return mounts, nil
}
func mergeContainerMount(mounts []specs.Mount, c *Container, s *specs.Spec) ([]specs.Mount, error) {
for _, mp := range c.Mounts {
if trySetupNetworkMount(mp, c) {
// ignore the network mount, we will handle it later.
continue
}
// check duplicate mountpoint
for _, sm := range mounts {
if sm.Destination == mp.Destination {
return nil, fmt.Errorf("duplicate mount point: %s", mp.Destination)
}
}
pg := mp.Propagation
rootfspg := s.Linux.RootfsPropagation
// Set rootfs propagation, default setting is private.
switch pg {
case SharedPropagationMode, RSharedPropagationMode:
if rootfspg != SharedPropagationMode && rootfspg != RSharedPropagationMode {
s.Linux.RootfsPropagation = SharedPropagationMode
}
case SlavePropagationMode, RSlavePropagationMode:
if rootfspg != SharedPropagationMode && rootfspg != RSharedPropagationMode &&
rootfspg != SlavePropagationMode && rootfspg != RSlavePropagationMode {
s.Linux.RootfsPropagation = RSlavePropagationMode
}
}
opts := []string{"rbind"}
if !mp.RW {
opts = append(opts, "ro")
}
// set rprivate propagation to bind mount if pg is ""
if pg == "" {
pg = RPrivatePropagationMode
}
opts = append(opts, pg)
// TODO: support copy data.
mounts = append(mounts, specs.Mount{
Source: mp.Source,
Destination: mp.Destination,
Type: "bind",
Options: opts,
})
}
// if disable hostfiles, we will not mount the hosts files into container.
if !c.Config.DisableNetworkFiles {
mounts = append(mounts, generateNetworkMounts(c)...)
}
return mounts, nil
}
// setupMounts create mount spec.
func setupMounts(ctx context.Context, c *Container, s *specs.Spec) error {
var (
mounts []specs.Mount
err error
)
// Override the default mounts which are duplicate with user defined ones.
mounts, err = overrideDefaultMount(mounts, c, s)
if err != nil {
return errors.Wrap(err, "failed to override default spec mounts")
}
// user defined mount
mounts, err = mergeContainerMount(mounts, c, s)
if err != nil {
return errors.Wrap(err, "failed to merge container mounts")
}
// modify share memory size, and change rw mode for privileged mode.
for i := range mounts {
if mounts[i].Destination == "/dev/shm" && c.HostConfig.ShmSize != nil &&
*c.HostConfig.ShmSize != 0 {
for idx, v := range mounts[i].Options {
if strings.Contains(v, "size=") {
mounts[i].Options[idx] = fmt.Sprintf("size=%s",
strconv.FormatInt(*c.HostConfig.ShmSize, 10))
}
}
}
if c.HostConfig.Privileged {
// Clear readonly for /sys.
if mounts[i].Destination == "/sys" && !s.Root.Readonly {
clearReadonly(&mounts[i])
}
// Clear readonly for cgroup
if mounts[i].Type == "cgroup" {
clearReadonly(&mounts[i])
}
}
}
s.Mounts = sortMounts(mounts)
return nil
}
// generateNetworkMounts will generate network mounts.
func generateNetworkMounts(c *Container) []specs.Mount {
mounts := make([]specs.Mount, 0)
fileBinds := []struct {
Name string
Source string
Dest string
}{
{"HostnamePath", c.HostnamePath, "/etc/hostname"},
{"HostsPath", c.HostsPath, "/etc/hosts"},
{"ResolvConfPath", c.ResolvConfPath, "/etc/resolv.conf"},
}
for _, bind := range fileBinds {
if bind.Source != "" {
_, err := os.Stat(bind.Source)
if err != nil {
log.With(nil).Warnf("%s set to %s, but stat error: %v, skip it", bind.Name, bind.Source, err)
} else {
mounts = append(mounts, specs.Mount{
Source: bind.Source,
Destination: bind.Dest,
Type: "bind",
Options: []string{"rbind", "rprivate"},
})
}
}
}
return mounts
}
// trySetupNetworkMount will try to set network mount.
func trySetupNetworkMount(mount *types.MountPoint, c *Container) bool {
if mount.Destination == "/etc/hostname" {
c.HostnamePath = mount.Source
return true
}
if mount.Destination == "/etc/hosts" {
c.HostsPath = mount.Source
return true
}
if mount.Destination == "/etc/resolv.conf" {
c.ResolvConfPath = mount.Source
return true
}
return false
}
// mounts defines how to sort specs.Mount.
type mounts []specs.Mount
// Len returns the number of mounts.
func (m mounts) Len() int {
return len(m)
}
// Less returns true if the destination of mount i < destination of mount j
// in lexicographic order.
func (m mounts) Less(i, j int) bool {
return filepath.Clean(m[i].Destination) < filepath.Clean(m[j].Destination)
}
// Swap swaps two items in an array of mounts.
func (m mounts) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
// sortMounts sorts an array of mounts in lexicographic order. This ensure that
// the mount like /etc/resolv.conf will not mount before /etc, so /etc will
// not shadow /etc/resolv.conf
func sortMounts(m []specs.Mount) []specs.Mount {
sort.Stable(mounts(m))
return m
}