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.

75 lines
2.0 KiB

2 years ago
3 months ago
  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) HasTag(CommandTag) bool {
  28. return false
  29. }
  30. func (c *commandVolumeUnmount) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  31. volUnmountCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  32. volumeIdInt := volUnmountCommand.Int("volumeId", 0, "the volume id")
  33. nodeStr := volUnmountCommand.String("node", "", "the volume server <host>:<port>")
  34. if err = volUnmountCommand.Parse(args); err != nil {
  35. return nil
  36. }
  37. if *nodeStr == "" {
  38. return fmt.Errorf("-node option is required")
  39. }
  40. if *volumeIdInt == 0 {
  41. return fmt.Errorf("-volumeId option is required")
  42. }
  43. if err = commandEnv.confirmIsLocked(args); err != nil {
  44. return
  45. }
  46. sourceVolumeServer := pb.ServerAddress(*nodeStr)
  47. volumeId := needle.VolumeId(*volumeIdInt)
  48. return unmountVolume(commandEnv.option.GrpcDialOption, volumeId, sourceVolumeServer)
  49. }
  50. func unmountVolume(grpcDialOption grpc.DialOption, volumeId needle.VolumeId, sourceVolumeServer pb.ServerAddress) (err error) {
  51. return operation.WithVolumeServerClient(false, sourceVolumeServer, grpcDialOption, func(volumeServerClient volume_server_pb.VolumeServerClient) error {
  52. _, unmountErr := volumeServerClient.VolumeUnmount(context.Background(), &volume_server_pb.VolumeUnmountRequest{
  53. VolumeId: uint32(volumeId),
  54. })
  55. return unmountErr
  56. })
  57. }