forked from openshift/cluster-api-provider-libvirt
/
volume.go
118 lines (102 loc) · 2.95 KB
/
volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package client
import (
"encoding/xml"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/golang/glog"
libvirt "github.com/libvirt/libvirt-go"
libvirtxml "github.com/libvirt/libvirt-go-xml"
)
const (
// TODO: support size in the API
size = 17706254336
)
// ErrVolumeNotFound is returned when a domain is not found
var ErrVolumeNotFound = errors.New("Domain not found")
var waitSleepInterval = 1 * time.Second
// waitTimeout time
var waitTimeout = 5 * time.Minute
// waitForSuccess wait for success and timeout after 5 minutes.
func waitForSuccess(errorMessage string, f func() error) error {
start := time.Now()
for {
err := f()
if err == nil {
return nil
}
glog.Infof("%s. Re-trying.\n", err)
time.Sleep(waitSleepInterval)
if time.Since(start) > waitTimeout {
return fmt.Errorf("%s: %s", errorMessage, err)
}
}
}
func newDefVolume() libvirtxml.StorageVolume {
return libvirtxml.StorageVolume{
Target: &libvirtxml.StorageVolumeTarget{
Format: &libvirtxml.StorageVolumeTargetFormat{
Type: "qcow2",
},
Permissions: &libvirtxml.StorageVolumeTargetPermissions{
Mode: "644",
},
},
Capacity: &libvirtxml.StorageVolumeSize{
Unit: "bytes",
Value: 1,
},
}
}
func newDefBackingStoreFromLibvirt(baseVolume *libvirt.StorageVol) (libvirtxml.StorageVolumeBackingStore, error) {
baseVolumeDef, err := newDefVolumeFromLibvirt(baseVolume)
if err != nil {
return libvirtxml.StorageVolumeBackingStore{}, fmt.Errorf("could not get volume: %s", err)
}
baseVolPath, err := baseVolume.GetPath()
if err != nil {
return libvirtxml.StorageVolumeBackingStore{}, fmt.Errorf("could not get base image path: %s", err)
}
backingStoreDef := libvirtxml.StorageVolumeBackingStore{
Path: baseVolPath,
Format: &libvirtxml.StorageVolumeTargetFormat{
Type: baseVolumeDef.Target.Format.Type,
},
}
return backingStoreDef, nil
}
func newDefVolumeFromLibvirt(volume *libvirt.StorageVol) (libvirtxml.StorageVolume, error) {
name, err := volume.GetName()
if err != nil {
return libvirtxml.StorageVolume{}, fmt.Errorf("could not get name for volume: %s", err)
}
volumeDefXML, err := volume.GetXMLDesc(0)
if err != nil {
return libvirtxml.StorageVolume{}, fmt.Errorf("could not get XML description for volume %s: %s", name, err)
}
volumeDef, err := newDefVolumeFromXML(volumeDefXML)
if err != nil {
return libvirtxml.StorageVolume{}, fmt.Errorf("could not get a volume definition from XML for %s: %s", volumeDef.Name, err)
}
return volumeDef, nil
}
// Creates a volume definition from a XML
func newDefVolumeFromXML(s string) (libvirtxml.StorageVolume, error) {
var volumeDef libvirtxml.StorageVolume
err := xml.Unmarshal([]byte(s), &volumeDef)
if err != nil {
return libvirtxml.StorageVolume{}, err
}
return volumeDef, nil
}
func timeFromEpoch(str string) time.Time {
var s, ns int
ts := strings.Split(str, ".")
if len(ts) == 2 {
ns, _ = strconv.Atoi(ts[1])
}
s, _ = strconv.Atoi(ts[0])
return time.Unix(int64(s), int64(ns))
}