Debian Codegraph @

launcher/internal/launchermount/tmpfs.go

raw
1 package launchermount
2
3 import (
4 "errors"
5 "fmt"
6 "path/filepath"
7 "strconv"
8
9 "github.com/opencontainers/runtime-spec/specs-go"
10 )
11
12 var errTmpfsMustHaveDest = errors.New("mount type \"tmpfs\" must have destination specified")
13
14 // TmpfsMount creates a launcher mount type backed by tmpfs, with an optional
15 // size. If size is not specified, it is 50% of memory.
16 // Example input: `type=tmpfs,source=tmpfs,destination=/tmpmount`
17 // `type=tmpfs,source=tmpfs,destination=/sizedtmpmount,size=123345`
18 type TmpfsMount struct {
19 // If the path is relative, it will be interpreted as relative to "/".
20 Destination string
21 // Size in bytes. No support for k, m, g suffixes.
22 Size uint64
23 }
24
25 // CreateTmpfsMount takes a map of tmpfs options, with keys defined in the spec package.
26 // Typically, this is called when creating a LaunchSpec and should not be used
27 // in other settings.
28 func CreateTmpfsMount(mountMap map[string]string) (TmpfsMount, error) {
29 if val := mountMap[TypeKey]; val != TypeTmpfs {
30 return TmpfsMount{}, fmt.Errorf("received wrong mount type %v, expected %v", val, TypeTmpfs)
31 }
32 delete(mountMap, TypeKey)
33
34 if val := mountMap[SourceKey]; val != TypeTmpfs {
35 return TmpfsMount{}, fmt.Errorf("received wrong mount source %v, expected %v", val, TypeTmpfs)
36 }
37 delete(mountMap, SourceKey)
38
39 dst := mountMap[DestinationKey]
40 if dst == "" {
41 return TmpfsMount{}, errTmpfsMustHaveDest
42 }
43 if !filepath.IsAbs(dst) {
44 dst = filepath.Join("/", dst)
45 }
46 delete(mountMap, DestinationKey)
47 mnt := TmpfsMount{Destination: dst}
48
49 szStr, ok := mountMap[SizeKey]
50 if ok {
51 sz, err := strconv.ParseUint(szStr, 10, 64)
52 if err != nil {
53 return TmpfsMount{}, fmt.Errorf("failed to convert size option \"%v\" to uint64: %v", szStr, err)
54 }
55 mnt.Size = sz
56 delete(mountMap, SizeKey)
57 }
58
59 if len(mountMap) != 0 {
60 return TmpfsMount{}, fmt.Errorf("received unknown mount options for tmpfs mount: %+v", mountMap)
61 }
62 return mnt, nil
63 }
64
65 // SpecsMount returns the OCI runtime spec Mount for the given TmpfsMount.
66 func (tm TmpfsMount) SpecsMount() specs.Mount {
67 specsMnt := specs.Mount{Type: TypeTmpfs,
68 Source: TypeTmpfs,
69 Destination: tm.Destination,
70 Options: []string{"nosuid", "noexec", "nodev"}}
71 if tm.Size != 0 {
72 specsMnt.Options = append(specsMnt.Options, fmt.Sprintf("size=%s", strconv.FormatUint(tm.Size, 10)))
73 }
74 return specsMnt
75 }
76
77 // Mountpoint gives the place in the container where the tmpfs is mounted.
78 func (tm TmpfsMount) Mountpoint() string {
79 return tm.Destination
80 }