forked from kata-containers/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
293 lines (248 loc) · 7.65 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// Copyright (c) 2017 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
pb "github.com/kata-containers/agent/protocols/grpc"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
grpcStatus "google.golang.org/grpc/status"
)
const (
type9pFs = "9p"
typeTmpFs = "tmpfs"
devPrefix = "/dev/"
timeoutHotplug = 3
mountPerm = os.FileMode(0755)
)
var flagList = map[string]int{
"acl": unix.MS_POSIXACL,
"bind": unix.MS_BIND,
"defaults": 0,
"dirsync": unix.MS_DIRSYNC,
"iversion": unix.MS_I_VERSION,
"lazytime": unix.MS_LAZYTIME,
"mand": unix.MS_MANDLOCK,
"noatime": unix.MS_NOATIME,
"nodev": unix.MS_NODEV,
"nodiratime": unix.MS_NODIRATIME,
"noexec": unix.MS_NOEXEC,
"nosuid": unix.MS_NOSUID,
"rbind": unix.MS_BIND | unix.MS_REC,
"relatime": unix.MS_RELATIME,
"remount": unix.MS_REMOUNT,
"ro": unix.MS_RDONLY,
"silent": unix.MS_SILENT,
"strictatime": unix.MS_STRICTATIME,
"sync": unix.MS_SYNCHRONOUS,
"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 createDestinationDir(dest string) error {
targetPath, _ := filepath.Split(dest)
return os.MkdirAll(targetPath, mountPerm)
}
// mount mounts a source in to a destination. This will do some bookkeeping:
// * evaluate all symlinks
// * ensure the source exists
func mount(source, destination, fsType string, flags int, options string) error {
var absSource string
// Log before validation. This is useful to debug cases where the gRPC
// protocol version being used by the client is out-of-sync with the
// agents version. gRPC message members are strictly ordered, so it's
// quite possible that if the protocol changes, the client may
// try to pass a valid mountpoint, but the gRPC layer may change that
// through the member ordering to be a mount *option* for example.
agentLog.WithFields(logrus.Fields{
"mount-source": source,
"mount-destination": destination,
"mount-fstype": fsType,
"mount-flags": flags,
"mount-options": options,
}).Debug()
if source == "" {
return fmt.Errorf("need mount source")
}
if destination == "" {
return fmt.Errorf("need mount destination")
}
if fsType == "" {
return fmt.Errorf("need mount FS type")
}
var err error
switch fsType {
case type9pFs:
if err = createDestinationDir(destination); err != nil {
return err
}
absSource = source
case typeTmpFs:
absSource = source
default:
absSource, err = filepath.EvalSymlinks(source)
if err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not resolve symlink for source %v", source)
}
if err = ensureDestinationExists(absSource, destination, fsType); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not create destination mount point: %v: %v",
destination, err)
}
}
if err = syscall.Mount(absSource, destination,
fsType, uintptr(flags), options); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not mount %v to %v: %v",
absSource, destination, err)
}
return nil
}
// ensureDestinationExists will recursively create a given mountpoint. If directories
// are created, their permissions are initialized to mountPerm
func ensureDestinationExists(source, destination string, fsType string) error {
fileInfo, err := os.Stat(source)
if err != nil {
return grpcStatus.Errorf(codes.Internal, "could not stat source location: %v",
source)
}
if err := createDestinationDir(destination); err != nil {
return grpcStatus.Errorf(codes.Internal, "could not create parent directory: %v",
destination)
}
if fsType != "bind" || fileInfo.IsDir() {
if err := os.Mkdir(destination, mountPerm); !os.IsExist(err) {
return err
}
} else {
file, err := os.OpenFile(destination, os.O_CREATE, mountPerm)
if err != nil {
return err
}
file.Close()
}
return nil
}
func parseMountFlagsAndOptions(optionList []string) (int, string, error) {
var (
flags int
options []string
)
for _, opt := range optionList {
flag, ok := flagList[opt]
if ok {
flags |= flag
continue
}
options = append(options, opt)
}
return flags, strings.Join(options, ","), nil
}
func removeMounts(mounts []string) error {
for _, mount := range mounts {
if err := syscall.Unmount(mount, 0); err != nil {
return err
}
}
return nil
}
// storageHandler is the type of callback to be defined to handle every
// type of storage driver.
type storageHandler func(storage pb.Storage, s *sandbox) (string, error)
// storageHandlerList lists the supported drivers.
var storageHandlerList = map[string]storageHandler{
driver9pType: virtio9pStorageHandler,
driverBlkType: virtioBlkStorageHandler,
driverSCSIType: virtioSCSIStorageHandler,
driverEphemeralType: ephemeralStorageHandler,
}
func ephemeralStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
s.Lock()
defer s.Unlock()
newStorage := s.setSandboxStorage(storage.MountPoint)
if newStorage {
var err error
if err = os.MkdirAll(storage.MountPoint, os.ModePerm); err == nil {
_, err = commonStorageHandler(storage)
}
return "", err
}
return "", nil
}
// virtio9pStorageHandler handles the storage for 9p driver.
func virtio9pStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
return commonStorageHandler(storage)
}
// virtioBlkStorageHandler handles the storage for blk driver.
func virtioBlkStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
// Get the device node path based on the PCI address provided
// in Storage Source
devPath, err := getPCIDeviceName(s, storage.Source)
if err != nil {
return "", err
}
storage.Source = devPath
return commonStorageHandler(storage)
}
// virtioSCSIStorageHandler handles the storage for scsi driver.
func virtioSCSIStorageHandler(storage pb.Storage, s *sandbox) (string, error) {
// Retrieve the device path from SCSI address.
devPath, err := getSCSIDevPath(storage.Source)
if err != nil {
return "", err
}
storage.Source = devPath
return commonStorageHandler(storage)
}
func commonStorageHandler(storage pb.Storage) (string, error) {
// Mount the storage device.
if err := mountStorage(storage); err != nil {
return "", err
}
return storage.MountPoint, nil
}
// mountStorage performs the mount described by the storage structure.
func mountStorage(storage pb.Storage) error {
flags, options, err := parseMountFlagsAndOptions(storage.Options)
if err != nil {
return err
}
return mount(storage.Source, storage.MountPoint, storage.Fstype, flags, options)
}
// addStorages takes a list of storages passed by the caller, and perform the
// associated operations such as waiting for the device to show up, and mount
// it to a specific location, according to the type of handler chosen, and for
// each storage.
func addStorages(storages []*pb.Storage, s *sandbox) ([]string, error) {
var mountList []string
for _, storage := range storages {
if storage == nil {
continue
}
devHandler, ok := storageHandlerList[storage.Driver]
if !ok {
return nil, grpcStatus.Errorf(codes.InvalidArgument,
"Unknown storage driver %q", storage.Driver)
}
mountPoint, err := devHandler(*storage, s)
if err != nil {
return nil, err
}
if mountPoint != "" {
// Prepend mount point to mount list.
mountList = append([]string{mountPoint}, mountList...)
}
}
return mountList, nil
}