2 * Jailhouse, a Linux-based partitioning hypervisor
4 * Copyright (c) Siemens AG, 2013
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 enum shutdown_load_mode {LOAD, SHUTDOWN};
31 char *cmd, *subcmd, *help;
34 static const struct extension extensions[] = {
35 { "cell", "list", "" },
36 { "cell", "stats", "{ ID | [--name] NAME }" },
37 { "config", "create", "[-h] [-g] [-r ROOT] "
38 "[--mem-inmates MEM_INMATES] [--mem-hv MEM_HV] FILE" },
39 { "config", "collect", "FILE.TAR" },
43 static void __attribute__((noreturn)) help(char *prog, int exit_status)
45 const struct extension *ext;
47 printf("Usage: %s { COMMAND | --help }\n"
48 "\nAvailable commands:\n"
51 " cell create CELLCONFIG\n"
52 " cell load { ID | [--name] NAME } IMAGE "
53 "[ -a | --address ADDRESS] ...\n"
54 " cell start { ID | [--name] NAME }\n"
55 " cell shutdown { ID | [--name] NAME }\n"
56 " cell destroy { ID | [--name] NAME }\n",
58 for (ext = extensions; ext->cmd; ext++)
59 printf(" %s %s %s\n", ext->cmd, ext->subcmd, ext->help);
64 static void call_extension_script(const char *cmd, int argc, char *argv[])
66 const struct extension *ext;
67 char new_path[PATH_MAX];
73 for (ext = extensions; ext->cmd; ext++) {
74 if (strcmp(ext->cmd, cmd) != 0 ||
75 strcmp(ext->subcmd, argv[2]) != 0)
78 snprintf(new_path, sizeof(new_path), "PATH=%s:%s:%s",
79 dirname(argv[0]), "/usr/lib/jailhouse",
80 getenv("PATH") ? : "");
83 snprintf(script, sizeof(script), "jailhouse-%s-%s",
85 execvp(script, &argv[2]);
96 fd = open("/dev/jailhouse", O_RDWR);
98 perror("opening /dev/jailhouse");
104 static void *read_file(const char *name, size_t *size)
110 fd = open(name, O_RDONLY);
112 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
116 if (fstat(fd, &stat) < 0) {
121 buffer = malloc(stat.st_size);
123 fprintf(stderr, "insufficient memory\n");
127 if (read(fd, buffer, stat.st_size) < stat.st_size) {
128 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
135 *size = stat.st_size;
140 static int enable(int argc, char *argv[])
148 config = read_file(argv[2], NULL);
152 err = ioctl(fd, JAILHOUSE_ENABLE, config);
154 perror("JAILHOUSE_ENABLE");
162 static int cell_create(int argc, char *argv[])
164 struct jailhouse_cell_create cell_create;
171 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
172 cell_create.config_size = size;
176 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
178 perror("JAILHOUSE_CELL_CREATE");
185 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
188 bool use_name = false;
195 if (strcmp(argv[0], "--name") == 0) {
202 cell_id->id = strtoll(argv[0], &endp, 0);
203 if (errno != 0 || *endp != 0 || cell_id->id < 0)
208 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
209 strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
210 cell_id->name[sizeof(cell_id->name) - 1] = 0;
216 static bool match_opt(const char *argv, const char *short_opt,
217 const char *long_opt)
219 return strcmp(argv, short_opt) == 0 ||
220 strcmp(argv, long_opt) == 0;
223 static int cell_shutdown_load(int argc, char *argv[],
224 enum shutdown_load_mode mode)
226 unsigned int images, id_args, arg_num, n;
227 struct jailhouse_preload_image *image;
228 struct jailhouse_cell_load *cell_load;
229 struct jailhouse_cell_id cell_id;
234 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
235 arg_num = 3 + id_args;
236 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
237 (mode == LOAD && arg_num == argc))
241 while (arg_num < argc) {
245 if (arg_num < argc &&
246 match_opt(argv[arg_num], "-a", "--address")) {
247 if (arg_num + 1 >= argc)
253 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
255 fprintf(stderr, "insufficient memory\n");
258 cell_load->cell_id = cell_id;
259 cell_load->num_preload_images = images;
261 arg_num = 3 + id_args;
263 for (n = 0, image = cell_load->image; n < images; n++, image++) {
264 image->source_address =
265 (unsigned long)read_file(argv[arg_num++], &size);
267 image->target_address = 0;
269 if (arg_num < argc &&
270 match_opt(argv[arg_num], "-a", "--address")) {
272 image->target_address =
273 strtoll(argv[arg_num + 1], &endp, 0);
274 if (errno != 0 || *endp != 0)
282 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
284 perror("JAILHOUSE_CELL_LOAD");
287 for (n = 0, image = cell_load->image; n < images; n++, image++)
288 free((void *)(unsigned long)image->source_address);
294 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
296 struct jailhouse_cell_id cell_id;
297 int id_args, err, fd;
299 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
300 if (id_args == 0 || 3 + id_args != argc)
305 err = ioctl(fd, command, &cell_id);
307 perror(command == JAILHOUSE_CELL_START ?
308 "JAILHOUSE_CELL_START" :
309 command == JAILHOUSE_CELL_DESTROY ?
310 "JAILHOUSE_CELL_DESTROY" :
311 "<unknown command>");
318 static int cell_management(int argc, char *argv[])
325 if (strcmp(argv[2], "create") == 0) {
326 err = cell_create(argc, argv);
327 } else if (strcmp(argv[2], "load") == 0) {
328 err = cell_shutdown_load(argc, argv, LOAD);
329 } else if (strcmp(argv[2], "start") == 0) {
330 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
331 } else if (strcmp(argv[2], "shutdown") == 0) {
332 err = cell_shutdown_load(argc, argv, SHUTDOWN);
333 } else if (strcmp(argv[2], "destroy") == 0) {
334 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
336 call_extension_script("cell", argc, argv);
343 int main(int argc, char *argv[])
351 if (strcmp(argv[1], "enable") == 0) {
352 err = enable(argc, argv);
353 } else if (strcmp(argv[1], "disable") == 0) {
355 err = ioctl(fd, JAILHOUSE_DISABLE);
357 perror("JAILHOUSE_DISABLE");
359 } else if (strcmp(argv[1], "cell") == 0) {
360 err = cell_management(argc, argv);
361 } else if (strcmp(argv[1], "config") == 0) {
362 call_extension_script(argv[1], argc, argv);
364 } else if (strcmp(argv[1], "--help") == 0) {