Skip to content

Commit 4d7eb2a

Browse files
committed
Disable device when unstaging
Change-Id: I8379729f9c1914bea21b3c5dc743388a9adc2ce1
1 parent a524d2c commit 4d7eb2a

File tree

6 files changed

+72
-2
lines changed

6 files changed

+72
-2
lines changed

pkg/gce-pd-csi-driver/node.go

+7
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,13 @@ func (ns *GCENodeServer) NodeUnstageVolume(ctx context.Context, req *csi.NodeUns
364364
return nil, status.Error(codes.Internal, fmt.Sprintf("NodeUnstageVolume failed: %v\nUnmounting arguments: %s\n", err, stagingTargetPath))
365365
}
366366

367+
devicePath, err := getDevicePath(ns, volumeID, "" /* partition, which is unused */)
368+
if err != nil {
369+
klog.Errorf("Failed to find device path for volume %s. Device may not be detached cleanly (error is ignored and unstaging is continuing): %v", volumeID, err)
370+
} else if err := ns.DeviceUtils.DisableDevice(devicePath); err != nil {
371+
klog.Errorf("Failed to disabled device %s for volume %s. Device may not be detached cleanly (error is ignored and unstaging is continuing): %v", devicePath, volumeID, err)
372+
}
373+
367374
klog.V(4).Infof("NodeUnstageVolume succeeded on %v from %s", volumeID, stagingTargetPath)
368375
return &csi.NodeUnstageVolumeResponse{}, nil
369376
}

pkg/gce-pd-csi-driver/utils_windows.go

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func getDevicePath(ns *GCENodeServer, volumeID, partition string) (string, error
9393
return proxy.GetDiskNumber(deviceName, partition, volumeKey.Name)
9494
}
9595

96+
func disableDevice(devicePath string) error {
97+
// This is a no-op on windows.
98+
return nil
99+
}
100+
96101
func getBlockSizeBytes(devicePath string, m *mount.SafeFormatAndMount) (int64, error) {
97102
proxy, ok := m.Interface.(mounter.CSIProxyMounter)
98103
if !ok {

pkg/mount-manager/device-utils.go

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ type DeviceUtils interface {
8080
// VerifyDevicePath returns the first of the list of device paths that
8181
// exists on the machine, or an empty string if none exists
8282
VerifyDevicePath(devicePaths []string, deviceName string) (string, error)
83+
84+
// DisableDevice performs necessary disabling prior to a device being
85+
// detached from a node. The path is that from GetDiskByIdPaths.
86+
DisableDevice(devicePath string) error
8387
}
8488

8589
type deviceUtils struct {
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build linux
2+
3+
/*
4+
Copyright 2022 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mountmanager
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"path/filepath"
23+
)
24+
25+
func (_ *deviceUtils) DisableDevice(devicePath string) error {
26+
deviceName := filepath.Base(devicePath)
27+
return os.WriteFile(fmt.Sprintf("/sys/block/%s/device/state", deviceName), []byte("offline"), 0644)
28+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build windows
2+
3+
/*
4+
Copyright 2022 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package mountmanager
18+
19+
func (_ *deviceUtils) DisableDevice(devicePath string) error {
20+
// No disabling is necessary on windows.
21+
return nil
22+
}

pkg/mount-manager/fake-device-utils.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ func NewFakeDeviceUtils() *fakeDeviceUtils {
2525

2626
// Returns list of all /dev/disk/by-id/* paths for given PD.
2727
func (m *fakeDeviceUtils) GetDiskByIdPaths(pdName string, partition string) []string {
28-
// Don't need to implement this in the fake because we have no actual device paths
29-
return nil
28+
return []string{"/dev/disk/fake-path"}
3029
}
3130

3231
// Returns the first path that exists, or empty string if none exist.
3332
func (m *fakeDeviceUtils) VerifyDevicePath(devicePaths []string, diskName string) (string, error) {
3433
// Return any random device path to use as mount source
3534
return "/dev/disk/fake-path", nil
3635
}
36+
37+
func (_ *fakeDeviceUtils) DisableDevice(devicePath string) error {
38+
// No-op for testing.
39+
return nil
40+
}

0 commit comments

Comments
 (0)