Skip to content

Commit

Permalink
Change VM customization to use a YAML output file instead of XML.
Browse files Browse the repository at this point in the history
As part of the virt-v2v migration, we inspect the OS type and disk paths.
Until now, this has been done with the XML output file. Due to the change
to YAML format, this process needs to be adapted as well.
Since we cannot use external libraries in the virt-v2v directory,
it was implemented using bufio.

Signed-off-by: Bella Khizgiyaev <bkhizgiy@redhat.com>
  • Loading branch information
bkhizgiy committed Sep 2, 2024
1 parent 5405091 commit ed26a11
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 116 deletions.
26 changes: 7 additions & 19 deletions virt-v2v/cold/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,8 @@ func main() {
}
}

func getVmDiskPaths(domain *OvaVmconfig) []string {
var resp []string
for _, disk := range domain.Devices.Disks {
if disk.Source.File != "" {
resp = append(resp, disk.Source.File)
}
}
return resp
}

func customizeVM(source string, xmlFilePath string) error {
domain, err := GetDomainFromXml(xmlFilePath)
func customizeVM(source string, yamlFilePath string) error {
vmConfig, err := GetVmConfigYaml(yamlFilePath)
if err != nil {
fmt.Printf("Error mapping xml to domain: %v\n", err)

Expand All @@ -102,7 +92,7 @@ func customizeVM(source string, xmlFilePath string) error {
}

// Get operating system.
operatingSystem := domain.Metadata.LibOsInfo.V2VOS.ID
operatingSystem := vmConfig.OSInfo
if operatingSystem == "" {
fmt.Printf("Warning: no operating system found")

Expand All @@ -112,15 +102,13 @@ func customizeVM(source string, xmlFilePath string) error {
fmt.Printf("Operating System ID: %s\n", operatingSystem)
}

// Get domain disks.
disks := getVmDiskPaths(domain)
if len(disks) == 0 {
if len(vmConfig.DiskPaths) == 0 {
fmt.Printf("Warning: no V2V domain disks found")

// No customization when no disks found.
return nil
} else {
fmt.Printf("V2V domain disks: %v\n", disks)
fmt.Printf("V2V domain disks: %v\n", vmConfig.DiskPaths)
}

// Customization for vSphere source.
Expand All @@ -129,7 +117,7 @@ func customizeVM(source string, xmlFilePath string) error {
if strings.Contains(operatingSystem, "win") {
t := EmbedTool{filesystem: &scriptFS}

err = CustomizeWindows(disks, DIR, &t)
err = CustomizeWindows(vmConfig.DiskPaths, DIR, &t)
if err != nil {
fmt.Println("Error customizing disk image:", err)
return err
Expand All @@ -140,7 +128,7 @@ func customizeVM(source string, xmlFilePath string) error {
if !strings.Contains(operatingSystem, "win") {
t := EmbedTool{filesystem: &scriptFS}

err = CustomizeLinux(CustomizeDomainExec, disks, DIR, &t)
err = CustomizeLinux(CustomizeDomainExec, vmConfig.DiskPaths, DIR, &t)
if err != nil {
fmt.Println("Error customizing disk image:", err)
return err
Expand Down
97 changes: 0 additions & 97 deletions virt-v2v/cold/xml-reader.go

This file was deleted.

73 changes: 73 additions & 0 deletions virt-v2v/cold/yaml-reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"bufio"
"fmt"
"os"
"strings"
)

type VmConfig struct {
OSInfo string
DiskPaths []string
}

// ReadYAMLFile reads the YAML file and extracts the HostDisk paths and osinfo label.
func GetVmConfigYaml(filePath string) (*VmConfig, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening YAML file: %w", err)
}
defer file.Close()

config := &VmConfig{}
scanner := bufio.NewScanner(file)
var inLabels, inVolumes bool
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())

if line == "" || strings.HasPrefix(line, "#") {
continue
}

if strings.HasPrefix(line, "labels:") {
inLabels = true
continue
}

if inLabels {
if strings.HasPrefix(line, "libguestfs.org/osinfo:") {
config.OSInfo = strings.TrimSpace(strings.TrimPrefix(line, "libguestfs.org/osinfo:"))
}
if !strings.HasPrefix(line, "libguestfs.org/") {
inLabels = false
}
}

if strings.Contains(line, "volumes:") {
inVolumes = true
continue
}
if inVolumes {
if strings.Contains(line, "hostDisk:") {
scanner.Scan()
pathLine := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(pathLine, "path:") {
pathValue := strings.TrimSpace(strings.TrimPrefix(pathLine, "path:"))
if pathValue != "" {
config.DiskPaths = append(config.DiskPaths, pathValue)
}
}
}
if strings.Contains(line, "- name:") {
inVolumes = false
}
}
}

if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading YAML file: %w", err)
}

return config, nil
}

0 comments on commit ed26a11

Please sign in to comment.