Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/workspacekit/cmd/nsenter.go
2498 views
1
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"log"
9
"os"
10
11
"github.com/spf13/cobra"
12
"golang.org/x/sys/unix"
13
14
"github.com/gitpod-io/gitpod/common-go/nsenter"
15
)
16
17
var nsenterOpts struct {
18
Target int
19
MountNS bool
20
NetNS bool
21
}
22
23
var nsenterCmd = &cobra.Command{
24
Use: "nsenter <cmd> <args ...>",
25
Short: "enters namespaces and executes the arg",
26
Args: cobra.MinimumNArgs(1),
27
Aliases: []string{"handler"},
28
Run: func(_ *cobra.Command, args []string) {
29
if os.Getenv("_LIBNSENTER_INIT") != "" {
30
err := unix.Exec(args[0], args, os.Environ())
31
if err != nil {
32
log.Fatalf("cannot exec: %v", err)
33
}
34
return
35
}
36
37
var ns []nsenter.Namespace
38
if nsenterOpts.MountNS {
39
ns = append(ns, nsenter.NamespaceMount)
40
}
41
if nsenterOpts.NetNS {
42
ns = append(ns, nsenter.NamespaceNet)
43
}
44
err := nsenter.Run(nsenterOpts.Target, args, nil, ns...)
45
if err != nil {
46
log.Fatal(err)
47
}
48
},
49
}
50
51
func init() {
52
rootCmd.AddCommand(nsenterCmd)
53
54
nsenterCmd.Flags().IntVar(&nsenterOpts.Target, "target", 0, "target PID")
55
nsenterCmd.Flags().BoolVar(&nsenterOpts.MountNS, "mount", false, "enter mount namespace")
56
nsenterCmd.Flags().BoolVar(&nsenterOpts.NetNS, "net", false, "enter network namespace")
57
}
58
59