2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013-2016
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
22 #include <sys/types.h>
23 #include <sys/ioctl.h>
26 #include <jailhouse.h>
28 #define JAILHOUSE_EXEC_DIR LIBEXECDIR "/jailhouse"
30 enum shutdown_load_mode {LOAD, SHUTDOWN};
33 char *cmd, *subcmd, *help;
36 static const struct extension extensions[] = {
37 { "cell", "linux", "CELLCONFIG KERNEL [-i | --initrd FILE]\n"
38 " [-c | --cmdline \"STRING\"] "
39 "[-w | --write-params FILE]" },
40 { "cell", "list", "" },
41 { "cell", "stats", "{ ID | [--name] NAME }" },
42 { "config", "create", "[-h] [-g] [-r ROOT] "
43 "[--mem-inmates MEM_INMATES]\n"
44 " [--mem-hv MEM_HV] FILE" },
45 { "config", "collect", "FILE.TAR" },
49 static void __attribute__((noreturn)) help(char *prog, int exit_status)
51 const struct extension *ext;
53 printf("Usage: %s { COMMAND | --help || --version }\n"
54 "\nAvailable commands:\n"
57 " cell create CELLCONFIG\n"
58 " cell load { ID | [--name] NAME } "
59 "{ IMAGE | { -s | --string } \"STRING\" }\n"
60 " [-a | --address ADDRESS] ...\n"
61 " cell start { ID | [--name] NAME }\n"
62 " cell shutdown { ID | [--name] NAME }\n"
63 " cell destroy { ID | [--name] NAME }\n",
65 for (ext = extensions; ext->cmd; ext++)
66 printf(" %s %s %s\n", ext->cmd, ext->subcmd, ext->help);
71 static void call_extension_script(const char *cmd, int argc, char *argv[])
73 const struct extension *ext;
74 char new_path[PATH_MAX];
80 for (ext = extensions; ext->cmd; ext++) {
81 if (strcmp(ext->cmd, cmd) != 0 ||
82 strcmp(ext->subcmd, argv[2]) != 0)
85 snprintf(new_path, sizeof(new_path), "PATH=%s:%s:%s",
86 dirname(argv[0]), JAILHOUSE_EXEC_DIR,
87 getenv("PATH") ? : "");
90 snprintf(script, sizeof(script), "jailhouse-%s-%s",
92 execvp(script, &argv[2]);
103 fd = open("/dev/jailhouse", O_RDWR);
105 perror("opening /dev/jailhouse");
111 static void *read_string(const char *string, size_t *size)
115 *size = strlen(string) + 1;
117 buffer = strdup(string);
119 fprintf(stderr, "insufficient memory\n");
126 static void *read_file(const char *name, size_t *size)
132 fd = open(name, O_RDONLY);
134 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
138 if (fstat(fd, &stat) < 0) {
143 buffer = malloc(stat.st_size);
145 fprintf(stderr, "insufficient memory\n");
149 if (read(fd, buffer, stat.st_size) < stat.st_size) {
150 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
157 *size = stat.st_size;
162 static int enable(int argc, char *argv[])
170 config = read_file(argv[2], NULL);
174 err = ioctl(fd, JAILHOUSE_ENABLE, config);
176 perror("JAILHOUSE_ENABLE");
184 static int cell_create(int argc, char *argv[])
186 struct jailhouse_cell_create cell_create;
193 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
194 cell_create.config_size = size;
198 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
200 perror("JAILHOUSE_CELL_CREATE");
203 free((void *)(unsigned long)cell_create.config_address);
208 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
211 bool use_name = false;
215 memset(cell_id, 0, sizeof(*cell_id));
220 if (strcmp(argv[0], "--name") == 0) {
227 cell_id->id = strtoll(argv[0], &endp, 0);
228 if (errno != 0 || *endp != 0 || cell_id->id < 0)
233 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
234 /* cell_id is initialized with zeros, so leaving out the last
235 * byte ensures that the string is always terminated. */
236 strncpy(cell_id->name, argv[arg_pos],
237 sizeof(cell_id->name) - 1);
243 static bool match_opt(const char *argv, const char *short_opt,
244 const char *long_opt)
246 return strcmp(argv, short_opt) == 0 ||
247 strcmp(argv, long_opt) == 0;
250 static int cell_shutdown_load(int argc, char *argv[],
251 enum shutdown_load_mode mode)
253 unsigned int images, id_args, arg_num, n;
254 struct jailhouse_preload_image *image;
255 struct jailhouse_cell_load *cell_load;
256 struct jailhouse_cell_id cell_id;
261 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
262 arg_num = 3 + id_args;
263 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
264 (mode == LOAD && arg_num == argc))
268 while (arg_num < argc) {
269 if (match_opt(argv[arg_num], "-s", "--string")) {
270 if (arg_num + 1 >= argc)
278 if (arg_num < argc &&
279 match_opt(argv[arg_num], "-a", "--address")) {
280 if (arg_num + 1 >= argc)
286 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
288 fprintf(stderr, "insufficient memory\n");
291 cell_load->cell_id = cell_id;
292 cell_load->num_preload_images = images;
294 arg_num = 3 + id_args;
296 for (n = 0, image = cell_load->image; n < images; n++, image++) {
297 if (match_opt(argv[arg_num], "-s", "--string")) {
299 image->source_address =
300 (unsigned long)read_string(argv[arg_num++],
303 image->source_address =
304 (unsigned long)read_file(argv[arg_num++],
308 image->target_address = 0;
310 if (arg_num < argc &&
311 match_opt(argv[arg_num], "-a", "--address")) {
313 image->target_address =
314 strtoll(argv[arg_num + 1], &endp, 0);
315 if (errno != 0 || *endp != 0)
323 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
325 perror("JAILHOUSE_CELL_LOAD");
328 for (n = 0, image = cell_load->image; n < images; n++, image++)
329 free((void *)(unsigned long)image->source_address);
335 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
337 struct jailhouse_cell_id cell_id;
338 int id_args, err, fd;
340 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
341 if (id_args == 0 || 3 + id_args != argc)
346 err = ioctl(fd, command, &cell_id);
348 perror(command == JAILHOUSE_CELL_START ?
349 "JAILHOUSE_CELL_START" :
350 command == JAILHOUSE_CELL_DESTROY ?
351 "JAILHOUSE_CELL_DESTROY" :
352 "<unknown command>");
359 static int cell_management(int argc, char *argv[])
366 if (strcmp(argv[2], "create") == 0) {
367 err = cell_create(argc, argv);
368 } else if (strcmp(argv[2], "load") == 0) {
369 err = cell_shutdown_load(argc, argv, LOAD);
370 } else if (strcmp(argv[2], "start") == 0) {
371 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
372 } else if (strcmp(argv[2], "shutdown") == 0) {
373 err = cell_shutdown_load(argc, argv, SHUTDOWN);
374 } else if (strcmp(argv[2], "destroy") == 0) {
375 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
377 call_extension_script("cell", argc, argv);
384 int main(int argc, char *argv[])
392 if (strcmp(argv[1], "enable") == 0) {
393 err = enable(argc, argv);
394 } else if (strcmp(argv[1], "disable") == 0) {
396 err = ioctl(fd, JAILHOUSE_DISABLE);
398 perror("JAILHOUSE_DISABLE");
400 } else if (strcmp(argv[1], "cell") == 0) {
401 err = cell_management(argc, argv);
402 } else if (strcmp(argv[1], "config") == 0) {
403 call_extension_script(argv[1], argc, argv);
405 } else if (strcmp(argv[1], "--version") == 0) {
406 printf("Jailhouse management tool %s\n", JAILHOUSE_VERSION);
408 } else if (strcmp(argv[1], "--help") == 0) {