/
storage_volumes_utils.go
154 lines (129 loc) · 5.21 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
package main
import (
"context"
"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/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) error {
// 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()
for _, devName := range usedByDevices {
_, exists := localDevices[devName]
if exists {
localDevices[devName]["pool"] = newPoolName
localDevices[devName]["source"] = newVol.Name
}
}
args := db.InstanceArgs{
Architecture: inst.Architecture(),
Description: inst.Description(),
Config: inst.LocalConfig(),
Devices: localDevices,
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
}
return nil
})
if err != nil {
return 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 {
for name, dev := range profile.Devices {
if shared.ValueInSlice(name, usedByDevices) {
dev["pool"] = newPoolName
dev["source"] = newVol.Name
}
}
pUpdate := api.ProfilePut{}
pUpdate.Config = profile.Config
pUpdate.Description = profile.Description
pUpdate.Devices = profile.Devices
err = doProfileUpdate(s, p, profile.Name, profileID, &profile, pUpdate)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
return nil
}
// storagePoolVolumeUsedByGet returns a list of URL resources that use the volume.
func storagePoolVolumeUsedByGet(s *state.State, requestProjectName string, vol *db.StorageVolume) ([]string, error) {
// Handle instance volumes.
if vol.Type == cluster.StoragePoolVolumeTypeNameContainer || 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
}
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
}
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
}