make the seed ISO unique per VM ID

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-05-16 04:33:07 +00:00
parent a735e4f67e
commit a2dd31a1bb
3 changed files with 38 additions and 23 deletions

56
main.go
View File

@ -23,18 +23,18 @@ import (
"github.com/digitalocean/go-libvirt" "github.com/digitalocean/go-libvirt"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/philandstuff/dhall-golang/v5" "github.com/philandstuff/dhall-golang/v5"
"golang.org/x/tools/txtar"
) )
//go:embed data/* templates/* //go:embed data/* templates/*
var data embed.FS var data embed.FS
var ( var (
distro = flag.String("distro", "alpine-edge", "the linux distro to install in the VM") distro = flag.String("distro", "alpine-edge", "the linux distro to install in the VM")
name = flag.String("name", "", "the name of the VM, defaults to a random common blade name") name = flag.String("name", "", "the name of the VM, defaults to a random common blade name")
zvolPrefix = flag.String("zvol-prefix", "rpool/mkvm-test/", "the prefix to use for zvol names") zvolPrefix = flag.String("zvol-prefix", "rpool/mkvm-test/", "the prefix to use for zvol names")
zvolSize = flag.Int("zvol-size", 0, "the number of gigabytes for the virtual machine disk") zvolSize = flag.Int("zvol-size", 0, "the number of gigabytes for the virtual machine disk")
memory = flag.Int("memory", 512, "the number of megabytes of ram for the virtual machine") memory = flag.Int("memory", 512, "the number of megabytes of ram for the virtual machine")
cloudConfig = flag.String("user-data", "./var/xe-base.yaml", "path to a cloud-config userdata file")
) )
func main() { func main() {
@ -87,12 +87,16 @@ func main() {
log.Fatalf("can't connect to libvirt: %v", err) log.Fatalf("can't connect to libvirt: %v", err)
} }
vmID := uuid.New().String()
log.Println("plan:") log.Println("plan:")
log.Printf("name: %s", *name) log.Printf("name: %s", *name)
log.Printf("zvol: %s (%d GB)", zvol, *zvolSize) log.Printf("zvol: %s (%d GB)", zvol, *zvolSize)
log.Printf("base image url: %s", resultDistro.DownloadURL) log.Printf("base image url: %s", resultDistro.DownloadURL)
log.Printf("mac address: %s", macAddress) log.Printf("mac address: %s", macAddress)
log.Printf("ram: %d MB", *memory) log.Printf("ram: %d MB", *memory)
log.Printf("id: %s", vmID)
log.Printf("cloud config: %s", *cloudConfig)
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Print("press enter if this looks okay:") fmt.Print("press enter if this looks okay:")
@ -133,29 +137,38 @@ func main() {
tmpl := template.Must(template.ParseFS(data, "templates/*")) tmpl := template.Must(template.ParseFS(data, "templates/*"))
var buf = bytes.NewBuffer(nil) var buf = bytes.NewBuffer(nil)
err = tmpl.ExecuteTemplate(buf, "cloud-config.txtar", struct{ Name string }{Name: *name}) err = tmpl.ExecuteTemplate(buf, "meta-data", struct {
Name string
ID string
}{
Name: *name,
ID: vmID,
})
if err != nil { if err != nil {
log.Fatalf("can't generate cloud-config: %v", err) log.Fatalf("can't generate cloud-config: %v", err)
} }
arc := txtar.Parse(buf.Bytes())
dir, err := os.MkdirTemp("", "mkvm") dir, err := os.MkdirTemp("", "mkvm")
if err != nil { if err != nil {
log.Fatalf("can't make directory: %v", err) log.Fatalf("can't make directory: %v", err)
} }
for _, file := range arc.Files { fout, err := os.Create(filepath.Join(dir, "meta-data"))
fout, err := os.Create(filepath.Join(dir, file.Name)) if err != nil {
if err != nil { log.Fatal(err)
log.Fatal(err) }
} _, err = fout.Write(buf.Bytes())
_, err = fout.Write(file.Data) if err != nil {
if err != nil { log.Fatal(err)
log.Fatal(err) }
} fout.Close()
err = run("cp", *cloudConfig, filepath.Join(dir, "user-data"))
if err != nil {
log.Fatal(err)
} }
isoPath := filepath.Join(cdir, "seed", fmt.Sprintf("%s-%s.iso", *name, resultDistro.Name)) isoPath := filepath.Join(cdir, "seed", fmt.Sprintf("%s-%s.iso", *name, vmID))
err = run( err = run(
"genisoimage", "genisoimage",
@ -173,7 +186,6 @@ func main() {
} }
ram := *memory * 1024 ram := *memory * 1024
vmID := uuid.New().String()
buf.Reset() buf.Reset()
// zfs create -V 20G rpool/safe/vm/sena // zfs create -V 20G rpool/safe/vm/sena
@ -274,7 +286,11 @@ func connectToLibvirt() (*libvirt.Libvirt, error) {
} }
func mkVM(l *libvirt.Libvirt, buf *bytes.Buffer) (*libvirt.Domain, error) { func mkVM(l *libvirt.Libvirt, buf *bytes.Buffer) (*libvirt.Domain, error) {
domain, err := l.DomainCreateXML(buf.String(), libvirt.DomainNone) domain, err := l.DomainDefineXML(buf.String())
if err != nil {
return nil, err
}
err = l.DomainCreate(domain)
return &domain, err return &domain, err
} }

2
templates/meta-data Normal file
View File

@ -0,0 +1,2 @@
instance-id: {{.ID}}
local-hostname: {{.Name}}

View File

@ -1,6 +1,3 @@
-- meta-data --
local-hostname: {{.Name}}
-- user-data --
#cloud-config #cloud-config
#vim:syntax=yaml #vim:syntax=yaml