From d72394fbb4fe35a9a050ff69261b86ed8edfd4a8 Mon Sep 17 00:00:00 2001 From: Bella Khizgiyaev Date: Thu, 13 Jun 2024 13:27:12 +0300 Subject: [PATCH] Pass rootdisk parameter to virt-v2v and adjust boot order Signed-off-by: Bella Khizgiyaev --- .../forklift.konveyor.io_migrations.yaml | 3 +-- .../crd/bases/forklift.konveyor.io_plans.yaml | 3 +-- pkg/apis/forklift/v1beta1/plan/vm.go | 2 +- .../plan/adapter/vsphere/builder.go | 23 +++++++++++++++++-- pkg/controller/plan/kubevirt.go | 19 ++++----------- .../provider/container/vsphere/model.go | 18 +++++++-------- .../provider/model/vsphere/model.go | 2 +- virt-v2v/cold/entrypoint.go | 18 +++++++++++---- 8 files changed, 52 insertions(+), 36 deletions(-) diff --git a/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml b/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml index 5646ebb93..a6be5b8bf 100644 --- a/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml +++ b/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml @@ -510,8 +510,7 @@ spec: type: string rootDisk: description: Choose the primary disk the VM boots from - format: int32 - type: integer + type: string started: description: Started timestamp. format: date-time diff --git a/operator/config/crd/bases/forklift.konveyor.io_plans.yaml b/operator/config/crd/bases/forklift.konveyor.io_plans.yaml index 4d7b4d308..784ccbf37 100644 --- a/operator/config/crd/bases/forklift.konveyor.io_plans.yaml +++ b/operator/config/crd/bases/forklift.konveyor.io_plans.yaml @@ -1014,8 +1014,7 @@ spec: type: string rootDisk: description: Choose the primary disk the VM boots from - format: int32 - type: integer + type: string started: description: Started timestamp. format: date-time diff --git a/pkg/apis/forklift/v1beta1/plan/vm.go b/pkg/apis/forklift/v1beta1/plan/vm.go index 0069eb409..21e41c44e 100644 --- a/pkg/apis/forklift/v1beta1/plan/vm.go +++ b/pkg/apis/forklift/v1beta1/plan/vm.go @@ -67,7 +67,7 @@ type VMStatus struct { // The Operating System detected by virt-v2v. OperatingSystem string `json:"operatingSystem,omitempty"` //Choose the primary disk the VM boots from - RootDisk int32 `json:"rootDisk,omitempty"` + RootDisk string `json:"rootDisk,omitempty"` // Conditions. libcnd.Conditions `json:",inline"` } diff --git a/pkg/controller/plan/adapter/vsphere/builder.go b/pkg/controller/plan/adapter/vsphere/builder.go index 0dfae61e9..26d10a41b 100644 --- a/pkg/controller/plan/adapter/vsphere/builder.go +++ b/pkg/controller/plan/adapter/vsphere/builder.go @@ -9,6 +9,8 @@ import ( "regexp" "sort" "strings" + "strconv" + api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan" @@ -498,7 +500,7 @@ func (r *Builder) VirtualMachine(vmRef ref.Ref, object *cnv.VirtualMachineSpec, if object.Template == nil { object.Template = &cnv.VirtualMachineInstanceTemplateSpec{} } - r.mapDisks(vm, persistentVolumeClaims, object) + r.mapDisks(vm, vmRef, persistentVolumeClaims, object) r.mapFirmware(vm, object) r.mapCPU(vm, object) r.mapMemory(vm, object) @@ -635,7 +637,7 @@ func (r *Builder) mapFirmware(vm *model.VM, object *cnv.VirtualMachineSpec) { object.Template.Spec.Domain.Firmware = firmware } -func (r *Builder) mapDisks(vm *model.VM, persistentVolumeClaims []*core.PersistentVolumeClaim, object *cnv.VirtualMachineSpec) { +func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims []*core.PersistentVolumeClaim, object *cnv.VirtualMachineSpec) { var kVolumes []cnv.Volume var kDisks []cnv.Disk @@ -653,6 +655,20 @@ func (r *Builder) mapDisks(vm *model.VM, persistentVolumeClaims []*core.Persiste pvcMap[pvc.Annotations[AnnImportBackingFile]] = pvc } } + + var bootDisk int + for _, vmConf := range r.Migration.Status.VMs { + if vmConf.ID == vmRef.ID { + var err error + bootDisk, err = strconv.Atoi(vmConf.RootDisk) + if err != nil { + bootDisk = 1 + } + break + } + } + bootOrder := func(order uint) *uint { return &order } + for i, disk := range disks { pvc := pvcMap[r.baseVolume(disk.File)] volumeName := fmt.Sprintf("vol-%v", i) @@ -674,6 +690,9 @@ func (r *Builder) mapDisks(vm *model.VM, persistentVolumeClaims []*core.Persiste }, }, } + if bootDisk == i+1 { + kubevirtDisk.BootOrder = bootOrder(1) + } kVolumes = append(kVolumes, volume) kDisks = append(kDisks, kubevirtDisk) } diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index 8171df156..4ec920b0b 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -38,7 +38,6 @@ import ( "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter" ovfparser "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/ova" plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context" - model "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" libcnd "github.com/konveyor/forklift-controller/pkg/lib/condition" liberr "github.com/konveyor/forklift-controller/pkg/lib/error" core "k8s.io/api/core/v1" @@ -1610,26 +1609,18 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume, return } - if vm.RootDisk > 0 { - vmInventory := &model.VM{} - err = r.Source.Inventory.Find(vmInventory, vm.Ref) - if err != nil { - err = liberr.Wrap(err, "vm", vm.Ref.String()) + if vm.RootDisk != "" { + diskNum, errDisk := strconv.Atoi(vm.RootDisk) + if errDisk != nil { return } - var rootDisk string - for diskNum, disk := range vmInventory.Disks { - if disk.Number == vm.RootDisk { - rootDisk = fmt.Sprintf("/dev/sd%c", 'a'+(diskNum)%26) - } - } + if diskNum > 0 { - if rootDisk != "" { environment = append(environment, core.EnvVar{ Name: "V2V_RootDisk", - Value: rootDisk, + Value: vm.RootDisk, }) } } diff --git a/pkg/controller/provider/container/vsphere/model.go b/pkg/controller/provider/container/vsphere/model.go index 075b576db..a1d915dfb 100644 --- a/pkg/controller/provider/container/vsphere/model.go +++ b/pkg/controller/provider/container/vsphere/model.go @@ -701,7 +701,7 @@ func (v *VmAdapter) Apply(u types.ObjectUpdate) { // Update virtual disk devices. func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { disks := []model.Disk{} - var diskNum int32 = 1 + //var diskNum int32 = 1 for _, dev := range devArray.VirtualDevice { switch dev.(type) { case *types.VirtualDisk: @@ -713,7 +713,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { File: backing.FileName, Capacity: disk.CapacityInBytes, Mode: backing.DiskMode, - Number: diskNum, + //Number: diskNum, } if backing.Datastore != nil { datastoreId, _ := sanitize(backing.Datastore.Value) @@ -723,7 +723,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { } } disks = append(disks, md) - diskNum++ + //diskNum++ case *types.VirtualDiskFlatVer2BackingInfo: md := model.Disk{ Key: disk.Key, @@ -731,7 +731,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { Capacity: disk.CapacityInBytes, Shared: backing.Sharing != "sharingNone", Mode: backing.DiskMode, - Number: diskNum, + //Number: diskNum, } if backing.Datastore != nil { datastoreId, _ := sanitize(backing.Datastore.Value) @@ -741,7 +741,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { } } disks = append(disks, md) - diskNum++ + //diskNum++ case *types.VirtualDiskRawDiskMappingVer1BackingInfo: md := model.Disk{ Key: disk.Key, @@ -750,7 +750,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { Shared: backing.Sharing != "sharingNone", Mode: backing.DiskMode, RDM: true, - Number: diskNum, + //Number: diskNum, } if backing.Datastore != nil { datastoreId, _ := sanitize(backing.Datastore.Value) @@ -760,7 +760,7 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { } } disks = append(disks, md) - diskNum++ + //diskNum++ case *types.VirtualDiskRawDiskVer2BackingInfo: md := model.Disk{ Key: disk.Key, @@ -768,10 +768,10 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) { Capacity: disk.CapacityInBytes, Shared: backing.Sharing != "sharingNone", RDM: true, - Number: diskNum, + //Number: diskNum, } disks = append(disks, md) - diskNum++ + //diskNum++ } } } diff --git a/pkg/controller/provider/model/vsphere/model.go b/pkg/controller/provider/model/vsphere/model.go index 649d6ed2a..c33f4cdb0 100644 --- a/pkg/controller/provider/model/vsphere/model.go +++ b/pkg/controller/provider/model/vsphere/model.go @@ -279,7 +279,7 @@ type Disk struct { Shared bool `json:"shared"` RDM bool `json:"rdm"` Mode string `json:"mode,omitempty"` - Number int32 `json:"number,omitempty"` + //Number int32 `json:"number,omitempty"` } // Virtual Device. diff --git a/virt-v2v/cold/entrypoint.go b/virt-v2v/cold/entrypoint.go index 46a162b8e..124a46088 100644 --- a/virt-v2v/cold/entrypoint.go +++ b/virt-v2v/cold/entrypoint.go @@ -97,13 +97,21 @@ func buildCommand() []string { } fmt.Println("Preparing virt-v2v") + + if checkEnvVariablesSet("V2V_RootDisk") { + diskNum, err := strconv.Atoi(os.Getenv("V2V_RootDisk")) + if err != nil { + fmt.Println("Error:", err) + os.Exit(1) + } + rootDisk := fmt.Sprintf("/dev/sd%s", genName(diskNum)) + virtV2vArgs = append(virtV2vArgs, "--root", rootDisk) + } else { + virtV2vArgs = append(virtV2vArgs, "--root", "first") + } + switch source { case vSphere: - if checkEnvVariablesSet("V2V_RootDisk") { - virtV2vArgs = append(virtV2vArgs, "--root", os.Getenv("V2V_RootDisk")) - } else { - virtV2vArgs = append(virtV2vArgs, "--root", "first") - } virtV2vArgs = append(virtV2vArgs, "-i", "libvirt", "-ic", os.Getenv("V2V_libvirtURL")) case OVA: virtV2vArgs = append(virtV2vArgs, "-i", "ova", os.Getenv("V2V_diskPath"))