diff options
author | Eamonn O'Toole <eamonn.otoole@hpe.com> | 2017-01-20 15:28:40 +0000 |
---|---|---|
committer | Eamonn O'Toole <eamonn.otoole@hpe.com> | 2017-02-08 13:58:27 +0000 |
commit | ea96b6696d29713639d9f167e1d38616bcd5f753 (patch) | |
tree | 2873c40b8bbc76c3309195ef281278313ff49443 | |
parent | b43db5136f5d80267a8172289af3133e4ab241ab (diff) | |
download | terraform-provider-libvirt-ea96b6696d29713639d9f167e1d38616bcd5f753.tar terraform-provider-libvirt-ea96b6696d29713639d9f167e1d38616bcd5f753.tar.gz |
Add support for CoreOS ignition and console blocks
A few changes here:
1. CoreOS Ignition support: A CoreOS Ignition file can be
specified for a domain using the "coreos_ignition"
parameter. This requires the emission of additional
XML and also the setting of the XML name-space to
"http://libvirt.org/schemas/domain/qemu/1.0"
2. "graphics" block: A "graphics" block can be specified
in a domain definition. We've added this because we've
found that for some builds of qemu the default "spice"
emulator doesn't work and results in failure to boot the
VMs.
3. "console" block: One or more "console" blocks can be
specified in a domain definition. Note the description
in domain.html.markdown, and the description in
https://libvirt.org/formatdomain.html#elementsConsole
-rw-r--r-- | docs/providers/libvirt/r/domain.html.markdown | 53 | ||||
-rw-r--r-- | libvirt/domain_def.go | 24 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 63 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain_console.go | 34 |
4 files changed, 173 insertions, 1 deletions
diff --git a/docs/providers/libvirt/r/domain.html.markdown b/docs/providers/libvirt/r/domain.html.markdown index ae08872d..995420da 100644 --- a/docs/providers/libvirt/r/domain.html.markdown +++ b/docs/providers/libvirt/r/domain.html.markdown @@ -41,6 +41,13 @@ The following arguments are supported: cloud-init won't cause the domain to be recreated, however the change will have effect on the next reboot. +The following extra argument is provided for CoreOS images: + +* `coreos_ignition` - (Optional) The name of a CoreOS Ignition file. + +Note that to make use of Ignition files with CoreOS the host must be running +QEMU v2.6 or greater. + Some extra arguments are also provided for using UEFI images: * `firmware` - (Optional) The UEFI rom images for exercising UEFI secure boot in a qemu @@ -145,6 +152,7 @@ resource "libvirt_domain" "my_machine" { } ``` + The `network_interface` specifies a network interface that can be connected either to a virtual network (the preferred method on hosts with dynamic / wireless networking configs) or directly to a LAN. @@ -197,6 +205,51 @@ resource "libvirt_domain" "my-domain" { must be installed and running inside of the domain in order to discover the IP addresses of all the network interfaces attached to a LAN. +The optional `graphics` block allows you to override the default graphics settings. The +block supports: + +* `type` - the type of graphics emulation (default is "spice") +* `autoport` - defaults to "yes" +* `listen_type` - "listen type", defaults to "none" + +On occasion we have found it necessary to set a `type` of "vnc" and a `listen_type` of "address" +with certain builds of QEMU. + +The `graphics` block will look as follows: +``` +resource "libvirt_domain" "my_machine" { + ... + graphics { + type = "vnc" + listen_type = "address" + } +} +``` + +The optional `console` block allows you to define a console for the domain. The block +looks as follows: +``` +resource "libvirt_domain" "my_machine" { + ... + console { + type = "pty" + target_port = "0" + target_type = <"serial" or "virtio"> + source_path = "/dev/pts/4" + } +} +``` + +Note the following: +* You can repeat the `console` block to create more than one console, in the same way +that you can repeat `disk` blocks (see above) +* The `target_type` is optional for the first console +* All subsequent `console` blocks must specify a `target_type` of `virtio` +* The `source_path` is optional for all consoles + +See [libvirt Domain XML Console element](https://libvirt.org/formatdomain.html#elementsConsole) +for more information. + ## Attributes Reference * `id` - a unique identifier for the resource diff --git a/libvirt/domain_def.go b/libvirt/domain_def.go index 953fcdb7..e6952d79 100644 --- a/libvirt/domain_def.go +++ b/libvirt/domain_def.go @@ -8,6 +8,7 @@ type defDomain struct { XMLName xml.Name `xml:"domain"` Name string `xml:"name"` Type string `xml:"type,attr"` + Xmlns string `xml:"xmlns:qemu,attr"` Os defOs `xml:"os"` Memory defMemory `xml:"memory"` VCpu defVCpu `xml:"vcpu"` @@ -23,7 +24,8 @@ type defDomain struct { Devices struct { Disks []defDisk `xml:"disk"` NetworkInterfaces []defNetworkInterface `xml:"interface"` - Graphics struct { + Console []defConsole `xml:"console"` + Graphics struct { Type string `xml:"type,attr"` Autoport string `xml:"autoport,attr"` Listen struct { @@ -48,6 +50,10 @@ type defDomain struct { } `xml:"backend"` } `xml:"rng"` } `xml:"devices"` + CmdLine struct { + XMLName xml.Name `xml:"qemu:commandline"` + Cmd []defCmd `xml:"qemu:arg"` + } } type defMetadata struct { @@ -77,6 +83,10 @@ type defVCpu struct { Amount int `xml:",chardata"` } +type defCmd struct { + Value string `xml:"value,attr"` +} + type defLoader struct { ReadOnly string `xml:"readonly,attr,omitempty"` Type string `xml:"type,attr,omitempty"` @@ -88,12 +98,24 @@ type defNvRam struct { File string `xml:",chardata"` } +type defConsole struct { + Type string `xml:"type,attr"` + Source struct { + Path string `xml:"path,attr,omitempty"` + } `xml:"source"` + Target struct { + Type string `xml:"type,attr,omitempty"` + Port string `xml:"port,attr"` + } `xml:"target"` +} + // Creates a domain definition with the defaults // the provider uses func newDomainDef() defDomain { // libvirt domain definition domainDef := defDomain{} domainDef.Type = "kvm" + domainDef.Xmlns = "" domainDef.Os = defOs{} domainDef.Os.Type = defOsType{} diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index bfbf235d..26a121ff 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -73,6 +73,13 @@ func resourceLibvirtDomain() *schema.Resource { Optional: true, ForceNew: false, }, + "coreos_ignition": &schema.Schema{ + Type: schema.TypeString, + Required: false, + Optional: true, + ForceNew: false, + Default: "", + }, "disk": &schema.Schema{ Type: schema.TypeList, Optional: true, @@ -90,6 +97,19 @@ func resourceLibvirtDomain() *schema.Resource { Schema: networkInterfaceCommonSchema(), }, }, + "graphics": &schema.Schema{ + Type: schema.TypeMap, + Optional: true, + Required: false, + }, + "console": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Required: false, + Elem: &schema.Resource{ + Schema: consoleSchema(), + }, + }, }, } } @@ -115,6 +135,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error } domainDef := newDomainDef() + if name, ok := d.GetOk("name"); ok { domainDef.Name = name.(string) } @@ -123,6 +144,32 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error domainDef.Metadata.TerraformLibvirt.Xml = metadata.(string) } + if ignition, ok := d.GetOk("coreos_ignition"); ok { + ignitionFile := ignition.(string) + if _, err := os.Stat(ignitionFile); os.IsNotExist(err) { + return fmt.Errorf("Could not find ignition file '%s'", ignitionFile) + } + var fw_cfg []defCmd + ign_str := fmt.Sprintf("name=opt/com.coreos/config,file=%s", ignitionFile) + fw_cfg = append(fw_cfg, defCmd{"-fw_cfg"}) + fw_cfg = append(fw_cfg, defCmd{ign_str}) + domainDef.CmdLine.Cmd = fw_cfg + domainDef.Xmlns = "http://libvirt.org/schemas/domain/qemu/1.0" + } + + if graphics, ok := d.GetOk("graphics"); ok { + graphics_map := graphics.(map[string]interface{}) + if graphics_type, ok := graphics_map["type"]; ok { + domainDef.Devices.Graphics.Type = graphics_type.(string) + } + if autoport, ok := graphics_map["autoport"]; ok { + domainDef.Devices.Graphics.Type = autoport.(string) + } + if listen_type, ok := graphics_map["listen_type"]; ok { + domainDef.Devices.Graphics.Listen.Type = listen_type.(string) + } + } + if firmware, ok := d.GetOk("firmware"); ok { firmwareFile := firmware.(string) if _, err := os.Stat(firmwareFile); os.IsNotExist(err) { @@ -148,6 +195,22 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error domainDef.Memory.Amount = d.Get("memory").(int) domainDef.VCpu.Amount = d.Get("vcpu").(int) + if consoleCount, ok := d.GetOk("console.#"); ok { + var consoles []defConsole + for i :=0; i < consoleCount.(int); i++ { + console := defConsole{} + consolePrefix := fmt.Sprintf("console.%d", i) + console.Type = d.Get(consolePrefix + ".type").(string) + console.Source.Path = d.Get(consolePrefix + ".source_path").(string) + console.Target.Port = d.Get(consolePrefix + ".target_port").(string) + if target_type, ok := d.GetOk(consolePrefix + ".target_type"); ok { + console.Target.Type = target_type.(string) + } + consoles = append(consoles, console) + } + domainDef.Devices.Console = consoles + } + disksCount := d.Get("disk.#").(int) var disks []defDisk for i := 0; i < disksCount; i++ { diff --git a/libvirt/resource_libvirt_domain_console.go b/libvirt/resource_libvirt_domain_console.go new file mode 100644 index 00000000..eadb4483 --- /dev/null +++ b/libvirt/resource_libvirt_domain_console.go @@ -0,0 +1,34 @@ +package libvirt + +import ( + "github.com/hashicorp/terraform/helper/schema" +) + +func consoleSchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "type": &schema.Schema{ + Type: schema.TypeString, + Optional: false, + Required: true, + ForceNew: true, + }, + "source_path": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Required: false, + ForceNew: true, + }, + "target_port": &schema.Schema{ + Type: schema.TypeString, + Optional: false, + Required: true, + ForceNew: true, + }, + "target_type": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Required: false, + ForceNew: true, + }, + } +} |