2016-11-05 17:09:05 -07:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2016-12-01 16:10:46 -08:00
|
|
|
package block
|
2016-09-30 12:56:56 -07:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
2017-06-20 12:10:30 -07:00
|
|
|
rc "github.com/rook/rook/cmd/rookctl/client"
|
|
|
|
|
"github.com/rook/rook/cmd/rookctl/rook"
|
2017-12-04 08:33:19 -08:00
|
|
|
"github.com/rook/rook/pkg/daemon/ceph/util"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/model"
|
2016-10-27 13:26:25 +02:00
|
|
|
"github.com/rook/rook/pkg/rook/client"
|
2016-10-26 00:38:39 +02:00
|
|
|
"github.com/rook/rook/pkg/util/exec"
|
|
|
|
|
"github.com/rook/rook/pkg/util/flags"
|
|
|
|
|
"github.com/rook/rook/pkg/util/sys"
|
2016-09-30 12:56:56 -07:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
rbdKernelModuleName = "rbd"
|
|
|
|
|
devicePathPrefix = "/dev/"
|
|
|
|
|
rbdAddNode = "add"
|
|
|
|
|
rbdAddSingleMajorNode = "add_single_major"
|
|
|
|
|
rbdRemoveNode = "remove"
|
|
|
|
|
rbdRemoveSingleMajorNode = "remove_single_major"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2017-03-23 16:09:17 -07:00
|
|
|
mapImageName string
|
|
|
|
|
mapImagePoolName string
|
|
|
|
|
mapImagePath string
|
|
|
|
|
mapFormatRequested bool
|
2016-09-30 12:56:56 -07:00
|
|
|
)
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
var mapCmd = &cobra.Command{
|
|
|
|
|
Use: "map",
|
|
|
|
|
Short: "Maps a block image from the cluster as a local block device and optionally formats and mounts it with the given file system path",
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2017-03-23 16:09:17 -07:00
|
|
|
mapCmd.Flags().StringVar(&mapImageName, "name", "", "Name of block image to map (required)")
|
|
|
|
|
mapCmd.Flags().StringVar(&mapImagePoolName, "pool-name", "rbd", "Name of storage pool that contains block image to map")
|
|
|
|
|
mapCmd.Flags().BoolVar(&mapFormatRequested, "format", false, "Format a filesystem after mapping")
|
|
|
|
|
mapCmd.Flags().StringVar(&mapImagePath, "mount", "", "Mount a filesystem on the indicated path")
|
2016-09-30 12:56:56 -07:00
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
mapCmd.MarkFlagRequired("name")
|
2016-09-30 12:56:56 -07:00
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
mapCmd.RunE = mapBlockEntry
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
func mapBlockEntry(cmd *cobra.Command, args []string) error {
|
2016-12-01 16:10:46 -08:00
|
|
|
rook.SetupLogging()
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
if err := flags.VerifyRequiredFlags(cmd, []string{"name"}); err != nil {
|
2016-09-30 12:56:56 -07:00
|
|
|
return err
|
|
|
|
|
}
|
2016-12-09 17:29:53 -08:00
|
|
|
c := rook.NewRookNetworkRestClient()
|
2016-10-12 09:04:07 -07:00
|
|
|
e := &exec.CommandExecutor{}
|
2017-10-09 14:36:21 -07:00
|
|
|
out, err := mapBlock(mapImageName, mapImagePoolName, mapImagePath, util.RBDSysBusPathDefault, util.RBDDevicePathPrefix, mapFormatRequested, c, e)
|
2016-09-30 12:56:56 -07:00
|
|
|
if err != nil {
|
2016-10-19 15:40:40 -07:00
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
os.Exit(1)
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Println(out)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func mapBlock(name, poolName, mountPoint, rbdSysBusPath, rbdDevicePathPrefix string, formatRequested bool, c client.RookRestClient, executor exec.Executor) (string, error) {
|
2016-11-28 17:38:06 -08:00
|
|
|
clientAccessInfo, err := c.GetClientAccessInfo()
|
2016-09-30 12:56:56 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hasSingleMajor := checkRBDSingleMajor(executor)
|
|
|
|
|
|
|
|
|
|
var options []string
|
|
|
|
|
if hasSingleMajor {
|
|
|
|
|
options = []string{"single_major=Y"}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load the rbd kernel module with options
|
2017-04-28 08:21:35 -07:00
|
|
|
if err := sys.LoadKernelModule(rbdKernelModuleName, options, executor); err != nil {
|
2016-09-30 12:56:56 -07:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addSingleMajorPath := filepath.Join(rbdSysBusPath, rbdAddSingleMajorNode)
|
|
|
|
|
addPath := filepath.Join(rbdSysBusPath, rbdAddNode)
|
|
|
|
|
|
|
|
|
|
addFile, err := openRBDFile(hasSingleMajor, addSingleMajorPath, addPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
defer addFile.Close()
|
|
|
|
|
|
|
|
|
|
// generate the data string that will be written to the rbd add path
|
2016-11-28 17:38:06 -08:00
|
|
|
rbdAddData, err := getRBDAddData(name, poolName, clientAccessInfo)
|
2016-09-30 12:56:56 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to generate rbd add data: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// write the rbd data string to the rbd add path
|
|
|
|
|
if _, err := addFile.Write([]byte(rbdAddData)); err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to write rbd add data: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// wait for the device to become available so we can find out its name/ID
|
2017-09-14 09:40:34 -07:00
|
|
|
devicePath, err := waitForDevicePath(name, poolName, rbdSysBusPath, rbdDevicePathPrefix, 10, 1)
|
2016-09-30 12:56:56 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
successMessage := fmt.Sprintf("succeeded mapping image %s on device %s", name, devicePath)
|
|
|
|
|
|
|
|
|
|
if formatRequested {
|
|
|
|
|
// format the device with a default file system
|
|
|
|
|
if err := sys.FormatDevice(devicePath, executor); err != nil {
|
|
|
|
|
return "", fmt.Errorf("%s but failed to format device %s: %+v", successMessage, devicePath, err)
|
|
|
|
|
}
|
|
|
|
|
successMessage += ", formatted"
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
if len(mountPoint) > 0 {
|
|
|
|
|
// mount the device at the given mount point
|
|
|
|
|
if err := sys.MountDevice(devicePath, mountPoint, executor); err != nil {
|
|
|
|
|
return "", fmt.Errorf("%s but failed to mount device %s at '%s': %+v", successMessage, devicePath, mountPoint, err)
|
|
|
|
|
}
|
|
|
|
|
successMessage += fmt.Sprintf(", and mounted at %s", mountPoint)
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2017-03-23 16:09:17 -07:00
|
|
|
return successMessage, nil
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-28 17:38:06 -08:00
|
|
|
func getRBDAddData(name, poolName string, clientAccessInfo model.ClientAccessInfo) (string, error) {
|
2016-12-01 16:10:46 -08:00
|
|
|
if err := rc.VerifyClientAccessInfo(clientAccessInfo); err != nil {
|
2016-11-28 17:38:06 -08:00
|
|
|
return "", err
|
2016-09-30 12:56:56 -07:00
|
|
|
}
|
|
|
|
|
|
2016-12-01 16:10:46 -08:00
|
|
|
monAddrs := rc.ProcessMonAddresses(clientAccessInfo)
|
2016-09-30 12:56:56 -07:00
|
|
|
|
|
|
|
|
// mon address list (comma separated), user name, secret, pool name, image name
|
|
|
|
|
rbdAddData := fmt.Sprintf(
|
|
|
|
|
"%s name=%s,secret=%s %s %s",
|
|
|
|
|
strings.Join(monAddrs, ","),
|
2016-11-28 17:38:06 -08:00
|
|
|
clientAccessInfo.UserName,
|
|
|
|
|
clientAccessInfo.SecretKey,
|
2016-09-30 12:56:56 -07:00
|
|
|
poolName,
|
|
|
|
|
name)
|
|
|
|
|
|
|
|
|
|
return rbdAddData, nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-12 09:04:07 -07:00
|
|
|
func checkRBDSingleMajor(executor exec.Executor) bool {
|
2016-09-30 12:56:56 -07:00
|
|
|
// check to see if the rbd kernel module has single_major support
|
2017-04-28 08:21:35 -07:00
|
|
|
hasSingleMajor, err := sys.CheckKernelModuleParam(rbdKernelModuleName, "single_major", executor)
|
2016-09-30 12:56:56 -07:00
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Noticef("failed %s single_major check, assuming it's unsupported: %+v", rbdKernelModuleName, err)
|
2016-09-30 12:56:56 -07:00
|
|
|
hasSingleMajor = false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return hasSingleMajor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func openRBDFile(hasSingleMajor bool, singleMajorPath, path string) (*os.File, error) {
|
|
|
|
|
var fd *os.File
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
// attempt to open single_major if its supported, but fall back if needed
|
|
|
|
|
if hasSingleMajor {
|
|
|
|
|
fd, err = os.OpenFile(singleMajorPath, os.O_WRONLY, 0200)
|
|
|
|
|
if err != nil {
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Noticef("failed to open %s, falling back to %s: %+v", singleMajorPath, path, err)
|
2016-09-30 12:56:56 -07:00
|
|
|
fd = nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// still don't have an open file handle, try the regular path
|
|
|
|
|
if fd == nil {
|
|
|
|
|
fd, err = os.OpenFile(path, os.O_WRONLY, 0200)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to open %s: %+v", path, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fd, nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 09:40:34 -07:00
|
|
|
func waitForDevicePath(imageName, poolName, rbdSysBusPath, rbdDevicePathPrefix string, maxRetries, sleepSecs int) (string, error) {
|
2016-09-30 12:56:56 -07:00
|
|
|
retryCount := 0
|
|
|
|
|
for {
|
2017-10-09 14:36:21 -07:00
|
|
|
mappedFile, err := util.FindRBDMappedFile(imageName, poolName, rbdSysBusPath)
|
2017-09-14 09:40:34 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("failed to find mapped image: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if mappedFile != "" {
|
|
|
|
|
devicePath := rbdDevicePathPrefix + mappedFile
|
|
|
|
|
if _, err := os.Lstat(devicePath); err != nil {
|
|
|
|
|
return "", fmt.Errorf("sysfs information for image '%s' in pool '%s' found but the rbd device path %s does not exist", imageName, poolName, devicePath)
|
|
|
|
|
}
|
2016-09-30 12:56:56 -07:00
|
|
|
return devicePath, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retryCount++
|
|
|
|
|
if retryCount >= maxRetries {
|
|
|
|
|
return "", fmt.Errorf("exceeded retry count while finding device path: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-11 17:37:08 -08:00
|
|
|
logger.Noticef("failed to find device path, sleeping %d seconds: %+v", sleepSecs, err)
|
2016-09-30 12:56:56 -07:00
|
|
|
<-time.After(time.Duration(sleepSecs) * time.Second)
|
|
|
|
|
}
|
|
|
|
|
}
|