command_volume_mount.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  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 <volume server host:port> <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 len(args) != 2 {
  27. fmt.Fprintf(writer, "received args: %+v\n", args)
  28. return fmt.Errorf("need 2 args of <volume server host:port> <volume id>")
  29. }
  30. sourceVolumeServer, volumeIdString := args[0], args[1]
  31. volumeId, err := needle.NewVolumeId(volumeIdString)
  32. if err != nil {
  33. return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
  34. }
  35. return mountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)
  36. }
  37. func mountVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  38. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  39. _, mountErr := volumeServerClient.VolumeMount(context.Background(), &volume_server_pb.VolumeMountRequest{
  40. VolumeId: uint32(volumeId),
  41. })
  42. return mountErr
  43. })
  44. }