command_volume_unmount.go 1.9 KB

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