diff options
author | Thomas Hipp <thipp@suse.de> | 2017-12-01 13:29:00 +0100 |
---|---|---|
committer | Thomas Hipp <thipp@suse.de> | 2017-12-01 15:23:31 +0100 |
commit | 9be42d2996fd6acd34b1bda8a4245d3aa7544135 (patch) | |
tree | 182fc4aa0093693c723f967fd68748e3bf0c302b | |
parent | 380076c1943ebd10c5e5f54ad4ac751a736c5c1f (diff) | |
download | terraform-provider-libvirt-9be42d2996fd6acd34b1bda8a4245d3aa7544135.tar terraform-provider-libvirt-9be42d2996fd6acd34b1bda8a4245d3aa7544135.tar.gz |
support local cdroms
Local cdroms can now be added directly to the domain. See the
following example:
```hcl
resource "libvirt_domain" "test-domain" {
name = "test"
disk {
file = "path/to/file.iso"
}
}
```
Furthermore, a small Tiny Core Linux (TCL) ISO image has been added to
the testdata. This can be used for testing purposes.
Signed-off-by: Thomas Hipp <thipp@suse.de>
-rw-r--r-- | libvirt/disk_def.go | 15 | ||||
-rw-r--r-- | libvirt/disk_def_test.go | 10 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 33 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain_test.go | 15 | ||||
-rw-r--r-- | libvirt/testdata/tcl.iso | bin | 0 -> 16834560 bytes | |||
-rwxr-xr-x | travis/setup-guest | 3 | ||||
-rw-r--r-- | website/docs/r/domain.html.markdown | 8 |
7 files changed, 49 insertions, 35 deletions
diff --git a/libvirt/disk_def.go b/libvirt/disk_def.go index f4e4e86a..6e4cceea 100644 --- a/libvirt/disk_def.go +++ b/libvirt/disk_def.go @@ -24,21 +24,6 @@ func newDefDisk(i int) libvirtxml.DomainDisk { } } -func newCDROM() libvirtxml.DomainDisk { - return libvirtxml.DomainDisk{ - Type: "file", - Device: "cdrom", - Target: &libvirtxml.DomainDiskTarget{ - Dev: "hda", - Bus: "ide", - }, - Driver: &libvirtxml.DomainDiskDriver{ - Name: "qemu", - Type: "raw", - }, - } -} - func randomWWN(strlen int) string { const chars = "abcdef0123456789" result := make([]byte, strlen) diff --git a/libvirt/disk_def_test.go b/libvirt/disk_def_test.go index a6184cc6..d13d26d8 100644 --- a/libvirt/disk_def_test.go +++ b/libvirt/disk_def_test.go @@ -21,13 +21,3 @@ func TestDefaultDiskMarshall(t *testing.T) { t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) } } - -func TestDefaultCDROMMarshall(t *testing.T) { - b := newCDROM() - buf := new(bytes.Buffer) - enc := xml.NewEncoder(buf) - enc.Indent(" ", " ") - if err := enc.Encode(b); err != nil { - t.Fatalf("could not marshall this:\n%s", spew.Sdump(b)) - } -} diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 95a7b7fe..95ac6c52 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -450,6 +450,24 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error if !strings.HasSuffix(url.Path, ".qcow2") { disk.Driver.Type = "raw" } + } else if _, ok := diskMap["file"].(string); ok { + // support for local disks, e.g. CDs + disk.Type = "file" + disk.Source = &libvirtxml.DomainDiskSource{ + File: diskMap["file"].(string), + } + + if strings.HasSuffix(diskMap["file"].(string), ".iso") { + disk.Device = "cdrom" + disk.Target = &libvirtxml.DomainDiskTarget{ + Dev: "hda", + Bus: "ide", + } + disk.Driver = &libvirtxml.DomainDiskDriver{ + Name: "qemu", + Type: "raw", + } + } } disks = append(disks, disk) @@ -899,7 +917,10 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { // Emulator is the same as the default don't set it in domainDef // or it will show as changed d.Set("emulator", domainDef.Devices.Emulator) - var disks []map[string]interface{} + var ( + disks []map[string]interface{} + disk map[string]interface{} + ) for _, diskDef := range domainDef.Devices.Disks { // network drives do not have a volume associated if diskDef.Type == "network" { @@ -914,10 +935,14 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { if err != nil { return err } - disk := map[string]interface{}{ + disk = map[string]interface{}{ "url": url.String(), } disks = append(disks, disk) + } else if diskDef.Device == "cdrom" { + disk = map[string]interface{}{ + "file": diskDef.Source.File, + } } else { var virVol *libvirt.StorageVol if len(diskDef.Source.File) > 0 { @@ -942,11 +967,11 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error retrieving volume for disk: %s", err) } - disk := map[string]interface{}{ + disk = map[string]interface{}{ "volume_id": virVolKey, } - disks = append(disks, disk) } + disks = append(disks, disk) } d.Set("disks", disks) var filesystems []map[string]interface{} diff --git a/libvirt/resource_libvirt_domain_test.go b/libvirt/resource_libvirt_domain_test.go index 47c99ef5..43ce2478 100644 --- a/libvirt/resource_libvirt_domain_test.go +++ b/libvirt/resource_libvirt_domain_test.go @@ -290,9 +290,15 @@ func TestAccLibvirtDomainKernelInitrdCmdline(t *testing.T) { func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { var domain libvirt.Domain + currentDir, err := os.Getwd() + if err != nil { + t.Fatal("Unexpected error:", err) + } + var config = fmt.Sprintf(` - resource "libvirt_volume" "acceptance-test-volume" { - name = "terraform-test" + resource "libvirt_network" "acceptance-test-network" { + name = "terraform-test" + addresses = ["10.17.3.0/24"] } resource "libvirt_domain" "acceptance-test-domain" { @@ -303,11 +309,12 @@ func TestAccLibvirtDomain_NetworkInterface(t *testing.T) { network_interface = { network_name = "default" mac = "52:54:00:A9:F5:17" + wait_for_lease = 1 } disk { - volume_id = "${libvirt_volume.acceptance-test-volume.id}" + file = "%s/testdata/tcl.iso" } - }`) + }`, currentDir) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/libvirt/testdata/tcl.iso b/libvirt/testdata/tcl.iso Binary files differnew file mode 100644 index 00000000..303088d0 --- /dev/null +++ b/libvirt/testdata/tcl.iso diff --git a/travis/setup-guest b/travis/setup-guest index 42568e81..0a671b2f 100755 --- a/travis/setup-guest +++ b/travis/setup-guest @@ -20,3 +20,6 @@ mkdir /pool-default chmod a+rwx /pool-default virsh pool-define pool.xml virsh pool-start default +echo -e 'user = "root"\ngroup = "root"' >> /etc/libvirt/qemu.conf +systemctl restart libvirtd + diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index 01ed1fc3..ff9ab0a3 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -237,9 +237,9 @@ The `disk` block supports: * `volume_id` - (Optional) The volume id to use for this disk. * `url` - (Optional) The http url to use as the block device for this disk (read-only) +* `file` - (Optional) The filename to use as the block device for this disk (read-only) -While both `volume_id` and `url` are optional, it is intended that you use either a volume or a -url. +While `volume_id`, `url` and `file` are optional, it is intended that you use one of them. * `scsi` - (Optional) Use a scsi controller for this disk. The controller model is set to `virtio-scsi` @@ -268,6 +268,10 @@ resource "libvirt_domain" "domain1" { disk { url = "http://foo.com/install.iso" } + + disk { + file = "/absolute/path/to/disk.iso" + } } ``` |