File: host_linux.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (55 lines) | stat: -rw-r--r-- 1,944 bytes parent folder | download
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
// Copyright (c) 2022, 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 singularity

import (
	"context"
	"fmt"
	"os"

	"github.com/sylabs/singularity/v4/internal/pkg/util/fs"
	"github.com/sylabs/singularity/v4/internal/pkg/util/fs/fuse"
	"github.com/sylabs/singularity/v4/pkg/sylog"
)

// PostStartHost cleans up a SIF FUSE image mount and the temporary directory
// that holds it. It is called from a POST_START_HOST process that exists in the
// original host namespaces.
func (e *EngineOperations) PostStartHost(ctx context.Context) (err error) {
	if e.EngineConfig.GetImageFuse() && e.EngineConfig.GetDeleteTempDir() != "" {
		return cleanFUSETempDir(ctx, e)
	}
	return nil
}

// CleanupHost cleans up a SIF FUSE image mount and the temporary directory that
// holds it. If container creation fails early, in STAGE 1, it will be called
// directly from STAGE 1. Otherwise, it will be called from a CLEANUP_HOST
// process, when the container cleanly exits, or is killed.
func (e *EngineOperations) CleanupHost(ctx context.Context) (err error) {
	tmpDir := e.EngineConfig.GetDeleteTempDir()
	if e.EngineConfig.GetImageFuse() && tmpDir != "" {
		if fs.IsDir(tmpDir) {
			return cleanFUSETempDir(ctx, e)
		}
	}
	return nil
}

func cleanFUSETempDir(ctx context.Context, e *EngineOperations) error {
	sylog.Debugf("Lazy Unmounting SIF with FUSE...")
	if err := fuse.UnmountWithFuseLazy(ctx, e.EngineConfig.GetImage()); err != nil {
		return fmt.Errorf("while unmounting fuse directory: %s: %w", e.EngineConfig.GetImage(), err)
	}
	tmpDir := e.EngineConfig.GetDeleteTempDir()
	if tmpDir != "" {
		err := os.RemoveAll(tmpDir)
		if err != nil {
			return fmt.Errorf("failed to delete container image tempDir %s: %s", tmpDir, err)
		}
	}
	return nil
}