Skip to content

Commit 0173a3b

Browse files
committed
Improve auto-detection of the bus type of the VMware importer
Related to: harvester/harvester#7987 Signed-off-by: Volker Theile <[email protected]>
1 parent 6956a12 commit 0173a3b

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

pkg/source/vmware/client.go

+29-7
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ func (c *Client) ExportVirtualMachine(vm *migration.VirtualMachineImport) (err e
187187
i.Path = vm.Name + "-" + vm.Namespace + "-" + i.Path
188188
}
189189

190+
busType := detectBusType(i.DeviceId)
191+
190192
logrus.WithFields(logrus.Fields{
191193
"name": vm.Name,
192194
"namespace": vm.Namespace,
@@ -195,6 +197,7 @@ func (c *Client) ExportVirtualMachine(vm *migration.VirtualMachineImport) (err e
195197
"spec.sourceCluster.kind": vm.Spec.SourceCluster.Kind,
196198
"deviceId": i.DeviceId,
197199
"path": i.Path,
200+
"busType": busType,
198201
"size": i.Size,
199202
}).Info("Downloading an image")
200203

@@ -206,7 +209,7 @@ func (c *Client) ExportVirtualMachine(vm *migration.VirtualMachineImport) (err e
206209
vm.Status.DiskImportStatus = append(vm.Status.DiskImportStatus, migration.DiskInfo{
207210
Name: i.Path,
208211
DiskSize: i.Size,
209-
BusType: adapterType(i.DeviceId),
212+
BusType: busType,
210213
})
211214
} else {
212215
logrus.WithFields(logrus.Fields{
@@ -501,14 +504,33 @@ func mapNetworkCards(networkCards []networkInfo, mapping []migration.NetworkMapp
501504
return retNetwork
502505
}
503506

504-
// adapterType tries to identify the disk bus type from vmware
505-
// to attempt and set correct bus types in kubevirt
506-
// default is to switch to SATA to ensure device boots
507-
func adapterType(deviceID string) kubevirt.DiskBus {
508-
if strings.Contains(deviceID, "SCSI") {
507+
// detectBusType tries to identify the disk bus type from VMware to attempt and
508+
// set correct bus types in kubevirt.
509+
// Examples:
510+
// .-----------------------------------------------.
511+
// | Bus | Device ID |
512+
// |------|----------------------------------------|
513+
// | SCSI | /vm-13010/ParaVirtualSCSIController0:0 |
514+
// | SATA | /vm-13767/VirtualAHCIController0:1 |
515+
// | IDE | /vm-5678/VirtualIDEController1:0 |
516+
// | NVMe | /vm-2468/VirtualNVMEController0:0 |
517+
// | USB | /vm-54321/VirtualUSBController0:0 |
518+
// '-----------------------------------------------'
519+
func detectBusType(deviceID string) kubevirt.DiskBus {
520+
deviceID = strings.ToLower(deviceID)
521+
// https://kubevirt.io/api-reference/v1.1.0/definitions.html#_v1_disktarget
522+
switch {
523+
case strings.Contains(deviceID, "scsi"):
509524
return kubevirt.DiskBusSCSI
525+
case strings.Contains(deviceID, "ahci"), strings.Contains(deviceID, "sata"), strings.Contains(deviceID, "ide"):
526+
return kubevirt.DiskBusSATA
527+
case strings.Contains(deviceID, "nvme"):
528+
return kubevirt.DiskBusVirtio
529+
case strings.Contains(deviceID, "usb"):
530+
return kubevirt.DiskBusUSB
531+
default:
532+
return kubevirt.DiskBusVirtio
510533
}
511-
return kubevirt.DiskBusSATA
512534
}
513535

514536
// SanitizeVirtualMachineImport is used to sanitize the VirtualMachineImport object.

pkg/source/vmware/client_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/vmware/govmomi/vim25/mo"
1414
corev1 "k8s.io/api/core/v1"
1515
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16+
kubevirt "kubevirt.io/api/core/v1"
1617

1718
migration "github.com/harvester/vm-import-controller/pkg/apis/migration.harvesterhci.io/v1beta1"
1819
"github.com/harvester/vm-import-controller/pkg/server"
@@ -304,3 +305,48 @@ func Test_identifyNetworkCards(t *testing.T) {
304305
noMappedInfo := mapNetworkCards(networkInfo, noNetworkMapping)
305306
assert.Len(noMappedInfo, 0, "expected to find no item in the mapped networkinfo")
306307
}
308+
309+
func Test_adapterType(t *testing.T) {
310+
assert := require.New(t)
311+
testCases := []struct {
312+
desc string
313+
deviceID string
314+
expected kubevirt.DiskBus
315+
}{
316+
{
317+
desc: "SCSI disk",
318+
deviceID: "/vm-13010/ParaVirtualSCSIController0:0",
319+
expected: kubevirt.DiskBusSCSI,
320+
},
321+
{
322+
desc: "NVMe disk",
323+
deviceID: "/vm-2468/VirtualNVMEController0:0",
324+
expected: kubevirt.DiskBusVirtio,
325+
},
326+
{
327+
desc: "USB disk",
328+
deviceID: "/vm-54321/VirtualUSBController0:0",
329+
expected: kubevirt.DiskBusUSB,
330+
},
331+
{
332+
desc: "SATA disk",
333+
deviceID: "/vm-13767/VirtualAHCIController0:1",
334+
expected: kubevirt.DiskBusSATA,
335+
},
336+
{
337+
desc: "IDE disk",
338+
deviceID: "/vm-5678/VirtualIDEController1:0",
339+
expected: kubevirt.DiskBusSATA,
340+
},
341+
{
342+
desc: "Unknown disk",
343+
deviceID: "foo",
344+
expected: kubevirt.DiskBusVirtio,
345+
},
346+
}
347+
348+
for _, tc := range testCases {
349+
busType := detectBusType(tc.deviceID)
350+
assert.Equal(tc.expected, busType)
351+
}
352+
}

0 commit comments

Comments
 (0)