Skip to content

Commit dd5771f

Browse files
committed
Add logic for identifying device path on VMware
1 parent b990746 commit dd5771f

File tree

1 file changed

+81
-14
lines changed

1 file changed

+81
-14
lines changed

pkg/mount/mount.go

Lines changed: 81 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,23 @@ func (m *mounter) GetDevicePath(ctx context.Context, volumeID string) (string, e
115115

116116
func (m *mounter) getDevicePathBySerialID(volumeID string) (string, error) {
117117
// First try XenServer device paths
118-
for i := 'b'; i <= 'z'; i++ {
119-
devicePath := fmt.Sprintf("/dev/xvd%c", i)
120-
fmt.Printf("Checking XenServer device path: %s\n", devicePath)
121-
122-
if _, err := os.Stat(devicePath); err == nil {
123-
isBlock, err := m.IsBlockDevice(devicePath)
124-
if err == nil && isBlock {
125-
if m.verifyXenServerDevice(devicePath, volumeID) {
126-
fmt.Printf("Found and verified XenServer device: %s\n", devicePath)
127-
return devicePath, nil
128-
}
129-
}
130-
}
118+
xenDevicePath, err := m.getDevicePathForXenServer(volumeID)
119+
if err != nil {
120+
fmt.Printf("Failed to get VMware device path: %v\n", err)
121+
}
122+
if xenDevicePath != "" {
123+
return xenDevicePath, nil
131124
}
132125

133-
// Fall back to standard device paths
126+
// Try VMware device paths
127+
vmwareDevicePath, err := m.getDevicePathForVMware(volumeID)
128+
if err != nil {
129+
fmt.Printf("Failed to get VMware device path: %v\n", err)
130+
}
131+
if vmwareDevicePath != "" {
132+
return vmwareDevicePath, nil
133+
}
134+
// Fall back to standard device paths (for KVM)
134135
sourcePathPrefixes := []string{"virtio-", "scsi-", "scsi-0QEMU_QEMU_HARDDISK_"}
135136
serial := diskUUIDToSerial(volumeID)
136137
fmt.Printf("Searching for device with serial: %s\n", serial)
@@ -150,6 +151,24 @@ func (m *mounter) getDevicePathBySerialID(volumeID string) (string, error) {
150151
return "", nil
151152
}
152153

154+
func (m *mounter) getDevicePathForXenServer(volumeID string) (string, error) {
155+
for i := 'b'; i <= 'z'; i++ {
156+
devicePath := fmt.Sprintf("/dev/xvd%c", i)
157+
fmt.Printf("Checking XenServer device path: %s\n", devicePath)
158+
159+
if _, err := os.Stat(devicePath); err == nil {
160+
isBlock, err := m.IsBlockDevice(devicePath)
161+
if err == nil && isBlock {
162+
if m.verifyXenServerDevice(devicePath, volumeID) {
163+
fmt.Printf("Found and verified XenServer device: %s\n", devicePath)
164+
return devicePath, nil
165+
}
166+
}
167+
}
168+
}
169+
return "", fmt.Errorf("device not found for volume %s", volumeID)
170+
}
171+
153172
func (m *mounter) verifyXenServerDevice(devicePath string, volumeID string) bool {
154173
size, err := m.GetBlockSizeBytes(devicePath)
155174
if err != nil {
@@ -372,3 +391,51 @@ func (m *mounter) Unpublish(path string) error {
372391
func (m *mounter) Unstage(path string) error {
373392
return mount.CleanupMountPoint(path, m, true)
374393
}
394+
395+
func (m *mounter) getDevicePathForVMware(volumeID string) (string, error) {
396+
// Loop through /dev/sdb to /dev/sdz (/dev/sda -> the root disk)
397+
for i := 'b'; i <= 'z'; i++ {
398+
devicePath := fmt.Sprintf("/dev/sd%c", i)
399+
fmt.Printf("Checking VMware device path: %s\n", devicePath)
400+
401+
if _, err := os.Stat(devicePath); err == nil {
402+
isBlock, err := m.IsBlockDevice(devicePath)
403+
if err == nil && isBlock {
404+
// Use the same verification as for XenServer
405+
if m.verifyVMwareDevice(devicePath, volumeID) {
406+
fmt.Printf("Found and verified VMware device: %s\n", devicePath)
407+
return devicePath, nil
408+
}
409+
}
410+
}
411+
}
412+
return "", fmt.Errorf("device not found for volume %s", volumeID)
413+
}
414+
415+
func (m *mounter) verifyVMwareDevice(devicePath string, volumeID string) bool {
416+
size, err := m.GetBlockSizeBytes(devicePath)
417+
if err != nil {
418+
fmt.Printf("Failed to get device size: %v\n", err)
419+
return false
420+
}
421+
fmt.Printf("Device size: %d bytes\n", size)
422+
423+
mounted, err := m.isDeviceMounted(devicePath)
424+
if err != nil {
425+
fmt.Printf("Failed to check if device is mounted: %v\n", err)
426+
return false
427+
}
428+
if mounted {
429+
fmt.Printf("Device is already mounted: %s\n", devicePath)
430+
return false
431+
}
432+
433+
props, err := m.getDeviceProperties(devicePath)
434+
if err != nil {
435+
fmt.Printf("Failed to get device properties: %v\n", err)
436+
return false
437+
}
438+
fmt.Printf("Device properties: %v\n", props)
439+
440+
return true
441+
}

0 commit comments

Comments
 (0)