-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_volumes_utils.go
297 lines (245 loc) · 7.62 KB
/
storage_volumes_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
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
package main
import (
"fmt"
"path/filepath"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/instance"
"github.com/lxc/lxd/lxd/project"
"github.com/lxc/lxd/lxd/state"
storagePools "github.com/lxc/lxd/lxd/storage"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/version"
)
// 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 (
storagePoolVolumeAPIEndpointContainers string = "containers"
storagePoolVolumeAPIEndpointVMs string = "virtual-machines"
storagePoolVolumeAPIEndpointImages string = "images"
storagePoolVolumeAPIEndpointCustom string = "custom"
)
var supportedVolumeTypes = []int{db.StoragePoolVolumeTypeContainer, db.StoragePoolVolumeTypeVM, db.StoragePoolVolumeTypeCustom, db.StoragePoolVolumeTypeImage}
var supportedVolumeTypesInstances = []int{db.StoragePoolVolumeTypeContainer, db.StoragePoolVolumeTypeVM}
func storagePoolVolumeTypeNameToAPIEndpoint(volumeTypeName string) (string, error) {
switch volumeTypeName {
case db.StoragePoolVolumeTypeNameContainer:
return storagePoolVolumeAPIEndpointContainers, nil
case db.StoragePoolVolumeTypeNameVM:
return storagePoolVolumeAPIEndpointVMs, nil
case db.StoragePoolVolumeTypeNameImage:
return storagePoolVolumeAPIEndpointImages, nil
case db.StoragePoolVolumeTypeNameCustom:
return storagePoolVolumeAPIEndpointCustom, nil
}
return "", fmt.Errorf("Invalid storage volume type name")
}
func storagePoolVolumeTypeToAPIEndpoint(volumeType int) (string, error) {
switch volumeType {
case db.StoragePoolVolumeTypeContainer:
return storagePoolVolumeAPIEndpointContainers, nil
case db.StoragePoolVolumeTypeVM:
return storagePoolVolumeAPIEndpointVMs, nil
case db.StoragePoolVolumeTypeImage:
return storagePoolVolumeAPIEndpointImages, nil
case db.StoragePoolVolumeTypeCustom:
return storagePoolVolumeAPIEndpointCustom, nil
}
return "", fmt.Errorf("Invalid storage volume type")
}
func storagePoolVolumeUpdateUsers(d *Daemon, projectName string, oldPoolName string, oldVolumeName string, newPoolName string, newVolumeName string) error {
s := d.State()
// update all instances
insts, err := instance.LoadByProject(s, projectName)
if err != nil {
return err
}
for _, inst := range insts {
devices := inst.LocalDevices()
found := false
for k := range devices {
if devices[k]["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(devices[k]["source"]) {
continue
}
if filepath.Clean(devices[k]["pool"]) != oldPoolName {
continue
}
dir, file := filepath.Split(devices[k]["source"])
dir = filepath.Clean(dir)
if dir != db.StoragePoolVolumeTypeNameCustom {
continue
}
file = filepath.Clean(file)
if file != oldVolumeName {
continue
}
// found entry
found = true
if oldPoolName != newPoolName {
devices[k]["pool"] = newPoolName
}
if oldVolumeName != newVolumeName {
newSource := newVolumeName
if dir != "" {
newSource = fmt.Sprintf("%s/%s", db.StoragePoolVolumeTypeNameCustom, newVolumeName)
}
devices[k]["source"] = newSource
}
}
if !found {
continue
}
args := db.InstanceArgs{
Architecture: inst.Architecture(),
Description: inst.Description(),
Config: inst.LocalConfig(),
Devices: devices,
Ephemeral: inst.IsEphemeral(),
Profiles: inst.Profiles(),
Project: inst.Project(),
Type: inst.Type(),
Snapshot: inst.IsSnapshot(),
}
err = inst.Update(args, false)
if err != nil {
return err
}
}
// update all profiles
profiles, err := s.Cluster.Profiles(project.Default)
if err != nil {
return err
}
for _, pName := range profiles {
id, profile, err := s.Cluster.ProfileGet(project.Default, pName)
if err != nil {
return err
}
found := false
for k := range profile.Devices {
if profile.Devices[k]["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(profile.Devices[k]["source"]) {
continue
}
if filepath.Clean(profile.Devices[k]["pool"]) != oldPoolName {
continue
}
dir, file := filepath.Split(profile.Devices[k]["source"])
dir = filepath.Clean(dir)
if dir != db.StoragePoolVolumeTypeNameCustom {
continue
}
file = filepath.Clean(file)
if file != oldVolumeName {
continue
}
// found entry
found = true
if oldPoolName != newPoolName {
profile.Devices[k]["pool"] = newPoolName
}
if oldVolumeName != newVolumeName {
newSource := newVolumeName
if dir != "" {
newSource = fmt.Sprintf("%s/%s", db.StoragePoolVolumeTypeNameCustom, newVolumeName)
}
profile.Devices[k]["source"] = newSource
}
}
if !found {
continue
}
pUpdate := api.ProfilePut{}
pUpdate.Config = profile.Config
pUpdate.Description = profile.Description
pUpdate.Devices = profile.Devices
err = doProfileUpdate(d, project.Default, pName, id, profile, pUpdate)
if err != nil {
return err
}
}
return nil
}
// volumeUsedBy = append(volumeUsedBy, fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
func storagePoolVolumeUsedByGet(s *state.State, project, poolName string, volumeName string, volumeTypeName string) ([]string, error) {
// Handle container volumes
if volumeTypeName == "container" {
cName, sName, snap := shared.InstanceGetParentAndSnapshotName(volumeName)
if snap {
return []string{fmt.Sprintf("/%s/containers/%s/snapshots/%s", version.APIVersion, cName, sName)}, nil
}
return []string{fmt.Sprintf("/%s/containers/%s", version.APIVersion, cName)}, nil
}
// Handle image volumes
if volumeTypeName == "image" {
return []string{fmt.Sprintf("/%s/images/%s", version.APIVersion, volumeName)}, nil
}
// Check if the daemon itself is using it
used, err := storagePools.VolumeUsedByDaemon(s, poolName, volumeName)
if err != nil {
return []string{}, err
}
if used {
return []string{fmt.Sprintf("/%s", version.APIVersion)}, nil
}
// Look for containers using this volume
ctsUsingVolume, err := storagePools.VolumeUsedByInstancesGet(s, project, poolName, volumeName)
if err != nil {
return []string{}, err
}
volumeUsedBy := []string{}
for _, ct := range ctsUsingVolume {
volumeUsedBy = append(volumeUsedBy,
fmt.Sprintf("/%s/containers/%s", version.APIVersion, ct))
}
profiles, err := profilesUsingPoolVolumeGetNames(s.Cluster, volumeName, volumeTypeName)
if err != nil {
return []string{}, err
}
if len(volumeUsedBy) == 0 && len(profiles) == 0 {
return []string{}, nil
}
for _, pName := range profiles {
volumeUsedBy = append(volumeUsedBy, fmt.Sprintf("/%s/profiles/%s", version.APIVersion, pName))
}
return volumeUsedBy, nil
}
func profilesUsingPoolVolumeGetNames(db *db.Cluster, volumeName string, volumeType string) ([]string, error) {
usedBy := []string{}
profiles, err := db.Profiles(project.Default)
if err != nil {
return usedBy, err
}
for _, pName := range profiles {
_, profile, err := db.ProfileGet(project.Default, pName)
if err != nil {
return usedBy, err
}
volumeNameWithType := fmt.Sprintf("%s/%s", volumeType, volumeName)
for _, v := range profile.Devices {
if v["type"] != "disk" {
continue
}
// Can't be a storage volume.
if filepath.IsAbs(v["source"]) {
continue
}
// Make sure that we don't compare against stuff
// like "container////bla" but only against
// "container/bla".
cleanSource := filepath.Clean(v["source"])
if cleanSource == volumeName || cleanSource == volumeNameWithType {
usedBy = append(usedBy, pName)
}
}
}
return usedBy, nil
}