100 lines
2.4 KiB
Go
100 lines
2.4 KiB
Go
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
package virtigolib
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"libvirt.org/go/libvirtxml"
|
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
// generate the XML for 'virsh create'
|
|
func GenerateDropletXml(dirs []string, d *pb.Droplet, domcfg *libvirtxml.Domain, hostname string) error {
|
|
if d == nil {
|
|
return errors.New("*droplet == nil")
|
|
}
|
|
domcfg.Type = "kvm"
|
|
domcfg.Name = d.Hostname
|
|
domcfg.UUID = d.Uuid
|
|
|
|
var i uint
|
|
i = uint(d.Memory / (1024 * 1024))
|
|
|
|
// var tmp string
|
|
// tmp = domcfg.VCPU
|
|
domcfg.VCPU = new(libvirtxml.DomainVCPU)
|
|
domcfg.VCPU.Value = uint(d.Cpus)
|
|
|
|
domcfg.Memory = new(libvirtxml.DomainMemory)
|
|
domcfg.Memory.Value = i
|
|
domcfg.Memory.Unit = "MiB"
|
|
|
|
fmt.Printf("Virt Memory %d %s\n", domcfg.Memory.Value, domcfg.Memory.Unit)
|
|
|
|
// addEthernet(domcfg, "04:44:33:11:22:11", "worldbr")
|
|
// addEthernet(domcfg, "04:44:33:33:44:55", "greenbr")
|
|
|
|
var count int = 0
|
|
for _, n := range d.Networks {
|
|
log.Info("add network", d.Hostname, "mac addr", n.Mac, "interface", n.Name)
|
|
if n.Name != "worldbr" {
|
|
log.Info("OVERRIDE BRIDGE WITH 'worldbr'")
|
|
}
|
|
addEthernetBridge(domcfg, n.Mac, "worldbr")
|
|
// addEthernetTap(domcfg, n.Mac)
|
|
count += 1
|
|
}
|
|
if count == 1 {
|
|
// this is normal
|
|
} else {
|
|
log.Info("WRONG NUMBER OF ETHERNET INTERFACES:", count)
|
|
}
|
|
|
|
// add a check here to make these unique
|
|
// setRandomMacs(domcfg)
|
|
|
|
if len(dirs) == 0 {
|
|
dirs = append(dirs, "/var/lib/libvirt/images")
|
|
dirs = append(dirs, "/home/nfs2")
|
|
dirs = append(dirs, "/home/nfs3")
|
|
}
|
|
log.Info("Should look in dirs:", dirs)
|
|
for _, disk := range d.Disks {
|
|
fullname := findDisk(dirs, disk.Filename)
|
|
if fullname == "" {
|
|
log.Info("can not find disk", d.Hostname, "dir", disk.Filepath, "filename", disk.Filename)
|
|
os.Exit(-1)
|
|
} else {
|
|
// qcow := "/home/nfs/" + d.Hostname + ".qcow2"
|
|
setSimpleDisk(domcfg, fullname)
|
|
}
|
|
}
|
|
|
|
return writeoutXml(domcfg, d.Hostname)
|
|
}
|
|
|
|
func findDisk(dirs []string, filename string) string {
|
|
for _, dirname := range dirs {
|
|
// log.Info("look in dir", dirname)
|
|
var count int
|
|
newdir, _ := os.ReadDir(dirname)
|
|
for _, file := range newdir {
|
|
count += 1
|
|
if file.Name() == filename {
|
|
log.Info("Found file", filename, "in", dirname)
|
|
return filepath.Join(dirname, file.Name())
|
|
}
|
|
}
|
|
if count == 0 {
|
|
log.Info("Warning? dirname", dirname, "was empty. Not mounted?")
|
|
}
|
|
}
|
|
return ""
|
|
}
|