-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathchroot_vm.c
More file actions
359 lines (315 loc) · 10.1 KB
/
chroot_vm.c
File metadata and controls
359 lines (315 loc) · 10.1 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* This is an example implementing chroot-like functionality with libkrun.
*
* It executes the requested command (relative to NEWROOT) inside a fresh
* Virtual Machine created and managed by libkrun.
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <libkrun.h>
#include <getopt.h>
#include <stdbool.h>
#include <assert.h>
#define MAX_ARGS_LEN 4096
#ifndef MAX_PATH
#define MAX_PATH 4096
#endif
enum net_mode {
NET_MODE_PASST = 0,
NET_MODE_TSI,
};
static void print_help(char *const name)
{
fprintf(stderr,
"Usage: %s [OPTIONS] NEWROOT COMMAND [COMMAND_ARGS...]\n"
"OPTIONS: \n"
" -h --help Show help\n"
" --log=PATH Write libkrun log to file or named pipe at PATH\n"
" --color-log=PATH Write libkrun log to file or named pipe at PATH, use color\n"
" --net=NET_MODE Set network mode\n"
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH\n"
" --vhost-user-rng=PATH Use vhost-user RNG backend at socket PATH\n"
"NET_MODE can be either TSI (default) or PASST\n"
"\n"
"NEWROOT: the root directory of the vm\n"
"COMMAND: the command you want to execute in the vm\n"
"COMMAND_ARGS: arguments of COMMAND\n",
name
);
}
static const struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "log", required_argument, NULL, 'L' },
{ "color-log", required_argument, NULL, 'C' },
{ "net_mode", required_argument, NULL, 'N' },
{ "passt-socket", required_argument, NULL, 'P' },
{ "vhost-user-rng", required_argument, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
struct cmdline {
bool show_help;
int log_target;
uint32_t log_style;
enum net_mode net_mode;
char const *passt_socket_path;
char const *vhost_user_rng_socket;
char const *new_root;
char *const *guest_argv;
};
bool cmdline_set_log_target(struct cmdline *cmdline, const char *arg) {
int fd = open(arg, O_WRONLY);
if (fd < 0) {
perror(arg);
return false;
}
if (cmdline->log_target > 0) {
close(cmdline->log_target);
}
cmdline->log_target = fd;
return true;
}
bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
{
assert(cmdline != NULL);
// set the defaults
*cmdline = (struct cmdline){
.show_help = false,
.net_mode = NET_MODE_TSI,
.passt_socket_path = NULL,
.vhost_user_rng_socket = NULL,
.new_root = NULL,
.guest_argv = NULL,
.log_target = KRUN_LOG_TARGET_DEFAULT,
.log_style = KRUN_LOG_STYLE_AUTO
};
int option_index = 0;
int c;
// the '+' in optstring is a GNU extension that disables permutating argv
while ((c = getopt_long(argc, argv, "+h", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
cmdline->show_help = true;
return true;
case 'C':
cmdline->log_style = KRUN_LOG_STYLE_ALWAYS;
/* fall through */
case 'L':
if (!cmdline_set_log_target(cmdline, optarg)) {
return false;
}
break;
case 'N':
if (strcasecmp("TSI", optarg) == 0) {
cmdline->net_mode = NET_MODE_TSI;
} else if(strcasecmp("PASST", optarg) == 0) {
cmdline->net_mode = NET_MODE_PASST;
} else {
fprintf(stderr, "Unknown mode %s\n", optarg);
return false;
}
break;
case 'P':
cmdline->passt_socket_path = optarg;
break;
case 'V':
cmdline->vhost_user_rng_socket = optarg;
break;
case '?':
return false;
default:
fprintf(stderr, "internal argument parsing error (returned character code 0x%x)\n", c);
return false;
}
}
if (optind <= argc - 2) {
cmdline->new_root = argv[optind];
cmdline->guest_argv = &argv[optind + 1];
return true;
}
if (optind >= argc - 1) {
fprintf(stderr, "Missing COMMAND argument\n");
}
if (optind == argc) {
fprintf(stderr, "Missing NEWROOT argument\n");
}
return false;
}
int start_passt()
{
int socket_fds[2];
const int PARENT = 0;
const int CHILD = 1;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_fds) < 0) {
perror("Failed to create passt socket fd");
return -1;
}
int pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) { // child
if (close(socket_fds[PARENT]) < 0) {
perror("close PARENT");
}
char fd_as_str[16];
snprintf(fd_as_str, sizeof(fd_as_str), "%d", socket_fds[CHILD]);
printf("passing fd %s to passt", fd_as_str);
if (execlp("passt", "passt", "-f", "--fd", fd_as_str, NULL) < 0) {
perror("execlp");
return -1;
}
} else { // parent
if (close(socket_fds[CHILD]) < 0) {
perror("close CHILD");
}
return socket_fds[PARENT];
}
}
int main(int argc, char *const argv[])
{
const char *const envp[] =
{
"TEST=works",
0
};
const char *const port_map[] =
{
"18000:8000",
0
};
const char *const rlimits[] =
{
// RLIMIT_NPROC = 6
"6=4096:8192",
0
};
int ctx_id;
int err;
int i;
struct cmdline cmdline;
struct rlimit rlim;
if (!parse_cmdline(argc, argv, &cmdline)) {
putchar('\n');
print_help(argv[0]);
return -1;
}
if (cmdline.show_help){
print_help(argv[0]);
return 0;
}
// Set the log level to "warn".
err = krun_init_log(cmdline.log_target, KRUN_LOG_LEVEL_WARN, cmdline.log_style, 0);
if (err) {
errno = -err;
perror("Error configuring log level");
return -1;
}
// Create the configuration context.
ctx_id = krun_create_ctx();
if (ctx_id < 0) {
errno = -ctx_id;
perror("Error creating configuration context");
return -1;
}
// Configure the number of vCPUs (1) and the amount of RAM (512 MiB).
if (err = krun_set_vm_config(ctx_id, 4, 4096)) {
errno = -err;
perror("Error configuring the number of vCPUs and/or the amount of RAM");
return -1;
}
// Configure vhost-user RNG if requested
if (cmdline.vhost_user_rng_socket != NULL) {
// Test sentinel-terminated array: auto-detect queue count, use custom size
uint16_t custom_sizes[] = {512, 0}; // 0 = sentinel terminator
if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_RNG,
cmdline.vhost_user_rng_socket, NULL, 0, custom_sizes)) {
errno = -err;
perror("Error adding vhost-user RNG device");
return -1;
}
printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket);
}
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
getrlimit(RLIMIT_NOFILE, &rlim);
rlim.rlim_cur = rlim.rlim_max;
setrlimit(RLIMIT_NOFILE, &rlim);
if (err = krun_set_root(ctx_id, cmdline.new_root)) {
errno = -err;
perror("Error configuring root path");
return -1;
}
uint32_t virgl_flags = VIRGLRENDERER_USE_EGL | VIRGLRENDERER_DRM |
VIRGLRENDERER_THREAD_SYNC | VIRGLRENDERER_USE_ASYNC_FENCE_CB;
if (err = krun_set_gpu_options(ctx_id, virgl_flags)) {
errno = -err;
perror("Error configuring gpu");
return -1;
}
// Map port 18000 in the host to 8000 in the guest (if networking uses TSI)
if (cmdline.net_mode == NET_MODE_TSI) {
if (err = krun_set_port_map(ctx_id, &port_map[0])) {
errno = -err;
perror("Error configuring port map");
return -1;
}
} else {
uint8_t mac[] = {0x5a, 0x94, 0xef, 0xe4, 0x0c, 0xee};
if (cmdline.passt_socket_path != NULL) {
if (err = krun_add_net_unixstream(ctx_id, cmdline.passt_socket_path, -1, &mac[0], COMPAT_NET_FEATURES, 0)) {
errno = -err;
perror("Error configuring net mode");
return -1;
}
} else {
int passt_fd = start_passt();
if (passt_fd < 0) {
return -1;
}
if (err = krun_add_net_unixstream(ctx_id, NULL, passt_fd, &mac[0], COMPAT_NET_FEATURES, 0)) {
errno = -err;
perror("Error configuring net mode");
return -1;
}
}
}
// Configure the rlimits that will be set in the guest
if (err = krun_set_rlimits(ctx_id, &rlimits[0])) {
errno = -err;
perror("Error configuring rlimits");
return -1;
}
// Set the working directory to "/", just for the sake of completeness.
if (err = krun_set_workdir(ctx_id, "/")) {
errno = -err;
perror("Error configuring \"/\" as working directory");
return -1;
}
// Specify the path of the binary to be executed in the isolated context, relative to the root path.
if (err = krun_set_exec(ctx_id, cmdline.guest_argv[0], (const char* const*) &cmdline.guest_argv[1], &envp[0])) {
errno = -err;
perror("Error configuring the parameters for the executable to be run");
return -1;
}
if (err = krun_split_irqchip(ctx_id, false)) {
errno = -err;
perror("Error setting split IRQCHIP property");
return -1;
}
// Start and enter the microVM. Unless there is some error while creating the microVM
// this function never returns.
if (err = krun_start_enter(ctx_id)) {
errno = -err;
perror("Error creating the microVM");
return -1;
}
// Not reached.
return 0;
}