diff --git a/go.mod b/go.mod index a3e0ee1bc..43f70d179 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( kubevirt.io/containerized-data-importer-api v1.59.0 libvirt.org/libvirt-go-xml v7.4.0+incompatible sigs.k8s.io/controller-runtime v0.16.3 + sigs.k8s.io/yaml v1.3.0 ) require ( @@ -115,7 +116,6 @@ require ( kubevirt.io/controller-lifecycle-operator-sdk/api v0.0.0-20220329064328-f3cc58c6ed90 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect - sigs.k8s.io/yaml v1.3.0 // indirect ) replace github.com/gophercloud/gophercloud => github.com/kubev2v/gophercloud v0.0.0-20230629135522-9d701a75c760 diff --git a/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml b/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml index fc1f38d66..ff0554bc6 100644 --- a/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml +++ b/operator/config/crd/bases/forklift.konveyor.io_migrations.yaml @@ -377,6 +377,9 @@ spec: The VM Namespace Only relevant for an openshift source. type: string + newName: + description: The new name of the VM after matching DNS1123 requirements. + type: string operatingSystem: description: The Operating System detected by virt-v2v. type: string diff --git a/operator/config/crd/bases/forklift.konveyor.io_plans.yaml b/operator/config/crd/bases/forklift.konveyor.io_plans.yaml index 1a47659d9..2c19fb8ca 100644 --- a/operator/config/crd/bases/forklift.konveyor.io_plans.yaml +++ b/operator/config/crd/bases/forklift.konveyor.io_plans.yaml @@ -888,6 +888,10 @@ spec: The VM Namespace Only relevant for an openshift source. type: string + newName: + description: The new name of the VM after matching DNS1123 + requirements. + type: string operatingSystem: description: The Operating System detected by virt-v2v. type: string diff --git a/pkg/apis/forklift/v1beta1/plan/vm.go b/pkg/apis/forklift/v1beta1/plan/vm.go index a92820add..45f5c8b3b 100644 --- a/pkg/apis/forklift/v1beta1/plan/vm.go +++ b/pkg/apis/forklift/v1beta1/plan/vm.go @@ -72,6 +72,8 @@ type VMStatus struct { Firmware string `json:"firmware,omitempty"` // The Operating System detected by virt-v2v. OperatingSystem string `json:"operatingSystem,omitempty"` + // The new name of the VM after matching DNS1123 requirements. + NewName string `json:"newName,omitempty"` // Conditions. libcnd.Conditions `json:",inline"` diff --git a/pkg/controller/plan/BUILD.bazel b/pkg/controller/plan/BUILD.bazel index 251295330..ce4fc4458 100644 --- a/pkg/controller/plan/BUILD.bazel +++ b/pkg/controller/plan/BUILD.bazel @@ -22,11 +22,11 @@ go_library( "//pkg/controller/base", "//pkg/controller/plan/adapter", "//pkg/controller/plan/adapter/base", - "//pkg/controller/plan/adapter/ova", "//pkg/controller/plan/adapter/vsphere", "//pkg/controller/plan/context", "//pkg/controller/plan/handler", "//pkg/controller/plan/scheduler", + "//pkg/controller/plan/util", "//pkg/controller/provider/web", "//pkg/controller/provider/web/vsphere", "//pkg/controller/validation", diff --git a/pkg/controller/plan/kubevirt.go b/pkg/controller/plan/kubevirt.go index ac45c6954..46cc4d1e4 100644 --- a/pkg/controller/plan/kubevirt.go +++ b/pkg/controller/plan/kubevirt.go @@ -16,6 +16,7 @@ import ( "time" planbase "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/base" + "github.com/konveyor/forklift-controller/pkg/controller/plan/util" "github.com/konveyor/forklift-controller/pkg/controller/provider/web" model "github.com/konveyor/forklift-controller/pkg/controller/provider/web/vsphere" libref "github.com/konveyor/forklift-controller/pkg/lib/ref" @@ -28,7 +29,6 @@ import ( "k8s.io/apimachinery/pkg/conversion" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" - k8svalidation "k8s.io/apimachinery/pkg/util/validation" cnv "kubevirt.io/api/core/v1" instancetypeapi "kubevirt.io/api/instancetype" instancetype "kubevirt.io/api/instancetype/v1beta1" @@ -38,7 +38,6 @@ import ( "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan" "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref" "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter" - ovfparser "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/ova" inspectionparser "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/vsphere" plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context" libcnd "github.com/konveyor/forklift-controller/pkg/lib/condition" @@ -961,7 +960,8 @@ func (r *KubeVirt) UpdateVmByConvertedConfig(vm *plan.VMStatus, pod *core.Pod, s return nil } - url := fmt.Sprintf("http://%s:8080/ovf", pod.Status.PodIP) + url := fmt.Sprintf("http://%s:8080/vm", pod.Status.PodIP) + /* Due to the virt-v2v operation, the ovf file is only available after the command's execution, meaning it appears following the copydisks phase. The server will be accessible via virt-v2v only after the command has finished. @@ -980,22 +980,22 @@ func (r *KubeVirt) UpdateVmByConvertedConfig(vm *plan.VMStatus, pod *core.Pod, s switch r.Source.Provider.Type() { case api.Ova: - vmConfigBytes, err := io.ReadAll(resp.Body) + vmConf, err := io.ReadAll(resp.Body) if err != nil { return liberr.Wrap(err) } - vmConfigXML := string(vmConfigBytes) - if vm.Firmware, err = ovfparser.GetFirmwareFromConfig(vmConfigXML); err != nil { + if vm.Firmware, err = util.GetFirmwareFromYaml(vmConf); err != nil { return liberr.Wrap(err) } case api.VSphere: inspectionXML, err := r.getInspectionXml(pod) if err != nil { - return err + return liberr.Wrap(err) } if vm.OperatingSystem, err = inspectionparser.GetOperationSystemFromConfig(inspectionXML); err != nil { return liberr.Wrap(err) } + r.Log.Info("Setting the vm OS ", vm.OperatingSystem, "vmId", vm.ID) } shutdownURL := fmt.Sprintf("http://%s:8080/shutdown", pod.Status.PodIP) @@ -1279,13 +1279,10 @@ func (r *KubeVirt) virtualMachine(vm *plan.VMStatus) (object *cnv.VirtualMachine //convention it will be automatically changed. var originalName string - if errs := k8svalidation.IsDNS1123Label(vm.Name); len(errs) > 0 { + if vm.NewName != "" { originalName = vm.Name - vm.Name, err = r.changeVmNameDNS1123(vm.Name, r.Plan.Spec.TargetNamespace) - if err != nil { - r.Log.Error(err, "Failed to update the VM name to meet DNS1123 protocol requirements.") - return - } + vm.Name = vm.NewName + r.Log.Info("VM name is incompatible with DNS1123 RFC, renaming", "originalName", originalName, "newName", vm.Name) } @@ -1743,6 +1740,15 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume, Value: vm.RootDisk, }) } + + if vm.NewName != "" { + environment = append(environment, + core.EnvVar{ + Name: "V2V_NewName", + Value: vm.NewName, + }) + } + // pod annotations annotations := map[string]string{} if r.Plan.Spec.TransferNetwork != nil { @@ -1794,9 +1800,8 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume, InitContainers: initContainers, Containers: []core.Container{ { - ImagePullPolicy: core.PullAlways, - Name: "virt-v2v", - Env: environment, + Name: "virt-v2v", + Env: environment, EnvFrom: []core.EnvFromSource{ { Prefix: "V2V_", diff --git a/pkg/controller/plan/migration.go b/pkg/controller/plan/migration.go index 65b042e7a..eaee3b200 100644 --- a/pkg/controller/plan/migration.go +++ b/pkg/controller/plan/migration.go @@ -30,6 +30,7 @@ import ( meta "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" + k8svalidation "k8s.io/apimachinery/pkg/util/validation" cdi "kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -718,6 +719,13 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) { err = nil break } + if errs := k8svalidation.IsDNS1123Label(vm.Name); len(errs) > 0 { + vm.NewName, err = r.kubevirt.changeVmNameDNS1123(vm.Name, r.Plan.Spec.TargetNamespace) + if err != nil { + r.Log.Error(err, "Failed to update the VM name to meet DNS1123 protocol requirements.") + return + } + } vm.Phase = r.next(vm.Phase) case PreHook, PostHook: runner := HookRunner{Context: r.Context} @@ -1141,7 +1149,7 @@ func (r *Migration) execute(vm *plan.VMStatus) (err error) { if pod != nil && pod.Status.Phase != core.PodSucceeded { err := r.kubevirt.UpdateVmByConvertedConfig(vm, pod, step) if err != nil { - return err + return liberr.Wrap(err) } } } diff --git a/pkg/controller/plan/util/BUILD.bazel b/pkg/controller/plan/util/BUILD.bazel index 195f4cb78..ca7e8f33e 100644 --- a/pkg/controller/plan/util/BUILD.bazel +++ b/pkg/controller/plan/util/BUILD.bazel @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "util", srcs = [ + "kubevirtvmparser.go", "openstack.go", "ovirt.go", "utils.go", @@ -13,18 +14,23 @@ go_library( "//pkg/apis/forklift/v1beta1", "//pkg/controller/provider/web/openstack", "//pkg/controller/provider/web/ovirt", + "//pkg/lib/logging", "//pkg/settings", "//vendor/k8s.io/api/core/v1:core", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta", + "//vendor/kubevirt.io/api/core/v1:core", + "//vendor/sigs.k8s.io/yaml", ], ) go_test( name = "util_test", srcs = [ + "kubevirtvmparser_test.go", "util_suite_test.go", "utils_test.go", ], + data = glob(["testdata/**"]), embed = [":util"], deps = [ "//vendor/github.com/onsi/ginkgo/v2:ginkgo", diff --git a/pkg/controller/plan/util/kubevirtvmparser.go b/pkg/controller/plan/util/kubevirtvmparser.go new file mode 100644 index 000000000..b106ef11d --- /dev/null +++ b/pkg/controller/plan/util/kubevirtvmparser.go @@ -0,0 +1,78 @@ +package util + +import ( + cnv "kubevirt.io/api/core/v1" + "sigs.k8s.io/yaml" + + "github.com/konveyor/forklift-controller/pkg/lib/logging" +) + +const ( + // Name. + Name = "kubevirt-vm-parser" +) + +// Package logger. +var log = logging.WithName(Name) + +type OS struct { + Firmware string `yaml:"firmware"` +} + +type Domain struct { + OS OS `yaml:"os"` +} + +type TemplateSpec struct { + Domain Domain `yaml:"domain"` +} + +type Template struct { + Spec TemplateSpec `yaml:"spec"` +} + +type VirtualMachineSpec struct { + Template Template `yaml:"template"` +} + +type VirtualMachine struct { + APIVersion string `yaml:"apiVersion"` + Kind string `yaml:"kind"` + Metadata Metadata `yaml:"metadata"` + Spec VirtualMachineSpec `yaml:"spec"` +} + +type Metadata struct { + Name string `yaml:"name"` + Labels map[string]string `yaml:"labels"` +} + +func GetFirmwareFromYaml(yamlData []byte) (string, error) { + var vm VirtualMachine + if err := yaml.Unmarshal(yamlData, &vm); err != nil { + return "", err + } + + firmware := vm.Spec.Template.Spec.Domain.OS.Firmware + if firmware != "" { + return firmware, nil + } + + // FIXME: In newer version of virt-v2v the output will change to support CNV VM format. + // With this we will support both so the migrations should not fail during the update to newer virt-v2v. + // But we still need to remove the custom templating. + // https://issues.redhat.com/browse/RHEL-58065 + var cnvVm *cnv.VirtualMachine + if err := yaml.Unmarshal(yamlData, &cnvVm); err != nil { + return "", err + } + if cnvVm.Spec.Template.Spec.Domain.Firmware.Bootloader.BIOS != nil { + return "bios", nil + } + if cnvVm.Spec.Template.Spec.Domain.Firmware.Bootloader.EFI != nil { + return "uefi", nil + } + + log.Info("Firmware type was not detected") + return "", nil +} diff --git a/pkg/controller/plan/util/kubevirtvmparser_test.go b/pkg/controller/plan/util/kubevirtvmparser_test.go new file mode 100644 index 000000000..3819b4c3c --- /dev/null +++ b/pkg/controller/plan/util/kubevirtvmparser_test.go @@ -0,0 +1,31 @@ +package util + +import ( + "fmt" + "os" + "path/filepath" + "testing" +) + +func TestKubevirtVmParser(t *testing.T) { + fmt.Println("test") + testFile(t, "new_format_bios.yml", "bios") + testFile(t, "new_format_efi.yml", "uefi") + testFile(t, "old_format_bios.yml", "bios") + testFile(t, "old_format_efi.yml", "uefi") +} + +func testFile(t *testing.T, filename, expectedFormat string) { + data, err := os.ReadFile(filepath.Join("testdata", filename)) + if err != nil { + fmt.Println(err) + } + firmware, err := GetFirmwareFromYaml(data) + if err != nil { + fmt.Println(err) + } + fmt.Println(firmware) + if firmware != expectedFormat { + t.Fatalf("Failed to parse '%s' from file '%s'", expectedFormat, filename) + } +} diff --git a/pkg/controller/plan/util/testdata/new_format_bios.yml b/pkg/controller/plan/util/testdata/new_format_bios.yml new file mode 100644 index 000000000..bf1db743f --- /dev/null +++ b/pkg/controller/plan/util/testdata/new_format_bios.yml @@ -0,0 +1,38 @@ +--- +# generated by virt-v2v 2.5.8fedora=40,release=1.fc40 +apiVersion: kubevirt.io/v1 +kind: VirtualMachine +metadata: + name: mnecas-win-2022 + labels: + libguestfs.org/virt-v2v-version: "2.5.8" + libguestfs.org/genid: f2fc75e8-4961-1425-4740-fcf2bc09685a + libguestfs.org/osinfo: win2k22 + libguestfs.org/source: vmware +spec: + template: + spec: + domain: + firmware: + bootloader: + bios: {} + resources: + clock: + timer: + hpet: + present: false + hyperv: {} + pit: + tickPolicy: delay + rtc: + tickPolicy: catchup + utc: {} + requests: + memory: 8192Mi + features: + cpu: + sockets: 2 + cores: 2 + thread: 1 + terminationGracePeriodSeconds: 0 + diff --git a/pkg/controller/plan/util/testdata/new_format_efi.yml b/pkg/controller/plan/util/testdata/new_format_efi.yml new file mode 100644 index 000000000..76746aa05 --- /dev/null +++ b/pkg/controller/plan/util/testdata/new_format_efi.yml @@ -0,0 +1,39 @@ +--- +# generated by virt-v2v 2.5.8fedora=40,release=1.fc40 +apiVersion: kubevirt.io/v1 +kind: VirtualMachine +metadata: + name: mnecas-win-2022 + labels: + libguestfs.org/virt-v2v-version: "2.5.8" + libguestfs.org/genid: f2fc75e8-4961-1425-4740-fcf2bc09685a + libguestfs.org/osinfo: win2k22 + libguestfs.org/source: vmware +spec: + template: + spec: + domain: + firmware: + bootloader: + efi: + persistent: true + resources: + clock: + timer: + hpet: + present: false + hyperv: {} + pit: + tickPolicy: delay + rtc: + tickPolicy: catchup + utc: {} + requests: + memory: 8192Mi + features: + cpu: + sockets: 2 + cores: 2 + thread: 1 + terminationGracePeriodSeconds: 0 + diff --git a/pkg/controller/plan/util/testdata/old_format_bios.yml b/pkg/controller/plan/util/testdata/old_format_bios.yml new file mode 100644 index 000000000..2eac58e25 --- /dev/null +++ b/pkg/controller/plan/util/testdata/old_format_bios.yml @@ -0,0 +1,38 @@ +--- +# generated by virt-v2v 2.5.8fedora=40,release=1.fc40 +apiVersion: kubevirt.io/v1 +kind: VirtualMachine +metadata: + name: mnecas-win-2022 + labels: + libguestfs.org/virt-v2v-version: "2.5.8" + libguestfs.org/genid: 4f3c8a7c-3464-c9b0-cf79-08325a6bf1e8 + libguestfs.org/osinfo: win2k22 + libguestfs.org/source: kvm +spec: + template: + spec: + domain: + os: + firmware: bios + resources: + clock: + timer: + hpet: + present: false + hyperv: {} + pit: + tickPolicy: delay + rtc: + tickPolicy: catchup + utc: {} + requests: + memory: 8192Mi + features: + acpi: {} + apic: {} + cpu: + sockets: 2 + cores: 2 + thread: 1 + terminationGracePeriodSeconds: 0 diff --git a/pkg/controller/plan/util/testdata/old_format_efi.yml b/pkg/controller/plan/util/testdata/old_format_efi.yml new file mode 100644 index 000000000..3a60de620 --- /dev/null +++ b/pkg/controller/plan/util/testdata/old_format_efi.yml @@ -0,0 +1,38 @@ +--- +# generated by virt-v2v 2.5.8fedora=40,release=1.fc40 +apiVersion: kubevirt.io/v1 +kind: VirtualMachine +metadata: + name: mnecas-win-2022 + labels: + libguestfs.org/virt-v2v-version: "2.5.8" + libguestfs.org/genid: 4f3c8a7c-3464-c9b0-cf79-08325a6bf1e8 + libguestfs.org/osinfo: win2k22 + libguestfs.org/source: kvm +spec: + template: + spec: + domain: + os: + firmware: uefi + resources: + clock: + timer: + hpet: + present: false + hyperv: {} + pit: + tickPolicy: delay + rtc: + tickPolicy: catchup + utc: {} + requests: + memory: 8192Mi + features: + acpi: {} + apic: {} + cpu: + sockets: 2 + cores: 2 + thread: 1 + terminationGracePeriodSeconds: 0 diff --git a/virt-v2v/cmd/entrypoint.go b/virt-v2v/cmd/entrypoint.go index 11eb4f746..bbe893399 100644 --- a/virt-v2v/cmd/entrypoint.go +++ b/virt-v2v/cmd/entrypoint.go @@ -108,7 +108,7 @@ func virtV2vBuildCommand() (args []string, err error) { } return nil, fmt.Errorf("virt-v2v supports the following providers: {%v}. Provided: %s\n", strings.Join(providers, ", "), source) } - args = append(args, "-o", "local", "-os", global.DIR) + args = append(args, "-o", "kubevirt", "-os", global.DIR) switch source { case global.VSPHERE: @@ -121,6 +121,12 @@ func virtV2vBuildCommand() (args []string, err error) { args = append(args, "-i", "ova", os.Getenv("V2V_diskPath")) } + // When converting VM with name that do not meet DNS1123 RFC requirements, + // it should be changed to supported one to ensure the conversion does not fail. + if utils.CheckEnvVariablesSet("V2V_NewName") { + args = append(args, "-on", os.Getenv("V2V_NewName")) + } + return args, nil } diff --git a/virt-v2v/pkg/customize/testdata/valid_config.yml b/virt-v2v/pkg/customize/testdata/valid_config.yml new file mode 100644 index 000000000..3a60de620 --- /dev/null +++ b/virt-v2v/pkg/customize/testdata/valid_config.yml @@ -0,0 +1,38 @@ +--- +# generated by virt-v2v 2.5.8fedora=40,release=1.fc40 +apiVersion: kubevirt.io/v1 +kind: VirtualMachine +metadata: + name: mnecas-win-2022 + labels: + libguestfs.org/virt-v2v-version: "2.5.8" + libguestfs.org/genid: 4f3c8a7c-3464-c9b0-cf79-08325a6bf1e8 + libguestfs.org/osinfo: win2k22 + libguestfs.org/source: kvm +spec: + template: + spec: + domain: + os: + firmware: uefi + resources: + clock: + timer: + hpet: + present: false + hyperv: {} + pit: + tickPolicy: delay + rtc: + tickPolicy: catchup + utc: {} + requests: + memory: 8192Mi + features: + acpi: {} + apic: {} + cpu: + sockets: 2 + cores: 2 + thread: 1 + terminationGracePeriodSeconds: 0 diff --git a/virt-v2v/pkg/server/server.go b/virt-v2v/pkg/server/server.go index e66edfe47..23cdbbde2 100644 --- a/virt-v2v/pkg/server/server.go +++ b/virt-v2v/pkg/server/server.go @@ -21,7 +21,7 @@ var ( // This information is later used in the vm creation step such as the firmware for the OVA or // Operating System for the VM creation. func Start() error { - http.HandleFunc("/ovf", ovfHandler) + http.HandleFunc("/vm", vmHandler) http.HandleFunc("/inspection", inspectorHandler) http.HandleFunc("/shutdown", shutdownHandler) server = &http.Server{Addr: ":8080"} @@ -34,20 +34,25 @@ func Start() error { return nil } -func ovfHandler(w http.ResponseWriter, r *http.Request) { - xmlFilePath, err := GetDomainFile(global.DIR, "xml") +func vmHandler(w http.ResponseWriter, r *http.Request) { + yamlFilePath, err := GetVmYamlFile(global.DIR) + if yamlFilePath == "" { + fmt.Println("Error: YAML file path is empty.") + http.Error(w, "YAML file path is empty", http.StatusInternalServerError) + return + } if err != nil { fmt.Println("Error getting XML file:", err) } - xmlData, err := utils.ReadXMLFile(xmlFilePath) + yamlData, err := os.ReadFile(yamlFilePath) if err != nil { - fmt.Printf("Error: %v\n", err) - http.Error(w, err.Error(), http.StatusInternalServerError) + fmt.Printf("Error reading YAML file: %v\n", err) + http.Error(w, "Error reading YAML file", http.StatusInternalServerError) return } - w.Header().Set("Content-Type", "application/xml") - _, err = w.Write(xmlData) + w.Header().Set("Content-Type", "text/yaml") + _, err = w.Write(yamlData) if err == nil { w.WriteHeader(http.StatusOK) } else { @@ -57,7 +62,7 @@ func ovfHandler(w http.ResponseWriter, r *http.Request) { } func inspectorHandler(w http.ResponseWriter, r *http.Request) { - xmlData, err := utils.ReadXMLFile(global.INSPECTION) + xmlData, err := os.ReadFile(global.INSPECTION) if err != nil { fmt.Printf("Error: %v\n", err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -82,8 +87,8 @@ func shutdownHandler(w http.ResponseWriter, r *http.Request) { } } -func GetDomainFile(dir, fileExtension string) (string, error) { - files, err := filepath.Glob(filepath.Join(dir, fmt.Sprintf("%s.%s", os.Getenv("V2V_vmName"), fileExtension))) +func GetVmYamlFile(dir string) (string, error) { + files, err := filepath.Glob(filepath.Join(dir, fmt.Sprintf("%s.%s", utils.GetDiskName(), "yaml"))) if err != nil { return "", err } diff --git a/virt-v2v/pkg/utils/command.go b/virt-v2v/pkg/utils/command.go index 2b40c01be..41cf7c45b 100644 --- a/virt-v2v/pkg/utils/command.go +++ b/virt-v2v/pkg/utils/command.go @@ -101,6 +101,13 @@ func getDiskNumber(kind global.MountPath, disk string) (int, error) { } } +func GetDiskName() string { + if name := os.Getenv("V2V_newName"); name != "" { + return name + } + return os.Getenv("V2V_vmName") +} + func getDiskLink(kind global.MountPath, disk string) (string, error) { diskNum, err := getDiskNumber(kind, disk) if err != nil { @@ -109,7 +116,7 @@ func getDiskLink(kind global.MountPath, disk string) (string, error) { } return filepath.Join( global.DIR, - fmt.Sprintf("%s-sd%s", os.Getenv("V2V_vmName"), genName(diskNum+1)), + fmt.Sprintf("%s-sd%s", GetDiskName(), genName(diskNum+1)), ), nil } @@ -117,7 +124,7 @@ func GetLinkedDisks() ([]string, error) { disks, err := filepath.Glob( filepath.Join( global.DIR, - fmt.Sprintf("%s-sd*", os.Getenv("V2V_vmName")), + fmt.Sprintf("%s-sd*", GetDiskName()), ), ) if err != nil { diff --git a/virt-v2v/pkg/utils/xml-reader.go b/virt-v2v/pkg/utils/xml-reader.go index 634422a55..694e976e2 100644 --- a/virt-v2v/pkg/utils/xml-reader.go +++ b/virt-v2v/pkg/utils/xml-reader.go @@ -18,7 +18,7 @@ type InspectionV2V struct { } func GetInspectionV2vFromFile(xmlFilePath string) (*InspectionV2V, error) { - xmlData, err := ReadXMLFile(xmlFilePath) + xmlData, err := os.ReadFile(xmlFilePath) if err != nil { fmt.Printf("Error read XML: %v\n", err) return nil, err @@ -31,24 +31,3 @@ func GetInspectionV2vFromFile(xmlFilePath string) (*InspectionV2V, error) { } return &xmlConf, nil } - -// ReadXMLFile reads the content of an XML []byte from the given file path. -// -// Arguments: -// - filePath (string): The path to the XML file. -// -// Returns: -// - []byte: The content of the XML file. -// - error: An error if the file cannot be read, or nil if successful. -func ReadXMLFile(filePath string) ([]byte, error) { - if filePath == "" { - return nil, fmt.Errorf("XML file path is empty") - } - - xmlData, err := os.ReadFile(filePath) - if err != nil { - return nil, fmt.Errorf("error reading XML file: %w", err) - } - - return xmlData, nil -}