-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathstorage_volumes_utils.go
224 lines (186 loc) · 7.39 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
package main
import (
"context"
"slices"
"strings"
"github.com/canonical/lxd/lxd/backup"
"github.com/canonical/lxd/lxd/db"
"github.com/canonical/lxd/lxd/db/cluster"
"github.com/canonical/lxd/lxd/instance"
"github.com/canonical/lxd/lxd/state"
storagePools "github.com/canonical/lxd/lxd/storage"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
"github.com/canonical/lxd/shared/revert"
"github.com/canonical/lxd/shared/version"
)
var supportedVolumeTypes = []int{cluster.StoragePoolVolumeTypeContainer, cluster.StoragePoolVolumeTypeVM, cluster.StoragePoolVolumeTypeCustom, cluster.StoragePoolVolumeTypeImage}
func storagePoolVolumeUpdateUsers(s *state.State, projectName string, oldPoolName string, oldVol *api.StorageVolume, newPoolName string, newVol *api.StorageVolume) (revert.Hook, error) {
revert := revert.New()
defer revert.Fail()
// Update all instances that are using the volume with a local (non-expanded) device.
err := storagePools.VolumeUsedByInstanceDevices(s, oldPoolName, projectName, oldVol, false, func(dbInst db.InstanceArgs, project api.Project, usedByDevices []string) error {
inst, err := instance.Load(s, dbInst, project)
if err != nil {
return err
}
localDevices := inst.LocalDevices()
newDevices := localDevices.Clone()
for _, devName := range usedByDevices {
_, exists := newDevices[devName]
if exists {
newDevices[devName]["pool"] = newPoolName
if strings.Contains(localDevices[devName]["source"], "/") {
newDevices[devName]["source"] = newVol.Type + "/" + newVol.Name
} else {
newDevices[devName]["source"] = newVol.Name
}
}
}
args := db.InstanceArgs{
Architecture: inst.Architecture(),
Description: inst.Description(),
Config: inst.LocalConfig(),
Devices: newDevices,
Ephemeral: inst.IsEphemeral(),
Profiles: inst.Profiles(),
Project: inst.Project().Name,
Type: inst.Type(),
Snapshot: inst.IsSnapshot(),
}
err = inst.Update(args, false)
if err != nil {
return err
}
revert.Add(func() {
err := inst.Update(dbInst, false)
if err != nil {
logger.Error("Failed to revert instance update", logger.Ctx{"project": dbInst.Project, "instance": dbInst.Name, "error": err})
}
})
return nil
})
if err != nil {
return nil, err
}
// Update all profiles that are using the volume with a device.
err = storagePools.VolumeUsedByProfileDevices(s, oldPoolName, projectName, oldVol, func(profileID int64, profile api.Profile, p api.Project, usedByDevices []string) error {
newDevices := make(map[string]map[string]string, len(profile.Devices))
for devName, dev := range profile.Devices {
for key, val := range dev {
_, exists := newDevices[devName]
if !exists {
newDevices[devName] = make(map[string]string, len(dev))
}
newDevices[devName][key] = val
}
if shared.ValueInSlice(devName, usedByDevices) {
newDevices[devName]["pool"] = newPoolName
if strings.Contains(dev["source"], "/") {
newDevices[devName]["source"] = newVol.Type + "/" + newVol.Name
} else {
newDevices[devName]["source"] = newVol.Name
}
}
}
pUpdate := api.ProfilePut{
Config: profile.Config,
Description: profile.Description,
Devices: newDevices,
}
err = doProfileUpdate(s, p, profile.Name, profileID, &profile, pUpdate)
if err != nil {
return err
}
revert.Add(func() {
original := api.ProfilePut{
Config: profile.Config,
Description: profile.Description,
Devices: profile.Devices,
}
err := doProfileUpdate(s, p, profile.Name, profileID, &profile, original)
if err != nil {
logger.Error("Failed reverting profile update", logger.Ctx{"project": p.Name, "profile": profile.Name, "error": err})
}
})
return nil
})
if err != nil {
return nil, err
}
cleanup := revert.Clone().Fail
revert.Success()
return cleanup, nil
}
// storagePoolVolumeUsedByGet returns a list of URL resources that use the volume.
func storagePoolVolumeUsedByGet(s *state.State, requestProjectName string, vol *db.StorageVolume) ([]string, error) {
if vol.Type == cluster.StoragePoolVolumeTypeNameContainer {
volName, snapName, isSnap := api.GetParentAndSnapshotName(vol.Name)
if isSnap {
return []string{api.NewURL().Path(version.APIVersion, "instances", volName, "snapshots", snapName).Project(vol.Project).String()}, nil
}
return []string{api.NewURL().Path(version.APIVersion, "instances", volName).Project(vol.Project).String()}, nil
}
// Handle image volumes.
if vol.Type == cluster.StoragePoolVolumeTypeNameImage {
return []string{api.NewURL().Path(version.APIVersion, "images", vol.Name).Project(requestProjectName).Target(vol.Location).String()}, nil
}
// Check if the daemon itself is using it.
used, err := storagePools.VolumeUsedByDaemon(s, vol.Pool, vol.Name)
if err != nil {
return []string{}, err
}
if used {
return []string{api.NewURL().Path(version.APIVersion).String()}, nil
}
// Look for instances using this volume.
volumeUsedBy := []string{}
// Pass false to expandDevices, as we only want to see instances directly using a volume, rather than their
// profiles using a volume.
err = storagePools.VolumeUsedByInstanceDevices(s, vol.Pool, vol.Project, &vol.StorageVolume, false, func(inst db.InstanceArgs, p api.Project, usedByDevices []string) error {
volumeUsedBy = append(volumeUsedBy, api.NewURL().Path(version.APIVersion, "instances", inst.Name).Project(inst.Project).String())
return nil
})
if err != nil {
return []string{}, err
}
err = storagePools.VolumeUsedByProfileDevices(s, vol.Pool, requestProjectName, &vol.StorageVolume, func(profileID int64, profile api.Profile, p api.Project, usedByDevices []string) error {
volumeUsedBy = append(volumeUsedBy, api.NewURL().Path(version.APIVersion, "profiles", profile.Name).Project(p.Name).String())
return nil
})
if err != nil {
return []string{}, err
}
// Handle instance volumes.
if vol.Type == cluster.StoragePoolVolumeTypeNameVM {
volName, snapName, isSnap := api.GetParentAndSnapshotName(vol.Name)
if isSnap {
return []string{api.NewURL().Path(version.APIVersion, "instances", volName, "snapshots", snapName).Project(vol.Project).String()}, nil
}
// VolumeUsedByInstanceDevices will find virtual-machine/container volumes
// when they are a root disk device in an instance's unexpanded devices,
// but not in a profile's devices.
// Since every virtual-machine/container volume is always in use by its
// corresponding instance, this ensures that it is reported.
instancePath := api.NewURL().Path(version.APIVersion, "instances", volName).Project(vol.Project).String()
if !slices.Contains(volumeUsedBy, instancePath) {
volumeUsedBy = append(volumeUsedBy, instancePath)
}
}
return volumeUsedBy, nil
}
func storagePoolVolumeBackupLoadByName(s *state.State, projectName, poolName, backupName string) (*backup.VolumeBackup, error) {
var b db.StoragePoolVolumeBackup
err := s.DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
var err error
b, err = tx.GetStoragePoolVolumeBackup(ctx, projectName, poolName, backupName)
return err
})
if err != nil {
return nil, err
}
volumeName := strings.Split(backupName, "/")[0]
backup := backup.NewVolumeBackup(s, projectName, poolName, volumeName, b.ID, b.Name, b.CreationDate, b.ExpiryDate, b.VolumeOnly, b.OptimizedStorage)
return backup, nil
}