2024-10-18 06:00:05 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-10-23 11:37:51 -05:00
|
|
|
"encoding/xml"
|
2024-10-18 06:00:05 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2024-10-23 13:05:20 -05:00
|
|
|
"reflect"
|
2024-10-18 06:00:05 -05:00
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
"libvirt.org/go/libvirtxml"
|
|
|
|
)
|
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
func makeStandardXml(d *DropletT) *libvirtxml.Domain {
|
2024-10-22 17:27:24 -05:00
|
|
|
log.Info("create new xml file for:", d.pb.Hostname)
|
2024-10-18 06:00:05 -05:00
|
|
|
domcfg := &libvirtxml.Domain{}
|
|
|
|
|
|
|
|
addDefaults(domcfg, "standard.x86")
|
|
|
|
addDefaults(domcfg, "memory")
|
|
|
|
addDefaults(domcfg, "network")
|
|
|
|
addDefaults(domcfg, "spice")
|
|
|
|
addDefaults(domcfg, "qcow")
|
2024-10-22 17:27:24 -05:00
|
|
|
addDefaults(domcfg, d.pb.Hostname)
|
2024-10-18 06:00:05 -05:00
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
return domcfg
|
|
|
|
}
|
2024-10-18 06:00:05 -05:00
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
func writeoutXml(domcfg *libvirtxml.Domain, filename string) bool {
|
2024-10-18 06:00:05 -05:00
|
|
|
xmldoc, err := domcfg.Marshal()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("can't make xml file error:\n", err)
|
2024-10-18 14:36:40 -05:00
|
|
|
return false
|
2024-10-18 06:00:05 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
outfile := "/tmp/" + filename + ".xml"
|
2024-10-18 06:00:05 -05:00
|
|
|
regfile, _ := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0666)
|
|
|
|
fmt.Fprintln(regfile, xmldoc)
|
|
|
|
|
|
|
|
log.Info("File is in", outfile)
|
2024-10-18 14:36:40 -05:00
|
|
|
regfile.Close()
|
|
|
|
return true
|
2024-10-18 06:00:05 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 08:39:52 -05:00
|
|
|
func setDiskFilename(domcfg *libvirtxml.Domain, filename string) {
|
|
|
|
for i, x := range domcfg.Devices.Disks {
|
|
|
|
// Create a new DomainDiskSourceFile struct
|
|
|
|
newSource := &libvirtxml.DomainDiskSourceFile{
|
|
|
|
File: filename, // Set the file name here
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign it to the disk's source
|
|
|
|
domcfg.Devices.Disks[i].Source.File = newSource
|
|
|
|
|
|
|
|
// fmt.Printf("Disk Source %s\n", name)
|
|
|
|
fmt.Printf("Disk Device %s\n", x.Source.File)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 06:00:05 -05:00
|
|
|
func addDefaults(d *libvirtxml.Domain, filename string) {
|
|
|
|
fullname := "resources/xml/" + filename + ".xml"
|
|
|
|
pfile, err := resources.ReadFile(fullname)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Unmarshal(string(pfile))
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Marshal failed on file", filename)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-10-18 08:18:11 -05:00
|
|
|
|
2024-10-23 02:56:55 -05:00
|
|
|
func readXml(filename string) (*libvirtxml.Domain, error) {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Verbose("parse xml file:", filename)
|
2024-10-23 02:56:55 -05:00
|
|
|
|
|
|
|
pfile, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
domcfg := &libvirtxml.Domain{}
|
|
|
|
|
|
|
|
err = domcfg.Unmarshal(string(pfile))
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Marshal failed on file", filename, err)
|
|
|
|
return nil, ErrorParseXML
|
|
|
|
}
|
|
|
|
return domcfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DropletT) mergeXml(filename string) error {
|
2024-10-23 06:27:56 -05:00
|
|
|
log.Info("merge xml file:", filename)
|
2024-10-23 01:31:17 -05:00
|
|
|
|
|
|
|
pfile, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ERROR:", err)
|
|
|
|
return ErrorNoFile
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.xml.Unmarshal(string(pfile))
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Marshal failed on file", filename)
|
|
|
|
return ErrorParseXML
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
func setSimpleDisk(domcfg *libvirtxml.Domain, filename string) {
|
2024-10-18 08:18:11 -05:00
|
|
|
// Clear out the existing disks (if any)
|
|
|
|
domcfg.Devices.Disks = nil
|
|
|
|
|
|
|
|
// Define a new disk with "mynew.qcow2"
|
|
|
|
newDisk := libvirtxml.DomainDisk{
|
|
|
|
Device: "disk",
|
|
|
|
Driver: &libvirtxml.DomainDiskDriver{
|
|
|
|
Name: "qemu",
|
|
|
|
Type: "qcow2",
|
|
|
|
},
|
|
|
|
Source: &libvirtxml.DomainDiskSource{
|
|
|
|
File: &libvirtxml.DomainDiskSourceFile{
|
|
|
|
File: filename,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Target: &libvirtxml.DomainDiskTarget{
|
|
|
|
Dev: "vda",
|
|
|
|
Bus: "virtio",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new disk to the domain configuration
|
|
|
|
domcfg.Devices.Disks = append(domcfg.Devices.Disks, newDisk)
|
|
|
|
}
|
2024-10-18 08:39:52 -05:00
|
|
|
|
2024-10-18 20:22:37 -05:00
|
|
|
func getMacs(domcfg *libvirtxml.Domain) []string {
|
2024-10-18 08:39:52 -05:00
|
|
|
var macs []string
|
|
|
|
// Iterate over the network interfaces and print the MAC addresses
|
|
|
|
for _, iface := range domcfg.Devices.Interfaces {
|
|
|
|
if iface.MAC != nil {
|
2024-10-18 20:22:37 -05:00
|
|
|
// iface.MAC.Address = "aa:bb:aa:bb:aa:ff"
|
|
|
|
fmt.Printf("MAC Address: %+v\n", iface.MAC)
|
|
|
|
// fmt.Printf("Interface: %s, MAC Address: %s\n", iface.Target.Dev, iface.MAC.Address)
|
2024-10-18 08:39:52 -05:00
|
|
|
macs = append(macs, iface.MAC.Address)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Interface: %s, MAC Address: not available\n", iface.Target.Dev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return macs
|
|
|
|
}
|
2024-10-18 09:05:39 -05:00
|
|
|
|
2024-10-18 20:22:37 -05:00
|
|
|
// removes all the ethernet interfaces
|
|
|
|
func clearEthernet(domcfg *libvirtxml.Domain) {
|
2024-10-18 09:05:39 -05:00
|
|
|
// Clear out the existing disks (if any)
|
|
|
|
domcfg.Devices.Interfaces = nil
|
2024-10-18 20:22:37 -05:00
|
|
|
}
|
2024-10-18 09:05:39 -05:00
|
|
|
|
2024-10-18 20:22:37 -05:00
|
|
|
// add a new ethernet interface with mac assigned to bridge name
|
|
|
|
func addEthernet(domcfg *libvirtxml.Domain, mac string, brname string) {
|
2024-10-18 09:05:39 -05:00
|
|
|
// Define a new disk with "mynew.qcow2"
|
2024-10-23 17:49:15 -05:00
|
|
|
type DomainInterfaceType string
|
|
|
|
|
2024-10-18 09:05:39 -05:00
|
|
|
newNet := libvirtxml.DomainInterface{
|
|
|
|
MAC: &libvirtxml.DomainInterfaceMAC{
|
|
|
|
Address: mac,
|
|
|
|
},
|
|
|
|
Target: &libvirtxml.DomainInterfaceTarget{
|
|
|
|
Dev: brname,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the new disk to the domain configuration
|
|
|
|
domcfg.Devices.Interfaces = append(domcfg.Devices.Interfaces, newNet)
|
|
|
|
}
|
|
|
|
|
2024-10-18 14:36:40 -05:00
|
|
|
func setRandomMacs(domcfg *libvirtxml.Domain) {
|
2024-10-18 09:05:39 -05:00
|
|
|
for i, x := range domcfg.Devices.Interfaces {
|
|
|
|
// Create a new DomainDiskInterfaces struct
|
|
|
|
newMac := &libvirtxml.DomainInterfaceMAC{
|
|
|
|
Address: "aa:bb:cc:dd:ee:ff", // make sure this is unique
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign it to the disk's source
|
|
|
|
domcfg.Devices.Interfaces[i].MAC = newMac
|
|
|
|
|
|
|
|
// fmt.Printf("Disk Source %s\n", name)
|
2024-10-18 20:22:37 -05:00
|
|
|
// fmt.Printf("mac addr %+v\n", x.MAC)
|
|
|
|
fmt.Printf("mac addr %s\n", x.MAC.Address)
|
2024-10-18 09:05:39 -05:00
|
|
|
}
|
|
|
|
}
|
2024-10-23 11:37:51 -05:00
|
|
|
|
2024-10-23 13:05:20 -05:00
|
|
|
// go through the libvirt xml object and dump out everything
|
2024-10-23 11:37:51 -05:00
|
|
|
// that is "standard". This is just a way to double check that
|
|
|
|
// there might be something interesting in a VM
|
2024-10-23 13:05:20 -05:00
|
|
|
// 'standard' here means what I think is standard
|
2024-10-23 11:37:51 -05:00
|
|
|
func dumpNonStandardXML(domcfg *libvirtxml.Domain) {
|
2024-10-23 16:05:24 -05:00
|
|
|
// dump type
|
|
|
|
if domcfg.Type == "kvm" {
|
|
|
|
domcfg.Type = ""
|
|
|
|
} else {
|
|
|
|
fmt.Printf("type: %+v\n", domcfg.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump normal OS settings
|
|
|
|
var standardOS bool = false
|
|
|
|
if domcfg.OS != nil {
|
|
|
|
if domcfg.OS.Type != nil {
|
|
|
|
// OS Type: &{Arch:x86_64 Machine:pc-i440fx-5.2 Type:hvm}
|
|
|
|
t := domcfg.OS.Type
|
|
|
|
if t.Arch == "x86_64" || t.Machine == "pc-i440fx-5.2" {
|
|
|
|
standardOS = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if standardOS {
|
|
|
|
domcfg.OS = nil
|
|
|
|
} else {
|
|
|
|
fmt.Printf("OS: %+v\n", domcfg.OS)
|
|
|
|
fmt.Printf("OS Type: %+v\n", domcfg.OS.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore XMLName and IOThreads probably
|
|
|
|
// skip is hard coded in isDomainEmpty() function
|
|
|
|
// fmt.Printf("XMLName: %+v\n", domcfg.XMLName)
|
|
|
|
// fmt.Printf("IOThreads: %+v\n", domcfg.IOThreads)
|
|
|
|
|
2024-10-23 11:37:51 -05:00
|
|
|
// dump all the clock stuff if it's standard
|
|
|
|
var normalclock bool = true
|
|
|
|
if domcfg.Clock.Offset != "utc" {
|
|
|
|
normalclock = false
|
|
|
|
}
|
|
|
|
for i, t := range domcfg.Clock.Timer {
|
|
|
|
// fmt.Printf("Test Clock Timer: %d , %s , %+v\n", i, t.Name, t)
|
|
|
|
switch t.Name {
|
|
|
|
case "rtc":
|
|
|
|
if t.TickPolicy != "catchup" {
|
|
|
|
fmt.Printf("Clock Name: %+v , %+v\n", i, t)
|
|
|
|
normalclock = false
|
|
|
|
}
|
|
|
|
case "pit":
|
|
|
|
if t.TickPolicy != "delay" {
|
|
|
|
fmt.Printf("Clock Name: %+v , %+v\n", i, t)
|
|
|
|
normalclock = false
|
|
|
|
}
|
|
|
|
case "hpet":
|
|
|
|
if t.Present != "no" {
|
|
|
|
fmt.Printf("Clock Name: %+v , %+v\n", i, t)
|
|
|
|
normalclock = false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("Clock Name: %+v , %+v\n", i, t)
|
|
|
|
normalclock = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalclock {
|
|
|
|
domcfg.Clock = nil
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Clock was 'nonstandard' %+v\n", domcfg.Clock.Timer)
|
|
|
|
}
|
|
|
|
|
2024-10-23 13:05:20 -05:00
|
|
|
// probably just dump Features for now
|
2024-10-23 11:37:51 -05:00
|
|
|
// fmt.Printf("Features: %+v\n", domcfg.Features)
|
|
|
|
// fmt.Printf("Feature VMPort: %+v\n", domcfg.Features.VMPort)
|
|
|
|
// ignore if ACPI is set or not
|
|
|
|
var featurematch bool = true
|
|
|
|
if domcfg.Features.ACPI != nil {
|
|
|
|
domcfg.Features.ACPI = nil
|
|
|
|
} else {
|
|
|
|
featurematch = false
|
|
|
|
}
|
|
|
|
// ignore if APIC is set or not
|
|
|
|
if domcfg.Features.APIC != nil {
|
|
|
|
domcfg.Features.APIC = nil
|
|
|
|
} else {
|
|
|
|
featurematch = false
|
|
|
|
}
|
|
|
|
// what is VMPort anyway?
|
|
|
|
if domcfg.Features.VMPort.State == "off" {
|
|
|
|
domcfg.Features.VMPort = nil
|
|
|
|
} else {
|
|
|
|
featurematch = false
|
|
|
|
}
|
|
|
|
// screwit, if all three of those match just erase
|
|
|
|
// this. not sure what uses it anyway but it's probably obscure
|
|
|
|
// and I'm not using it on any of my machines right now
|
|
|
|
// also, this is dumb that I'm doing this but I want to
|
|
|
|
// fine tooth comb through this right now
|
|
|
|
// also, I don't have a boss so nobody can tell me what to do
|
|
|
|
if featurematch {
|
|
|
|
domcfg.Features = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fmt.Printf("Features: %+v\n", domcfg.Features)
|
|
|
|
|
|
|
|
// for i, f := range domcfg.Features {
|
|
|
|
// fmt.Printf("Feature: %+v , %+v\n", i, f)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// these should always just be strings?
|
|
|
|
domcfg.Name = ""
|
|
|
|
domcfg.UUID = ""
|
|
|
|
|
|
|
|
// todo: actually check these for anything different
|
|
|
|
domcfg.Memory = nil
|
|
|
|
domcfg.CurrentMemory = nil
|
|
|
|
domcfg.VCPU = nil
|
2024-10-23 17:49:15 -05:00
|
|
|
switch domcfg.CPU.Mode {
|
|
|
|
case "host-passthrough":
|
2024-10-23 14:32:59 -05:00
|
|
|
domcfg.CPU = nil
|
2024-10-23 17:49:15 -05:00
|
|
|
case "host-model":
|
|
|
|
domcfg.CPU = nil
|
|
|
|
default:
|
2024-10-23 14:32:59 -05:00
|
|
|
fmt.Printf("? CPU Mode: %+v\n", domcfg.CPU.Mode)
|
|
|
|
}
|
2024-10-23 11:37:51 -05:00
|
|
|
|
2024-10-23 18:37:07 -05:00
|
|
|
// this goes away if SecLabel is zero'd out?
|
|
|
|
//if domcfg.Metadata != nil {
|
|
|
|
// fmt.Printf("? Metadata: %+v\n", domcfg.Metadata)
|
|
|
|
//}
|
|
|
|
var secnormal bool = true
|
|
|
|
if len(domcfg.SecLabel) != 0 {
|
|
|
|
for _, sec := range domcfg.SecLabel {
|
|
|
|
if sec.Model == "apparmor" {
|
2024-10-23 18:53:02 -05:00
|
|
|
// this should be configured in dom0
|
2024-10-23 18:37:07 -05:00
|
|
|
} else {
|
|
|
|
fmt.Printf("? SecLabel: %+v\n", sec)
|
|
|
|
fmt.Printf("? SecLabel.Model: %+v\n", sec.Model)
|
|
|
|
secnormal = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if secnormal {
|
|
|
|
domcfg.SecLabel = nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 13:05:20 -05:00
|
|
|
// this will move elsewhere in the protobuf someday
|
2024-10-23 11:37:51 -05:00
|
|
|
if domcfg.OnPoweroff == "destroy" {
|
|
|
|
domcfg.OnPoweroff = ""
|
|
|
|
}
|
|
|
|
if domcfg.OnCrash == "destroy" {
|
|
|
|
domcfg.OnCrash = ""
|
|
|
|
}
|
|
|
|
if domcfg.OnReboot == "restart" {
|
|
|
|
domcfg.OnReboot = ""
|
|
|
|
}
|
2024-10-23 13:05:20 -05:00
|
|
|
// same with PM. move to protobuf
|
|
|
|
domcfg.PM = nil
|
2024-10-23 11:37:51 -05:00
|
|
|
|
|
|
|
// only keep non-qemu stuff
|
|
|
|
var qemu bool = true
|
|
|
|
for _, disk := range domcfg.Devices.Disks {
|
|
|
|
if disk.Driver.Name != "qemu" {
|
|
|
|
fmt.Printf("- Disk: %s, Device: %s, Source: %s\n", disk.Device, disk.Driver.Name, disk.Source.File.File)
|
|
|
|
fmt.Printf("FOUND NON QEMU DISK\n")
|
|
|
|
fmt.Printf("FOUND NON QEMU DISKS\n")
|
|
|
|
qemu = false
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if qemu {
|
|
|
|
domcfg.Devices.Disks = nil
|
|
|
|
} else {
|
|
|
|
// fmt.Printf("FOUND NON QEMU DISKS\n")
|
|
|
|
}
|
|
|
|
|
2024-10-23 13:05:20 -05:00
|
|
|
// network interfaces get processed elsewhere
|
2024-10-23 11:37:51 -05:00
|
|
|
domcfg.Devices.Interfaces = nil
|
2024-10-23 13:05:20 -05:00
|
|
|
|
|
|
|
// look for strange stuff here
|
|
|
|
var normalPCI bool = true
|
|
|
|
var keepPCI []libvirtxml.DomainController
|
2024-10-23 11:37:51 -05:00
|
|
|
for _, controller := range domcfg.Devices.Controllers {
|
2024-10-23 13:05:20 -05:00
|
|
|
switch controller.Type {
|
|
|
|
case "usb":
|
|
|
|
switch controller.Model {
|
|
|
|
case "ich9-ehci1":
|
2024-10-23 17:49:15 -05:00
|
|
|
case "qemu-xhci":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("OK USB: %s, %d\n", controller.Model, *controller.Index)
|
2024-10-23 13:05:20 -05:00
|
|
|
case "ich9-uhci1":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("OK USB: %s, %d\n", controller.Model, *controller.Index)
|
2024-10-23 13:05:20 -05:00
|
|
|
case "ich9-uhci2":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("OK USB: %s, %d\n", controller.Model, *controller.Index)
|
2024-10-23 13:05:20 -05:00
|
|
|
case "ich9-uhci3":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("OK USB: %s, %d\n", controller.Model, *controller.Index)
|
2024-10-23 13:05:20 -05:00
|
|
|
default:
|
|
|
|
keepPCI = append(keepPCI, controller)
|
|
|
|
normalPCI = false
|
|
|
|
fmt.Printf("USB: %s, %d\n", controller.Model, *controller.Index)
|
|
|
|
// Domain:0xc0002d2760 Bus:0xc0002d2768 Slot:0xc0002d2770 Function:0xc0002d2778 MultiFunction:
|
|
|
|
pci := controller.Address.PCI
|
|
|
|
fmt.Printf("USB: Domain: %+v Slot %d Function %d\n", *pci.Domain, *pci.Slot, *pci.Function)
|
|
|
|
}
|
|
|
|
case "ide":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("IGNORE IDE\n")
|
2024-10-23 13:05:20 -05:00
|
|
|
case "virtio-serial":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("IGNORE virtio-serial\n")
|
2024-10-23 17:49:15 -05:00
|
|
|
case "sata":
|
|
|
|
// fmt.Printf("SATA: %s, %d\n", controller.Model, *controller.Index)
|
|
|
|
// fmt.Printf("SATA: %+v\n", controller)
|
2024-10-23 13:05:20 -05:00
|
|
|
case "scsi":
|
|
|
|
switch controller.Model {
|
|
|
|
case "lsilogic":
|
2024-10-23 14:35:59 -05:00
|
|
|
// fmt.Printf("IGNORE SCSI: lsilogic\n")
|
2024-10-23 13:05:20 -05:00
|
|
|
default:
|
|
|
|
keepPCI = append(keepPCI, controller)
|
|
|
|
normalPCI = false
|
|
|
|
}
|
|
|
|
case "pci":
|
2024-10-23 17:49:15 -05:00
|
|
|
switch controller.Model {
|
|
|
|
case "pcie-root":
|
|
|
|
// pcie-root
|
|
|
|
case "pcie-root-port":
|
|
|
|
// pcie-root
|
|
|
|
case "pcie-to-pci-bridge":
|
|
|
|
// pcie-root
|
|
|
|
default:
|
|
|
|
fmt.Printf("PCI: %s, %d\n", controller.Model, *controller.Index)
|
|
|
|
// Domain:0xc0002d2760 Bus:0xc0002d2768 Slot:0xc0002d2770 Function:0xc0002d2778 MultiFunction:
|
|
|
|
pci := controller.Address.PCI
|
|
|
|
fmt.Printf("PCI: Domain: %+v Slot %d Function %d\n", *pci.Domain, *pci.Slot, *pci.Function)
|
|
|
|
normalPCI = false
|
|
|
|
keepPCI = append(keepPCI, controller)
|
|
|
|
}
|
2024-10-23 13:05:20 -05:00
|
|
|
default:
|
2024-10-23 17:49:15 -05:00
|
|
|
fmt.Printf("? controllerType: %s: %+v\n", controller.Type, controller)
|
2024-10-23 13:05:20 -05:00
|
|
|
normalPCI = false
|
|
|
|
keepPCI = append(keepPCI, controller)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalPCI {
|
|
|
|
domcfg.Devices.Controllers = nil
|
|
|
|
} else {
|
|
|
|
domcfg.Devices.Controllers = keepPCI
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore serial and console
|
|
|
|
domcfg.Devices.Serials = nil
|
|
|
|
domcfg.Devices.Consoles = nil
|
|
|
|
|
|
|
|
// ignore sound
|
|
|
|
domcfg.Devices.Sounds = nil
|
|
|
|
|
|
|
|
// ignore input
|
|
|
|
domcfg.Devices.Inputs = nil
|
|
|
|
|
|
|
|
// ignore MemoryBalloon. This is cool, but no mortal humans
|
|
|
|
// are going to use it at this point. By that I mean me.
|
|
|
|
// someday this will be in protobuf?
|
|
|
|
domcfg.Devices.MemBalloon = nil
|
|
|
|
|
|
|
|
if domcfg.Devices.Emulator == "/usr/bin/qemu-system-x86_64" {
|
|
|
|
domcfg.Devices.Emulator = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore Graphics == Spice when AutoPort = 'yes'
|
|
|
|
var normalSpice bool = true
|
|
|
|
if domcfg.Devices.Graphics != nil {
|
2024-10-23 17:49:15 -05:00
|
|
|
for i, g := range domcfg.Devices.Graphics {
|
|
|
|
if g.Spice == nil {
|
|
|
|
// figure out what to do with non-spice stuff
|
|
|
|
fmt.Printf("Graphics: %d %+v\n", i, g)
|
2024-10-23 13:05:20 -05:00
|
|
|
normalSpice = false
|
2024-10-23 17:49:15 -05:00
|
|
|
continue
|
|
|
|
}
|
2024-10-23 18:06:46 -05:00
|
|
|
// this is all moved to updateDroplet()
|
2024-10-23 17:49:15 -05:00
|
|
|
// this is a spice definition, just ignore it
|
|
|
|
// because port mappings and network access will be handled
|
|
|
|
// somewhere else someday
|
|
|
|
// fmt.Printf("Graphics: %d %+v\n", i, g)
|
|
|
|
var s *libvirtxml.DomainGraphicSpice
|
|
|
|
s = g.Spice
|
|
|
|
// fmt.Printf("Spice: %d %+v %s\n", i, s, s.AutoPort)
|
|
|
|
if s.AutoPort == "yes" {
|
|
|
|
// should ignore either way
|
|
|
|
} else {
|
|
|
|
// print out, but ignore the port number
|
2024-10-23 18:06:46 -05:00
|
|
|
// fmt.Printf("Spice Port = %d\n", s.Port)
|
2024-10-23 13:05:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalSpice {
|
|
|
|
domcfg.Devices.Graphics = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore Channels == SpiceVMC
|
|
|
|
normalSpice = true
|
|
|
|
if domcfg.Devices.Channels != nil {
|
|
|
|
for _, c := range domcfg.Devices.Channels {
|
|
|
|
if c.Source != nil {
|
|
|
|
s := c.Source
|
|
|
|
if s != nil {
|
|
|
|
// fmt.Printf("Channels: %+v\n", s.SpiceVMC)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("? Channels: %+v\n", c)
|
|
|
|
normalSpice = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("? Channels: %+v\n", c)
|
|
|
|
normalSpice = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalSpice {
|
|
|
|
domcfg.Devices.Channels = nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 14:20:59 -05:00
|
|
|
// this is probably for spice to have keyboard and mouse input
|
|
|
|
normalSpice = true
|
|
|
|
if domcfg.Devices.RedirDevs != nil {
|
|
|
|
for _, c := range domcfg.Devices.RedirDevs {
|
|
|
|
s := c.Source
|
|
|
|
if s != nil {
|
|
|
|
if s.SpiceVMC != nil {
|
|
|
|
// this is the normal USB redirection (I guess)
|
|
|
|
} else {
|
|
|
|
normalSpice = false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
normalSpice = false
|
|
|
|
}
|
|
|
|
// fmt.Printf("? RedirDevs: %+v\n", c)
|
|
|
|
// fmt.Printf("? RedirDevs Source: %+v\n", s)
|
|
|
|
// fmt.Printf("? RedirDevs SpiceVMC: %d\n", *s.SpiceVMC)
|
|
|
|
// fmt.Printf("? RedirDevs Address: %+v\n", c.Address)
|
|
|
|
// fmt.Printf("? RedirDevs USB: %+v\n", c.Address.USB)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalSpice {
|
|
|
|
domcfg.Devices.RedirDevs = nil
|
|
|
|
}
|
2024-10-23 13:05:20 -05:00
|
|
|
|
2024-10-23 18:15:59 -05:00
|
|
|
var normalRNGs bool = true
|
|
|
|
if domcfg.Devices.RNGs != nil {
|
|
|
|
for _, rng := range domcfg.Devices.RNGs {
|
|
|
|
if rng.Model == "virtio" {
|
|
|
|
// nothing to do for this
|
|
|
|
} else {
|
|
|
|
fmt.Printf("? RNGs: %+v\n", rng)
|
|
|
|
normalRNGs = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if normalRNGs {
|
|
|
|
domcfg.Devices.RNGs = nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 14:20:59 -05:00
|
|
|
// this is probably for spice to have keyboard and mouse input
|
|
|
|
var normalVideo bool = true
|
|
|
|
if domcfg.Devices.Videos != nil {
|
2024-10-23 14:32:59 -05:00
|
|
|
for _, v := range domcfg.Devices.Videos {
|
2024-10-23 18:06:46 -05:00
|
|
|
switch v.Model.Type {
|
|
|
|
case "qxl":
|
2024-10-23 18:15:59 -05:00
|
|
|
if v.Model.VRam == 65536 {
|
|
|
|
// standard qxl video
|
|
|
|
} else {
|
|
|
|
fmt.Printf("? Video: %+v\n", v)
|
|
|
|
fmt.Printf("? Video Model: %+v\n", v.Model)
|
|
|
|
normalVideo = false
|
|
|
|
}
|
2024-10-23 18:06:46 -05:00
|
|
|
case "virtio":
|
|
|
|
// this should always be standard
|
|
|
|
//fmt.Printf("? Video: %+v\n", v)
|
|
|
|
//fmt.Printf("? Video Model: %+v\n", v.Model)
|
|
|
|
//fmt.Printf("? Video Address: %+v\n", v.Address)
|
|
|
|
//fmt.Printf("? Video PCI: %+v\n", v.Address.PCI)
|
|
|
|
default:
|
|
|
|
fmt.Printf("? Video: %+v\n", v)
|
|
|
|
fmt.Printf("? Video Model: %+v\n", v.Model)
|
|
|
|
normalVideo = false
|
|
|
|
}
|
2024-10-23 14:32:59 -05:00
|
|
|
}
|
2024-10-23 11:37:51 -05:00
|
|
|
}
|
2024-10-23 14:20:59 -05:00
|
|
|
if normalVideo {
|
|
|
|
domcfg.Devices.Videos = nil
|
|
|
|
}
|
|
|
|
|
2024-10-23 15:01:10 -05:00
|
|
|
// dumpLibvirtxmlDomainNames()
|
2024-10-23 16:05:24 -05:00
|
|
|
if libvirtxmlDomainDevicesEmpty(*domcfg.Devices) {
|
|
|
|
// fmt.Println("Domain Devices are empty")
|
2024-10-23 15:01:10 -05:00
|
|
|
domcfg.Devices = nil
|
|
|
|
} else {
|
|
|
|
fmt.Println("Domain Devices are not empty")
|
|
|
|
}
|
2024-10-23 11:37:51 -05:00
|
|
|
|
2024-10-23 16:05:24 -05:00
|
|
|
if libvirtxmlDomainEmpty(*domcfg) {
|
|
|
|
// fmt.Println("Domain Devices are empty")
|
|
|
|
domcfg = nil
|
|
|
|
} else {
|
|
|
|
fmt.Println("Domain is not yet empty")
|
|
|
|
}
|
|
|
|
|
2024-10-23 11:37:51 -05:00
|
|
|
updatedXML, err := xml.MarshalIndent(domcfg, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to marshal updated XML: %v\n", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print the updated XML to verify
|
|
|
|
fmt.Println(string(updatedXML))
|
2024-10-23 15:01:10 -05:00
|
|
|
// os.Exit(-1)
|
2024-10-23 11:37:51 -05:00
|
|
|
}
|
2024-10-23 14:20:59 -05:00
|
|
|
|
|
|
|
// dump out all the fields in libvirtxml.DomainDeviceList
|
2024-10-23 15:01:10 -05:00
|
|
|
func dumpLibvirtxmlDomainNames() {
|
2024-10-23 14:32:59 -05:00
|
|
|
var domain libvirtxml.Domain
|
2024-10-23 14:20:59 -05:00
|
|
|
t := reflect.TypeOf(domain)
|
|
|
|
|
2024-10-23 14:32:59 -05:00
|
|
|
fmt.Println("Fields in libvirtxml.Domain:")
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
field := t.Field(i)
|
|
|
|
fmt.Println("Domain:", field.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var device libvirtxml.DomainDeviceList
|
|
|
|
t = reflect.TypeOf(device)
|
|
|
|
|
2024-10-23 14:20:59 -05:00
|
|
|
fmt.Println("Fields in libvirtxml.DomainDeviceList:")
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
field := t.Field(i)
|
2024-10-23 14:32:59 -05:00
|
|
|
fmt.Println("DomainDeviceList:", field.Name)
|
2024-10-23 14:20:59 -05:00
|
|
|
}
|
2024-10-23 17:49:15 -05:00
|
|
|
|
|
|
|
var iface libvirtxml.DomainInterface
|
|
|
|
t = reflect.TypeOf(iface)
|
|
|
|
|
|
|
|
fmt.Println("Fields in libvirtxml.DomainInterface:")
|
|
|
|
for i := 0; i < t.NumField(); i++ {
|
|
|
|
field := t.Field(i)
|
|
|
|
fmt.Println("DomainInterface:", field.Name)
|
|
|
|
}
|
2024-10-23 14:20:59 -05:00
|
|
|
}
|
2024-10-23 15:01:10 -05:00
|
|
|
|
|
|
|
// dump out all the fields in libvirtxml.DomainDeviceList
|
2024-10-23 16:05:24 -05:00
|
|
|
func libvirtxmlDomainDevicesEmpty(mydom libvirtxml.DomainDeviceList) bool {
|
2024-10-23 15:01:10 -05:00
|
|
|
var empty bool = true
|
|
|
|
// Get the reflection object of the variable
|
|
|
|
v := reflect.ValueOf(mydom)
|
|
|
|
|
|
|
|
// Ensure that we are working with a struct
|
|
|
|
if v.Kind() == reflect.Struct {
|
2024-10-23 16:05:24 -05:00
|
|
|
// fmt.Println("Fields and values in libvirtxml.DomainDeviceList:")
|
2024-10-23 15:01:10 -05:00
|
|
|
|
|
|
|
// Loop through each field in the struct
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
// Get field name
|
|
|
|
field := v.Type().Field(i).Name
|
|
|
|
|
|
|
|
// Get field value
|
|
|
|
value := v.Field(i)
|
|
|
|
|
|
|
|
if !value.IsValid() {
|
|
|
|
fmt.Printf("Field: %s is nil or invalid\n", field)
|
|
|
|
continue
|
2024-10-23 16:05:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the field is a string, array, or slice
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
if value.String() != "" {
|
|
|
|
fmt.Printf("Field: %s is a String with value: %s\n", field, value.String())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
if value.Len() != 0 {
|
|
|
|
fmt.Printf("Field: %s is a Slice with length: %d\n", field, value.Len())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Array:
|
|
|
|
if value.Len() != 0 {
|
|
|
|
fmt.Printf("Field: %s is an Array with length: %d\n", field, value.Len())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !value.IsValid() {
|
|
|
|
fmt.Println("Field ptr: value:", value)
|
|
|
|
fmt.Printf("Field ptr: %s is of type: %s\n", field, value.Kind())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("Field: %s is of type: %s\n", field, value.Kind())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
// Print the field name and value
|
|
|
|
// fmt.Printf("Field: %s, Value: %v\n", field, value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("Provided variable is not a struct.")
|
|
|
|
}
|
|
|
|
return empty
|
|
|
|
}
|
|
|
|
|
|
|
|
// dump out all the fields in libvirtxml.DomainDeviceList
|
|
|
|
func libvirtxmlDomainEmpty(mydom libvirtxml.Domain) bool {
|
|
|
|
var empty bool = true
|
|
|
|
// Get the reflection object of the variable
|
|
|
|
v := reflect.ValueOf(mydom)
|
|
|
|
|
|
|
|
// Ensure that we are working with a struct
|
|
|
|
if v.Kind() == reflect.Struct {
|
|
|
|
// fmt.Println("Fields and values in libvirtxml.DomainDeviceList:")
|
|
|
|
|
|
|
|
// Loop through each field in the struct
|
|
|
|
for i := 0; i < v.NumField(); i++ {
|
|
|
|
// Get field name
|
|
|
|
field := v.Type().Field(i).Name
|
|
|
|
|
|
|
|
// Get field value
|
|
|
|
value := v.Field(i)
|
|
|
|
|
|
|
|
if !value.IsValid() {
|
|
|
|
fmt.Printf("Field: %s is nil or invalid\n", field)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (field == "IOThreads") || (field == "XMLName") {
|
|
|
|
continue
|
2024-10-23 15:01:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the field is a string, array, or slice
|
|
|
|
switch value.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
if value.String() != "" {
|
|
|
|
fmt.Printf("Field: %s is a String with value: %s\n", field, value.String())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Slice:
|
|
|
|
if value.Len() != 0 {
|
|
|
|
fmt.Printf("Field: %s is a Slice with length: %d\n", field, value.Len())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Array:
|
|
|
|
if value.Len() != 0 {
|
|
|
|
fmt.Printf("Field: %s is an Array with length: %d\n", field, value.Len())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
if !value.IsValid() {
|
|
|
|
fmt.Println("Field ptr: value:", value)
|
|
|
|
fmt.Printf("Field ptr: %s is of type: %s\n", field, value.Kind())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Printf("Field: %s is of type: %s\n", field, value.Kind())
|
|
|
|
empty = false
|
|
|
|
}
|
|
|
|
// Print the field name and value
|
|
|
|
// fmt.Printf("Field: %s, Value: %v\n", field, value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Println("Provided variable is not a struct.")
|
|
|
|
}
|
|
|
|
return empty
|
|
|
|
}
|