command_volume_unmount.go 1.8 KB

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