aboutsummaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorDarragh Bailey <dbailey@hpe.com>2017-05-18 14:41:37 +0100
committerDarragh Bailey <dbailey@hpe.com>2017-06-09 11:01:32 +0100
commit11e5470565452347efadf0b6d9f920daeb633d72 (patch)
tree5e9dc871ba032c5771b6579f38bc72536570143e /libvirt
parent6199c053aec485d010c66d95ff88cb2eca25cf4b (diff)
downloadterraform-provider-libvirt-11e5470565452347efadf0b6d9f920daeb633d72.tar
terraform-provider-libvirt-11e5470565452347efadf0b6d9f920daeb633d72.tar.gz
Use file type definition for disks
Using file type definition for disks allows virt-aa-helper to identify the backing file correctly from the generated XML and add the necessary permissions to permit qemu to be able to access the storage disk provided it is located within a storage pool. Ensure that reading of existing tfstate using pool/volume definition remains working for upgrade compatibility by checking first if `File` is a non-zero string. Fixes #126
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/disk_def.go10
-rw-r--r--libvirt/resource_libvirt_domain.go48
2 files changed, 24 insertions, 34 deletions
diff --git a/libvirt/disk_def.go b/libvirt/disk_def.go
index 737364c3..a659cdb3 100644
--- a/libvirt/disk_def.go
+++ b/libvirt/disk_def.go
@@ -17,8 +17,10 @@ type defDisk struct {
Type string `xml:"type,attr"`
} `xml:"format"`
Source struct {
- Pool string `xml:"pool,attr"`
- Volume string `xml:"volume,attr"`
+ File string `xml:"file,attr,omitempty"`
+ // retain Pool/Volume for compatibility with existing tfstate
+ Pool string `xml:"pool,attr,omitempty"`
+ Volume string `xml:"volume,attr,omitempty"`
} `xml:"source"`
Target struct {
Dev string `xml:"dev,attr"`
@@ -32,7 +34,7 @@ type defDisk struct {
func newDefDisk() defDisk {
disk := defDisk{}
- disk.Type = "volume"
+ disk.Type = "file"
disk.Device = "disk"
disk.Format.Type = "qcow2"
disk.Target.Bus = "virtio"
@@ -45,7 +47,7 @@ func newDefDisk() defDisk {
func newCDROM() defDisk {
disk := defDisk{}
- disk.Type = "volume"
+ disk.Type = "file"
disk.Device = "cdrom"
disk.Target.Dev = "hda"
disk.Target.Bus = "ide"
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index 6be18fe4..106f5fed 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -260,21 +260,12 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", volumeKey)
}
- diskVolumeName, err := diskVolume.GetName()
+ diskVolumeFile, err := diskVolume.GetPath()
if err != nil {
- return fmt.Errorf("Error retrieving volume name: %s", err)
- }
- diskPool, err := diskVolume.LookupPoolByVolume()
- if err != nil {
- return fmt.Errorf("Error retrieving pool for volume: %s", err)
- }
- diskPoolName, err := diskPool.GetName()
- if err != nil {
- return fmt.Errorf("Error retrieving pool name: %s", err)
+ return fmt.Errorf("Error retrieving volume file: %s", err)
}
- disk.Source.Volume = diskVolumeName
- disk.Source.Pool = diskPoolName
+ disk.Source.File = diskVolumeFile
disks = append(disks, disk)
}
@@ -664,17 +655,23 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error {
disks := make([]map[string]interface{}, 0)
for _, diskDef := range domainDef.Devices.Disks {
- virPool, err := virConn.LookupStoragePoolByName(diskDef.Source.Pool)
- if err != nil {
- return fmt.Errorf("Error retrieving pool for disk: %s", err)
+ var virVol libvirt.VirStorageVol
+ if len(diskDef.Source.File) > 0 {
+ virVol, err = virConn.LookupStorageVolByPath(diskDef.Source.File)
+ } else {
+ virPool, err := virConn.LookupStoragePoolByName(diskDef.Source.Pool)
+ if err != nil {
+ return fmt.Errorf("Error retrieving pool for disk: %s", err)
+ }
+ defer virPool.Free()
+
+ virVol, err = virPool.LookupStorageVolByName(diskDef.Source.Volume)
}
- defer virPool.Free()
+ defer virVol.Free()
- virVol, err := virPool.LookupStorageVolByName(diskDef.Source.Volume)
if err != nil {
return fmt.Errorf("Error retrieving volume for disk: %s", err)
}
- defer virVol.Free()
virVolKey, err := virVol.GetKey()
if err != nil {
@@ -964,21 +961,12 @@ func newDiskForCloudInit(virConn *libvirt.VirConnection, volumeKey string) (defD
if err != nil {
return disk, fmt.Errorf("Can't retrieve volume %s", volumeKey)
}
- diskVolumeName, err := diskVolume.GetName()
- if err != nil {
- return disk, fmt.Errorf("Error retrieving volume name: %s", err)
- }
- diskPool, err := diskVolume.LookupPoolByVolume()
- if err != nil {
- return disk, fmt.Errorf("Error retrieving pool for volume: %s", err)
- }
- diskPoolName, err := diskPool.GetName()
+ diskVolumeFile, err := diskVolume.GetPath()
if err != nil {
- return disk, fmt.Errorf("Error retrieving pool name: %s", err)
+ return disk, fmt.Errorf("Error retrieving volume file: %s", err)
}
- disk.Source.Volume = diskVolumeName
- disk.Source.Pool = diskPoolName
+ disk.Source.File = diskVolumeFile
return disk, nil
}