forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
253 lines (220 loc) · 7.49 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"fmt"
"os"
"os/user"
"runtime"
"strconv"
"strings"
"syscall"
"github.com/spf13/cobra"
"golang.org/x/sys/unix"
)
var (
mntNamespace string
cpuTime uint64
memoryBytes uint64
targetUser string
)
func init() {
// main needs to be locked on one thread and no go routines
runtime.LockOSThread()
}
func main() {
rootCmd := &cobra.Command{
Use: "virt-chroot",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if mntNamespace != "" {
// join the mount namespace of a process
fd, err := os.Open(mntNamespace)
if err != nil {
return fmt.Errorf("failed to open mount namespace: %v", err)
}
defer fd.Close()
if err = unix.Unshare(unix.CLONE_NEWNS); err != nil {
return fmt.Errorf("failed to detach from parent mount namespace: %v", err)
}
if err := unix.Setns(int(fd.Fd()), unix.CLONE_NEWNS); err != nil {
return fmt.Errorf("failed to join the mount namespace: %v", err)
}
}
// Looking up users needs resources, let's do it before we set rlimits.
var u *user.User
if targetUser != "" {
var err error
u, err = user.Lookup(targetUser)
if err != nil {
return fmt.Errorf("failed to look up user: %v", err)
}
}
if cpuTime > 0 {
value := &syscall.Rlimit{
Cur: cpuTime,
Max: cpuTime,
}
err := syscall.Setrlimit(unix.RLIMIT_CPU, value)
if err != nil {
return fmt.Errorf("error setting prlimit on cpu time with value %d: %v", value, err)
}
}
if memoryBytes > 0 {
value := &syscall.Rlimit{
Cur: memoryBytes,
Max: memoryBytes,
}
err := syscall.Setrlimit(unix.RLIMIT_AS, value)
if err != nil {
return fmt.Errorf("error setting prlimit on virtual memory with value %d: %v", value, err)
}
}
// Now let's switch users and drop privileges
if u != nil {
uid, err := strconv.ParseInt(u.Uid, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse uid: %v", err)
}
gid, err := strconv.ParseInt(u.Gid, 10, 32)
if err != nil {
return fmt.Errorf("failed to parse gid: %v", err)
}
err = unix.Setgroups([]int{int(gid)})
if err != nil {
return fmt.Errorf("failed to drop auxiliary groups: %v", err)
}
_, _, errno := syscall.Syscall(syscall.SYS_SETGID, uintptr(gid), 0, 0)
if errno != 0 {
return fmt.Errorf("failed to join the group of the user: %v", err)
}
_, _, errno = syscall.Syscall(syscall.SYS_SETUID, uintptr(uid), 0, 0)
if errno != 0 {
return fmt.Errorf("failed to switch to user: %v", err)
}
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
_, _ = fmt.Fprint(cmd.OutOrStderr(), cmd.UsageString())
},
}
rootCmd.PersistentFlags().Uint64Var(&cpuTime, "cpu", 0, "cpu time in seconds for the process")
rootCmd.PersistentFlags().Uint64Var(&memoryBytes, "memory", 0, "memory in bytes for the process")
rootCmd.PersistentFlags().StringVar(&mntNamespace, "mount", "", "mount namespace to use")
rootCmd.PersistentFlags().StringVar(&targetUser, "user", "", "switch to this targetUser to e.g. drop privileges")
execCmd := &cobra.Command{
Use: "exec",
Short: "execute a sandboxed command in a specific mount namespace",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
err := syscall.Exec(args[0], args, os.Environ())
if err != nil {
return fmt.Errorf("failed to execute command: %v", err)
}
return nil
},
}
mntCmd := &cobra.Command{
Use: "mount",
Short: "mount operations in a specific mount namespace",
Args: cobra.MinimumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
var mntOpts uint = 0
fsType := cmd.Flag("type").Value.String()
mntOptions := cmd.Flag("options").Value.String()
for _, opt := range strings.Split(mntOptions, ",") {
opt = strings.TrimSpace(opt)
switch opt {
case "ro":
mntOpts = mntOpts | syscall.MS_RDONLY
case "bind":
mntOpts = mntOpts | syscall.MS_BIND
default:
return fmt.Errorf("mount option %s is not supported", opt)
}
}
return syscall.Mount(args[0], args[1], fsType, uintptr(mntOpts), "")
},
}
mntCmd.Flags().StringP("options", "o", "", "comma separated list of mount options")
mntCmd.Flags().StringP("type", "t", "", "fstype")
umntCmd := &cobra.Command{
Use: "umount",
Short: "unmount in a specific mount namespace",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return syscall.Unmount(args[0], 0)
},
}
selinuxCmd := &cobra.Command{
Use: "selinux",
Short: "run selinux operations in specific namespaces",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
_, _ = fmt.Fprint(cmd.OutOrStderr(), cmd.UsageString())
},
}
selinuxCmd.AddCommand(
NewGetEnforceCommand(), RelabelCommand(),
)
createTapCmd := NewCreateTapCommand()
createTapCmd.Flags().String("tap-name", "tap0", "the name of the tap device")
createTapCmd.Flags().Uint("uid", 0, "the owner of the tap device")
createTapCmd.Flags().Uint("gid", 0, "the group of the owner of the tap device")
createTapCmd.Flags().Uint32("queue-number", 0, "the number of queues to use on multi-queued devices")
createTapCmd.Flags().Uint32("mtu", 1500, "the link MTU of the tap device")
createMDEVCmd := NewCreateMDEVCommand()
createMDEVCmd.Flags().String("type", "", "the type of a mediated device")
createMDEVCmd.Flags().String("parent", "", "id of a parent (e.g. PCI_ID) for the new mediated device")
createMDEVCmd.Flags().String("uuid", "", "uuid for the new mediated device")
removeMDEVCmd := NewRemoveMDEVCommand()
removeMDEVCmd.Flags().String("uuid", "", "uuid of the mediated device to remove")
cgroupsCmd := &cobra.Command{
Use: "set-cgroups-resources",
Short: "Set cgroups resources",
RunE: func(cmd *cobra.Command, args []string) error {
marshalledPathsHash := cmd.Flag("subsystem-paths").Value.String()
if marshalledPathsHash == "" {
return fmt.Errorf("path argument cannot be empty")
}
marshalledResourcesHash := cmd.Flag("resources").Value.String()
isRootless, err := strconv.ParseBool(cmd.Flag("rootless").Value.String())
if err != nil {
return fmt.Errorf("cannot convert rootless into bool. err: %v", err)
}
isV2, err := strconv.ParseBool(cmd.Flag("isV2").Value.String())
if err != nil {
return fmt.Errorf("cannot convert isV2 into bool. err: %v", err)
}
unmarshalledResources, err := decodeResources(marshalledResourcesHash)
if err != nil {
return err
}
unmarshalledPaths, err := decodePaths(marshalledPathsHash)
if err != nil {
return err
}
if err = setCgroupResources(unmarshalledPaths, unmarshalledResources, isRootless, isV2); err != nil {
return err
}
return nil
},
}
cgroupsCmd.Flags().String("subsystem-paths", "", "marshalled map[string]string type, encoded to base64 format. "+
"For v1 key is cgroup subsystem and value is its path, for v2 the only key is an empty string and the value is cgroup dir path.")
cgroupsCmd.Flags().String("resources", "", "marshalled Resources type (defined in github.com/opencontainers/runc/libcontainer/configs/cgroup_linux.go), encoded to base64 format")
cgroupsCmd.Flags().Bool("rootless", false, "true to run rootless")
cgroupsCmd.Flags().Bool("isV2", false, "true to run rootless")
rootCmd.AddCommand(
execCmd,
mntCmd,
umntCmd,
selinuxCmd,
createTapCmd,
createMDEVCmd,
removeMDEVCmd,
cgroupsCmd,
)
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}