// Copyright 2024 WIT.COM Inc Licensed GPL 3.0 package main import ( "encoding/xml" "fmt" "os" "go.wit.com/log" "libvirt.org/go/libvirtxml" ) func makeStandardXml(d *DropletT) *libvirtxml.Domain { log.Info("create new xml file for:", d.pb.Hostname) domcfg := &libvirtxml.Domain{} addDefaults(domcfg, "standard.x86") addDefaults(domcfg, "memory") addDefaults(domcfg, "network") addDefaults(domcfg, "spice") addDefaults(domcfg, "qcow") addDefaults(domcfg, d.pb.Hostname) return domcfg } func writeoutXml(domcfg *libvirtxml.Domain, filename string) bool { xmldoc, err := domcfg.Marshal() if err != nil { fmt.Println("can't make xml file error:\n", err) return false } outfile := "/tmp/" + filename + ".xml" regfile, _ := os.OpenFile(outfile, os.O_RDWR|os.O_CREATE, 0666) fmt.Fprintln(regfile, xmldoc) log.Info("File is in", outfile) regfile.Close() return true } 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) } } 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 } } func readXml(filename string) (*libvirtxml.Domain, error) { log.Verbose("parse xml file:", filename) 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 { log.Info("merge xml file:", filename) 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 } func setSimpleDisk(domcfg *libvirtxml.Domain, filename string) { // 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) } func getMacs(domcfg *libvirtxml.Domain) []string { var macs []string // Iterate over the network interfaces and print the MAC addresses for _, iface := range domcfg.Devices.Interfaces { if iface.MAC != nil { // 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) macs = append(macs, iface.MAC.Address) } else { fmt.Printf("Interface: %s, MAC Address: not available\n", iface.Target.Dev) } } return macs } // removes all the ethernet interfaces func clearEthernet(domcfg *libvirtxml.Domain) { // Clear out the existing disks (if any) domcfg.Devices.Interfaces = nil } // add a new ethernet interface with mac assigned to bridge name func addEthernet(domcfg *libvirtxml.Domain, mac string, brname string) { // Define a new disk with "mynew.qcow2" 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) } func setRandomMacs(domcfg *libvirtxml.Domain) { 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) // fmt.Printf("mac addr %+v\n", x.MAC) fmt.Printf("mac addr %s\n", x.MAC.Address) } } // just go through the libvirt xml object and dump out everything // that is "standard". This is just a way to double check that // there might be something interesting in a VM func dumpNonStandardXML(domcfg *libvirtxml.Domain) { // Add more parts you are interested in fmt.Printf("CPU Model: %+v\n", domcfg.CPU) // 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) } // 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 // clear out this crap if domcfg.OnPoweroff == "destroy" { domcfg.OnPoweroff = "" } if domcfg.OnCrash == "destroy" { domcfg.OnCrash = "" } if domcfg.OnReboot == "restart" { domcfg.OnReboot = "" } // 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") } domcfg.Devices.Interfaces = nil for _, iface := range domcfg.Devices.Interfaces { fmt.Printf("- Network Interface: %+v\n", iface) } for _, controller := range domcfg.Devices.Controllers { fmt.Printf("- Controller: Type: %s, Index: %d\n", controller.Type, controller.Index) } 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)) os.Exit(-1) }