2024-10-26 04:08:35 -05:00
|
|
|
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
|
2024-10-31 06:46:16 -05:00
|
|
|
package virtigolib
|
2024-10-26 04:08:35 -05:00
|
|
|
|
|
|
|
import (
|
2024-10-26 21:49:08 -05:00
|
|
|
"errors"
|
2024-10-26 04:08:35 -05:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"libvirt.org/go/libvirtxml"
|
|
|
|
|
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
2024-10-26 10:12:07 -05:00
|
|
|
"go.wit.com/log"
|
2024-10-26 04:08:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// generate the XML for 'virsh create'
|
2024-10-26 20:54:46 -05:00
|
|
|
func GenerateDropletXml(dirs []string, d *pb.Droplet, domcfg *libvirtxml.Domain, hostname string) error {
|
2024-10-26 21:49:08 -05:00
|
|
|
if d == nil {
|
|
|
|
return errors.New("*droplet == nil")
|
|
|
|
}
|
2024-10-26 04:08:35 -05:00
|
|
|
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)
|
|
|
|
|
2024-10-26 20:54:46 -05:00
|
|
|
if len(dirs) == 0 {
|
2024-10-26 21:49:08 -05:00
|
|
|
dirs = append(dirs, "/var/lib/libvirt/images")
|
2024-10-26 20:54:46 -05:00
|
|
|
dirs = append(dirs, "/home/nfs2")
|
|
|
|
dirs = append(dirs, "/home/nfs3")
|
2024-10-26 10:34:26 -05:00
|
|
|
}
|
2024-10-26 20:54:46 -05:00
|
|
|
log.Info("Should look in dirs:", dirs)
|
2024-10-26 04:08:35 -05:00
|
|
|
for _, disk := range d.Disks {
|
2024-10-26 20:54:46 -05:00
|
|
|
fullname := findDisk(dirs, disk.Filename)
|
2024-10-26 04:08:35 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-26 12:32:52 -05:00
|
|
|
return writeoutXml(domcfg, d.Hostname)
|
2024-10-26 04:08:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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 ""
|
|
|
|
}
|