forked from clearcontainers/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoci.go
306 lines (250 loc) · 7.83 KB
/
oci.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
// Copyright (c) 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
vc "github.com/containers/virtcontainers"
"github.com/containers/virtcontainers/pkg/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// Contants related to cgroup memory directory
const (
cgroupsTasksFile = "tasks"
cgroupsProcsFile = "cgroup.procs"
cgroupsDirMode = os.FileMode(0750)
cgroupsFileMode = os.FileMode(0640)
cgroupsMountType = "cgroup"
// Filesystem type corresponding to CGROUP_SUPER_MAGIC as listed
// here: http://man7.org/linux/man-pages/man2/statfs.2.html
cgroupFsType = 0x27e0eb
)
var (
errNeedLinuxResource = errors.New("Linux resource cannot be empty")
errPrefixContIDNotUnique = errors.New("Partial container ID not unique")
)
var cgroupsDirPath = "/sys/fs/cgroup"
// getContainerInfo returns the container status and its pod ID.
// It internally expands the container ID from the prefix provided.
// An error is returned if >1 containers are found with the specified
// prefix.
func getContainerInfo(containerID string) (vc.ContainerStatus, string, error) {
var cStatus vc.ContainerStatus
var podID string
// container ID MUST be provided.
if containerID == "" {
return vc.ContainerStatus{}, "", fmt.Errorf("Missing container ID")
}
podStatusList, err := vc.ListPod()
if err != nil {
return vc.ContainerStatus{}, "", err
}
matchFound := false
for _, podStatus := range podStatusList {
for _, containerStatus := range podStatus.ContainersStatus {
if containerStatus.ID == containerID {
return containerStatus, podStatus.ID, nil
}
if strings.HasPrefix(containerStatus.ID, containerID) {
if matchFound {
return vc.ContainerStatus{}, "", errPrefixContIDNotUnique
}
matchFound = true
cStatus = containerStatus
podID = podStatus.ID
}
}
}
if matchFound {
return cStatus, podID, nil
}
return vc.ContainerStatus{}, "", nil
}
func getExistingContainerInfo(containerID string) (vc.ContainerStatus, string, error) {
cStatus, podID, err := getContainerInfo(containerID)
if err != nil {
return vc.ContainerStatus{}, "", err
}
// container ID MUST exist.
if cStatus.ID == "" {
return vc.ContainerStatus{}, "", fmt.Errorf("Container ID does not exist")
}
return cStatus, podID, nil
}
func validCreateParams(containerID, bundlePath string) error {
// container ID MUST be provided.
if containerID == "" {
return fmt.Errorf("Missing container ID")
}
// container ID MUST be unique.
cStatus, _, err := getContainerInfo(containerID)
if err != nil {
return err
}
if cStatus.ID != "" {
return fmt.Errorf("ID already in use, unique ID should be provided")
}
// bundle path MUST be provided.
if bundlePath == "" {
return fmt.Errorf("Missing bundle path")
}
// bundle path MUST be valid.
fileInfo, err := os.Stat(bundlePath)
if err != nil {
return fmt.Errorf("Invalid bundle path '%s': %s", bundlePath, err)
}
if fileInfo.IsDir() == false {
return fmt.Errorf("Invalid bundle path '%s', it should be a directory", bundlePath)
}
return nil
}
func processRunning(pid int) (bool, error) {
process, err := os.FindProcess(pid)
if err != nil {
return false, err
}
if err := process.Signal(syscall.Signal(0)); err != nil {
return false, nil
}
return true, nil
}
func stopContainer(podID string, status vc.ContainerStatus) error {
containerType, err := oci.GetContainerType(status.Annotations)
if err != nil {
return err
}
switch containerType {
case vc.PodSandbox:
// Calling StopPod allows to make sure the pod is properly
// stopped. That way, containers/pod states are updated to
// the expected "stopped" state.
if _, err := vc.StopPod(podID); err != nil {
return err
}
case vc.PodContainer:
// Calling StopContainer allows to make sure the container is
// properly stopped and removed from the pod. That way, the
// container's state is updated to the expected "stopped" state.
if _, err := vc.StopContainer(podID, status.ID); err != nil {
return err
}
default:
return fmt.Errorf("Invalid container type found")
}
return nil
}
// processCgroupsPath process the cgroups path as expected from the
// OCI runtime specification. It returns a list of complete paths
// that should be created and used for every specified resource.
func processCgroupsPath(ociSpec oci.CompatOCISpec, isPod bool) ([]string, error) {
var cgroupsPathList []string
if ociSpec.Linux.CgroupsPath == "" {
return []string{}, nil
}
if ociSpec.Linux.Resources == nil {
return []string{}, nil
}
if ociSpec.Linux.Resources.Memory != nil {
memCgroupsPath, err := processCgroupsPathForResource(ociSpec, "memory", isPod)
if err != nil {
return []string{}, err
}
if memCgroupsPath != "" {
cgroupsPathList = append(cgroupsPathList, memCgroupsPath)
}
}
if ociSpec.Linux.Resources.CPU != nil {
cpuCgroupsPath, err := processCgroupsPathForResource(ociSpec, "cpu", isPod)
if err != nil {
return []string{}, err
}
if cpuCgroupsPath != "" {
cgroupsPathList = append(cgroupsPathList, cpuCgroupsPath)
}
}
if ociSpec.Linux.Resources.Pids != nil {
pidsCgroupsPath, err := processCgroupsPathForResource(ociSpec, "pids", isPod)
if err != nil {
return []string{}, err
}
if pidsCgroupsPath != "" {
cgroupsPathList = append(cgroupsPathList, pidsCgroupsPath)
}
}
if ociSpec.Linux.Resources.BlockIO != nil {
blkIOCgroupsPath, err := processCgroupsPathForResource(ociSpec, "blkio", isPod)
if err != nil {
return []string{}, err
}
if blkIOCgroupsPath != "" {
cgroupsPathList = append(cgroupsPathList, blkIOCgroupsPath)
}
}
return cgroupsPathList, nil
}
func processCgroupsPathForResource(ociSpec oci.CompatOCISpec, resource string, isPod bool) (string, error) {
if resource == "" {
return "", errNeedLinuxResource
}
// Relative cgroups path provided.
if filepath.IsAbs(ociSpec.Linux.CgroupsPath) == false {
return filepath.Join(cgroupsDirPath, resource, ociSpec.Linux.CgroupsPath), nil
}
// Absolute cgroups path provided.
var cgroupMount specs.Mount
cgroupMountFound := false
for _, mount := range ociSpec.Mounts {
if mount.Type == "cgroup" {
cgroupMount = mount
cgroupMountFound = true
break
}
}
if !cgroupMountFound {
if isPod {
return "", fmt.Errorf("cgroupsPath %q is absolute, cgroup mount MUST exist",
ociSpec.Linux.CgroupsPath)
}
// In case of container (CRI-O), if the mount point is not
// provided, we assume this is a relative path.
return filepath.Join(cgroupsDirPath, resource, ociSpec.Linux.CgroupsPath), nil
}
if cgroupMount.Destination == "" {
return "", fmt.Errorf("cgroupsPath is absolute, cgroup mount destination cannot be empty")
}
cgroupPath := filepath.Join(cgroupMount.Destination, resource)
// It is not an error to have this cgroup not mounted. It is usually
// due to an old kernel version with missing support for specific
// cgroups.
if !isCgroupMounted(cgroupPath) {
ccLog.Infof("cgroup path %s not mounted", cgroupPath)
return "", nil
}
ccLog.Infof("cgroup path %s mounted", cgroupPath)
return filepath.Join(cgroupPath, ociSpec.Linux.CgroupsPath), nil
}
func isCgroupMounted(cgroupPath string) bool {
var statFs syscall.Statfs_t
if err := syscall.Statfs(cgroupPath, &statFs); err != nil {
return false
}
if statFs.Type != int64(cgroupFsType) {
return false
}
return true
}