Skip to content

Commit cea582b

Browse files
committed
Add support for identifying device path of attached volume on XenServer
1 parent 9ac8b75 commit cea582b

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

cmd/cloudstack-csi-driver/Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ RUN apk add --no-cache \
1414
# blkid, mount and umount are required by k8s.io/mount-utils \
1515
blkid \
1616
mount \
17-
umount
17+
umount \
18+
# Provides udevadm for device path detection
19+
udev
1820

1921
COPY ./bin/cloudstack-csi-driver /cloudstack-csi-driver
2022
ENTRYPOINT ["/cloudstack-csi-driver"]

deploy/k8s/node-daemonset.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ spec:
6464
mountPath: /dev
6565
- name: cloud-init-dir
6666
mountPath: /run/cloud-init/
67+
- name: sys-dir
68+
mountPath: /sys
6769
# Comment the above 2 lines and uncomment the next 2 lines for Ignition support
6870
# - name: ignition-dir
6971
# mountPath: /run/metadata
@@ -177,6 +179,10 @@ spec:
177179
hostPath:
178180
path: /run/cloud-init/
179181
type: Directory
182+
- name: sys-dir
183+
hostPath:
184+
path: /sys
185+
type: Directory
180186
# Comment the above 4 lines and uncomment the next 4 lines for Ignition support
181187
# - name: ignition-dir
182188
# hostPath:

pkg/mount/mount.go

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ func (m *mounter) GetBlockSizeBytes(devicePath string) (int64, error) {
8080

8181
func (m *mounter) GetDevicePath(ctx context.Context, volumeID string) (string, error) {
8282
backoff := wait.Backoff{
83-
Duration: 1 * time.Second,
84-
Factor: 1.1,
85-
Steps: 15,
83+
Duration: 2 * time.Second,
84+
Factor: 1.5,
85+
Steps: 20,
8686
}
8787

8888
var devicePath string
@@ -111,6 +111,15 @@ func (m *mounter) GetDevicePath(ctx context.Context, volumeID string) (string, e
111111
}
112112

113113
func (m *mounter) getDevicePathBySerialID(volumeID string) (string, error) {
114+
// First try XenServer device paths
115+
xenDevicePath, err := m.getDevicePathForXenServer(volumeID)
116+
if err != nil {
117+
fmt.Printf("Failed to get VMware device path: %v\n", err)
118+
}
119+
if xenDevicePath != "" {
120+
return xenDevicePath, nil
121+
}
122+
114123
// Try VMware device paths
115124
vmwareDevicePath, err := m.getDevicePathForVMware(volumeID)
116125
if err != nil {
@@ -129,13 +138,60 @@ func (m *mounter) getDevicePathBySerialID(volumeID string) (string, error) {
129138
return source, nil
130139
}
131140
if !os.IsNotExist(err) {
141+
fmt.Printf("Not found: %s\n", err.Error())
132142
return "", err
133143
}
134144
}
135145

136146
return "", nil
137147
}
138148

149+
func (m *mounter) getDevicePathForXenServer(volumeID string) (string, error) {
150+
for i := 'b'; i <= 'z'; i++ {
151+
devicePath := fmt.Sprintf("/dev/xvd%c", i)
152+
fmt.Printf("Checking XenServer device path: %s\n", devicePath)
153+
154+
if _, err := os.Stat(devicePath); err == nil {
155+
isBlock, err := m.IsBlockDevice(devicePath)
156+
if err == nil && isBlock {
157+
if m.verifyXenServerDevice(devicePath, volumeID) {
158+
fmt.Printf("Found and verified XenServer device: %s\n", devicePath)
159+
return devicePath, nil
160+
}
161+
}
162+
}
163+
}
164+
return "", fmt.Errorf("device not found for volume %s", volumeID)
165+
}
166+
167+
func (m *mounter) verifyXenServerDevice(devicePath string, volumeID string) bool {
168+
size, err := m.GetBlockSizeBytes(devicePath)
169+
if err != nil {
170+
fmt.Printf("Failed to get device size: %v\n", err)
171+
return false
172+
}
173+
fmt.Printf("Device size: %d bytes\n", size)
174+
175+
mounted, err := m.isDeviceMounted(devicePath)
176+
if err != nil {
177+
fmt.Printf("Failed to check if device is mounted: %v\n", err)
178+
return false
179+
}
180+
if mounted {
181+
fmt.Printf("Device is already mounted: %s\n", devicePath)
182+
return false
183+
}
184+
185+
props, err := m.getDeviceProperties(devicePath)
186+
if err != nil {
187+
fmt.Printf("Failed to get device properties: %v\n", err)
188+
return false
189+
}
190+
fmt.Printf("Device properties: %v\n", props)
191+
192+
return true
193+
}
194+
139195
func (m *mounter) getDevicePathForVMware(volumeID string) (string, error) {
140196
// Loop through /dev/sdb to /dev/sdz (/dev/sda -> the root disk)
141197
for i := 'b'; i <= 'z'; i++ {

0 commit comments

Comments
 (0)