Skip to content

Commit

Permalink
Add nsenter command
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Jul 5, 2024
1 parent ebc7d3f commit 12eb0b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/nsenter/nsenter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package nsenter

import (
"bufio"
"fmt"
"os"
"os/exec"
"strconv"
"syscall"

"github.com/USTC-vlab/vct/pkg/cgroup"
"github.com/spf13/cobra"
)

func nsenter(id string) error {
f, err := cgroup.OpenLXC(id, "ns/init.scope/cgroup.procs")
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
if !s.Scan() {
return fmt.Errorf("empty cgroup file %s", f.Name())
}
args := []string{"nsenter", "-a", "-t", s.Text()}
path, err := exec.LookPath(args[0])
if err != nil {
return err
}
return syscall.Exec(path, args, os.Environ())
}

func runE(cmd *cobra.Command, args []string) error {
idStr := args[0]
_, err := strconv.Atoi(idStr)
if err != nil {
return err
}
return nsenter(idStr)
}

func MakeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "nsenter PID...",
Short: "Enter container by ID",
Args: cobra.ExactArgs(1),
RunE: runE,
}
return cmd
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/USTC-vlab/vct/cmd/iolimit"
"github.com/USTC-vlab/vct/cmd/iostat"
"github.com/USTC-vlab/vct/cmd/killall"
"github.com/USTC-vlab/vct/cmd/nsenter"
"github.com/USTC-vlab/vct/cmd/pressure"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -33,6 +34,7 @@ func MakeCmd() *cobra.Command {
iostat.MakeCmd(),
iolimit.MakeCmd(),
killall.MakeCmd(),
nsenter.MakeCmd(),
pressure.MakeCmd(),
versionCmd,
)
Expand Down

0 comments on commit 12eb0b6

Please sign in to comment.