]> git.arvados.org - arvados.git/blob - lib/crunchrun/singularity.go
Merge branch '23009-multiselect-bug' into main. Closes #23009
[arvados.git] / lib / crunchrun / singularity.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package crunchrun
6
7 import (
8         "bytes"
9         "context"
10         "encoding/json"
11         "errors"
12         "fmt"
13         "io/ioutil"
14         "net"
15         "os"
16         "os/exec"
17         "os/user"
18         "path"
19         "regexp"
20         "sort"
21         "strconv"
22         "strings"
23         "syscall"
24         "time"
25
26         "git.arvados.org/arvados.git/sdk/go/arvados"
27 )
28
29 type singularityExecutor struct {
30         logf          func(string, ...interface{})
31         sudo          bool // use sudo to run singularity (only used by tests)
32         spec          containerSpec
33         tmpdir        string
34         child         *exec.Cmd
35         imageFilename string // "sif" image
36 }
37
38 func newSingularityExecutor(logf func(string, ...interface{})) (*singularityExecutor, error) {
39         tmpdir, err := ioutil.TempDir("", "crunch-run-singularity-")
40         if err != nil {
41                 return nil, err
42         }
43         sudo := os.Getenv("ARVADOS_TEST_PRIVESC") == "sudo" && strings.Contains(os.Args[0], "crunch-run~")
44         return &singularityExecutor{
45                 logf:   logf,
46                 tmpdir: tmpdir,
47                 sudo:   sudo,
48         }, nil
49 }
50
51 func (e *singularityExecutor) Runtime() string {
52         buf, err := exec.Command("singularity", "--version").CombinedOutput()
53         if err != nil {
54                 return "singularity (unknown version)"
55         }
56         return strings.TrimSuffix(string(buf), "\n")
57 }
58
59 func (e *singularityExecutor) getOrCreateProject(ownerUuid string, name string, containerClient *arvados.Client) (*arvados.Group, error) {
60         var gp arvados.GroupList
61         err := containerClient.RequestAndDecode(&gp,
62                 arvados.EndpointGroupList.Method,
63                 arvados.EndpointGroupList.Path,
64                 nil, arvados.ListOptions{Filters: []arvados.Filter{
65                         arvados.Filter{"owner_uuid", "=", ownerUuid},
66                         arvados.Filter{"name", "=", name},
67                         arvados.Filter{"group_class", "=", "project"},
68                 },
69                         Limit: 1})
70         if err != nil {
71                 return nil, err
72         }
73         if len(gp.Items) == 1 {
74                 return &gp.Items[0], nil
75         }
76
77         var rgroup arvados.Group
78         err = containerClient.RequestAndDecode(&rgroup,
79                 arvados.EndpointGroupCreate.Method,
80                 arvados.EndpointGroupCreate.Path,
81                 nil, map[string]interface{}{
82                         "group": map[string]string{
83                                 "owner_uuid":  ownerUuid,
84                                 "name":        name,
85                                 "group_class": "project",
86                         },
87                 })
88         if err != nil {
89                 return nil, err
90         }
91         return &rgroup, nil
92 }
93
94 func (e *singularityExecutor) getImageCacheProject(userUUID string, containerClient *arvados.Client) (*arvados.Group, error) {
95         cacheProject, err := e.getOrCreateProject(userUUID, ".cache", containerClient)
96         if err != nil {
97                 return nil, fmt.Errorf("error getting '.cache' project: %v", err)
98         }
99         imageProject, err := e.getOrCreateProject(cacheProject.UUID, "auto-generated singularity images", containerClient)
100         if err != nil {
101                 return nil, fmt.Errorf("error getting 'auto-generated singularity images' project: %s", err)
102         }
103         return imageProject, nil
104 }
105
106 func (e *singularityExecutor) imageCacheExp() time.Time {
107         return time.Now().Add(e.imageCacheTTL()).UTC()
108 }
109
110 func (e *singularityExecutor) imageCacheTTL() time.Duration {
111         return 24 * 7 * 2 * time.Hour
112 }
113
114 // getCacheCollection returns an existing collection with a cached
115 // singularity image with the given name, or nil if none exists.
116 //
117 // Note that if there is no existing collection, this is not
118 // considered an error -- all return values will be nil/empty.
119 func (e *singularityExecutor) getCacheCollection(collectionName string, containerClient *arvados.Client, cacheProject *arvados.Group, arvMountPoint string) (collection *arvados.Collection, imageFile string, err error) {
120         var cl arvados.CollectionList
121         err = containerClient.RequestAndDecode(&cl,
122                 arvados.EndpointCollectionList.Method,
123                 arvados.EndpointCollectionList.Path,
124                 nil, arvados.ListOptions{Filters: []arvados.Filter{
125                         arvados.Filter{"owner_uuid", "=", cacheProject.UUID},
126                         arvados.Filter{"name", "=", collectionName},
127                 },
128                         Limit: 1})
129         if err != nil {
130                 return nil, "", fmt.Errorf("error querying for collection %q in project %s: %w", collectionName, cacheProject.UUID, err)
131         }
132         if len(cl.Items) == 0 {
133                 // Successfully discovered that there's no cached
134                 // image collection.
135                 return nil, "", nil
136         }
137         // Check that the collection actually contains an "image.sif"
138         // file.  If not, we can't use it, and trying to create a new
139         // cache collection will probably fail too, so the caller
140         // should not bother trying.
141         coll := cl.Items[0]
142         sifFile := path.Join(arvMountPoint, "by_id", coll.PortableDataHash, "image.sif")
143         _, err = os.Stat(sifFile)
144         if err != nil {
145                 return nil, "", fmt.Errorf("found collection %s (%s), but it did not contain an image file: %s", coll.UUID, coll.PortableDataHash, err)
146         }
147         if coll.TrashAt != nil && coll.TrashAt.Sub(time.Now()) < e.imageCacheTTL()*9/10 {
148                 // If the remaining TTL is less than 90% of our target
149                 // TTL, extend trash_at.  This avoids prematurely
150                 // trashing and re-converting images that are being
151                 // used regularly.
152                 err = containerClient.RequestAndDecode(nil,
153                         arvados.EndpointCollectionUpdate.Method,
154                         "arvados/v1/collections/"+coll.UUID,
155                         nil, map[string]interface{}{
156                                 "collection": map[string]string{
157                                         "trash_at": e.imageCacheExp().Format(time.RFC3339),
158                                 },
159                         })
160                 if err != nil {
161                         e.logf("could not update expiry time of cached image collection (proceeding anyway): %s", err)
162                 }
163         }
164         return &coll, sifFile, nil
165 }
166
167 func (e *singularityExecutor) createCacheCollection(collectionName string, containerClient *arvados.Client, cacheProject *arvados.Group) (*arvados.Collection, error) {
168         var coll arvados.Collection
169         err := containerClient.RequestAndDecode(&coll,
170                 arvados.EndpointCollectionCreate.Method,
171                 arvados.EndpointCollectionCreate.Path,
172                 nil, map[string]interface{}{
173                         "collection": map[string]string{
174                                 "owner_uuid": cacheProject.UUID,
175                                 "name":       collectionName,
176                                 "trash_at":   e.imageCacheExp().Format(time.RFC3339),
177                         },
178                         "ensure_unique_name": true,
179                 })
180         if err != nil {
181                 return nil, fmt.Errorf("error creating '%v' collection: %s", collectionName, err)
182         }
183         return &coll, nil
184 }
185
186 func (e *singularityExecutor) convertDockerImage(srcPath, dstPath string) error {
187         // Make sure the docker image is readable.
188         if _, err := os.Stat(srcPath); err != nil {
189                 return err
190         }
191
192         e.logf("building singularity image")
193         // "singularity build" does not accept a
194         // docker-archive://... filename containing a ":" character,
195         // as in "/path/to/sha256:abcd...1234.tar". Workaround: make a
196         // symlink that doesn't have ":" chars.
197         err := os.Symlink(srcPath, e.tmpdir+"/image.tar")
198         if err != nil {
199                 return err
200         }
201
202         // Set up a cache and tmp dir for singularity build
203         err = os.Mkdir(e.tmpdir+"/cache", 0700)
204         if err != nil {
205                 return err
206         }
207         defer os.RemoveAll(e.tmpdir + "/cache")
208         err = os.Mkdir(e.tmpdir+"/tmp", 0700)
209         if err != nil {
210                 return err
211         }
212         defer os.RemoveAll(e.tmpdir + "/tmp")
213
214         build := exec.Command("singularity", "build", dstPath, "docker-archive://"+e.tmpdir+"/image.tar")
215         build.Env = os.Environ()
216         build.Env = append(build.Env, "SINGULARITY_CACHEDIR="+e.tmpdir+"/cache")
217         build.Env = append(build.Env, "SINGULARITY_TMPDIR="+e.tmpdir+"/tmp")
218         e.logf("%v", build.Args)
219         out, err := build.CombinedOutput()
220         // INFO:    Starting build...
221         // Getting image source signatures
222         // Copying blob ab15617702de done
223         // Copying config 651e02b8a2 done
224         // Writing manifest to image destination
225         // Storing signatures
226         // 2021/04/22 14:42:14  info unpack layer: sha256:21cbfd3a344c52b197b9fa36091e66d9cbe52232703ff78d44734f85abb7ccd3
227         // INFO:    Creating SIF file...
228         // INFO:    Build complete: arvados-jobs.latest.sif
229         e.logf("%s", out)
230         return err
231 }
232
233 // LoadImage converts the given docker image to a singularity
234 // image.
235 //
236 // If containerClient is not nil, LoadImage first tries to use an
237 // existing image (in Home -> .cache -> auto-generated singularity
238 // images) and, if none was found there and the image was converted on
239 // the fly, tries to save the converted image to the cache so it can
240 // be reused next time.
241 //
242 // If containerClient is nil or a cache project/collection cannot be
243 // found or created, LoadImage converts the image on the fly and
244 // writes it to the local filesystem instead.
245 func (e *singularityExecutor) LoadImage(dockerImageID string, imageTarballPath string, container arvados.Container, arvMountPoint string, containerClient *arvados.Client) error {
246         convertWithoutCache := func(err error) error {
247                 if err != nil {
248                         e.logf("cannot use singularity image cache: %s", err)
249                 }
250                 e.imageFilename = path.Join(e.tmpdir, "image.sif")
251                 return e.convertDockerImage(imageTarballPath, e.imageFilename)
252         }
253
254         if containerClient == nil {
255                 return convertWithoutCache(nil)
256         }
257         cacheProject, err := e.getImageCacheProject(container.RuntimeUserUUID, containerClient)
258         if err != nil {
259                 return convertWithoutCache(err)
260         }
261         cacheCollectionName := fmt.Sprintf("singularity image for %s", dockerImageID)
262         existingCollection, sifFile, err := e.getCacheCollection(cacheCollectionName, containerClient, cacheProject, arvMountPoint)
263         if err != nil {
264                 return convertWithoutCache(err)
265         }
266         if existingCollection != nil {
267                 e.imageFilename = sifFile
268                 return nil
269         }
270
271         newCollection, err := e.createCacheCollection("converting "+cacheCollectionName, containerClient, cacheProject)
272         if err != nil {
273                 return convertWithoutCache(err)
274         }
275         dstDir := path.Join(arvMountPoint, "by_uuid", newCollection.UUID)
276         dstFile := path.Join(dstDir, "image.sif")
277         err = e.convertDockerImage(imageTarballPath, dstFile)
278         if err != nil {
279                 return err
280         }
281         buf, err := os.ReadFile(path.Join(dstDir, ".arvados#collection"))
282         if err != nil {
283                 return fmt.Errorf("could not sync image collection: %w", err)
284         }
285         var synced arvados.Collection
286         err = json.Unmarshal(buf, &synced)
287         if err != nil {
288                 return fmt.Errorf("could not parse .arvados#collection: %w", err)
289         }
290         e.logf("saved converted image in %s with PDH %s", newCollection.UUID, synced.PortableDataHash)
291         e.imageFilename = path.Join(arvMountPoint, "by_id", synced.PortableDataHash, "image.sif")
292
293         if errRename := containerClient.RequestAndDecode(nil,
294                 arvados.EndpointCollectionUpdate.Method,
295                 "arvados/v1/collections/"+newCollection.UUID,
296                 nil, map[string]interface{}{
297                         "collection": map[string]string{
298                                 "name": cacheCollectionName,
299                         },
300                 }); errRename != nil {
301                 // Error is probably a name collision caused by
302                 // another crunch-run process is converting the same
303                 // image concurrently.  In that case, we prefer to use
304                 // the one that won the race -- the resulting images
305                 // should be equivalent, but if they do differ at all,
306                 // it's better if all containers use the same
307                 // conversion.
308                 if existingCollection, sifFile, err := e.getCacheCollection(cacheCollectionName, containerClient, cacheProject, arvMountPoint); err == nil {
309                         e.logf("lost race -- abandoning our conversion in %s (%s) and using image from %s (%s) instead", newCollection.UUID, synced.PortableDataHash, existingCollection.UUID, existingCollection.PortableDataHash)
310                         e.imageFilename = sifFile
311                 } else {
312                         e.logf("using newly converted image anyway, despite error renaming collection: %v", errRename)
313                 }
314         }
315         return nil
316 }
317
318 func (e *singularityExecutor) Create(spec containerSpec) error {
319         e.spec = spec
320         return nil
321 }
322
323 func (e *singularityExecutor) execCmd(path string) *exec.Cmd {
324         args := []string{path, "exec", "--containall", "--cleanenv", "--pwd=" + e.spec.WorkingDir}
325         if !e.spec.EnableNetwork {
326                 args = append(args, "--net", "--network=none")
327         } else if u, err := user.Current(); err == nil && u.Uid == "0" || e.sudo {
328                 // Specifying --network=bridge fails unless
329                 // singularity is running as root.
330                 //
331                 // Note this used to be possible with --fakeroot, or
332                 // configuring singularity like so:
333                 //
334                 // singularity config global --set 'allow net networks' bridge
335                 // singularity config global --set 'allow net groups' mygroup
336                 //
337                 // However, these options no longer work (as of debian
338                 // bookworm) because iptables now refuses to run in a
339                 // setuid environment.
340                 args = append(args, "--net", "--network=bridge")
341         } else {
342                 // If we don't pass a --net argument at all, the
343                 // container will be in the same network namespace as
344                 // the host.
345                 //
346                 // Note this allows the container to listen on the
347                 // host's external ports.
348         }
349         if e.spec.GPUStack == "cuda" && e.spec.GPUDeviceCount > 0 {
350                 args = append(args, "--nv")
351         }
352         if e.spec.GPUStack == "rocm" && e.spec.GPUDeviceCount > 0 {
353                 args = append(args, "--rocm")
354         }
355
356         // If we ask for resource limits that aren't supported,
357         // singularity will not run the container at all. So we probe
358         // for support first, and only apply the limits that appear to
359         // be supported.
360         //
361         // Default debian configuration lets non-root users set memory
362         // limits but not CPU limits, so we enable/disable those
363         // limits independently.
364         //
365         // https://rootlesscontaine.rs/getting-started/common/cgroup2/
366         checkCgroupSupport(e.logf)
367         if e.spec.VCPUs > 0 {
368                 if cgroupSupport["cpu"] {
369                         args = append(args, "--cpus", fmt.Sprintf("%d", e.spec.VCPUs))
370                 } else {
371                         e.logf("cpu limits are not supported by current systemd/cgroup configuration, not setting --cpu %d", e.spec.VCPUs)
372                 }
373         }
374         if e.spec.RAM > 0 {
375                 if cgroupSupport["memory"] {
376                         args = append(args, "--memory", fmt.Sprintf("%d", e.spec.RAM))
377                 } else {
378                         e.logf("memory limits are not supported by current systemd/cgroup configuration, not setting --memory %d", e.spec.RAM)
379                 }
380         }
381
382         readonlyflag := map[bool]string{
383                 false: "rw",
384                 true:  "ro",
385         }
386         var binds []string
387         for path, _ := range e.spec.BindMounts {
388                 binds = append(binds, path)
389         }
390         sort.Strings(binds)
391         for _, path := range binds {
392                 mount := e.spec.BindMounts[path]
393                 if path == e.spec.Env["HOME"] {
394                         // Singularity treats $HOME as special case
395                         args = append(args, "--home", mount.HostPath+":"+path)
396                 } else {
397                         args = append(args, "--bind", mount.HostPath+":"+path+":"+readonlyflag[mount.ReadOnly])
398                 }
399         }
400
401         // This is for singularity 3.5.2. There are some behaviors
402         // that will change in singularity 3.6, please see:
403         // https://sylabs.io/guides/3.7/user-guide/environment_and_metadata.html
404         // https://sylabs.io/guides/3.5/user-guide/environment_and_metadata.html
405         env := make([]string, 0, len(e.spec.Env))
406         for k, v := range e.spec.Env {
407                 if k == "HOME" {
408                         // Singularity treats $HOME as special case,
409                         // this is handled with --home above
410                         continue
411                 }
412                 env = append(env, "SINGULARITYENV_"+k+"="+v)
413         }
414
415         // Singularity always makes all nvidia devices visible to the
416         // container.  If a resource manager such as slurm or LSF told
417         // us to select specific devices we need to propagate that.
418         if cudaVisibleDevices := os.Getenv("CUDA_VISIBLE_DEVICES"); cudaVisibleDevices != "" {
419                 // If a resource manager such as slurm or LSF told
420                 // us to select specific devices we need to propagate that.
421                 env = append(env, "SINGULARITYENV_CUDA_VISIBLE_DEVICES="+cudaVisibleDevices)
422         }
423         // Singularity's default behavior is to evaluate each
424         // SINGULARITYENV_* env var with a shell as a double-quoted
425         // string and pass the result to the contained
426         // process. Singularity 3.10+ has an option to pass env vars
427         // through literally without evaluating, which is what we
428         // want. See https://github.com/sylabs/singularity/pull/704
429         // and https://dev.arvados.org/issues/19081
430         env = append(env, "SINGULARITY_NO_EVAL=1")
431
432         // If we don't propagate XDG_RUNTIME_DIR and
433         // DBUS_SESSION_BUS_ADDRESS, singularity resource limits fail
434         // with "FATAL: container creation failed: while applying
435         // cgroups config: system configuration does not support
436         // cgroup management" or "FATAL: container creation failed:
437         // while applying cgroups config: rootless cgroups require a
438         // D-Bus session - check that XDG_RUNTIME_DIR and
439         // DBUS_SESSION_BUS_ADDRESS are set".
440         env = append(env, "XDG_RUNTIME_DIR="+os.Getenv("XDG_RUNTIME_DIR"))
441         env = append(env, "DBUS_SESSION_BUS_ADDRESS="+os.Getenv("DBUS_SESSION_BUS_ADDRESS"))
442
443         args = append(args, e.imageFilename)
444         args = append(args, e.spec.Command...)
445
446         return &exec.Cmd{
447                 Path:   path,
448                 Args:   args,
449                 Env:    env,
450                 Stdin:  e.spec.Stdin,
451                 Stdout: e.spec.Stdout,
452                 Stderr: e.spec.Stderr,
453         }
454 }
455
456 func (e *singularityExecutor) Start() error {
457         path, err := exec.LookPath("singularity")
458         if err != nil {
459                 return err
460         }
461         child := e.execCmd(path)
462         if e.sudo {
463                 child.Args = append([]string{child.Path}, child.Args...)
464                 child.Path, err = exec.LookPath("sudo")
465                 if err != nil {
466                         return err
467                 }
468         }
469         err = child.Start()
470         if err != nil {
471                 return err
472         }
473         e.child = child
474         return nil
475 }
476
477 func (e *singularityExecutor) Pid() int {
478         childproc, err := e.containedProcess()
479         if err != nil {
480                 return 0
481         }
482         return childproc
483 }
484
485 func (e *singularityExecutor) Stop() error {
486         if e.child == nil || e.child.Process == nil {
487                 // no process started, or Wait already called
488                 return nil
489         }
490         if err := e.child.Process.Signal(syscall.Signal(0)); err != nil {
491                 // process already exited
492                 return nil
493         }
494         return e.child.Process.Signal(syscall.SIGKILL)
495 }
496
497 func (e *singularityExecutor) Wait(context.Context) (int, error) {
498         err := e.child.Wait()
499         if err, ok := err.(*exec.ExitError); ok {
500                 return err.ProcessState.ExitCode(), nil
501         }
502         if err != nil {
503                 return 0, err
504         }
505         return e.child.ProcessState.ExitCode(), nil
506 }
507
508 func (e *singularityExecutor) Close() {
509         err := os.RemoveAll(e.tmpdir)
510         if err != nil {
511                 e.logf("error removing temp dir: %s", err)
512         }
513 }
514
515 func (e *singularityExecutor) InjectCommand(ctx context.Context, detachKeys, username string, usingTTY bool, injectcmd []string) (*exec.Cmd, error) {
516         target, err := e.containedProcess()
517         if err != nil {
518                 return nil, err
519         }
520         return exec.CommandContext(ctx, "nsenter", append([]string{fmt.Sprintf("--target=%d", target), "--all"}, injectcmd...)...), nil
521 }
522
523 var (
524         errContainerHasNoIPAddress = errors.New("container has no IP address distinct from host")
525 )
526
527 func (e *singularityExecutor) IPAddress() (string, error) {
528         target, err := e.containedProcess()
529         if err != nil {
530                 return "", err
531         }
532         targetIPs, err := processIPs(target)
533         if err != nil {
534                 return "", err
535         }
536         selfIPs, err := processIPs(os.Getpid())
537         if err != nil {
538                 return "", err
539         }
540         for ip := range targetIPs {
541                 if !selfIPs[ip] {
542                         return ip, nil
543                 }
544         }
545         return "", errContainerHasNoIPAddress
546 }
547
548 func processIPs(pid int) (map[string]bool, error) {
549         fibtrie, err := os.ReadFile(fmt.Sprintf("/proc/%d/net/fib_trie", pid))
550         if err != nil {
551                 return nil, err
552         }
553
554         addrs := map[string]bool{}
555         // When we see a pair of lines like this:
556         //
557         //              |-- 10.1.2.3
558         //                 /32 host LOCAL
559         //
560         // ...we set addrs["10.1.2.3"] = true
561         lines := bytes.Split(fibtrie, []byte{'\n'})
562         for linenumber, line := range lines {
563                 if !bytes.HasSuffix(line, []byte("/32 host LOCAL")) {
564                         continue
565                 }
566                 if linenumber < 1 {
567                         continue
568                 }
569                 i := bytes.LastIndexByte(lines[linenumber-1], ' ')
570                 if i < 0 || i >= len(line)-7 {
571                         continue
572                 }
573                 addr := string(lines[linenumber-1][i+1:])
574                 if net.ParseIP(addr).To4() != nil {
575                         addrs[addr] = true
576                 }
577         }
578         return addrs, nil
579 }
580
581 var (
582         errContainerNotStarted = errors.New("container has not started yet")
583         errCannotFindChild     = errors.New("failed to find any process inside the container")
584         reProcStatusPPid       = regexp.MustCompile(`\nPPid:\t(\d+)\n`)
585 )
586
587 // Return the PID of a process that is inside the container (not
588 // necessarily the topmost/pid=1 process in the container).
589 func (e *singularityExecutor) containedProcess() (int, error) {
590         if e.child == nil || e.child.Process == nil {
591                 return 0, errContainerNotStarted
592         }
593         cmd := exec.Command("lsns")
594         if e.sudo {
595                 cmd = exec.Command("sudo", "lsns")
596         }
597         lsns, err := cmd.CombinedOutput()
598         if err != nil {
599                 return 0, fmt.Errorf("lsns: %w", err)
600         }
601         for _, line := range bytes.Split(lsns, []byte{'\n'}) {
602                 fields := bytes.Fields(line)
603                 if len(fields) < 4 {
604                         continue
605                 }
606                 if !bytes.Equal(fields[1], []byte("pid")) {
607                         continue
608                 }
609                 pid, err := strconv.ParseInt(string(fields[3]), 10, 64)
610                 if err != nil {
611                         return 0, fmt.Errorf("error parsing PID field in lsns output: %q", fields[3])
612                 }
613                 for parent := pid; ; {
614                         procstatus, err := os.ReadFile(fmt.Sprintf("/proc/%d/status", parent))
615                         if err != nil {
616                                 break
617                         }
618                         m := reProcStatusPPid.FindSubmatch(procstatus)
619                         if m == nil {
620                                 break
621                         }
622                         parent, err = strconv.ParseInt(string(m[1]), 10, 64)
623                         if err != nil {
624                                 break
625                         }
626                         if int(parent) == e.child.Process.Pid {
627                                 return int(pid), nil
628                         }
629                 }
630         }
631         return 0, errCannotFindChild
632 }