command_volume_mount.go 1.8 KB

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