aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libvirt/resource_libvirt_domain.go9
-rw-r--r--libvirt/utils_domain_def.go27
-rw-r--r--libvirt/utils_domain_def_test.go36
3 files changed, 71 insertions, 1 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index 272d1eec..4dae7c29 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -305,7 +305,6 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
cmdlineArgs = append(cmdlineArgs, fmt.Sprintf("%s=%v", k, v))
}
}
-
sort.Strings(cmdlineArgs)
domainDef.OS.KernelArgs = strings.Join(cmdlineArgs, " ")
@@ -905,6 +904,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/utils_domain_def.go b/libvirt/utils_domain_def.go
index a712f145..4e46cb62 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,29 @@ 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
+ 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..b497c02c
--- /dev/null
+++ b/libvirt/utils_domain_def_test.go
@@ -0,0 +1,36 @@
+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)
+ }
+ }
+
+ {
+ 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)
+ }
+ }
+}