91 lines
2.1 KiB
Go
91 lines
2.1 KiB
Go
// Copyright 2024 WIT.COM Inc Licensed GPL 3.0
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"go.wit.com/log"
|
|
"libvirt.org/go/libvirtxml"
|
|
)
|
|
|
|
// generate the XML for 'virsh create'
|
|
func startDropletXml(start string) {
|
|
meDrop := findDroplet(start)
|
|
d := meDrop.pb
|
|
if d == nil {
|
|
log.Info("droplet is unknown:", start)
|
|
os.Exit(0)
|
|
}
|
|
log.Info("start droplet here:", d.Hostname)
|
|
domcfg := &libvirtxml.Domain{}
|
|
|
|
addDefaultXml(domcfg, "standard.x86")
|
|
// addDefaultXml(domcfg, "memory")
|
|
// addDefaultXml(domcfg, "network")
|
|
addDefaultXml(domcfg, "spice")
|
|
addDefaultXml(domcfg, "qcow")
|
|
|
|
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")
|
|
|
|
for _, n := range d.Networks {
|
|
log.Info("add network", d.Hostname, "mac addr", n.Mac, "interface", n.Name)
|
|
}
|
|
|
|
// add a check here to make these unique
|
|
// setRandomMacs(domcfg)
|
|
|
|
for _, disk := range d.Disks {
|
|
fullname := findDisk(disk.Filename)
|
|
if fullname == "" {
|
|
log.Info("can not find disk", d.Hostname, "dir", disk.Filepath, "filename", disk.Filename)
|
|
} else {
|
|
// qcow := "/home/nfs/" + d.Hostname + ".qcow2"
|
|
setSimpleDisk(domcfg, fullname)
|
|
}
|
|
}
|
|
|
|
writeoutXml(domcfg, d.Hostname)
|
|
os.Exit(-1)
|
|
}
|
|
|
|
func findDisk(filename string) string {
|
|
for _, dirname := range me.cluster.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 ""
|
|
}
|