Skip to content

Commit c86d63d

Browse files
committed
Added Release() method to the to unmount and return stub to freeStubs list
Signed-off-by: bpopovschi <[email protected]>
1 parent 13050dd commit c86d63d

File tree

6 files changed

+334
-56
lines changed

6 files changed

+334
-56
lines changed

agent/drive_handler.go

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,22 +177,11 @@ func (dh driveHandler) MountDrive(ctx context.Context, req *drivemount.MountDriv
177177
logger = logger.WithField("drive_path", drive.Path())
178178

179179
// Do a basic check that we won't be mounting over any important system directories
180-
resolvedDest, err := evalAnySymlinks(req.DestinationPath)
181-
if err != nil {
182-
return nil, errors.Wrapf(err,
183-
"failed to evaluate any symlinks in drive mount destination %q", req.DestinationPath)
184-
}
185-
186-
for _, systemDir := range bannedSystemDirs {
187-
if isOrUnderDir(resolvedDest, systemDir) {
188-
return nil, errors.Errorf(
189-
"drive mount destination %q resolves to path %q under banned system directory %q",
190-
req.DestinationPath, resolvedDest, systemDir,
191-
)
192-
}
180+
if err := isSystemDir(req.DestinationPath); err != nil {
181+
return nil, err
193182
}
194183

195-
err = os.MkdirAll(req.DestinationPath, 0700)
184+
err := os.MkdirAll(req.DestinationPath, 0700)
196185
if err != nil {
197186
return nil, errors.Wrapf(err, "failed to create drive mount destination %q", req.DestinationPath)
198187
}
@@ -229,6 +218,40 @@ func (dh driveHandler) MountDrive(ctx context.Context, req *drivemount.MountDriv
229218
drive.Path(), req.DestinationPath)
230219
}
231220

221+
func (dh driveHandler) UnmountDrive(ctx context.Context, req *drivemount.UnmountDriveRequest) (*empty.Empty, error) {
222+
drive, ok := dh.GetDrive(req.DriveID)
223+
if !ok {
224+
return nil, fmt.Errorf("drive %q could not be found", req.DriveID)
225+
}
226+
227+
err := mount.Unmount(drive.Path(), 0)
228+
if err == nil {
229+
return &empty.Empty{}, nil
230+
}
231+
232+
return nil, errors.Errorf("failed to unmount the drive %q",
233+
drive.Path())
234+
}
235+
236+
func isSystemDir(path string) error {
237+
resolvedDest, err := evalAnySymlinks(path)
238+
if err != nil {
239+
return errors.Wrapf(err,
240+
"failed to evaluate any symlinks in drive ummount destination %q", path)
241+
}
242+
243+
for _, systemDir := range bannedSystemDirs {
244+
if isOrUnderDir(resolvedDest, systemDir) {
245+
return errors.Errorf(
246+
"drive mount destination %q resolves to path %q under banned system directory %q",
247+
path, resolvedDest, systemDir,
248+
)
249+
}
250+
}
251+
252+
return nil
253+
}
254+
232255
// evalAnySymlinks is similar to filepath.EvalSymlinks, except it will not return an error if part of the
233256
// provided path does not exist. It will evaluate symlinks present in the path up to a component that doesn't
234257
// exist, at which point it will just append the rest of the provided path to what has been resolved so far.

proto/service/drivemount/drivemount.proto

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ option go_package = "drivemount";
66

77
service DriveMounter {
88
rpc MountDrive(MountDriveRequest) returns (google.protobuf.Empty);
9+
rpc UnmountDrive(UnmountDriveRequest) returns (google.protobuf.Empty);
910
}
1011

1112
message MountDriveRequest {
13+
string DriveID = 1;
14+
string DestinationPath = 2;
15+
string FilesytemType = 3;
16+
repeated string Options = 4;
17+
}
18+
19+
message UnmountDriveRequest {
1220
string DriveID = 1;
13-
string DestinationPath = 2;
14-
string FilesytemType = 3;
15-
repeated string Options = 4;
1621
}

proto/service/drivemount/ttrpc/drivemount.pb.go

Lines changed: 212 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)