forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_pools.go
511 lines (417 loc) · 12.2 KB
/
storage_pools.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package db
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/lxc/lxd/shared/api"
)
// Get all storage pools.
func (n *Node) StoragePools() ([]string, error) {
var name string
query := "SELECT name FROM storage_pools"
inargs := []interface{}{}
outargs := []interface{}{name}
result, err := queryScan(n.db, query, inargs, outargs)
if err != nil {
return []string{}, err
}
if len(result) == 0 {
return []string{}, NoSuchObjectError
}
pools := []string{}
for _, r := range result {
pools = append(pools, r[0].(string))
}
return pools, nil
}
// Get the names of all storage volumes attached to a given storage pool.
func (n *Node) StoragePoolsGetDrivers() ([]string, error) {
var poolDriver string
query := "SELECT DISTINCT driver FROM storage_pools"
inargs := []interface{}{}
outargs := []interface{}{poolDriver}
result, err := queryScan(n.db, query, inargs, outargs)
if err != nil {
return []string{}, err
}
if len(result) == 0 {
return []string{}, NoSuchObjectError
}
drivers := []string{}
for _, driver := range result {
drivers = append(drivers, driver[0].(string))
}
return drivers, nil
}
// Get id of a single storage pool.
func (n *Node) StoragePoolGetID(poolName string) (int64, error) {
poolID := int64(-1)
query := "SELECT id FROM storage_pools WHERE name=?"
inargs := []interface{}{poolName}
outargs := []interface{}{&poolID}
err := dbQueryRowScan(n.db, query, inargs, outargs)
if err != nil {
if err == sql.ErrNoRows {
return -1, NoSuchObjectError
}
}
return poolID, nil
}
// Get a single storage pool.
func (n *Node) StoragePoolGet(poolName string) (int64, *api.StoragePool, error) {
var poolDriver string
poolID := int64(-1)
description := sql.NullString{}
query := "SELECT id, driver, description FROM storage_pools WHERE name=?"
inargs := []interface{}{poolName}
outargs := []interface{}{&poolID, &poolDriver, &description}
err := dbQueryRowScan(n.db, query, inargs, outargs)
if err != nil {
if err == sql.ErrNoRows {
return -1, nil, NoSuchObjectError
}
return -1, nil, err
}
config, err := n.StoragePoolConfigGet(poolID)
if err != nil {
return -1, nil, err
}
storagePool := api.StoragePool{
Name: poolName,
Driver: poolDriver,
}
storagePool.Description = description.String
storagePool.Config = config
return poolID, &storagePool, nil
}
// Get config of a storage pool.
func (n *Node) StoragePoolConfigGet(poolID int64) (map[string]string, error) {
var key, value string
query := "SELECT key, value FROM storage_pools_config WHERE storage_pool_id=?"
inargs := []interface{}{poolID}
outargs := []interface{}{key, value}
results, err := queryScan(n.db, query, inargs, outargs)
if err != nil {
return nil, err
}
config := map[string]string{}
for _, r := range results {
key = r[0].(string)
value = r[1].(string)
config[key] = value
}
return config, nil
}
// Create new storage pool.
func (n *Node) StoragePoolCreate(poolName string, poolDescription string, poolDriver string, poolConfig map[string]string) (int64, error) {
tx, err := begin(n.db)
if err != nil {
return -1, err
}
result, err := tx.Exec("INSERT INTO storage_pools (name, description, driver) VALUES (?, ?, ?)", poolName, poolDescription, poolDriver)
if err != nil {
tx.Rollback()
return -1, err
}
id, err := result.LastInsertId()
if err != nil {
tx.Rollback()
return -1, err
}
err = StoragePoolConfigAdd(tx, id, poolConfig)
if err != nil {
tx.Rollback()
return -1, err
}
err = TxCommit(tx)
if err != nil {
return -1, err
}
return id, nil
}
// Add new storage pool config.
func StoragePoolConfigAdd(tx *sql.Tx, poolID int64, poolConfig map[string]string) error {
str := "INSERT INTO storage_pools_config (storage_pool_id, key, value) VALUES(?, ?, ?)"
stmt, err := tx.Prepare(str)
defer stmt.Close()
if err != nil {
return err
}
for k, v := range poolConfig {
if v == "" {
continue
}
_, err = stmt.Exec(poolID, k, v)
if err != nil {
return err
}
}
return nil
}
// Update storage pool.
func (n *Node) StoragePoolUpdate(poolName, description string, poolConfig map[string]string) error {
poolID, _, err := n.StoragePoolGet(poolName)
if err != nil {
return err
}
tx, err := begin(n.db)
if err != nil {
return err
}
err = StoragePoolUpdateDescription(tx, poolID, description)
if err != nil {
tx.Rollback()
return err
}
err = StoragePoolConfigClear(tx, poolID)
if err != nil {
tx.Rollback()
return err
}
err = StoragePoolConfigAdd(tx, poolID, poolConfig)
if err != nil {
tx.Rollback()
return err
}
return TxCommit(tx)
}
// Update the storage pool description.
func StoragePoolUpdateDescription(tx *sql.Tx, id int64, description string) error {
_, err := tx.Exec("UPDATE storage_pools SET description=? WHERE id=?", description, id)
return err
}
// Delete storage pool config.
func StoragePoolConfigClear(tx *sql.Tx, poolID int64) error {
_, err := tx.Exec("DELETE FROM storage_pools_config WHERE storage_pool_id=?", poolID)
if err != nil {
return err
}
return nil
}
// Delete storage pool.
func (n *Node) StoragePoolDelete(poolName string) (*api.StoragePool, error) {
poolID, pool, err := n.StoragePoolGet(poolName)
if err != nil {
return nil, err
}
_, err = exec(n.db, "DELETE FROM storage_pools WHERE id=?", poolID)
if err != nil {
return nil, err
}
return pool, nil
}
// Get the names of all storage volumes attached to a given storage pool.
func (n *Node) StoragePoolVolumesGetNames(poolID int64) (int, error) {
var volumeName string
query := "SELECT name FROM storage_volumes WHERE storage_pool_id=?"
inargs := []interface{}{poolID}
outargs := []interface{}{volumeName}
result, err := queryScan(n.db, query, inargs, outargs)
if err != nil {
return -1, err
}
if len(result) == 0 {
return 0, nil
}
return len(result), nil
}
// Get all storage volumes attached to a given storage pool.
func (n *Node) StoragePoolVolumesGet(poolID int64, volumeTypes []int) ([]*api.StorageVolume, error) {
// Get all storage volumes of all types attached to a given storage
// pool.
result := []*api.StorageVolume{}
for _, volumeType := range volumeTypes {
volumeNames, err := n.StoragePoolVolumesGetType(volumeType, poolID)
if err != nil && err != sql.ErrNoRows {
return nil, err
}
for _, volumeName := range volumeNames {
_, volume, err := n.StoragePoolVolumeGetType(volumeName, volumeType, poolID)
if err != nil {
return nil, err
}
result = append(result, volume)
}
}
if len(result) == 0 {
return result, NoSuchObjectError
}
return result, nil
}
// Get all storage volumes attached to a given storage pool of a given volume
// type.
func (n *Node) StoragePoolVolumesGetType(volumeType int, poolID int64) ([]string, error) {
var poolName string
query := "SELECT name FROM storage_volumes WHERE storage_pool_id=? AND type=?"
inargs := []interface{}{poolID, volumeType}
outargs := []interface{}{poolName}
result, err := queryScan(n.db, query, inargs, outargs)
if err != nil {
return []string{}, err
}
response := []string{}
for _, r := range result {
response = append(response, r[0].(string))
}
return response, nil
}
// Get a single storage volume attached to a given storage pool of a given type.
func (n *Node) StoragePoolVolumeGetType(volumeName string, volumeType int, poolID int64) (int64, *api.StorageVolume, error) {
volumeID, err := n.StoragePoolVolumeGetTypeID(volumeName, volumeType, poolID)
if err != nil {
return -1, nil, err
}
volumeConfig, err := n.StorageVolumeConfigGet(volumeID)
if err != nil {
return -1, nil, err
}
volumeDescription, err := n.StorageVolumeDescriptionGet(volumeID)
if err != nil {
return -1, nil, err
}
volumeTypeName, err := StoragePoolVolumeTypeToName(volumeType)
if err != nil {
return -1, nil, err
}
storageVolume := api.StorageVolume{
Type: volumeTypeName,
}
storageVolume.Name = volumeName
storageVolume.Description = volumeDescription
storageVolume.Config = volumeConfig
return volumeID, &storageVolume, nil
}
// Update storage volume attached to a given storage pool.
func (n *Node) StoragePoolVolumeUpdate(volumeName string, volumeType int, poolID int64, volumeDescription string, volumeConfig map[string]string) error {
volumeID, _, err := n.StoragePoolVolumeGetType(volumeName, volumeType, poolID)
if err != nil {
return err
}
tx, err := begin(n.db)
if err != nil {
return err
}
err = StorageVolumeConfigClear(tx, volumeID)
if err != nil {
tx.Rollback()
return err
}
err = StorageVolumeConfigAdd(tx, volumeID, volumeConfig)
if err != nil {
tx.Rollback()
return err
}
err = StorageVolumeDescriptionUpdate(tx, volumeID, volumeDescription)
if err != nil {
tx.Rollback()
return err
}
return TxCommit(tx)
}
// Delete storage volume attached to a given storage pool.
func (n *Node) StoragePoolVolumeDelete(volumeName string, volumeType int, poolID int64) error {
volumeID, _, err := n.StoragePoolVolumeGetType(volumeName, volumeType, poolID)
if err != nil {
return err
}
_, err = exec(n.db, "DELETE FROM storage_volumes WHERE id=?", volumeID)
if err != nil {
return err
}
return nil
}
// Rename storage volume attached to a given storage pool.
func (n *Node) StoragePoolVolumeRename(oldVolumeName string, newVolumeName string, volumeType int, poolID int64) error {
volumeID, _, err := n.StoragePoolVolumeGetType(oldVolumeName, volumeType, poolID)
if err != nil {
return err
}
tx, err := begin(n.db)
if err != nil {
return err
}
_, err = tx.Exec("UPDATE storage_volumes SET name=? WHERE id=? AND type=?", newVolumeName, volumeID, volumeType)
if err != nil {
tx.Rollback()
return err
}
return TxCommit(tx)
}
// Create new storage volume attached to a given storage pool.
func (n *Node) StoragePoolVolumeCreate(volumeName, volumeDescription string, volumeType int, poolID int64, volumeConfig map[string]string) (int64, error) {
tx, err := begin(n.db)
if err != nil {
return -1, err
}
result, err := tx.Exec("INSERT INTO storage_volumes (storage_pool_id, type, name, description) VALUES (?, ?, ?, ?)",
poolID, volumeType, volumeName, volumeDescription)
if err != nil {
tx.Rollback()
return -1, err
}
volumeID, err := result.LastInsertId()
if err != nil {
tx.Rollback()
return -1, err
}
err = StorageVolumeConfigAdd(tx, volumeID, volumeConfig)
if err != nil {
tx.Rollback()
return -1, err
}
err = TxCommit(tx)
if err != nil {
return -1, err
}
return volumeID, nil
}
// Get ID of a storage volume on a given storage pool of a given storage volume
// type.
func (n *Node) StoragePoolVolumeGetTypeID(volumeName string, volumeType int, poolID int64) (int64, error) {
volumeID := int64(-1)
query := `SELECT storage_volumes.id
FROM storage_volumes
JOIN storage_pools
ON storage_volumes.storage_pool_id = storage_pools.id
WHERE storage_volumes.storage_pool_id=?
AND storage_volumes.name=? AND storage_volumes.type=?`
inargs := []interface{}{poolID, volumeName, volumeType}
outargs := []interface{}{&volumeID}
err := dbQueryRowScan(n.db, query, inargs, outargs)
if err != nil {
return -1, NoSuchObjectError
}
return volumeID, nil
}
// XXX: this was extracted from lxd/storage_volume_utils.go, we find a way to
// factor it independently from both the db and main packages.
const (
StoragePoolVolumeTypeContainer = iota
StoragePoolVolumeTypeImage
StoragePoolVolumeTypeCustom
)
// Leave the string type in here! This guarantees that go treats this is as a
// typed string constant. Removing it causes go to treat these as untyped string
// constants which is not what we want.
const (
StoragePoolVolumeTypeNameContainer string = "container"
StoragePoolVolumeTypeNameImage string = "image"
StoragePoolVolumeTypeNameCustom string = "custom"
)
// StoragePoolVolumeTypeToName converts a volume integer type code to its
// human-readable name.
func StoragePoolVolumeTypeToName(volumeType int) (string, error) {
switch volumeType {
case StoragePoolVolumeTypeContainer:
return StoragePoolVolumeTypeNameContainer, nil
case StoragePoolVolumeTypeImage:
return StoragePoolVolumeTypeNameImage, nil
case StoragePoolVolumeTypeCustom:
return StoragePoolVolumeTypeNameCustom, nil
}
return "", fmt.Errorf("invalid storage volume type")
}
func (n *Node) StoragePoolInsertZfsDriver() error {
_, err := exec(n.db, "UPDATE storage_pools SET driver='zfs', description='' WHERE driver=''")
return err
}