summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDuncan Mac-Vicar P <dmacvicar@gmail.com>2017-11-27 08:29:46 +0100
committerGitHub <noreply@github.com>2017-11-27 08:29:46 +0100
commitb7894a88a97a5882084d6ccfa5c1496f51dc5ae2 (patch)
tree2af08467c51e8b55af7facc54dab8bcc7a18f326
parentbdbfc7ca1d4251fe5ecad10857ccec65a69afc65 (diff)
parent9f7f0253a320a3fe96c2970fa486064fd7cb3963 (diff)
downloadterraform-provider-libvirt-b7894a88a97a5882084d6ccfa5c1496f51dc5ae2.tar
terraform-provider-libvirt-b7894a88a97a5882084d6ccfa5c1496f51dc5ae2.tar.gz
Merge pull request #242 from dmacvicar/kernel_initrd
Add support for kernel/initrd/cmdline options
-rw-r--r--libvirt/resource_libvirt_domain.go46
-rw-r--r--libvirt/resource_libvirt_domain_test.go87
-rw-r--r--libvirt/testdata/README.md13
-rw-r--r--libvirt/testdata/initrd.imgbin0 -> 98 bytes
-rwxr-xr-xlibvirt/testdata/tetris.elfbin0 -> 19384 bytes
-rw-r--r--libvirt/utils_domain_def.go31
-rw-r--r--libvirt/utils_domain_def_test.go45
-rw-r--r--website/docs/r/domain.html.markdown94
8 files changed, 316 insertions, 0 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index a3b16792..b5d0fb6d 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -9,6 +9,7 @@ import (
"net"
"net/url"
"os"
+ "sort"
"strconv"
"strings"
"time"
@@ -170,6 +171,27 @@ func resourceLibvirtDomain() *schema.Resource {
Default: "/usr/bin/qemu-system-x86_64",
Optional: true,
},
+ "kernel": {
+ Type: schema.TypeString,
+ Required: false,
+ Optional: true,
+ ForceNew: false,
+ },
+ "initrd": {
+ Type: schema.TypeString,
+ Required: false,
+ Optional: true,
+ ForceNew: false,
+ },
+ "cmdline": {
+ Type: schema.TypeList,
+ Optional: true,
+ Required: false,
+ ForceNew: true,
+ Elem: &schema.Schema{
+ Type: schema.TypeMap,
+ },
+ },
},
}
}
@@ -267,6 +289,22 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
}
}
+ if kernel, ok := d.GetOk("kernel"); ok {
+ domainDef.OS.Kernel = kernel.(string)
+ }
+ if initrd, ok := d.GetOk("initrd"); ok {
+ domainDef.OS.Initrd = initrd.(string)
+ }
+
+ var cmdlineArgs []string
+ for i := 0; i < d.Get("cmdline.#").(int); i++ {
+ for k, v := range d.Get(fmt.Sprintf("cmdline.%d", i)).(map[string]interface{}) {
+ cmdlineArgs = append(cmdlineArgs, fmt.Sprintf("%s=%v", k, v))
+ }
+ }
+ sort.Strings(cmdlineArgs)
+ domainDef.OS.KernelArgs = strings.Join(cmdlineArgs, " ")
+
if cpu, ok := d.GetOk("cpu"); ok {
cpuMap := cpu.(map[string]interface{})
if cpuMode, ok := cpuMap["mode"]; ok {
@@ -863,6 +901,14 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
d.Set("autostart", autostart)
d.Set("arch", domainDef.OS.Type.Arch)
+ cmdLines, err := splitKernelCmdLine(domainDef.OS.KernelArgs)
+ if err != nil {
+ return err
+ }
+ d.Set("cmdline", cmdLines)
+ d.Set("kernel", domainDef.OS.Kernel)
+ d.Set("initrd", domainDef.OS.Initrd)
+
caps, err := getHostCapabilities(virConn)
if err != nil {
return err
diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go
index 2c9f1b52..055c67c4 100644
--- a/libvirt/resource_libvirt_domain_test.go
+++ b/libvirt/resource_libvirt_domain_test.go
@@ -235,6 +235,58 @@ func TestAccLibvirtDomainURLDisk(t *testing.T) {
}
+func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) {
+ var domain libvirt.Domain
+ var kernel libvirt.StorageVol
+ var initrd libvirt.StorageVol
+
+ var config = fmt.Sprintf(`
+ resource "libvirt_volume" "kernel" {
+ source = "testdata/tetris.elf"
+ name = "kernel"
+ pool = "default"
+ format = "raw"
+ }
+
+ resource "libvirt_volume" "initrd" {
+ source = "testdata/initrd.img"
+ name = "initrd"
+ pool = "default"
+ format = "raw"
+ }
+
+ resource "libvirt_domain" "acceptance-test-domain" {
+ name = "terraform-test-domain"
+ kernel = "${libvirt_volume.kernel.id}"
+ initrd = "${libvirt_volume.initrd.id}"
+ cmdline {
+ foo = 1
+ bar = "bye"
+ }
+ cmdline {
+ foo = 2
+ }
+ }`)
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtDomainDestroy,
+ Steps: []resource.TestStep{
+ resource.TestStep{
+ Config: config,
+ Check: resource.ComposeTestCheckFunc(
+ testAccCheckLibvirtVolumeExists("libvirt_volume.kernel", &kernel),
+ testAccCheckLibvirtVolumeExists("libvirt_volume.initrd", &initrd),
+ testAccCheckLibvirtDomainExists("libvirt_domain.acceptance-test-domain", &domain),
+ testAccCheckLibvirtDomainKernelInitrdCmdline(&domain, &kernel, &initrd),
+ ),
+ },
+ },
+ })
+
+}
+
func TestAccLibvirtDomain_NetworkInterface(t *testing.T) {
var domain libvirt.Domain
@@ -658,6 +710,41 @@ func testAccCheckLibvirtURLDisk(u *url.URL, domain *libvirt.Domain) resource.Tes
}
}
+func testAccCheckLibvirtDomainKernelInitrdCmdline(domain *libvirt.Domain, kernel *libvirt.StorageVol, initrd *libvirt.StorageVol) resource.TestCheckFunc {
+ return func(s *terraform.State) error {
+ xmlDesc, err := domain.GetXMLDesc(0)
+ if err != nil {
+ return fmt.Errorf("Error retrieving libvirt domain XML description: %s", err)
+ }
+
+ domainDef := newDomainDef()
+ err = xml.Unmarshal([]byte(xmlDesc), &domainDef)
+ if err != nil {
+ return fmt.Errorf("Error reading libvirt domain XML description: %s", err)
+ }
+
+ key, err := kernel.GetKey()
+ if err != nil {
+ return fmt.Errorf("Can't get kernel volume id")
+ }
+ if domainDef.OS.Kernel != key {
+ return fmt.Errorf("Kernel is not set correctly: '%s' vs '%s'", domainDef.OS.Kernel, key)
+ }
+
+ key, err = initrd.GetKey()
+ if err != nil {
+ return fmt.Errorf("Can't get initrd volume id")
+ }
+ if domainDef.OS.Initrd != key {
+ return fmt.Errorf("Initrd is not set correctly: '%s' vs '%s'", domainDef.OS, key)
+ }
+ if domainDef.OS.KernelArgs != "bar=bye foo=1 foo=2" {
+ return fmt.Errorf("Kernel args not set correctly: '%s'", domainDef.OS.KernelArgs)
+ }
+ return nil
+ }
+}
+
func createNvramFile() (string, error) {
// size of an accepted, valid, nvram backing store
NVRAMDummyBuffer := make([]byte, 131072)
diff --git a/libvirt/testdata/README.md b/libvirt/testdata/README.md
new file mode 100644
index 00000000..d571154f
--- /dev/null
+++ b/libvirt/testdata/README.md
@@ -0,0 +1,13 @@
+# Test data
+
+* tetris.elf: https://github.com/programble/bare-metal-tetris
+
+```
+License
+
+Copyright © 2013–2014, Curtis McEnroe programble@gmail.com
+
+Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+```
diff --git a/libvirt/testdata/initrd.img b/libvirt/testdata/initrd.img
new file mode 100644
index 00000000..d2f6e345
--- /dev/null
+++ b/libvirt/testdata/initrd.img
Binary files differ
diff --git a/libvirt/testdata/tetris.elf b/libvirt/testdata/tetris.elf
new file mode 100755
index 00000000..6bbc9c7c
--- /dev/null
+++ b/libvirt/testdata/tetris.elf
Binary files differ
diff --git a/libvirt/utils_domain_def.go b/libvirt/utils_domain_def.go
index a712f145..670d8b02 100644
--- a/libvirt/utils_domain_def.go
+++ b/libvirt/utils_domain_def.go
@@ -4,6 +4,7 @@ import (
"fmt"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"log"
+ "strings"
)
func getGuestForArchType(caps libvirtxml.Caps, arch string, virttype string) (libvirtxml.CapsGuest, error) {
@@ -47,3 +48,33 @@ func getOriginalMachineName(caps libvirtxml.Caps, arch string, virttype string,
}
return targetmachine, nil // There wasn't a canonical mapping to this
}
+
+// as kernal args allow duplicate keys, we use a list of maps
+// we jump to a next map as soon as we find a duplicate
+// key
+func splitKernelCmdLine(cmdLine string) ([]map[string]string, error) {
+ var cmdLines []map[string]string
+ if len(cmdLine) == 0 {
+ return cmdLines, nil
+ }
+
+ currCmdLine := make(map[string]string)
+ argVals := strings.Split(cmdLine, " ")
+ for _, argVal := range argVals {
+ kv := strings.Split(argVal, "=")
+ if len(kv) != 2 {
+ return nil, fmt.Errorf("Can't parse kernel command line: '%s'", cmdLine)
+ }
+ k, v := kv[0], kv[1]
+ // if the key is duplicate, start a new map
+ if _, ok := currCmdLine[k]; ok {
+ cmdLines = append(cmdLines, currCmdLine)
+ currCmdLine = make(map[string]string)
+ }
+ currCmdLine[k] = v
+ }
+ if len(currCmdLine) > 0 {
+ cmdLines = append(cmdLines, currCmdLine)
+ }
+ return cmdLines, nil
+}
diff --git a/libvirt/utils_domain_def_test.go b/libvirt/utils_domain_def_test.go
new file mode 100644
index 00000000..7ff57203
--- /dev/null
+++ b/libvirt/utils_domain_def_test.go
@@ -0,0 +1,45 @@
+package libvirt
+
+import (
+ "reflect"
+ "testing"
+
+ "github.com/davecgh/go-spew/spew"
+)
+
+func init() {
+ spew.Config.Indent = "\t"
+}
+
+func TestSplitKernelCmdLine(t *testing.T) {
+ e := []map[string]string{{"foo": "bar"}, {"foo": "bar", "key": "val"}}
+ r, err := splitKernelCmdLine("foo=bar foo=bar key=val")
+ if !reflect.DeepEqual(r, e) {
+ t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), spew.Sdump(e))
+ }
+ if err != nil {
+ t.Error(err)
+ }
+}
+
+func TestSplitKernelInvalidCmdLine(t *testing.T) {
+ v := "foo=barfoo=bar"
+ r, err := splitKernelCmdLine(v)
+ if r != nil {
+ t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), err)
+ }
+ if err == nil {
+ t.Errorf("Expected error for parsing '%s'", v)
+ }
+}
+
+func TestSplitKernelEmptyCmdLine(t *testing.T) {
+ var e []map[string]string
+ r, err := splitKernelCmdLine("")
+ if !reflect.DeepEqual(r, e) {
+ t.Fatalf("got='%s' expected='%s'", spew.Sdump(r), spew.Sdump(e))
+ }
+ if err != nil {
+ t.Error(err)
+ }
+}
diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown
index b047a6e7..f008501e 100644
--- a/website/docs/r/domain.html.markdown
+++ b/website/docs/r/domain.html.markdown
@@ -59,6 +59,100 @@ The following arguments are supported:
[below](#define-boot-device-order).
* `emulator` - (Optional) The path of the emulator to use
+### Kernel and boot arguments
+
+* `kernel` - (Optional) The path of the kernel to boot
+
+If you are using a qcow2 volume, you can pass the id of the volume (eg. `${libvirt_volume.kernel.id}`)
+as they are local to the hypervisor.
+
+Given that you can define a volume from a remote http file, this means, you can also have remote kernels.
+
+```hcl
+resource "libvirt_volume" "kernel" {
+ source = "http://download.opensuse.org/tumbleweed/repo/oss/boot/x86_64/loader/linux"
+ name = "kernel"
+ pool = "default"
+ format = "raw"
+}
+
+resource "libvirt_domain" "domain-suse" {
+ name = "suse"
+ memory = "1024"
+ vcpu = 1
+
+ kernel = "${libvirt_volume.kernel.id}"
+
+ // ...
+}
+```
+
+* `kernel` - (Optional) The path of the initrd to boot.
+
+You can use it in the same way as the kernel.
+
+* `cmdline` - (Optional) Arguments to the kernel
+
+```hcl
+resource "libvirt_domain" "domain-suse" {
+ name = "suse"
+ memory = "1024"
+ vcpu = 1
+
+ kernel = "${libvirt_volume.kernel.id}"
+
+ cmdline {
+ arg1 = "value1"
+ arg2 = "value2"
+ }
+}
+```
+
+Also note that the `cmd` block is actually a list of maps, so it is possible to
+declare several of them by using either the literal list and map syntax as in
+the following examples:
+
+```hcl
+resource "libvirt_domain" "my_machine" {
+ //...
+
+ cmdline {
+ arg1 = "value1"
+ }
+ cmdline {
+ arg2 = "value2"
+ }
+}
+```
+
+```hcl
+resource "libvirt_domain" "my_machine" {
+ ...
+ cmdline = [
+ {
+ arg1 = "value1"
+ },
+ {
+ arg2 = "value2"
+ }
+ ]
+}
+```
+The kernel supports passing the same option multiple times. If you need this, use separate cmdline blocks.
+
+```hcl
+resource "libvirt_domain" "my_machine" {
+ //...
+
+ cmdline {
+ arg1 = "value1"
+ }
+ cmdline {
+ arg1 = "value2"
+ }
+}
+```
+
### UEFI images
Some extra arguments are also provided for using UEFI images: