command_volume_mount.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "io"
  6. "github.com/chrislusf/seaweedfs/weed/operation"
  7. "github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "google.golang.org/grpc"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandVolumeMount{})
  13. }
  14. type commandVolumeMount struct {
  15. }
  16. func (c *commandVolumeMount) Name() string {
  17. return "volume.mount"
  18. }
  19. func (c *commandVolumeMount) Help() string {
  20. return `mount a volume from one volume server
  21. volume.mount -node <volume server host:port> -volumeId <volume id>
  22. This command mounts a volume from one volume server.
  23. `
  24. }
  25. func (c *commandVolumeMount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. if err = commandEnv.confirmIsLocked(); err != nil {
  27. return
  28. }
  29. volMountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  30. volumeIdInt := volMountCommand.Int("volumeId", 0, "the volume id")
  31. nodeStr := volMountCommand.String("node", "", "the volume server <host>:<port>")
  32. if err = volMountCommand.Parse(args); err != nil {
  33. return nil
  34. }
  35. sourceVolumeServer := *nodeStr
  36. volumeId := needle.VolumeId(*volumeIdInt)
  37. return mountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)
  38. }
  39. func mountVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  40. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  41. _, mountErr := volumeServerClient.VolumeMount(context.Background(), &volume_server_pb.VolumeMountRequest{
  42. VolumeId: uint32(volumeId),
  43. })
  44. return mountErr
  45. })
  46. }