This repository has been archived by the owner on Jul 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
workload_container.go
211 lines (187 loc) · 5.82 KB
/
workload_container.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
package hcsv2
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/Microsoft/opengcs/internal/log"
"github.com/Microsoft/opengcs/internal/oc"
"github.com/opencontainers/runc/libcontainer/configs"
"github.com/opencontainers/runc/libcontainer/devices"
oci "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"go.opencensus.io/trace"
)
func getWorkloadRootDir(id string) string {
return filepath.Join("/run/gcs/c", id)
}
func updateSandboxMounts(sbid string, spec *oci.Spec) error {
sandboxMountPrefix := "sandbox://"
for i, m := range spec.Mounts {
if strings.HasPrefix(m.Source, sandboxMountPrefix) {
mountsDir := getSandboxMountsDir(sbid)
subPath := strings.TrimPrefix(m.Source, sandboxMountPrefix)
sandboxSource := filepath.Join(mountsDir, subPath)
if !strings.HasPrefix(sandboxSource, mountsDir) {
return errors.Errorf("mount path %v for mount %v is not within sandbox's mounts dir", sandboxSource, m.Source)
}
spec.Mounts[i].Source = sandboxSource
_, err := os.Stat(sandboxSource)
if os.IsNotExist(err) {
if err := os.MkdirAll(sandboxSource, 0755); err != nil {
return err
}
}
}
}
return nil
}
func specHasGPUDevice(spec *oci.Spec) bool {
for _, d := range spec.Windows.Devices {
if d.IDType == "gpu" {
return true
}
}
return false
}
func setupWorkloadContainerSpec(ctx context.Context, sbid, id string, spec *oci.Spec) (err error) {
ctx, span := trace.StartSpan(ctx, "hcsv2::setupWorkloadContainerSpec")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(
trace.StringAttribute("sandboxID", sbid),
trace.StringAttribute("cid", id))
// Verify no hostname
if spec.Hostname != "" {
return errors.Errorf("workload container must not change hostname: %s", spec.Hostname)
}
// update any sandbox mounts with the sandboxMounts directory path and create files
if err = updateSandboxMounts(sbid, spec); err != nil {
return errors.Wrapf(err, "failed to update sandbox mounts for container %v in sandbox %v", id, sbid)
}
// Add /etc/hostname if the spec did not override it.
if !isInMounts("/etc/hostname", spec.Mounts) {
mt := oci.Mount{
Destination: "/etc/hostname",
Type: "bind",
Source: getSandboxHostnamePath(sbid),
Options: []string{"bind"},
}
if isRootReadonly(spec) {
mt.Options = append(mt.Options, "ro")
}
spec.Mounts = append(spec.Mounts, mt)
}
// Add /etc/hosts if the spec did not override it.
if !isInMounts("/etc/hosts", spec.Mounts) {
mt := oci.Mount{
Destination: "/etc/hosts",
Type: "bind",
Source: getSandboxHostsPath(sbid),
Options: []string{"bind"},
}
if isRootReadonly(spec) {
mt.Options = append(mt.Options, "ro")
}
spec.Mounts = append(spec.Mounts, mt)
}
// Add /etc/resolv.conf if the spec did not override it.
if !isInMounts("/etc/resolv.conf", spec.Mounts) {
mt := oci.Mount{
Destination: "/etc/resolv.conf",
Type: "bind",
Source: getSandboxResolvPath(sbid),
Options: []string{"bind"},
}
if isRootReadonly(spec) {
mt.Options = append(mt.Options, "ro")
}
spec.Mounts = append(spec.Mounts, mt)
}
// TODO: JTERRY75 /dev/shm is not properly setup for LCOW I believe. CRI
// also has a concept of a sandbox/shm file when the IPC NamespaceMode !=
// NODE.
// Check if we need to do any capability/device mappings
if spec.Annotations["io.microsoft.virtualmachine.lcow.privileged"] == "true" {
log.G(ctx).Debug("'io.microsoft.virtualmachine.lcow.privileged' set for privileged container")
// Add all host devices
hostDevices, err := devices.HostDevices()
if err != nil {
return err
}
for _, hostDevice := range hostDevices {
addLinuxDeviceToSpec(ctx, hostDevice, spec, false)
}
// Set the cgroup access
spec.Linux.Resources.Devices = []oci.LinuxDeviceCgroup{
{
Allow: true,
Access: "rwm",
},
}
} else {
tempLinuxDevices := spec.Linux.Devices
spec.Linux.Devices = []oci.LinuxDevice{}
for _, ld := range tempLinuxDevices {
hostDevice, err := devices.DeviceFromPath(ld.Path, "rwm")
if err != nil {
return err
}
addLinuxDeviceToSpec(ctx, hostDevice, spec, true)
}
}
if userstr, ok := spec.Annotations["io.microsoft.lcow.userstr"]; ok {
if err := setUserStr(spec, userstr); err != nil {
return err
}
}
// Force the parent cgroup into our /containers root
spec.Linux.CgroupsPath = "/containers/" + id
if spec.Windows != nil && specHasGPUDevice(spec) {
// we only support Nvidia gpus right now
if err := addNvidiaDevicePreHook(ctx, spec); err != nil {
return err
}
}
// Clear the windows section as we dont want to forward to runc
spec.Windows = nil
return nil
}
func addLinuxDeviceToSpec(ctx context.Context, hostDevice *configs.Device, spec *oci.Spec, addCgroupDevice bool) {
rd := oci.LinuxDevice{
Path: hostDevice.Path,
Type: string(hostDevice.Type),
Major: hostDevice.Major,
Minor: hostDevice.Minor,
UID: &hostDevice.Uid,
GID: &hostDevice.Gid,
}
if hostDevice.Major == 0 && hostDevice.Minor == 0 {
// Invalid device, most likely a symbolic link, skip it.
return
}
found := false
for i, dev := range spec.Linux.Devices {
if dev.Path == rd.Path {
found = true
spec.Linux.Devices[i] = rd
break
}
if dev.Type == rd.Type && dev.Major == rd.Major && dev.Minor == rd.Minor {
log.G(ctx).Warnf("The same type '%s', major '%d' and minor '%d', should not be used for multiple devices.", dev.Type, dev.Major, dev.Minor)
}
}
if !found {
spec.Linux.Devices = append(spec.Linux.Devices, rd)
if addCgroupDevice {
deviceCgroup := oci.LinuxDeviceCgroup{
Allow: true,
Type: string(hostDevice.Type),
Major: &hostDevice.Major,
Minor: &hostDevice.Minor,
Access: hostDevice.Permissions,
}
spec.Linux.Resources.Devices = append(spec.Linux.Resources.Devices, deviceCgroup)
}
}
}