forked from rook/rook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
275 lines (235 loc) · 10.4 KB
/
image.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
/*
Copyright 2016 The Rook Authors. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"encoding/json"
"fmt"
"syscall"
"strconv"
"regexp"
"github.com/pkg/errors"
"github.com/rook/rook/pkg/clusterd"
"github.com/rook/rook/pkg/util/display"
"github.com/rook/rook/pkg/util/exec"
)
const (
ImageMinSize = uint64(1048576) // 1 MB
)
type CephBlockImage struct {
ID string `json:"id"`
Name string `json:"image"`
Size uint64 `json:"size"`
Format int `json:"format"`
InfoName string `json:"name"`
}
type CephBlockImageSnapshot struct {
Name string `json:"name"`
}
// ListImagesInPool returns a list of images created in a cephblockpool
func ListImagesInPool(context *clusterd.Context, clusterInfo *ClusterInfo, poolName string) ([]CephBlockImage, error) {
return ListImagesInRadosNamespace(context, clusterInfo, poolName, "")
}
// ListImagesInRadosNamespace returns a list if images created in a cephblockpool rados namespace
func ListImagesInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, namespace string) ([]CephBlockImage, error) {
args := []string{"ls", "-l", poolName}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
cmd := NewRBDCommand(context, clusterInfo, args)
cmd.JsonOutput = true
buf, err := cmd.Run()
if err != nil {
return nil, errors.Wrapf(err, "failed to list images for pool %s", poolName)
}
//The regex expression captures the json result at the end buf
//When logLevel is DEBUG buf contains log statements of librados (see tests for examples)
//It can happen that the end of the "real" output doesn't not contain a new line
//that's why looking for the end isn't an option here (anymore?)
res := regexp.MustCompile(`(?m)^\[(.*)\]`).FindStringSubmatch(string(buf))
if len(res) == 0 {
return []CephBlockImage{}, nil
}
buf = []byte(res[0])
var images []CephBlockImage
if err = json.Unmarshal(buf, &images); err != nil {
return nil, errors.Wrapf(err, "unmarshal failed, raw buffer response: %s", string(buf))
}
return images, nil
}
// ListSnapshotsInRadosNamespace lists all the snapshots created for an image in a cephblockpool in a given rados namespace
func ListSnapshotsInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, imageName, namespace string) ([]CephBlockImageSnapshot, error) {
snapshots := []CephBlockImageSnapshot{}
args := []string{"snap", "ls", getImageSpec(imageName, poolName), "--namespace", namespace}
cmd := NewRBDCommand(context, clusterInfo, args)
cmd.JsonOutput = true
buf, err := cmd.Run()
if err != nil {
return snapshots, errors.Wrapf(err, "failed to list snapshots of image %q in cephblockpool %q", imageName, poolName)
}
if err = json.Unmarshal(buf, &snapshots); err != nil {
return snapshots, errors.Wrapf(err, "unmarshal failed, raw buffer response: %s", string(buf))
}
return snapshots, nil
}
// DeleteSnapshotInRadosNamespace deletes a image snapshot created in block pool in a given rados namespace
func DeleteSnapshotInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, imageName, snapshot, namespace string) error {
args := []string{"snap", "rm", getImageSnapshotSpec(poolName, imageName, snapshot), "--namespace", namespace}
cmd := NewRBDCommand(context, clusterInfo, args)
_, err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "failed to delete snapshot %q of image %q in cephblockpool %q", snapshot, imageName, poolName)
}
return nil
}
// MoveImageToTrashInRadosNamespace moves the cephblockpool image to trash in the rados namespace
func MoveImageToTrashInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, imageName, namespace string) error {
args := []string{"trash", "mv", getImageSpec(imageName, poolName), "--namespace", namespace}
cmd := NewRBDCommand(context, clusterInfo, args)
_, err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "failed to move image %q in cephblockpool %q to trash", imageName, poolName)
}
return nil
}
// DeleteImageFromTrashInRadosNamespace deletes the image from trash in the rados namespace
func DeleteImageFromTrashInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, poolName, imageID, namespace string) error {
args := []string{"rbd", "task", "add", "trash", "remove", getImageSpecInRadosNamespace(poolName, namespace, imageID)}
cmd := NewCephCommand(context, clusterInfo, args)
_, err := cmd.Run()
if err != nil {
return errors.Wrapf(err, "failed to delete image %q in cephblockpool %q from trash", imageID, poolName)
}
return nil
}
// CreateImage creates a block storage image.
// If dataPoolName is not empty, the image will use poolName as the metadata pool and the dataPoolname for data.
// If size is zero an empty image will be created. Otherwise, an image will be
// created with a size rounded up to the nearest Mi. The adjusted image size is
// placed in return value CephBlockImage.Size.
func CreateImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName, dataPoolName string, size uint64) (*CephBlockImage, error) {
if size > 0 && size < ImageMinSize {
// rbd tool uses MB as the smallest unit for size input. 0 is OK but anything else smaller
// than 1 MB should just be rounded up to 1 MB.
logger.Warningf("requested image size %d is less than the minimum size of %d, using the minimum.", size, ImageMinSize)
size = ImageMinSize
}
// Roundup the size of the volume image since we only create images on 1MB boundaries and we should never create an image
// size that's smaller than the requested one, e.g, requested 1048698 bytes should be 2MB while not be truncated to 1MB
sizeMB := int((size + ImageMinSize - 1) / ImageMinSize)
imageSpec := getImageSpec(name, poolName)
args := []string{"create", imageSpec, "--size", strconv.Itoa(sizeMB)}
if dataPoolName != "" {
args = append(args, fmt.Sprintf("--data-pool=%s", dataPoolName))
}
logger.Infof("creating rbd image %q with size %dMB in pool %q", imageSpec, sizeMB, dataPoolName)
buf, err := NewRBDCommand(context, clusterInfo, args).Run()
if err != nil {
if code, ok := exec.ExitStatus(err); ok && code == int(syscall.EEXIST) {
// Image with the same name already exists in the given rbd pool. Continuing with the link to PV.
logger.Warningf("Requested image %s exists in pool %s. Continuing", name, poolName)
} else {
return nil, errors.Wrapf(err, "failed to create image %s in pool %s of size %d, output: %s",
name, poolName, size, string(buf))
}
}
// report the adjusted size which will always be >= to the requested size
var newSizeBytes uint64
if sizeMB > 0 {
newSizeBytes = display.MbTob(uint64(sizeMB))
} else {
newSizeBytes = 0
}
return &CephBlockImage{Name: name, Size: newSizeBytes}, nil
}
func DeleteImageInPool(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName string) error {
return DeleteImageInRadosNamespace(context, clusterInfo, name, poolName, "")
}
func DeleteImageInRadosNamespace(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName, namespace string) error {
logger.Infof("deleting rbd image %q from pool %q", name, poolName)
imageSpec := getImageSpec(name, poolName)
args := []string{"rm", imageSpec}
if namespace != "" {
args = append(args, "--namespace", namespace)
}
buf, err := NewRBDCommand(context, clusterInfo, args).Run()
if err != nil {
return errors.Wrapf(err, "failed to delete image %s in pool %s, output: %s",
name, poolName, string(buf))
}
return nil
}
func ExpandImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName, monitors, keyring string, size uint64) error {
logger.Infof("expanding rbd image %q in pool %q to size %dMB", name, poolName, display.BToMb(size))
imageSpec := getImageSpec(name, poolName)
args := []string{
"resize",
imageSpec,
fmt.Sprintf("--size=%s", strconv.FormatUint(size, 10)),
fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
fmt.Sprintf("--keyring=%s", keyring),
"-m", monitors,
}
output, err := ExecuteRBDCommandWithTimeout(context, args)
if err != nil {
return errors.Wrapf(err, "failed to resize image %s in pool %s, output: %s", name, poolName, string(output))
}
return nil
}
// MapImage maps an RBD image using admin cephfx and returns the device path
func MapImage(context *clusterd.Context, clusterInfo *ClusterInfo, imageName, poolName, id, keyring, monitors string) error {
imageSpec := getImageSpec(imageName, poolName)
args := []string{
"map",
imageSpec,
fmt.Sprintf("--id=%s", id),
fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
fmt.Sprintf("--keyring=%s", keyring),
"-m", monitors,
"--conf=/dev/null", // no config file needed because we are passing all required config as arguments
}
output, err := ExecuteRBDCommandWithTimeout(context, args)
if err != nil {
return errors.Wrapf(err, "failed to map image %s, output: %s", imageSpec, output)
}
return nil
}
// UnMapImage unmap an RBD image from the node
func UnMapImage(context *clusterd.Context, clusterInfo *ClusterInfo, imageName, poolName, id, keyring, monitors string, force bool) error {
deviceImage := getImageSpec(imageName, poolName)
args := []string{
"unmap",
deviceImage,
fmt.Sprintf("--id=%s", id),
fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
fmt.Sprintf("--keyring=%s", keyring),
"-m", monitors,
"--conf=/dev/null", // no config file needed because we are passing all required config as arguments
}
if force {
args = append(args, "-o", "force")
}
output, err := ExecuteRBDCommandWithTimeout(context, args)
if err != nil {
return errors.Wrapf(err, "failed to unmap image %s, output: %s", deviceImage, output)
}
return nil
}
func getImageSpec(name, poolName string) string {
return fmt.Sprintf("%s/%s", poolName, name)
}
func getImageSpecInRadosNamespace(poolName, namespace, imageID string) string {
return fmt.Sprintf("%s/%s/%s", poolName, namespace, imageID)
}
func getImageSnapshotSpec(poolName, imageName, snapshot string) string {
return fmt.Sprintf("%s/%s@%s", poolName, imageName, snapshot)
}