/
tmpfs.go
73 lines (66 loc) · 1.67 KB
/
tmpfs.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
package tmpfs
import (
"fmt"
"strings"
"github.com/docker/docker/daemon/graphdriver"
"github.com/inaccel/daemon/pkg/runtime"
"github.com/moby/sys/mount"
)
type Options struct {
Size string
NrBlocks string
NrInodes string
Mode string
GID string
UID string
Huge string
Mpol string
}
func IsMounted(path string) bool {
return graphdriver.NewFsChecker(graphdriver.FsMagicTmpFs).IsMounted(path)
}
func Mount(path string, options Options) error {
if !IsMounted(path) {
var o []string
if options.Size != "" {
o = append(o, fmt.Sprintf("size=%s", options.Size))
}
if options.NrBlocks != "" {
o = append(o, fmt.Sprintf("nr_blocks=%s", options.NrBlocks))
}
if options.NrInodes != "" {
o = append(o, fmt.Sprintf("nr_inodes=%s", options.NrInodes))
}
if options.Mode != "" {
o = append(o, fmt.Sprintf("mode=%s", options.Mode))
}
if options.GID != "" {
if runtime.LinuxVersionCode >= runtime.KernelVersion(2, 5, 7) {
o = append(o, fmt.Sprintf("gid=%s", options.GID))
}
}
if options.UID != "" {
if runtime.LinuxVersionCode >= runtime.KernelVersion(2, 5, 7) {
o = append(o, fmt.Sprintf("uid=%s", options.UID))
}
}
if options.Huge != "" {
if runtime.LinuxVersionCode >= runtime.KernelVersion(4, 7, 0) {
o = append(o, fmt.Sprintf("huge=%s", options.Huge))
}
}
if options.Mpol != "" {
if runtime.LinuxVersionCode >= runtime.KernelVersion(2, 6, 15) {
o = append(o, fmt.Sprintf("mpol=%s", options.Mpol))
}
}
return mount.Mount("tmpfs", path, "tmpfs", strings.Join(o, ","))
}
return nil
}
func Unmount(path string) error {
if IsMounted(path) {
return mount.Unmount(path)
}
return nil
}