-
Notifications
You must be signed in to change notification settings - Fork 241
/
mount.go
183 lines (165 loc) · 5.06 KB
/
mount.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
//go:build linux
// +build linux
package storage
import (
"bufio"
"context"
gerrors "errors"
"fmt"
"os"
"strings"
"syscall"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"golang.org/x/sys/unix"
"github.com/Microsoft/hcsshim/internal/oc"
)
const procMountFile = "/proc/mounts"
const numProcMountFields = 6
// Test dependencies
var (
osStat = os.Stat
unixUnmount = unix.Unmount
unixMount = unix.Mount
osRemoveAll = os.RemoveAll
listMounts = listMountPointsUnderPath
flags = map[string]struct {
clear bool
flag uintptr
}{
"acl": {false, unix.MS_POSIXACL},
"async": {true, unix.MS_SYNCHRONOUS},
"atime": {true, unix.MS_NOATIME},
"bind": {false, unix.MS_BIND},
"defaults": {false, 0},
"dev": {true, unix.MS_NODEV},
"diratime": {true, unix.MS_NODIRATIME},
"dirsync": {false, unix.MS_DIRSYNC},
"exec": {true, unix.MS_NOEXEC},
"iversion": {false, unix.MS_I_VERSION},
"lazytime": {false, unix.MS_LAZYTIME},
"loud": {true, unix.MS_SILENT},
"mand": {false, unix.MS_MANDLOCK},
"noacl": {true, unix.MS_POSIXACL},
"noatime": {false, unix.MS_NOATIME},
"nodev": {false, unix.MS_NODEV},
"nodiratime": {false, unix.MS_NODIRATIME},
"noexec": {false, unix.MS_NOEXEC},
"noiversion": {true, unix.MS_I_VERSION},
"nolazytime": {true, unix.MS_LAZYTIME},
"nomand": {true, unix.MS_MANDLOCK},
"norelatime": {true, unix.MS_RELATIME},
"nostrictatime": {true, unix.MS_STRICTATIME},
"nosuid": {false, unix.MS_NOSUID},
"rbind": {false, unix.MS_BIND | unix.MS_REC},
"relatime": {false, unix.MS_RELATIME},
"remount": {false, unix.MS_REMOUNT},
"ro": {false, unix.MS_RDONLY},
"rw": {true, unix.MS_RDONLY},
"silent": {false, unix.MS_SILENT},
"strictatime": {false, unix.MS_STRICTATIME},
"suid": {true, unix.MS_NOSUID},
"sync": {false, unix.MS_SYNCHRONOUS},
}
propagationFlags = map[string]uintptr{
"private": unix.MS_PRIVATE,
"shared": unix.MS_SHARED,
"slave": unix.MS_SLAVE,
"unbindable": unix.MS_UNBINDABLE,
"rprivate": unix.MS_PRIVATE | unix.MS_REC,
"rshared": unix.MS_SHARED | unix.MS_REC,
"rslave": unix.MS_SLAVE | unix.MS_REC,
"runbindable": unix.MS_UNBINDABLE | unix.MS_REC,
}
)
func ParseMountOptions(options []string) (flagOpts uintptr, pgFlags []uintptr, data []string) {
for _, o := range options {
if f, exists := flags[o]; exists && f.flag != 0 {
if f.clear {
flagOpts &= ^f.flag
} else {
flagOpts |= f.flag
}
} else if f, exists := propagationFlags[o]; exists && f != 0 {
pgFlags = append(pgFlags, f)
} else {
data = append(data, o)
}
}
return
}
// MountRShared creates a bind mountpoint and marks it as rshared
// Expected that the filepath exists before calling this function
func MountRShared(path string) error {
if path == "" {
return errors.New("path must not be empty to mount as rshared")
}
if err := unixMount(path, path, "", syscall.MS_BIND, ""); err != nil {
return fmt.Errorf("failed to create bind mount for %v: %w", path, err)
}
if err := unixMount(path, path, "", syscall.MS_SHARED|syscall.MS_REC, ""); err != nil {
return fmt.Errorf("failed to make %v rshared: %w", path, err)
}
return nil
}
// UnmountPath unmounts the target path if it exists and is a mount path. If
// removeTarget this will remove the previously mounted folder.
func UnmountPath(ctx context.Context, target string, removeTarget bool) (err error) {
_, span := oc.StartSpan(ctx, "storage::UnmountPath")
defer span.End()
defer func() { oc.SetSpanStatus(span, err) }()
span.AddAttributes(
trace.StringAttribute("target", target),
trace.BoolAttribute("remove", removeTarget))
if _, err := osStat(target); err != nil {
if os.IsNotExist(err) {
return nil
}
return errors.Wrapf(err, "failed to determine if path '%s' exists", target)
}
if err := unixUnmount(target, 0); err != nil {
// If `Unmount` returns `EINVAL` it's not mounted. Just delete the
// folder.
if !gerrors.Is(err, unix.EINVAL) {
return errors.Wrapf(err, "failed to unmount path '%s'", target)
}
}
if removeTarget {
return osRemoveAll(target)
}
return nil
}
func UnmountAllInPath(ctx context.Context, path string, removeTarget bool) (err error) {
childMounts, err := listMounts(path)
if err != nil {
return err
}
for i := len(childMounts) - 1; i >= 0; i-- {
childPath := childMounts[i]
if err := UnmountPath(ctx, childPath, removeTarget); err != nil {
return err
}
}
return nil
}
func listMountPointsUnderPath(path string) ([]string, error) {
var mountPoints []string
f, err := os.Open(procMountFile)
if err != nil {
return nil, err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, " ")
if len(fields) < numProcMountFields {
continue
}
destPath := fields[1]
if strings.HasPrefix(destPath, path) {
mountPoints = append(mountPoints, destPath)
}
}
return mountPoints, nil
}