/
actions_linux.go
392 lines (335 loc) · 11 KB
/
actions_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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) 2019, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/opencontainers/runtime-tools/generate"
"github.com/sylabs/singularity/internal/pkg/util/nvidiautils"
"github.com/spf13/cobra"
"github.com/sylabs/singularity/internal/pkg/buildcfg"
"github.com/sylabs/singularity/internal/pkg/instance"
"github.com/sylabs/singularity/internal/pkg/runtime/engines/config"
"github.com/sylabs/singularity/internal/pkg/runtime/engines/config/oci"
singularityConfig "github.com/sylabs/singularity/internal/pkg/runtime/engines/singularity/config"
"github.com/sylabs/singularity/internal/pkg/security"
"github.com/sylabs/singularity/internal/pkg/sylog"
"github.com/sylabs/singularity/internal/pkg/util/env"
"github.com/sylabs/singularity/internal/pkg/util/exec"
"github.com/sylabs/singularity/internal/pkg/util/user"
)
// TODO: Let's stick this in another file so that that CLI is just CLI
func execStarter(cobraCmd *cobra.Command, image string, args []string, name string) {
targetUID := 0
targetGID := make([]int, 0)
procname := ""
uid := uint32(os.Getuid())
gid := uint32(os.Getgid())
syscall.Umask(0022)
starter := buildcfg.LIBEXECDIR + "/singularity/bin/starter-suid"
engineConfig := singularityConfig.NewConfig()
configurationFile := buildcfg.SYSCONFDIR + "/singularity/singularity.conf"
if err := config.Parser(configurationFile, engineConfig.File); err != nil {
sylog.Fatalf("Unable to parse singularity.conf file: %s", err)
}
ociConfig := &oci.Config{}
generator := generate.Generator{Config: &ociConfig.Spec}
engineConfig.OciConfig = ociConfig
generator.SetProcessArgs(args)
uidParam := security.GetParam(Security, "uid")
gidParam := security.GetParam(Security, "gid")
// handle target UID/GID for root user
if os.Getuid() == 0 && uidParam != "" {
u, err := strconv.ParseUint(uidParam, 10, 32)
if err != nil {
sylog.Fatalf("failed to parse provided UID")
}
targetUID = int(u)
uid = uint32(targetUID)
engineConfig.SetTargetUID(targetUID)
} else if uidParam != "" {
sylog.Warningf("uid security feature requires root privileges")
}
if os.Getuid() == 0 && gidParam != "" {
gids := strings.Split(gidParam, ":")
for _, id := range gids {
g, err := strconv.ParseUint(id, 10, 32)
if err != nil {
sylog.Fatalf("failed to parse provided GID")
}
targetGID = append(targetGID, int(g))
}
if len(gids) > 0 {
gid = uint32(targetGID[0])
}
engineConfig.SetTargetGID(targetGID)
} else if gidParam != "" {
sylog.Warningf("gid security feature requires root privileges")
}
if strings.HasPrefix(image, "instance://") {
instanceName := instance.ExtractName(image)
file, err := instance.Get(instanceName)
if err != nil {
sylog.Fatalf("%s", err)
}
if !file.Privileged {
UserNamespace = true
}
generator.AddProcessEnv("SINGULARITY_CONTAINER", file.Image)
generator.AddProcessEnv("SINGULARITY_NAME", filepath.Base(file.Image))
engineConfig.SetImage(image)
engineConfig.SetInstanceJoin(true)
} else {
abspath, err := filepath.Abs(image)
generator.AddProcessEnv("SINGULARITY_CONTAINER", abspath)
generator.AddProcessEnv("SINGULARITY_NAME", filepath.Base(abspath))
if err != nil {
sylog.Fatalf("Failed to determine image absolute path for %s: %s", image, err)
}
engineConfig.SetImage(abspath)
}
if !NoNvidia && (Nvidia || engineConfig.File.AlwaysUseNv) {
userPath := os.Getenv("USER_PATH")
if engineConfig.File.AlwaysUseNv {
sylog.Verbosef("'always use nv = yes' found in singularity.conf")
sylog.Verbosef("binding nvidia files into container")
}
libs, bins, err := nvidiautils.GetNvidiaPath(buildcfg.SINGULARITY_CONFDIR, userPath)
if err != nil {
sylog.Infof("Unable to capture nvidia bind points: %v", err)
} else {
if len(bins) == 0 {
sylog.Infof("Could not find any NVIDIA binaries on this host!")
} else {
if IsWritable {
sylog.Warningf("NVIDIA binaries may not be bound with --writable")
}
for _, binary := range bins {
usrBinBinary := filepath.Join("/usr/bin", filepath.Base(binary))
bind := strings.Join([]string{binary, usrBinBinary}, ":")
BindPaths = append(BindPaths, bind)
}
}
if len(libs) == 0 {
sylog.Warningf("Could not find any NVIDIA libraries on this host!")
sylog.Warningf("You may need to edit %v/nvliblist.conf", buildcfg.SINGULARITY_CONFDIR)
} else {
ContainLibsPath = append(ContainLibsPath, libs...)
}
}
}
engineConfig.SetBindPath(BindPaths)
engineConfig.SetNetwork(Network)
engineConfig.SetDNS(DNS)
engineConfig.SetNetworkArgs(NetworkArgs)
engineConfig.SetOverlayImage(OverlayPath)
engineConfig.SetWritableImage(IsWritable)
engineConfig.SetNoHome(NoHome)
engineConfig.SetNv(Nvidia)
engineConfig.SetAddCaps(AddCaps)
engineConfig.SetDropCaps(DropCaps)
engineConfig.SetAllowSUID(AllowSUID)
engineConfig.SetKeepPrivs(KeepPrivs)
engineConfig.SetNoPrivs(NoPrivs)
engineConfig.SetSecurity(Security)
engineConfig.SetShell(ShellPath)
engineConfig.SetLibrariesPath(ContainLibsPath)
if ShellPath != "" {
generator.AddProcessEnv("SINGULARITY_SHELL", ShellPath)
}
if os.Getuid() != 0 && CgroupsPath != "" {
sylog.Warningf("--apply-cgroups requires root privileges")
} else {
engineConfig.SetCgroupsPath(CgroupsPath)
}
if IsWritable && IsWritableTmpfs {
sylog.Warningf("Disabling --writable-tmpfs flag, mutually exclusive with --writable")
engineConfig.SetWritableTmpfs(false)
} else {
engineConfig.SetWritableTmpfs(IsWritableTmpfs)
}
homeFlag := cobraCmd.Flag("home")
engineConfig.SetCustomHome(homeFlag.Changed)
// set home directory for the targeted UID if it exists on host system
if !homeFlag.Changed && targetUID != 0 {
if targetUID > 500 {
if pwd, err := user.GetPwUID(uint32(targetUID)); err == nil {
sylog.Debugf("Target UID requested, set home directory to %s", pwd.Dir)
HomePath = pwd.Dir
engineConfig.SetCustomHome(true)
} else {
sylog.Verbosef("Home directory for UID %d not found, home won't be mounted", targetUID)
engineConfig.SetNoHome(true)
HomePath = "/"
}
} else {
sylog.Verbosef("System UID %d requested, home won't be mounted", targetUID)
engineConfig.SetNoHome(true)
HomePath = "/"
}
}
if Hostname != "" {
UtsNamespace = true
engineConfig.SetHostname(Hostname)
}
if IsContained || IsContainAll || IsBoot {
engineConfig.SetContain(true)
if IsContainAll {
PidNamespace = true
IpcNamespace = true
IsCleanEnv = true
}
}
engineConfig.SetScratchDir(ScratchPath)
engineConfig.SetWorkdir(WorkdirPath)
homeSlice := strings.Split(HomePath, ":")
if len(homeSlice) > 2 || len(homeSlice) == 0 {
sylog.Fatalf("home argument has incorrect number of elements: %v", len(homeSlice))
}
engineConfig.SetHomeSource(homeSlice[0])
if len(homeSlice) == 1 {
engineConfig.SetHomeDest(homeSlice[0])
} else {
engineConfig.SetHomeDest(homeSlice[1])
}
if !engineConfig.File.AllowSetuid || IsFakeroot {
UserNamespace = true
}
/* if name submitted, run as instance */
if name != "" {
PidNamespace = true
IpcNamespace = true
engineConfig.SetInstance(true)
engineConfig.SetBootInstance(IsBoot)
_, err := instance.Get(name)
if err == nil {
sylog.Fatalf("instance %s already exists", name)
}
if IsBoot {
UtsNamespace = true
NetNamespace = true
if Hostname == "" {
engineConfig.SetHostname(name)
}
if !KeepPrivs {
engineConfig.SetDropCaps("CAP_SYS_BOOT,CAP_SYS_RAWIO")
}
generator.SetProcessArgs([]string{"/sbin/init"})
}
pwd, err := user.GetPwUID(uint32(os.Getuid()))
if err != nil {
sylog.Fatalf("failed to retrieve user information for UID %d: %s", os.Getuid(), err)
}
procname = instance.ProcName(name, pwd.Name)
} else {
generator.SetProcessArgs(args)
procname = "Singularity runtime parent"
}
if NetNamespace {
generator.AddOrReplaceLinuxNamespace("network", "")
}
if UtsNamespace {
generator.AddOrReplaceLinuxNamespace("uts", "")
}
if PidNamespace {
generator.AddOrReplaceLinuxNamespace("pid", "")
engineConfig.SetNoInit(NoInit)
}
if IpcNamespace {
generator.AddOrReplaceLinuxNamespace("ipc", "")
}
if !UserNamespace {
if _, err := os.Stat(starter); os.IsNotExist(err) {
sylog.Verbosef("starter-suid not found, using user namespace")
UserNamespace = true
}
}
if UserNamespace {
generator.AddOrReplaceLinuxNamespace("user", "")
starter = buildcfg.LIBEXECDIR + "/singularity/bin/starter"
if IsFakeroot {
generator.AddLinuxUIDMapping(uid, 0, 1)
generator.AddLinuxGIDMapping(gid, 0, 1)
} else {
generator.AddLinuxUIDMapping(uid, uid, 1)
generator.AddLinuxGIDMapping(gid, gid, 1)
}
}
// Copy and cache environment
environment := os.Environ()
// Clean environment
env.SetContainerEnv(&generator, environment, IsCleanEnv, engineConfig.GetHomeDest())
// force to use getwd syscall
os.Unsetenv("PWD")
if pwd, err := os.Getwd(); err == nil {
if PwdPath != "" {
generator.SetProcessCwd(PwdPath)
} else {
if engineConfig.GetContain() {
generator.SetProcessCwd(engineConfig.GetHomeDest())
} else {
generator.SetProcessCwd(pwd)
}
}
} else {
sylog.Warningf("can't determine current working directory: %s", err)
}
Env := []string{sylog.GetEnvVar()}
generator.AddProcessEnv("SINGULARITY_APPNAME", AppName)
cfg := &config.Common{
EngineName: singularityConfig.Name,
ContainerID: name,
EngineConfig: engineConfig,
}
configData, err := json.Marshal(cfg)
if err != nil {
sylog.Fatalf("CLI Failed to marshal CommonEngineConfig: %s\n", err)
}
if engineConfig.GetInstance() {
stdout, stderr, err := instance.SetLogFile(name, int(uid))
if err != nil {
sylog.Fatalf("failed to create instance log files: %s", err)
}
start, err := stderr.Seek(0, os.SEEK_END)
if err != nil {
sylog.Warningf("failed to get standard error stream offset: %s", err)
}
cmd, err := exec.PipeCommand(starter, []string{procname}, Env, configData)
cmd.Stdout = stdout
cmd.Stderr = stderr
cmdErr := cmd.Run()
if sylog.GetLevel() != 0 {
// starter can exit a bit before all errors has been reported
// by instance process, wait a bit to catch all errors
time.Sleep(100 * time.Millisecond)
end, err := stderr.Seek(0, os.SEEK_END)
if err != nil {
sylog.Warningf("failed to get standard error stream offset: %s", err)
}
if end-start > 0 {
output := make([]byte, end-start)
stderr.ReadAt(output, start)
fmt.Println(string(output))
}
}
if cmdErr != nil {
sylog.Fatalf("failed to start instance: %s", cmdErr)
} else {
sylog.Verbosef("you will find instance output here: %s", stdout.Name())
sylog.Verbosef("you will find instance error here: %s", stderr.Name())
sylog.Infof("instance started successfully")
}
} else {
if err := exec.Pipe(starter, []string{procname}, Env, configData); err != nil {
sylog.Fatalf("%s", err)
}
}
}