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 #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", "list", "" },
38 { "cell", "stats", "{ ID | [--name] NAME }" },
39 { "config", "create", "[-h] [-g] [-r ROOT] "
40 "[--mem-inmates MEM_INMATES] [--mem-hv MEM_HV] FILE" },
41 { "config", "collect", "FILE.TAR" },
45 static void __attribute__((noreturn)) help(char *prog, int exit_status)
47 const struct extension *ext;
49 printf("Usage: %s { COMMAND | --help || --version }\n"
50 "\nAvailable commands:\n"
53 " cell create CELLCONFIG\n"
54 " cell load { ID | [--name] NAME } IMAGE "
55 "[ -a | --address ADDRESS] ...\n"
56 " cell start { ID | [--name] NAME }\n"
57 " cell shutdown { ID | [--name] NAME }\n"
58 " cell destroy { ID | [--name] NAME }\n",
60 for (ext = extensions; ext->cmd; ext++)
61 printf(" %s %s %s\n", ext->cmd, ext->subcmd, ext->help);
66 static void call_extension_script(const char *cmd, int argc, char *argv[])
68 const struct extension *ext;
69 char new_path[PATH_MAX];
75 for (ext = extensions; ext->cmd; ext++) {
76 if (strcmp(ext->cmd, cmd) != 0 ||
77 strcmp(ext->subcmd, argv[2]) != 0)
80 snprintf(new_path, sizeof(new_path), "PATH=%s:%s:%s",
81 dirname(argv[0]), JAILHOUSE_EXEC_DIR,
82 getenv("PATH") ? : "");
85 snprintf(script, sizeof(script), "jailhouse-%s-%s",
87 execvp(script, &argv[2]);
98 fd = open("/dev/jailhouse", O_RDWR);
100 perror("opening /dev/jailhouse");
106 static void *read_file(const char *name, size_t *size)
112 fd = open(name, O_RDONLY);
114 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
118 if (fstat(fd, &stat) < 0) {
123 buffer = malloc(stat.st_size);
125 fprintf(stderr, "insufficient memory\n");
129 if (read(fd, buffer, stat.st_size) < stat.st_size) {
130 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
137 *size = stat.st_size;
142 static int enable(int argc, char *argv[])
150 config = read_file(argv[2], NULL);
154 err = ioctl(fd, JAILHOUSE_ENABLE, config);
156 perror("JAILHOUSE_ENABLE");
164 static int cell_create(int argc, char *argv[])
166 struct jailhouse_cell_create cell_create;
173 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
174 cell_create.config_size = size;
178 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
180 perror("JAILHOUSE_CELL_CREATE");
183 free((void *)(unsigned long)cell_create.config_address);
188 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
191 bool use_name = false;
198 if (strcmp(argv[0], "--name") == 0) {
205 cell_id->id = strtoll(argv[0], &endp, 0);
206 if (errno != 0 || *endp != 0 || cell_id->id < 0)
211 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
212 strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
213 cell_id->name[sizeof(cell_id->name) - 1] = 0;
219 static bool match_opt(const char *argv, const char *short_opt,
220 const char *long_opt)
222 return strcmp(argv, short_opt) == 0 ||
223 strcmp(argv, long_opt) == 0;
226 static int cell_shutdown_load(int argc, char *argv[],
227 enum shutdown_load_mode mode)
229 unsigned int images, id_args, arg_num, n;
230 struct jailhouse_preload_image *image;
231 struct jailhouse_cell_load *cell_load;
232 struct jailhouse_cell_id cell_id;
237 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
238 arg_num = 3 + id_args;
239 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
240 (mode == LOAD && arg_num == argc))
244 while (arg_num < argc) {
248 if (arg_num < argc &&
249 match_opt(argv[arg_num], "-a", "--address")) {
250 if (arg_num + 1 >= argc)
256 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
258 fprintf(stderr, "insufficient memory\n");
261 cell_load->cell_id = cell_id;
262 cell_load->num_preload_images = images;
264 arg_num = 3 + id_args;
266 for (n = 0, image = cell_load->image; n < images; n++, image++) {
267 image->source_address =
268 (unsigned long)read_file(argv[arg_num++], &size);
270 image->target_address = 0;
272 if (arg_num < argc &&
273 match_opt(argv[arg_num], "-a", "--address")) {
275 image->target_address =
276 strtoll(argv[arg_num + 1], &endp, 0);
277 if (errno != 0 || *endp != 0)
285 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
287 perror("JAILHOUSE_CELL_LOAD");
290 for (n = 0, image = cell_load->image; n < images; n++, image++)
291 free((void *)(unsigned long)image->source_address);
297 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
299 struct jailhouse_cell_id cell_id;
300 int id_args, err, fd;
302 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
303 if (id_args == 0 || 3 + id_args != argc)
308 err = ioctl(fd, command, &cell_id);
310 perror(command == JAILHOUSE_CELL_START ?
311 "JAILHOUSE_CELL_START" :
312 command == JAILHOUSE_CELL_DESTROY ?
313 "JAILHOUSE_CELL_DESTROY" :
314 "<unknown command>");
321 static int cell_management(int argc, char *argv[])
328 if (strcmp(argv[2], "create") == 0) {
329 err = cell_create(argc, argv);
330 } else if (strcmp(argv[2], "load") == 0) {
331 err = cell_shutdown_load(argc, argv, LOAD);
332 } else if (strcmp(argv[2], "start") == 0) {
333 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
334 } else if (strcmp(argv[2], "shutdown") == 0) {
335 err = cell_shutdown_load(argc, argv, SHUTDOWN);
336 } else if (strcmp(argv[2], "destroy") == 0) {
337 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
339 call_extension_script("cell", argc, argv);
346 int main(int argc, char *argv[])
354 if (strcmp(argv[1], "enable") == 0) {
355 err = enable(argc, argv);
356 } else if (strcmp(argv[1], "disable") == 0) {
358 err = ioctl(fd, JAILHOUSE_DISABLE);
360 perror("JAILHOUSE_DISABLE");
362 } else if (strcmp(argv[1], "cell") == 0) {
363 err = cell_management(argc, argv);
364 } else if (strcmp(argv[1], "config") == 0) {
365 call_extension_script(argv[1], argc, argv);
367 } else if (strcmp(argv[1], "--version") == 0) {
368 printf("Jailhouse management tool %s\n", JAILHOUSE_VERSION);
370 } else if (strcmp(argv[1], "--help") == 0) {