This repository was archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
interface additions: kill -a flag, ps command, pause & resume #478
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/hyperhq/runv/containerd/api/grpc/types" | ||
| "github.com/hyperhq/runv/lib/linuxsignal" | ||
| "github.com/urfave/cli" | ||
| netcontext "golang.org/x/net/context" | ||
| ) | ||
|
|
||
| var pauseCommand = cli.Command{ | ||
| Name: "pause", | ||
| Usage: "suspend all processes in the container", | ||
| ArgsUsage: `<container-id>`, | ||
| Action: func(context *cli.Context) error { | ||
| container := context.Args().First() | ||
| if container == "" { | ||
| return cli.NewExitError(fmt.Sprintf("container id cannot be empty"), -1) | ||
| } | ||
|
|
||
| c, err := getClient(filepath.Join(context.GlobalString("root"), container, "namespace/namespaced.sock")) | ||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1) | ||
| } | ||
|
|
||
| plist, err := getProcessList(c, container) | ||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("can't get process list, %v", err), -1) | ||
| } | ||
|
|
||
| for _, p := range plist { | ||
| if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ | ||
| Id: container, | ||
| Pid: p, | ||
| Signal: uint32(linuxsignal.SIGSTOP), | ||
| }); err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("suspend signal failed, %v", err), -1) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| var resumeCommand = cli.Command{ | ||
| Name: "resume", | ||
| Usage: "resume all processes in the container", | ||
| ArgsUsage: `<container-id>`, | ||
| Action: func(context *cli.Context) error { | ||
| container := context.Args().First() | ||
| if container == "" { | ||
| return cli.NewExitError(fmt.Sprintf("container id cannot be empty"), -1) | ||
| } | ||
|
|
||
| c, err := getClient(filepath.Join(context.GlobalString("root"), container, "namespace/namespaced.sock")) | ||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("failed to get client: %v", err), -1) | ||
| } | ||
|
|
||
| plist, err := getProcessList(c, container) | ||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("can't get process list, %v", err), -1) | ||
| } | ||
|
|
||
| for _, p := range plist { | ||
| if _, err := c.Signal(netcontext.Background(), &types.SignalRequest{ | ||
| Id: container, | ||
| Pid: p, | ||
| Signal: uint32(linuxsignal.SIGCONT), | ||
| }); err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("resume signal failed, %v", err), -1) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "text/tabwriter" | ||
|
|
||
| "github.com/hyperhq/runv/containerd/api/grpc/types" | ||
| "github.com/urfave/cli" | ||
| netcontext "golang.org/x/net/context" | ||
| ) | ||
|
|
||
| var psCommand = cli.Command{ | ||
| Name: "ps", | ||
| Usage: "ps displays the processes running inside a container", | ||
| ArgsUsage: `<container-id> [ps options]`, Flags: []cli.Flag{ | ||
| cli.StringFlag{ | ||
| Name: "format, f", | ||
| Value: "table", | ||
| Usage: `select one of: ` + formatOptions, | ||
| }, | ||
| }, | ||
| Action: func(context *cli.Context) error { | ||
| container := context.Args().First() | ||
| if container == "" { | ||
| return cli.NewExitError("container id cannot be empty", -1) | ||
| } | ||
| c, err := getContainerApi(context, container) | ||
| if err != nil { | ||
| return cli.NewExitError(fmt.Sprintf("can't access container, %v", err), -1) | ||
| } | ||
|
|
||
| switch context.String("format") { | ||
| case "table": | ||
| w := tabwriter.NewWriter(os.Stdout, 12, 1, 3, ' ', 0) | ||
| fmt.Fprint(w, "PROCESS\tCMD\n") | ||
| // we are limited by the containerd interface for now | ||
| for _, p := range c.Processes { | ||
| fmt.Fprintf(w, "%s\t%s\n", | ||
| p.Pid, | ||
| p.Args) | ||
| } | ||
| if err := w.Flush(); err != nil { | ||
| fatal(err) | ||
| } | ||
| case "json": | ||
| pids := make([]string, 0) | ||
| for _, p := range c.Processes { | ||
| pids = append(pids, p.Pid) | ||
| } | ||
|
|
||
| data, err := json.Marshal(pids) | ||
| if err != nil { | ||
| fatal(err) | ||
| } | ||
| os.Stdout.Write(data) | ||
| return nil | ||
| default: | ||
| return cli.NewExitError(fmt.Sprintf("invalid format option"), -1) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| func getContainerApi(context *cli.Context, container string) (*types.Container, error) { | ||
| api, err := getClient(filepath.Join(context.GlobalString("root"), container, "namespace/namespaced.sock")) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get client: %v", err) | ||
| } | ||
|
|
||
| s, err := api.State(netcontext.Background(), &types.StateRequest{Id: container}) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("get container state failed, %v", err) | ||
| } | ||
|
|
||
| for _, c := range s.Containers { | ||
| if c.Id == container { | ||
| return c, nil | ||
| } | ||
| } | ||
|
|
||
| return nil, fmt.Errorf("container %s not found", container) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we print more information such as command args?Never mind, this format is right.
But there's another problem, the pids should be '[]int' not '[]string', and it must be real pid of int type, now I can see pid "init" which makes
docker topnot work with runvThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use
p.SystemPidinstead ofp.Pid, it works fine according to my testThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SystemPid reports the PID of the qemu process on the host for the container's main process, and AFAIU is invalid for the rest. See my original comment on this topic, in the first comment of this thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tvelocity SystemPid reports PID of "nslistener" but not the qemu process, this is what we want. Because we need to make the nslistener a pseudo-container for network management. In more detail, we report "nslistener" pid as container init pid so that docker will treat "nslistener" as it's container, when CNI network plugins try to insert a network interface into the "nslistener" namespace, "nslistener" will know it and copy the network configuration into VM. This is how the network part works in runv.
PIDs in guestOS of VM is unmeaning to docker or other processes in host.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will merge it although the problems your reported are not addressed... You can fix it later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@WeiZhang555 ok, thanks for the clarification