/
storage_pools_utils.go
174 lines (141 loc) · 5.16 KB
/
storage_pools_utils.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
package main
import (
"context"
"fmt"
"github.com/lxc/lxd/lxd/cluster/request"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/revert"
"github.com/lxc/lxd/lxd/state"
storagePools "github.com/lxc/lxd/lxd/storage"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/logger"
)
// storagePoolDBCreate creates a storage pool DB entry and returns the created Pool ID.
func storagePoolDBCreate(s *state.State, poolName string, poolDescription string, driver string, config map[string]string) (int64, error) {
// Check that the storage pool does not already exist.
_, err := s.DB.Cluster.GetStoragePoolID(poolName)
if err == nil {
return -1, fmt.Errorf("The storage pool already exists: %w", db.ErrAlreadyDefined)
}
// Make sure that we don't pass a nil to the next function.
if config == nil {
config = map[string]string{}
}
err = storagePoolValidate(s, poolName, driver, config)
if err != nil {
return -1, err
}
// Create the database entry for the storage pool.
id, err := dbStoragePoolCreateAndUpdateCache(s, poolName, poolDescription, driver, config)
if err != nil {
return -1, fmt.Errorf("Error inserting %s into database: %w", poolName, err)
}
return id, nil
}
func storagePoolValidate(s *state.State, poolName string, driverName string, config map[string]string) error {
poolType, err := storagePools.LoadByType(s, driverName)
if err != nil {
return err
}
// Check if the storage pool name is valid.
err = poolType.ValidateName(poolName)
if err != nil {
return err
}
// Validate the requested storage pool configuration.
err = poolType.Validate(config)
if err != nil {
return err
}
return nil
}
func storagePoolCreateGlobal(state *state.State, req api.StoragePoolsPost, clientType request.ClientType) error {
// Create the database entry.
id, err := storagePoolDBCreate(state, req.Name, req.Description, req.Driver, req.Config)
if err != nil {
return err
}
// Define a function which reverts everything. Defer this function
// so that it doesn't need to be explicitly called in every failing
// return path. Track whether or not we want to undo the changes
// using a closure.
revert := revert.New()
defer revert.Fail()
revert.Add(func() { _ = dbStoragePoolDeleteAndUpdateCache(state, req.Name) })
_, err = storagePoolCreateLocal(state, id, req, clientType)
if err != nil {
return err
}
revert.Success()
return nil
}
// This performs local pool setup and updates DB record if config was changed during pool setup.
// Returns resulting config.
func storagePoolCreateLocal(state *state.State, poolID int64, req api.StoragePoolsPost, clientType request.ClientType) (map[string]string, error) {
// Setup revert.
revert := revert.New()
defer revert.Fail()
// Load pool record.
pool, err := storagePools.LoadByName(state, req.Name)
if err != nil {
return nil, err
}
if pool.LocalStatus() == api.NetworkStatusCreated {
logger.Debug("Skipping local storage pool create as already created", logger.Ctx{"pool": pool.Name()})
return pool.Driver().Config(), nil
}
// Create the pool.
err = pool.Create(clientType, nil)
if err != nil {
return nil, err
}
revert.Add(func() { _ = pool.Delete(clientType, nil) })
// Mount the pool.
_, err = pool.Mount()
if err != nil {
return nil, err
}
// In case the storage pool config was changed during the pool creation, we need to update the database to
// reflect this change. This can e.g. happen, when we create a loop file image. This means we append ".img"
// to the path the user gave us and update the config in the storage callback. So diff the config here to
// see if something like this has happened.
configDiff, _ := storagePools.ConfigDiff(req.Config, pool.Driver().Config())
if len(configDiff) > 0 {
// Update the database entry for the storage pool.
err = state.DB.Cluster.UpdateStoragePool(req.Name, req.Description, pool.Driver().Config())
if err != nil {
return nil, fmt.Errorf("Error updating storage pool config after local create for %q: %w", req.Name, err)
}
}
// Set storage pool node to storagePoolCreated.
err = state.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
return tx.StoragePoolNodeCreated(poolID)
})
if err != nil {
return nil, err
}
logger.Debug("Marked storage pool local status as created", logger.Ctx{"pool": req.Name})
revert.Success()
return pool.Driver().Config(), nil
}
// Helper around the low-level DB API, which also updates the driver names cache.
func dbStoragePoolCreateAndUpdateCache(s *state.State, poolName string, poolDescription string, poolDriver string, poolConfig map[string]string) (int64, error) {
id, err := s.DB.Cluster.CreateStoragePool(poolName, poolDescription, poolDriver, poolConfig)
if err != nil {
return id, err
}
// Update the storage drivers cache in api_1.0.go.
storagePoolDriversCacheUpdate(s)
return id, nil
}
// Helper around the low-level DB API, which also updates the driver names
// cache.
func dbStoragePoolDeleteAndUpdateCache(s *state.State, poolName string) error {
_, err := s.DB.Cluster.RemoveStoragePool(poolName)
if err != nil {
return err
}
// Update the storage drivers cache in api_1.0.go.
storagePoolDriversCacheUpdate(s)
return err
}