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;
195 memset(cell_id, 0, sizeof(*cell_id));
200 if (strcmp(argv[0], "--name") == 0) {
207 cell_id->id = strtoll(argv[0], &endp, 0);
208 if (errno != 0 || *endp != 0 || cell_id->id < 0)
213 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
214 /* cell_id is initialized with zeros, so leaving out the last
215 * byte ensures that the string is always terminated. */
216 strncpy(cell_id->name, argv[arg_pos],
217 sizeof(cell_id->name) - 1);
223 static bool match_opt(const char *argv, const char *short_opt,
224 const char *long_opt)
226 return strcmp(argv, short_opt) == 0 ||
227 strcmp(argv, long_opt) == 0;
230 static int cell_shutdown_load(int argc, char *argv[],
231 enum shutdown_load_mode mode)
233 unsigned int images, id_args, arg_num, n;
234 struct jailhouse_preload_image *image;
235 struct jailhouse_cell_load *cell_load;
236 struct jailhouse_cell_id cell_id;
241 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
242 arg_num = 3 + id_args;
243 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
244 (mode == LOAD && arg_num == argc))
248 while (arg_num < argc) {
252 if (arg_num < argc &&
253 match_opt(argv[arg_num], "-a", "--address")) {
254 if (arg_num + 1 >= argc)
260 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
262 fprintf(stderr, "insufficient memory\n");
265 cell_load->cell_id = cell_id;
266 cell_load->num_preload_images = images;
268 arg_num = 3 + id_args;
270 for (n = 0, image = cell_load->image; n < images; n++, image++) {
271 image->source_address =
272 (unsigned long)read_file(argv[arg_num++], &size);
274 image->target_address = 0;
276 if (arg_num < argc &&
277 match_opt(argv[arg_num], "-a", "--address")) {
279 image->target_address =
280 strtoll(argv[arg_num + 1], &endp, 0);
281 if (errno != 0 || *endp != 0)
289 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
291 perror("JAILHOUSE_CELL_LOAD");
294 for (n = 0, image = cell_load->image; n < images; n++, image++)
295 free((void *)(unsigned long)image->source_address);
301 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
303 struct jailhouse_cell_id cell_id;
304 int id_args, err, fd;
306 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
307 if (id_args == 0 || 3 + id_args != argc)
312 err = ioctl(fd, command, &cell_id);
314 perror(command == JAILHOUSE_CELL_START ?
315 "JAILHOUSE_CELL_START" :
316 command == JAILHOUSE_CELL_DESTROY ?
317 "JAILHOUSE_CELL_DESTROY" :
318 "<unknown command>");
325 static int cell_management(int argc, char *argv[])
332 if (strcmp(argv[2], "create") == 0) {
333 err = cell_create(argc, argv);
334 } else if (strcmp(argv[2], "load") == 0) {
335 err = cell_shutdown_load(argc, argv, LOAD);
336 } else if (strcmp(argv[2], "start") == 0) {
337 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
338 } else if (strcmp(argv[2], "shutdown") == 0) {
339 err = cell_shutdown_load(argc, argv, SHUTDOWN);
340 } else if (strcmp(argv[2], "destroy") == 0) {
341 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
343 call_extension_script("cell", argc, argv);
350 int main(int argc, char *argv[])
358 if (strcmp(argv[1], "enable") == 0) {
359 err = enable(argc, argv);
360 } else if (strcmp(argv[1], "disable") == 0) {
362 err = ioctl(fd, JAILHOUSE_DISABLE);
364 perror("JAILHOUSE_DISABLE");
366 } else if (strcmp(argv[1], "cell") == 0) {
367 err = cell_management(argc, argv);
368 } else if (strcmp(argv[1], "config") == 0) {
369 call_extension_script(argv[1], argc, argv);
371 } else if (strcmp(argv[1], "--version") == 0) {
372 printf("Jailhouse management tool %s\n", JAILHOUSE_VERSION);
374 } else if (strcmp(argv[1], "--help") == 0) {