Skip to content

Commit

Permalink
Change the string passing for rootdisk from disk number to diskname +…
Browse files Browse the repository at this point in the history
… partition

Signed-off-by: Bella Khizgiyaev <[email protected]>
  • Loading branch information
bkhizgiy authored and ahadas committed Jun 19, 2024
1 parent d249b1a commit 155ec8a
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 48 deletions.
3 changes: 3 additions & 0 deletions operator/config/crd/bases/forklift.konveyor.io_plans.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,9 @@ spec:
The VM Namespace
Only relevant for an openshift source.
type: string
rootDisk:
description: Choose the primary disk the VM boots from
type: string
type:
description: Type used to qualify the name.
type: string
Expand Down
6 changes: 4 additions & 2 deletions pkg/apis/forklift/v1beta1/plan/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type VM struct {
// Disk decryption LUKS keys
// +optional
LUKS core.ObjectReference `json:"luks" ref:"Secret"`
// Choose the primary disk the VM boots from
// +optional
RootDisk string `json:"rootDisk,omitempty"`
}

// Find a Hook for the specified step.
Expand Down Expand Up @@ -66,8 +69,7 @@ type VMStatus struct {
Firmware string `json:"firmware,omitempty"`
// The Operating System detected by virt-v2v.
OperatingSystem string `json:"operatingSystem,omitempty"`
//Choose the primary disk the VM boots from
RootDisk string `json:"rootDisk,omitempty"`

// Conditions.
libcnd.Conditions `json:",inline"`
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/plan/adapter/vsphere/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ go_test(
"//pkg/apis/forklift/v1beta1/plan",
"//pkg/apis/forklift/v1beta1/ref",
"//pkg/controller/plan/context",
"//pkg/controller/plan/util",
"//pkg/controller/provider/model/vsphere",
"//pkg/controller/provider/web",
"//pkg/controller/provider/web/vsphere",
Expand Down
19 changes: 8 additions & 11 deletions pkg/controller/plan/adapter/vsphere/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ 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"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref"
planbase "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/base"
plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
utils "github.com/konveyor/forklift-controller/pkg/controller/plan/util"
container "github.com/konveyor/forklift-controller/pkg/controller/provider/container/vsphere"
"github.com/konveyor/forklift-controller/pkg/controller/provider/model/vsphere"
"github.com/konveyor/forklift-controller/pkg/controller/provider/web"
Expand All @@ -32,6 +31,7 @@ import (
"github.com/vmware/govmomi/vim25/types"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"
cnv "kubevirt.io/api/core/v1"
cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -662,17 +662,12 @@ func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims [
}

var bootDisk int
for _, vmConf := range r.Migration.Status.VMs {
for _, vmConf := range r.Plan.Spec.VMs {
if vmConf.ID == vmRef.ID {
var err error
bootDisk, err = strconv.Atoi(vmConf.RootDisk)
if err != nil {
bootDisk = 1
}
bootDisk = utils.GetDeviceNumber(vmConf.RootDisk)
break
}
}
bootOrder := func(order uint) *uint { return &order }

for i, disk := range disks {
pvc := pvcMap[r.baseVolume(disk.File)]
Expand All @@ -695,9 +690,11 @@ func (r *Builder) mapDisks(vm *model.VM, vmRef ref.Ref, persistentVolumeClaims [
},
},
}
// For multiboot VMs, if the selected boot device is the current disk,
// set it as the first in the boot order.
if bootDisk == i+1 {
kubevirtDisk.BootOrder = bootOrder(1)
}
kubevirtDisk.BootOrder = ptr.To(uint(1))
}
kVolumes = append(kVolumes, volume)
kDisks = append(kDisks, kubevirtDisk)
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/controller/plan/adapter/vsphere/vsphere_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package vsphere
import (
"testing"

utils "github.com/konveyor/forklift-controller/pkg/controller/plan/util"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand All @@ -11,3 +12,28 @@ func TestVsphere(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "vSphere Suite")
}

func TestGetDeviceNumber(t *testing.T) {
tests := []struct {
input string
expected int
}{
{"/dev/sda", 1},
{"/dev/sdb", 2},
{"/dev/sdz", 26},
{"/dev/sda1", 1},
{"/dev/sda5", 1},
{"/dev/sdb2", 2},
{"/dev/sdza", 26},
{"/dev/sdzb", 26},
{"/dev/sd", 0},
{"test", 0},
}

for _, test := range tests {
result := utils.GetDeviceNumber(test.input)
if result != test.expected {
t.Errorf("For input '%s', expected %d, but got %d", test.input, test.expected, result)
}
}
}
18 changes: 5 additions & 13 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1609,19 +1609,11 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume,
}

if vm.RootDisk != "" {
diskNum, errDisk := strconv.Atoi(vm.RootDisk)
if errDisk != nil {
return
}

if diskNum > 0 {

environment = append(environment,
core.EnvVar{
Name: "V2V_RootDisk",
Value: vm.RootDisk,
})
}
environment = append(environment,
core.EnvVar{
Name: "V2V_RootDisk",
Value: vm.RootDisk,
})
}
// pod annotations
annotations := map[string]string{}
Expand Down
22 changes: 22 additions & 0 deletions pkg/controller/plan/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package util

import (
"math"
"strings"
"unicode"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/settings"
Expand All @@ -14,6 +16,11 @@ const (
DefaultAlignBlockSize = 1024 * 1024
)

// RootDisk prefix for boot order.
const (
diskPrefix = "/dev/sd"
)

func roundUp(requestedSpace, multiple int64) int64 {
if multiple == 0 {
return requestedSpace
Expand All @@ -33,4 +40,19 @@ func CalculateSpaceWithOverhead(requestedSpace int64, volumeMode *core.Persisten
return spaceWithOverhead
}

func GetDeviceNumber(deviceString string) int {
if !(strings.HasPrefix(deviceString, diskPrefix) && len(deviceString) > len(diskPrefix)) {
// In case we encounter an issue detecting the root disk order,
// we will return zero to avoid failing the migration due to boot orde
return 0
}

for i := len(diskPrefix); i < len(deviceString); i++ {
if unicode.IsLetter(rune(deviceString[i])) {
return int(deviceString[i] - 'a' + 1)
}
}
return 0
}

type HostsFunc func() (map[string]*api.Host, error)
9 changes: 0 additions & 9 deletions pkg/controller/provider/container/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ 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
for _, dev := range devArray.VirtualDevice {
switch dev.(type) {
case *types.VirtualDisk:
Expand All @@ -713,7 +712,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Mode: backing.DiskMode,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -723,15 +721,13 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskFlatVer2BackingInfo:
md := model.Disk{
Key: disk.Key,
File: backing.FileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
Mode: backing.DiskMode,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -741,7 +737,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskRawDiskMappingVer1BackingInfo:
md := model.Disk{
Key: disk.Key,
Expand All @@ -750,7 +745,6 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
Shared: backing.Sharing != "sharingNone",
Mode: backing.DiskMode,
RDM: true,
//Number: diskNum,
}
if backing.Datastore != nil {
datastoreId, _ := sanitize(backing.Datastore.Value)
Expand All @@ -760,18 +754,15 @@ func (v *VmAdapter) updateDisks(devArray *types.ArrayOfVirtualDevice) {
}
}
disks = append(disks, md)
//diskNum++
case *types.VirtualDiskRawDiskVer2BackingInfo:
md := model.Disk{
Key: disk.Key,
File: backing.DescriptorFileName,
Capacity: disk.CapacityInBytes,
Shared: backing.Sharing != "sharingNone",
RDM: true,
//Number: diskNum,
}
disks = append(disks, md)
//diskNum++
}
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/controller/provider/model/vsphere/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ type Disk struct {
Shared bool `json:"shared"`
RDM bool `json:"rdm"`
Mode string `json:"mode,omitempty"`
//Number int32 `json:"number,omitempty"`
}

// Virtual Device.
Expand Down
18 changes: 6 additions & 12 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,14 @@ 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:
virtV2vArgs = append(virtV2vArgs, "--root")
if checkEnvVariablesSet("V2V_RootDisk") {
virtV2vArgs = append(virtV2vArgs, os.Getenv("V2V_RootDisk"))
} else {
virtV2vArgs = append(virtV2vArgs, "first")
}
virtV2vArgs = append(virtV2vArgs, "-i", "libvirt", "-ic", os.Getenv("V2V_libvirtURL"))
case OVA:
virtV2vArgs = append(virtV2vArgs, "-i", "ova", os.Getenv("V2V_diskPath"))
Expand Down

0 comments on commit 155ec8a

Please sign in to comment.