/
domain_def.go
137 lines (122 loc) · 3.24 KB
/
domain_def.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package libvirt
import (
"encoding/xml"
"fmt"
"os"
"github.com/hashicorp/terraform/helper/schema"
libvirt "github.com/libvirt/libvirt-go"
libvirtxml "github.com/libvirt/libvirt-go-xml"
)
// from existing domain return its XMLdefintion
func getXMLDomainDefFromLibvirt(domain *libvirt.Domain) (libvirtxml.Domain, error) {
domainXMLDesc, err := domain.GetXMLDesc(0)
if err != nil {
return libvirtxml.Domain{}, fmt.Errorf("Error retrieving libvirt domain XML description: %s", err)
}
domainDef := newDomainDef()
err = xml.Unmarshal([]byte(domainXMLDesc), &domainDef)
if err != nil {
return libvirtxml.Domain{}, fmt.Errorf("Error reading libvirt domain XML description: %s", err)
}
return domainDef, nil
}
// note source and target are not initialized
func newFilesystemDef() libvirtxml.DomainFilesystem {
return libvirtxml.DomainFilesystem{
AccessMode: "mapped", // A safe default value
ReadOnly: &libvirtxml.DomainFilesystemReadOnly{},
}
}
// Creates a domain definition with the defaults
// the provider uses
func newDomainDef() libvirtxml.Domain {
domainDef := libvirtxml.Domain{
OS: &libvirtxml.DomainOS{
Type: &libvirtxml.DomainOSType{
Type: "hvm",
},
},
Memory: &libvirtxml.DomainMemory{
Unit: "MiB",
Value: 512,
},
VCPU: &libvirtxml.DomainVCPU{
Placement: "static",
Value: 1,
},
CPU: &libvirtxml.DomainCPU{},
Devices: &libvirtxml.DomainDeviceList{
Graphics: []libvirtxml.DomainGraphic{
{
Spice: &libvirtxml.DomainGraphicSpice{
AutoPort: "yes",
},
},
},
Channels: []libvirtxml.DomainChannel{
{
Target: &libvirtxml.DomainChannelTarget{
VirtIO: &libvirtxml.DomainChannelTargetVirtIO{
Name: "org.qemu.guest_agent.0",
},
},
},
},
RNGs: []libvirtxml.DomainRNG{
{
Model: "virtio",
Backend: &libvirtxml.DomainRNGBackend{
Random: &libvirtxml.DomainRNGBackendRandom{},
},
},
},
},
Features: &libvirtxml.DomainFeatureList{
PAE: &libvirtxml.DomainFeature{},
ACPI: &libvirtxml.DomainFeature{},
APIC: &libvirtxml.DomainFeatureAPIC{},
},
}
if v := os.Getenv("TERRAFORM_LIBVIRT_TEST_DOMAIN_TYPE"); v != "" {
domainDef.Type = v
} else {
domainDef.Type = "kvm"
}
return domainDef
}
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
}
guest, err := getGuestForArchType(caps, d.OS.Type.Arch, d.OS.Type.Type)
if err != nil {
return d, err
}
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 {
return d, err
}
d.OS.Type.Machine = canonicalmachine
return d, nil
}