diff options
Diffstat (limited to 'libvirt/utils.go')
-rw-r--r-- | libvirt/utils.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/libvirt/utils.go b/libvirt/utils.go index ed645938..1ec739ae 100644 --- a/libvirt/utils.go +++ b/libvirt/utils.go @@ -3,6 +3,7 @@ package libvirt import ( "crypto/rand" "fmt" + libvirt "github.com/dmacvicar/libvirt-go" "log" "time" ) @@ -53,3 +54,40 @@ func RandomMACAddress() (string, error) { return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]), nil } + +// Remove the volume identified by `key` from libvirt +func RemoveVolume(virConn *libvirt.VirConnection, key string) error { + volume, err := virConn.LookupStorageVolByKey(key) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s", key) + } + defer volume.Free() + + // Refresh the pool of the volume so that libvirt knows it is + // not longer in use. + volPool, err := volume.LookupPoolByVolume() + if err != nil { + return fmt.Errorf("Error retrieving pool for volume: %s", err) + } + defer volPool.Free() + + WaitForSuccess("Error refreshing pool for volume", func() error { + return volPool.Refresh(0) + }) + + // Workaround for redhat#1293804 + // https://bugzilla.redhat.com/show_bug.cgi?id=1293804#c12 + // Does not solve the problem but it makes it happen less often. + _, err = volume.GetXMLDesc(0) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s XML desc: %s", key, err) + } + + err = volume.Delete(0) + if err != nil { + return fmt.Errorf("Can't delete volume %s: %s", key, err) + } + + return nil + +} |