-
Notifications
You must be signed in to change notification settings - Fork 129
/
libvirt_storage.go
236 lines (207 loc) · 6.36 KB
/
libvirt_storage.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
/*
Copyright 2017 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package libvirttools
import (
"fmt"
"sync"
"github.com/golang/glog"
libvirt "github.com/libvirt/libvirt-go"
libvirtxml "github.com/libvirt/libvirt-go-xml"
"github.com/Mirantis/virtlet/pkg/diskimage"
"github.com/Mirantis/virtlet/pkg/virt"
)
type libvirtStorageConnection struct {
// Trying to do several storage-related operations at the same time
// may cause a race condition.
// As right now Virtlet uses just one storage pool, a single sync.Mutex
// is enough for handling the storage.
sync.Mutex
conn libvirtConnection
}
var _ virt.StorageConnection = &libvirtStorageConnection{}
func newLibvirtStorageConnection(conn libvirtConnection) *libvirtStorageConnection {
return &libvirtStorageConnection{conn: conn}
}
func (sc *libvirtStorageConnection) CreateStoragePool(def *libvirtxml.StoragePool) (virt.StoragePool, error) {
xml, err := def.Marshal()
if err != nil {
return nil, err
}
glog.V(2).Infof("Creating storage pool:\n%s", xml)
p, err := sc.conn.invoke(func(c *libvirt.Connect) (interface{}, error) {
return c.StoragePoolCreateXML(xml, 0)
})
if err != nil {
return nil, err
}
return &libvirtStoragePool{Mutex: &sc.Mutex, conn: sc.conn, p: p.(*libvirt.StoragePool)}, nil
}
func (sc *libvirtStorageConnection) LookupStoragePoolByName(name string) (virt.StoragePool, error) {
p, err := sc.conn.invoke(func(c *libvirt.Connect) (interface{}, error) {
return c.LookupStoragePoolByName(name)
})
if err != nil {
libvirtErr, ok := err.(libvirt.Error)
if ok && libvirtErr.Code == libvirt.ERR_NO_STORAGE_POOL {
return nil, virt.ErrStoragePoolNotFound
}
return nil, err
}
return &libvirtStoragePool{Mutex: &sc.Mutex, conn: sc.conn, p: p.(*libvirt.StoragePool)}, nil
}
func (sc *libvirtStorageConnection) ListPools() ([]virt.StoragePool, error) {
pools, err := sc.conn.invoke(func(c *libvirt.Connect) (interface{}, error) {
return c.ListAllStoragePools(0)
})
if err != nil {
return nil, err
}
var r []virt.StoragePool
for _, p := range pools.([]libvirt.StoragePool) {
r = append(r, &libvirtStoragePool{Mutex: &sc.Mutex, conn: sc.conn, p: &p})
}
return r, nil
}
func (sc *libvirtStorageConnection) PutFiles(imagePath string, files map[string][]byte) error {
return diskimage.Put(imagePath, files)
}
type libvirtStoragePool struct {
*sync.Mutex
conn libvirtConnection
p *libvirt.StoragePool
}
var _ virt.StoragePool = &libvirtStoragePool{}
func (pool *libvirtStoragePool) CreateStorageVol(def *libvirtxml.StorageVolume) (virt.StorageVolume, error) {
pool.Lock()
defer pool.Unlock()
xml, err := def.Marshal()
if err != nil {
return nil, err
}
glog.V(2).Infof("Creating storage volume:\n%s", xml)
v, err := pool.p.StorageVolCreateXML(xml, 0)
if err != nil {
return nil, err
}
// libvirt may report qcow2 file size as 'capacity' for
// qcow2-based volumes for some time after creating them.
// Here we work around this problem by refreshing the pool
// which invokes acquiring volume info.
if err := pool.p.Refresh(0); err != nil {
v.Delete(0)
return nil, fmt.Errorf("failed to refresh the storage pool: %v", err)
}
return &libvirtStorageVolume{Mutex: pool.Mutex, name: def.Name, v: v}, nil
}
func (pool *libvirtStoragePool) ListVolumes() ([]virt.StorageVolume, error) {
pool.Lock()
defer pool.Unlock()
volumes, err := pool.p.ListAllStorageVolumes(0)
if err != nil {
return nil, err
}
r := make([]virt.StorageVolume, len(volumes))
for n, v := range volumes {
name, err := v.GetName()
if err != nil {
return nil, err
}
// need to make a copy here
curVolume := v
r[n] = &libvirtStorageVolume{Mutex: pool.Mutex, name: name, v: &curVolume}
}
return r, nil
}
func (pool *libvirtStoragePool) LookupVolumeByName(name string) (virt.StorageVolume, error) {
pool.Lock()
defer pool.Unlock()
v, err := pool.p.LookupStorageVolByName(name)
if err != nil {
libvirtErr, ok := err.(libvirt.Error)
if ok && libvirtErr.Code == libvirt.ERR_NO_STORAGE_VOL {
return nil, virt.ErrStorageVolumeNotFound
}
return nil, err
}
return &libvirtStorageVolume{Mutex: pool.Mutex, name: name, v: v}, nil
}
func (pool *libvirtStoragePool) RemoveVolumeByName(name string) error {
vol, err := pool.LookupVolumeByName(name)
switch {
case err == virt.ErrStorageVolumeNotFound:
return nil
case err != nil:
return err
default:
return vol.Remove()
}
}
func (pool *libvirtStoragePool) XML() (*libvirtxml.StoragePool, error) {
desc, err := pool.p.GetXMLDesc(libvirt.STORAGE_XML_INACTIVE)
if err != nil {
return nil, err
}
var p libvirtxml.StoragePool
if err := p.Unmarshal(desc); err != nil {
return nil, fmt.Errorf("error unmarshalling storage pool definition: %v", err)
}
return &p, nil
}
type libvirtStorageVolume struct {
*sync.Mutex
name string
v *libvirt.StorageVol
}
var _ virt.StorageVolume = &libvirtStorageVolume{}
func (volume *libvirtStorageVolume) Name() string {
return volume.name
}
func (volume *libvirtStorageVolume) Size() (uint64, error) {
volume.Lock()
defer volume.Unlock()
info, err := volume.v.GetInfo()
if err != nil {
return 0, err
}
return info.Capacity, nil
}
func (volume *libvirtStorageVolume) Path() (string, error) {
volume.Lock()
defer volume.Unlock()
return volume.v.GetPath()
}
func (volume *libvirtStorageVolume) Remove() error {
volume.Lock()
defer volume.Unlock()
return volume.v.Delete(0)
}
func (volume *libvirtStorageVolume) Format() error {
volume.Lock()
defer volume.Unlock()
volPath, err := volume.v.GetPath()
if err != nil {
return fmt.Errorf("can't get volume path: %v", err)
}
return diskimage.FormatDisk(volPath)
}
func (volume *libvirtStorageVolume) XML() (*libvirtxml.StorageVolume, error) {
desc, err := volume.v.GetXMLDesc(0)
if err != nil {
return nil, err
}
var v libvirtxml.StorageVolume
if err := v.Unmarshal(desc); err != nil {
return nil, fmt.Errorf("error unmarshalling storage volume definition: %v", err)
}
return &v, nil
}