You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.7 KiB

  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, &commandVolumeUnmount{})
  13. }
  14. type commandVolumeUnmount struct {
  15. }
  16. func (c *commandVolumeUnmount) Name() string {
  17. return "volume.unmount"
  18. }
  19. func (c *commandVolumeUnmount) Help() string {
  20. return `unmount a volume from one volume server
  21. volume.unmount <volume server host:port> <volume id>
  22. This command unmounts a volume from one volume server.
  23. `
  24. }
  25. func (c *commandVolumeUnmount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. if err = commandEnv.confirmIsLocked(); err != nil {
  27. return
  28. }
  29. if len(args) != 2 {
  30. fmt.Fprintf(writer, "received args: %+v\n", args)
  31. return fmt.Errorf("need 2 args of <volume server host:port> <volume id>")
  32. }
  33. sourceVolumeServer, volumeIdString := args[0], args[1]
  34. volumeId, err := needle.NewVolumeId(volumeIdString)
  35. if err != nil {
  36. return fmt.Errorf("wrong volume id format %s: %v", volumeId, err)
  37. }
  38. return unmountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)
  39. }
  40. func unmountVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer string) (err error) {
  41. return operation.WithVolumeServerClient(sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  42. _, unmountErr := volumeServerClient.VolumeUnmount(context.Background(), &volume_server_pb.VolumeUnmountRequest{
  43. VolumeId: uint32(volumeId),
  44. })
  45. return unmountErr
  46. })
  47. }