-
Notifications
You must be signed in to change notification settings - Fork 56
/
volume.go
262 lines (226 loc) · 7.29 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package libvirt
import (
"encoding/xml"
"fmt"
libvirt "github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
providerconfigv1 "github.com/openshift/cluster-api-provider-libvirt/cloud/libvirt/providerconfig/v1alpha1"
"io"
"log"
"time"
)
const (
// TODO: support size in the API
size = 17706254336
)
// WaitSleepInterval time
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
}
log.Printf("[DEBUG] %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,
},
}
}
// network transparent image
type image interface {
Size() (uint64, error)
Import(func(io.Reader) error, libvirtxml.StorageVolume) error
String() string
}
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 createVolume(machineName string, machineProviderConfig *providerconfigv1.LibvirtMachineProviderConfig) error {
var poolName string
var baseVolumeID string
var volumeName string
var volumeFormat = "qcow2"
var volume *libvirt.StorageVol
if machineProviderConfig.Volume.PoolName != "" {
poolName = machineProviderConfig.Volume.PoolName
} else {
return fmt.Errorf("machine does not has a Volume.PoolName value")
}
if machineProviderConfig.Volume.BaseVolumeID != "" {
baseVolumeID = machineProviderConfig.Volume.BaseVolumeID
} else {
return fmt.Errorf("machine does not has a Volume.BaseVolumeID value")
}
if machineProviderConfig.Volume.VolumeName != "" {
volumeName = machineProviderConfig.Volume.VolumeName
} else if machineName != "" {
volumeName = machineName
} else {
return fmt.Errorf("machine does not has a Volume.VolumeName value")
}
client, err := buildClient(machineProviderConfig.URI)
if err != nil {
return fmt.Errorf("Failed to build libvirt client: %s", err)
}
log.Printf("[DEBUG] Create a libvirt volume with name %s for pool %s from the base volume %s", volumeName, poolName, baseVolumeID)
virConn := client.libvirt
// TODO: lock pool
//client.poolMutexKV.Lock(poolName)
//defer client.poolMutexKV.Unlock(poolName)
pool, err := virConn.LookupStoragePoolByName(poolName)
if err != nil {
return fmt.Errorf("can't find storage pool '%s'", poolName)
}
defer pool.Free()
// Refresh the pool of the volume so that libvirt knows it is
// not longer in use.
waitForSuccess("error refreshing pool for volume", func() error {
return pool.Refresh(0)
})
// Check whether the storage volume already exists. Its name needs to be
// unique.
if _, err := pool.LookupStorageVolByName(volumeName); err == nil {
return fmt.Errorf("storage volume '%s' already exists", volumeName)
}
volumeDef := newDefVolume()
volumeDef.Name = volumeName
volumeDef.Target.Format.Type = volumeFormat
if baseVolumeID != "" {
volume = nil
volumeDef.Capacity.Value = uint64(size)
baseVolume, err := client.libvirt.LookupStorageVolByKey(baseVolumeID)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", baseVolumeID)
}
backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume)
if err != nil {
return fmt.Errorf("Could not retrieve backing store %s", baseVolumeID)
}
volumeDef.BackingStore = &backingStoreDef
}
if volume == nil {
volumeDefXML, err := xml.Marshal(volumeDef)
if err != nil {
return fmt.Errorf("Error serializing libvirt volume: %s", err)
}
// create the volume
v, err := pool.StorageVolCreateXML(string(volumeDefXML), 0)
if err != nil {
return fmt.Errorf("Error creating libvirt volume: %s", err)
}
volume = v
defer volume.Free()
}
// we use the key as the id
key, err := volume.GetKey()
if err != nil {
return fmt.Errorf("Error retrieving volume key: %s", err)
}
log.Printf("[INFO] Volume ID: %s", key)
return nil
}
func deleteVolume(volumeName string, URI string) error {
client, err := buildClient(URI)
if err != nil {
return fmt.Errorf("Failed to build libvirt client: %s", err)
}
return removeVolume(client, volumeName)
}
// removeVolume removes the volume identified by `key` from libvirt
func removeVolume(client *Client, name string) error {
volumePath := fmt.Sprintf(baseVolumePath+"%s", name)
volume, err := client.libvirt.LookupStorageVolByPath(volumePath)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", volumePath)
}
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()
// TODO: add locking support
//poolName, err := volPool.GetName()
//if err != nil {
// return fmt.Errorf("Error retrieving name of volume: %s", err)
//}
//client.poolMutexKV.Lock(poolName)
//defer client.poolMutexKV.Unlock(poolName)
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", volumePath, err)
}
err = volume.Delete(0)
if err != nil {
return fmt.Errorf("Can't delete volume %s: %s", volumePath, err)
}
return nil
}