forked from opencontainers/runc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_linux.go
370 lines (348 loc) · 11 KB
/
spec_linux.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// +build linux
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
)
type Spec struct {
PortableSpec
Linux Linux `json:"linux"`
}
type Linux struct {
UserMapping map[string]UserMapping `json:"userMapping"`
Rlimits []Rlimit `json:"rlimits"`
SystemProperties map[string]string `json:"systemProperties"`
Resources *Resources `json:"resources"`
Namespaces []Namespace `json:"namespaces"`
Capabilities []string `json:"capabilities"`
Devices []string `json:"devices"`
}
type Namespace struct {
Type string `json:"type"`
Path string `json:"path"`
}
type UserMapping struct {
From int `json:"from"`
To int `json:"to"`
Count int `json:"count"`
}
type Rlimit struct {
Type int `json:"type"`
Hard uint64 `json:"hard"`
Soft uint64 `json:"soft"`
}
type HugepageLimit struct {
Pagesize string `json:"pageSize"`
Limit int `json:"limit"`
}
type IfPrioMap struct {
Interface string `json:"interface"`
Priority int64 `json:"priority"`
}
type Resources struct {
// Memory limit (in bytes)
MemoryLimit int64 `json:"memoryLimit"`
// Memory reservation or soft_limit (in bytes)
MemoryReservation int64 `json:"memoryReservation"`
// Total memory usage (memory + swap); set `-1' to disable swap
MemorySwap int64 `json:"memorySwap"`
// Kernel memory limit (in bytes)
KernelMemory int64 `json:"kernelMemory"`
// CPU shares (relative weight vs. other containers)
CpuShares int64 `json:"cpuShares"`
// CPU hardcap limit (in usecs). Allowed cpu time in a given period.
CpuQuota int64 `json:"cpuQuota"`
// CPU period to be used for hardcapping (in usecs). 0 to use system default.
CpuPeriod int64 `json:"cpuPeriod"`
// How many time CPU will use in realtime scheduling (in usecs).
CpuRtRuntime int64 `json:"cpuQuota"`
// CPU period to be used for realtime scheduling (in usecs).
CpuRtPeriod int64 `json:"cpuPeriod"`
// CPU to use
CpusetCpus string `json:"cpusetCpus"`
// MEM to use
CpusetMems string `json:"cpusetMems"`
// IO read rate limit per cgroup per device, bytes per second.
BlkioThrottleReadBpsDevice string `json:"blkioThrottleReadBpsDevice"`
// IO write rate limit per cgroup per divice, bytes per second.
BlkioThrottleWriteBpsDevice string `json:"blkioThrottleWriteBpsDevice"`
// IO read rate limit per cgroup per device, IO per second.
BlkioThrottleReadIOpsDevice string `json:"blkioThrottleReadIopsDevice"`
// IO write rate limit per cgroup per device, IO per second.
BlkioThrottleWriteIOpsDevice string `json:"blkioThrottleWriteIopsDevice"`
// Specifies per cgroup weight, range is from 10 to 1000.
BlkioWeight int64 `json:"blkioWeight"`
// Weight per cgroup per device, can override BlkioWeight.
BlkioWeightDevice string `json:"blkioWeightDevice"`
// Hugetlb limit (in bytes)
HugetlbLimit []*HugepageLimit `json:"hugetlbLimit"`
// Whether to disable OOM Killer
DisableOOMKiller bool `json:"disableOOMKiller"`
// Set priority of network traffic for container
NetPrioIfpriomap []*IfPrioMap `json:"netPrioIfpriomap"`
// Set class identifier for container's network packets
NetClsClassid string `json:"netClsClassid"`
}
var namespaceMapping = map[string]configs.NamespaceType{
"process": configs.NEWPID,
"network": configs.NEWNET,
"mount": configs.NEWNS,
"user": configs.NEWUSER,
"ipc": configs.NEWIPC,
"uts": configs.NEWUTS,
}
// loadSpec loads the specification from the provided path.
// If the path is empty then the default path will be "config.json"
func loadSpec(path string) (*Spec, error) {
if path == "" {
path = "config.json"
}
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("JSON specification file for %s not found", path)
}
return nil, err
}
defer f.Close()
var s *Spec
if err := json.NewDecoder(f).Decode(&s); err != nil {
return nil, err
}
return s, checkSpecVersion(s)
}
// checkSpecVersion makes sure that the spec version matches runc's while we are in the initial
// development period. It is better to hard fail than have missing fields or options in the spec.
func checkSpecVersion(s *Spec) error {
if s.Version != version {
return fmt.Errorf("spec version is not compatible with runc version %q: spec %q", version, s.Version)
}
return nil
}
func createLibcontainerConfig(spec *Spec) (*configs.Config, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
rootfsPath := spec.Root.Path
if !filepath.IsAbs(rootfsPath) {
rootfsPath = filepath.Join(cwd, rootfsPath)
}
config := &configs.Config{
Rootfs: rootfsPath,
Capabilities: spec.Linux.Capabilities,
Readonlyfs: spec.Root.Readonly,
Hostname: spec.Hostname,
Privatefs: true,
}
for _, ns := range spec.Linux.Namespaces {
t, exists := namespaceMapping[ns.Type]
if !exists {
return nil, fmt.Errorf("namespace %q does not exist", ns)
}
config.Namespaces.Add(t, ns.Path)
}
for _, m := range spec.Mounts {
config.Mounts = append(config.Mounts, createLibcontainerMount(cwd, m))
}
if err := createDevices(spec, config); err != nil {
return nil, err
}
if err := setupUserNamespace(spec, config); err != nil {
return nil, err
}
c, err := createCgroupConfig(spec, config.Devices)
if err != nil {
return nil, err
}
config.Cgroups = c
if config.Readonlyfs {
setReadonly(config)
config.MaskPaths = []string{
"/proc/kcore",
}
config.ReadonlyPaths = []string{
"/proc/sys", "/proc/sysrq-trigger", "/proc/irq", "/proc/bus",
}
}
return config, nil
}
func createLibcontainerMount(cwd string, m Mount) *configs.Mount {
flags, data := parseMountOptions(m.Options)
source := m.Source
if m.Type == "bind" {
if !filepath.IsAbs(source) {
source = filepath.Join(cwd, m.Source)
}
}
return &configs.Mount{
Device: m.Type,
Source: source,
Destination: m.Destination,
Data: data,
Flags: flags,
}
}
func createCgroupConfig(spec *Spec, devices []*configs.Device) (*configs.Cgroup, error) {
myCgroupPath, err := cgroups.GetThisCgroupDir("devices")
if err != nil {
return nil, err
}
c := &configs.Cgroup{
Name: getDefaultID(),
Parent: myCgroupPath,
AllowedDevices: append(devices, allowedDevices...),
MemorySwap: -1,
MemorySwappiness: -1,
}
if r := spec.Linux.Resources; r != nil {
c.MemoryReservation = r.MemoryReservation
c.MemorySwap = r.MemorySwap
c.KernelMemory = r.KernelMemory
c.CpuShares = r.CpuShares
c.CpuQuota = r.CpuQuota
c.CpuPeriod = r.CpuPeriod
c.CpuRtRuntime = r.CpuRtRuntime
c.CpuRtPeriod = r.CpuRtPeriod
c.CpusetCpus = r.CpusetCpus
c.CpusetMems = r.CpusetMems
c.BlkioThrottleReadBpsDevice = r.BlkioThrottleReadBpsDevice
c.BlkioThrottleWriteBpsDevice = r.BlkioThrottleWriteBpsDevice
c.BlkioThrottleReadIOpsDevice = r.BlkioThrottleReadIOpsDevice
c.BlkioThrottleWriteIOpsDevice = r.BlkioThrottleWriteIOpsDevice
c.BlkioWeight = r.BlkioWeight
c.BlkioWeightDevice = r.BlkioWeightDevice
for _, l := range r.HugetlbLimit {
c.HugetlbLimit = append(c.HugetlbLimit, &configs.HugepageLimit{
Pagesize: l.Pagesize,
Limit: l.Limit,
})
}
c.OomKillDisable = r.DisableOOMKiller
for _, m := range r.NetPrioIfpriomap {
c.NetPrioIfpriomap = append(c.NetPrioIfpriomap, &configs.IfPrioMap{
Interface: m.Interface,
Priority: m.Priority,
})
}
c.NetClsClassid = r.NetClsClassid
}
return c, nil
}
func createDevices(spec *Spec, config *configs.Config) error {
for _, name := range spec.Linux.Devices {
d, err := devices.DeviceFromPath(filepath.Join("/dev", name), "rwm")
if err != nil {
return err
}
config.Devices = append(config.Devices, d)
}
return nil
}
func setReadonly(config *configs.Config) {
for _, m := range config.Mounts {
if m.Device == "sysfs" {
m.Flags |= syscall.MS_RDONLY
}
}
}
func getCPUQuota(cpus float64) int64 {
return int64(cpus * cpuQuotaMultiplyer)
}
func setupUserNamespace(spec *Spec, config *configs.Config) error {
if len(spec.Linux.UserMapping) == 0 {
return nil
}
config.Namespaces.Add(configs.NEWUSER, "")
mappings := make(map[string][]configs.IDMap)
for k, v := range spec.Linux.UserMapping {
mappings[k] = append(mappings[k], configs.IDMap{
ContainerID: v.From,
HostID: v.To,
Size: v.Count,
})
}
config.UidMappings = mappings["uid"]
config.GidMappings = mappings["gid"]
rootUid, err := config.HostUID()
if err != nil {
return err
}
rootGid, err := config.HostGID()
if err != nil {
return err
}
for _, node := range config.Devices {
node.Uid = uint32(rootUid)
node.Gid = uint32(rootGid)
}
return nil
}
// parseMountOptions parses the string and returns the flags and any mount data that
// it contains.
func parseMountOptions(options string) (int, string) {
var (
flag int
data []string
)
flags := map[string]struct {
clear bool
flag int
}{
"defaults": {false, 0},
"ro": {false, syscall.MS_RDONLY},
"rw": {true, syscall.MS_RDONLY},
"suid": {true, syscall.MS_NOSUID},
"nosuid": {false, syscall.MS_NOSUID},
"dev": {true, syscall.MS_NODEV},
"nodev": {false, syscall.MS_NODEV},
"exec": {true, syscall.MS_NOEXEC},
"noexec": {false, syscall.MS_NOEXEC},
"sync": {false, syscall.MS_SYNCHRONOUS},
"async": {true, syscall.MS_SYNCHRONOUS},
"dirsync": {false, syscall.MS_DIRSYNC},
"remount": {false, syscall.MS_REMOUNT},
"mand": {false, syscall.MS_MANDLOCK},
"nomand": {true, syscall.MS_MANDLOCK},
"atime": {true, syscall.MS_NOATIME},
"noatime": {false, syscall.MS_NOATIME},
"diratime": {true, syscall.MS_NODIRATIME},
"nodiratime": {false, syscall.MS_NODIRATIME},
"bind": {false, syscall.MS_BIND},
"rbind": {false, syscall.MS_BIND | syscall.MS_REC},
"unbindable": {false, syscall.MS_UNBINDABLE},
"runbindable": {false, syscall.MS_UNBINDABLE | syscall.MS_REC},
"private": {false, syscall.MS_PRIVATE},
"rprivate": {false, syscall.MS_PRIVATE | syscall.MS_REC},
"shared": {false, syscall.MS_SHARED},
"rshared": {false, syscall.MS_SHARED | syscall.MS_REC},
"slave": {false, syscall.MS_SLAVE},
"rslave": {false, syscall.MS_SLAVE | syscall.MS_REC},
"relatime": {false, syscall.MS_RELATIME},
"norelatime": {true, syscall.MS_RELATIME},
"strictatime": {false, syscall.MS_STRICTATIME},
"nostrictatime": {true, syscall.MS_STRICTATIME},
}
for _, o := range strings.Split(options, ",") {
// If the option does not exist in the flags table or the flag
// is not supported on the platform,
// then it is a data value for a specific fs type
if f, exists := flags[o]; exists && f.flag != 0 {
if f.clear {
flag &= ^f.flag
} else {
flag |= f.flag
}
} else {
data = append(data, o)
}
}
return flag, strings.Join(data, ",")
}