-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathvolume.go
261 lines (228 loc) · 7.21 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
package libvirt
import (
"encoding/xml"
"fmt"
libvirt "github.com/libvirt/libvirt-go"
"github.com/libvirt/libvirt-go-xml"
"log"
"strconv"
"strings"
"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,
},
}
}
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(volumeName, poolName, baseVolumeName, source, volumeFormat string, client *Client) error {
var volume *libvirt.StorageVol
log.Printf("[DEBUG] Create a libvirt volume with name %s for pool %s from the base volume %s", volumeName, poolName, baseVolumeName)
virConn := client.libvirt
// TODO: lock pool
//client.poolMutexKV.Lock(poolName)
//defer client.poolMutexKV.Unlock(poolName)
volume, err := getVolumeFromPool(volumeName, poolName, virConn)
if err == nil {
return fmt.Errorf("storage volume '%s' already exists", volumeName)
}
volumeDef := newDefVolume()
volumeDef.Name = volumeName
volumeDef.Target.Format.Type = volumeFormat
var img image
// an source image was given, this mean we can't choose size
if source != "" {
if baseVolumeName != "" {
return fmt.Errorf("'base_volume_id' can't be specified when also 'source' is given")
}
if img, err = newImage(source); err != nil {
return err
}
// update the image in the description, even if the file has not changed
size, err := img.Size()
if err != nil {
return err
}
log.Printf("Image %s image is: %d bytes", img, size)
volumeDef.Capacity.Unit = "B"
volumeDef.Capacity.Value = size
} else if baseVolumeName != "" {
volume = nil
volumeDef.Capacity.Value = uint64(size)
baseVolume, err := getVolumeFromPool(baseVolumeName, poolName, virConn)
if err != nil {
return fmt.Errorf("Can't retrieve volume %s", baseVolumeName)
}
backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume)
if err != nil {
return fmt.Errorf("Could not retrieve backing store %s", baseVolumeName)
}
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
pool, err := virConn.LookupStoragePoolByName(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)
})
if err != nil {
return fmt.Errorf("can't find storage pool '%s'", poolName)
}
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)
}
if source != "" {
err = img.Import(newCopier(client.libvirt, volume, volumeDef.Capacity.Value), volumeDef)
if err != nil {
return fmt.Errorf("Error while uploading source %s: %s", img.String(), err)
}
}
log.Printf("[INFO] Volume ID: %s", key)
return nil
}
func getVolumeFromPool(volumeName, poolName string, virConn *libvirt.Connect) (*libvirt.StorageVol, error) {
pool, err := virConn.LookupStoragePoolByName(poolName)
if err != nil {
return nil, fmt.Errorf("can't find storage pool %q: %v", poolName, err)
}
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 exists. Its name needs to be
// unique.
volume, err := pool.LookupStorageVolByName(volumeName)
if err != nil {
return nil, fmt.Errorf("can't retrieve volume %q: %v", volumeName, err)
}
//defer volume.Free()
return volume, nil
}
// DeleteVolume removes the volume identified by `key` from libvirt
func DeleteVolume(name string, poolName string, client *Client) error {
virConn := client.libvirt
volume, err := getVolumeFromPool(name, poolName, virConn)
if err != nil {
return fmt.Errorf("failed getting volume from pool: %v", err)
}
// TODO: add locking support
//poolName, err := pool.GetName()
//if err != nil {
// return fmt.Errorf("Error retrieving name of volume: %s", err)
//}
//client.poolMutexKV.Lock(poolName)
//defer client.poolMutexKV.Unlock(poolName)
// 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", name, err)
}
err = volume.Delete(0)
if err != nil {
return fmt.Errorf("Can't delete volume %s: %s", name, err)
}
return 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))
}