From fdb9486330b90d2f75d39b33a0c5abf5ccf5b4ea Mon Sep 17 00:00:00 2001 From: Thomas Hipp Date: Fri, 22 Dec 2017 14:02:17 +0100 Subject: domain: honor arch, machine, and emulator settings The settings 'arch', 'machine', and 'emulator' should be honored. The default values have been removed. Instead, if not provided, the values are determined by consulting libvirt directly. This fixes #262. Signed-off-by: Thomas Hipp --- libvirt/domain_def.go | 33 +++++++++++++++++++++++++++------ libvirt/resource_libvirt_domain.go | 18 +++++++----------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/libvirt/domain_def.go b/libvirt/domain_def.go index 5f443a62..dc74c608 100644 --- a/libvirt/domain_def.go +++ b/libvirt/domain_def.go @@ -1,9 +1,11 @@ package libvirt import ( + "os" + + "github.com/hashicorp/terraform/helper/schema" libvirt "github.com/libvirt/libvirt-go" libvirtxml "github.com/libvirt/libvirt-go-xml" - "os" ) func newFilesystemDef() libvirtxml.DomainFilesystem { @@ -20,9 +22,7 @@ func newDomainDef() libvirtxml.Domain { domainDef := libvirtxml.Domain{ OS: &libvirtxml.DomainOS{ Type: &libvirtxml.DomainOSType{ - Type: "hvm", - Arch: "x86_64", - Machine: "pc", + Type: "hvm", }, }, Memory: &libvirtxml.DomainMemory{ @@ -75,8 +75,19 @@ func newDomainDef() libvirtxml.Domain { return domainDef } -func newDomainDefForConnection(virConn *libvirt.Connect) (libvirtxml.Domain, error) { +func newDomainDefForConnection(virConn *libvirt.Connect, rd *schema.ResourceData) (libvirtxml.Domain, error) { d := newDomainDef() + + if arch, ok := rd.GetOk("arch"); ok { + d.OS.Type.Arch = arch.(string) + } else { + arch, err := getHostArchitecture(virConn) + if err != nil { + return d, err + } + d.OS.Type.Arch = arch + } + caps, err := getHostCapabilities(virConn) if err != nil { return d, err @@ -86,7 +97,17 @@ func newDomainDefForConnection(virConn *libvirt.Connect) (libvirtxml.Domain, err return d, err } - d.Devices.Emulator = guest.Arch.Emulator + if emulator, ok := rd.GetOk("emulator"); ok { + d.Devices.Emulator = emulator.(string) + } else { + d.Devices.Emulator = guest.Arch.Emulator + } + + if machine, ok := rd.GetOk("machine"); ok { + d.OS.Type.Machine = machine.(string) + } else if len(guest.Arch.Machines) > 0 { + d.OS.Type.Machine = guest.Arch.Machines[0].Name + } canonicalmachine, err := getCanonicalMachineName(caps, d.OS.Type.Arch, d.OS.Type.Type, d.OS.Type.Machine) if err != nil { diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 25516f7b..404fcb84 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -144,12 +144,12 @@ func resourceLibvirtDomain() *schema.Resource { "machine": { Type: schema.TypeString, Optional: true, - Default: "pc", + Computed: true, }, "arch": { Type: schema.TypeString, Optional: true, - Default: "x86_64", + Computed: true, }, "boot_device": { Type: schema.TypeList, @@ -161,8 +161,8 @@ func resourceLibvirtDomain() *schema.Resource { }, "emulator": { Type: schema.TypeString, - Default: "/usr/bin/qemu-system-x86_64", Optional: true, + Computed: true, }, "kernel": { Type: schema.TypeString, @@ -230,10 +230,11 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf(LibVirtConIsNil) } - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } + if name, ok := d.GetOk("name"); ok { domainDef.Name = name.(string) } @@ -307,10 +308,6 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } } - domainDef.OS.Type.Arch = d.Get("arch").(string) - domainDef.OS.Type.Machine = d.Get("machine").(string) - domainDef.Devices.Emulator = d.Get("emulator").(string) - if firmware, ok := d.GetOk("firmware"); ok { firmwareFile := firmware.(string) if _, err := os.Stat(firmwareFile); os.IsNotExist(err) { @@ -871,7 +868,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] read: obtained XML desc for domain:\n%s", xmlDesc) - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } @@ -894,7 +891,6 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { d.Set("cpu", domainDef.CPU) d.Set("arch", domainDef.OS.Type.Arch) d.Set("autostart", autostart) - d.Set("arch", domainDef.OS.Type.Arch) cmdLines, err := splitKernelCmdLine(domainDef.OS.KernelArgs) if err != nil { @@ -1116,7 +1112,7 @@ func resourceLibvirtDomainDelete(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Error retrieving libvirt domain XML description: %s", err) } - domainDef, err := newDomainDefForConnection(virConn) + domainDef, err := newDomainDefForConnection(virConn, d) if err != nil { return err } -- cgit v1.2.3