image.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2016 The Rook Authors. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "syscall"
  18. "strconv"
  19. "regexp"
  20. "github.com/pkg/errors"
  21. "github.com/rook/rook/pkg/clusterd"
  22. "github.com/rook/rook/pkg/util/display"
  23. "github.com/rook/rook/pkg/util/exec"
  24. )
  25. const (
  26. ImageMinSize = uint64(1048576) // 1 MB
  27. )
  28. type CephBlockImage struct {
  29. Name string `json:"image"`
  30. Size uint64 `json:"size"`
  31. Format int `json:"format"`
  32. InfoName string `json:"name"`
  33. }
  34. func ListImages(context *clusterd.Context, clusterInfo *ClusterInfo, poolName string) ([]CephBlockImage, error) {
  35. args := []string{"ls", "-l", poolName}
  36. cmd := NewRBDCommand(context, clusterInfo, args)
  37. cmd.JsonOutput = true
  38. buf, err := cmd.Run()
  39. if err != nil {
  40. return nil, errors.Wrapf(err, "failed to list images for pool %s", poolName)
  41. }
  42. //The regex expression captures the json result at the end buf
  43. //When logLevel is DEBUG buf contains log statements of librados (see tests for examples)
  44. //It can happen that the end of the "real" output doesn't not contain a new line
  45. //that's why looking for the end isn't an option here (anymore?)
  46. res := regexp.MustCompile(`(?m)^\[(.*)\]`).FindStringSubmatch(string(buf))
  47. if len(res) == 0 {
  48. return []CephBlockImage{}, nil
  49. }
  50. buf = []byte(res[0])
  51. var images []CephBlockImage
  52. if err = json.Unmarshal(buf, &images); err != nil {
  53. return nil, errors.Wrapf(err, "unmarshal failed, raw buffer response: %s", string(buf))
  54. }
  55. return images, nil
  56. }
  57. // CreateImage creates a block storage image.
  58. // If dataPoolName is not empty, the image will use poolName as the metadata pool and the dataPoolname for data.
  59. // If size is zero an empty image will be created. Otherwise, an image will be
  60. // created with a size rounded up to the nearest Mi. The adjusted image size is
  61. // placed in return value CephBlockImage.Size.
  62. func CreateImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName, dataPoolName string, size uint64) (*CephBlockImage, error) {
  63. if size > 0 && size < ImageMinSize {
  64. // rbd tool uses MB as the smallest unit for size input. 0 is OK but anything else smaller
  65. // than 1 MB should just be rounded up to 1 MB.
  66. logger.Warningf("requested image size %d is less than the minimum size of %d, using the minimum.", size, ImageMinSize)
  67. size = ImageMinSize
  68. }
  69. // Roundup the size of the volume image since we only create images on 1MB boundaries and we should never create an image
  70. // size that's smaller than the requested one, e.g, requested 1048698 bytes should be 2MB while not be truncated to 1MB
  71. sizeMB := int((size + ImageMinSize - 1) / ImageMinSize)
  72. imageSpec := getImageSpec(name, poolName)
  73. args := []string{"create", imageSpec, "--size", strconv.Itoa(sizeMB)}
  74. if dataPoolName != "" {
  75. args = append(args, fmt.Sprintf("--data-pool=%s", dataPoolName))
  76. }
  77. logger.Infof("creating rbd image %q with size %dMB in pool %q", imageSpec, sizeMB, dataPoolName)
  78. buf, err := NewRBDCommand(context, clusterInfo, args).Run()
  79. if err != nil {
  80. if code, ok := exec.ExitStatus(err); ok && code == int(syscall.EEXIST) {
  81. // Image with the same name already exists in the given rbd pool. Continuing with the link to PV.
  82. logger.Warningf("Requested image %s exists in pool %s. Continuing", name, poolName)
  83. } else {
  84. return nil, errors.Wrapf(err, "failed to create image %s in pool %s of size %d, output: %s",
  85. name, poolName, size, string(buf))
  86. }
  87. }
  88. // report the adjusted size which will always be >= to the requested size
  89. var newSizeBytes uint64
  90. if sizeMB > 0 {
  91. newSizeBytes = display.MbTob(uint64(sizeMB))
  92. } else {
  93. newSizeBytes = 0
  94. }
  95. return &CephBlockImage{Name: name, Size: newSizeBytes}, nil
  96. }
  97. func DeleteImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName string) error {
  98. logger.Infof("deleting rbd image %q from pool %q", name, poolName)
  99. imageSpec := getImageSpec(name, poolName)
  100. args := []string{"rm", imageSpec}
  101. buf, err := NewRBDCommand(context, clusterInfo, args).Run()
  102. if err != nil {
  103. return errors.Wrapf(err, "failed to delete image %s in pool %s, output: %s",
  104. name, poolName, string(buf))
  105. }
  106. return nil
  107. }
  108. func ExpandImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, poolName, monitors, keyring string, size uint64) error {
  109. logger.Infof("expanding rbd image %q in pool %q to size %dMB", name, poolName, display.BToMb(size))
  110. imageSpec := getImageSpec(name, poolName)
  111. args := []string{
  112. "resize",
  113. imageSpec,
  114. fmt.Sprintf("--size=%s", strconv.FormatUint(size, 10)),
  115. fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
  116. fmt.Sprintf("--keyring=%s", keyring),
  117. "-m", monitors,
  118. }
  119. output, err := ExecuteRBDCommandWithTimeout(context, args)
  120. if err != nil {
  121. return errors.Wrapf(err, "failed to resize image %s in pool %s, output: %s", name, poolName, string(output))
  122. }
  123. return nil
  124. }
  125. // MapImage maps an RBD image using admin cephfx and returns the device path
  126. func MapImage(context *clusterd.Context, clusterInfo *ClusterInfo, imageName, poolName, id, keyring, monitors string) error {
  127. imageSpec := getImageSpec(imageName, poolName)
  128. args := []string{
  129. "map",
  130. imageSpec,
  131. fmt.Sprintf("--id=%s", id),
  132. fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
  133. fmt.Sprintf("--keyring=%s", keyring),
  134. "-m", monitors,
  135. "--conf=/dev/null", // no config file needed because we are passing all required config as arguments
  136. }
  137. output, err := ExecuteRBDCommandWithTimeout(context, args)
  138. if err != nil {
  139. return errors.Wrapf(err, "failed to map image %s, output: %s", imageSpec, output)
  140. }
  141. return nil
  142. }
  143. // UnMapImage unmap an RBD image from the node
  144. func UnMapImage(context *clusterd.Context, clusterInfo *ClusterInfo, imageName, poolName, id, keyring, monitors string, force bool) error {
  145. deviceImage := getImageSpec(imageName, poolName)
  146. args := []string{
  147. "unmap",
  148. deviceImage,
  149. fmt.Sprintf("--id=%s", id),
  150. fmt.Sprintf("--cluster=%s", clusterInfo.Namespace),
  151. fmt.Sprintf("--keyring=%s", keyring),
  152. "-m", monitors,
  153. "--conf=/dev/null", // no config file needed because we are passing all required config as arguments
  154. }
  155. if force {
  156. args = append(args, "-o", "force")
  157. }
  158. output, err := ExecuteRBDCommandWithTimeout(context, args)
  159. if err != nil {
  160. return errors.Wrapf(err, "failed to unmap image %s, output: %s", deviceImage, output)
  161. }
  162. return nil
  163. }
  164. func getImageSpec(name, poolName string) string {
  165. return fmt.Sprintf("%s/%s", poolName, name)
  166. }