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", "" },
39 static void __attribute__((noreturn)) help(char *prog, int exit_status)
41 const struct extension *ext;
43 printf("Usage: %s { COMMAND | --help }\n"
44 "\nAvailable commands:\n"
47 " cell create CELLCONFIG\n"
48 " cell load { ID | [--name] NAME } IMAGE "
49 "[ -a | --address ADDRESS] ...\n"
50 " cell start { ID | [--name] NAME }\n"
51 " cell shutdown { ID | [--name] NAME }\n"
52 " cell destroy { ID | [--name] NAME }\n",
54 for (ext = extensions; ext->cmd; ext++)
55 printf(" %s %s %s\n", ext->cmd, ext->subcmd, ext->help);
60 static void call_extension_script(const char *cmd, int argc, char *argv[])
62 const struct extension *ext;
63 char new_path[PATH_MAX];
69 for (ext = extensions; ext->cmd; ext++) {
70 if (strcmp(ext->cmd, cmd) != 0 ||
71 strcmp(ext->subcmd, argv[2]) != 0)
74 snprintf(new_path, sizeof(new_path), "PATH=%s:%s:%s",
75 dirname(argv[0]), "/usr/lib/jailhouse",
76 getenv("PATH") ? : "");
79 snprintf(script, sizeof(script), "jailhouse-%s-%s",
81 execvp(script, &argv[2]);
92 fd = open("/dev/jailhouse", O_RDWR);
94 perror("opening /dev/jailhouse");
100 static void *read_file(const char *name, size_t *size)
106 fd = open(name, O_RDONLY);
108 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
112 if (fstat(fd, &stat) < 0) {
117 buffer = malloc(stat.st_size);
119 fprintf(stderr, "insufficient memory\n");
123 if (read(fd, buffer, stat.st_size) < stat.st_size) {
124 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
131 *size = stat.st_size;
136 static int enable(int argc, char *argv[])
144 config = read_file(argv[2], NULL);
148 err = ioctl(fd, JAILHOUSE_ENABLE, config);
150 perror("JAILHOUSE_ENABLE");
158 static int cell_create(int argc, char *argv[])
160 struct jailhouse_cell_create cell_create;
167 cell_create.config_address = (unsigned long)read_file(argv[3], &size);
168 cell_create.config_size = size;
172 err = ioctl(fd, JAILHOUSE_CELL_CREATE, &cell_create);
174 perror("JAILHOUSE_CELL_CREATE");
181 static int parse_cell_id(struct jailhouse_cell_id *cell_id, int argc,
184 bool use_name = false;
191 if (strcmp(argv[0], "--name") == 0) {
198 cell_id->id = strtoll(argv[0], &endp, 0);
199 if (errno != 0 || *endp != 0 || cell_id->id < 0)
204 cell_id->id = JAILHOUSE_CELL_ID_UNUSED;
205 strncpy(cell_id->name, argv[arg_pos], sizeof(cell_id->name));
206 cell_id->name[sizeof(cell_id->name) - 1] = 0;
212 static bool match_opt(const char *argv, const char *short_opt,
213 const char *long_opt)
215 return strcmp(argv, short_opt) == 0 ||
216 strcmp(argv, long_opt) == 0;
219 static int cell_shutdown_load(int argc, char *argv[],
220 enum shutdown_load_mode mode)
222 unsigned int images, id_args, arg_num, n;
223 struct jailhouse_preload_image *image;
224 struct jailhouse_cell_load *cell_load;
225 struct jailhouse_cell_id cell_id;
230 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
231 arg_num = 3 + id_args;
232 if (id_args == 0 || (mode == SHUTDOWN && arg_num != argc) ||
233 (mode == LOAD && arg_num == argc))
237 while (arg_num < argc) {
241 if (arg_num < argc &&
242 match_opt(argv[arg_num], "-a", "--address")) {
243 if (arg_num + 1 >= argc)
249 cell_load = malloc(sizeof(*cell_load) + sizeof(*image) * images);
251 fprintf(stderr, "insufficient memory\n");
254 cell_load->cell_id = cell_id;
255 cell_load->num_preload_images = images;
257 arg_num = 3 + id_args;
259 for (n = 0, image = cell_load->image; n < images; n++, image++) {
260 image->source_address =
261 (unsigned long)read_file(argv[arg_num++], &size);
263 image->target_address = 0;
265 if (arg_num < argc &&
266 match_opt(argv[arg_num], "-a", "--address")) {
268 image->target_address =
269 strtoll(argv[arg_num + 1], &endp, 0);
270 if (errno != 0 || *endp != 0)
278 err = ioctl(fd, JAILHOUSE_CELL_LOAD, cell_load);
280 perror("JAILHOUSE_CELL_LOAD");
283 for (n = 0, image = cell_load->image; n < images; n++, image++)
284 free((void *)(unsigned long)image->source_address);
290 static int cell_simple_cmd(int argc, char *argv[], unsigned int command)
292 struct jailhouse_cell_id cell_id;
293 int id_args, err, fd;
295 id_args = parse_cell_id(&cell_id, argc - 3, &argv[3]);
296 if (id_args == 0 || 3 + id_args != argc)
301 err = ioctl(fd, command, &cell_id);
303 perror(command == JAILHOUSE_CELL_START ?
304 "JAILHOUSE_CELL_START" :
305 command == JAILHOUSE_CELL_DESTROY ?
306 "JAILHOUSE_CELL_DESTROY" :
307 "<unknown command>");
314 static int cell_management(int argc, char *argv[])
321 if (strcmp(argv[2], "create") == 0) {
322 err = cell_create(argc, argv);
323 } else if (strcmp(argv[2], "load") == 0) {
324 err = cell_shutdown_load(argc, argv, LOAD);
325 } else if (strcmp(argv[2], "start") == 0) {
326 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_START);
327 } else if (strcmp(argv[2], "shutdown") == 0) {
328 err = cell_shutdown_load(argc, argv, SHUTDOWN);
329 } else if (strcmp(argv[2], "destroy") == 0) {
330 err = cell_simple_cmd(argc, argv, JAILHOUSE_CELL_DESTROY);
332 call_extension_script("cell", argc, argv);
339 int main(int argc, char *argv[])
347 if (strcmp(argv[1], "enable") == 0) {
348 err = enable(argc, argv);
349 } else if (strcmp(argv[1], "disable") == 0) {
351 err = ioctl(fd, JAILHOUSE_DISABLE);
353 perror("JAILHOUSE_DISABLE");
355 } else if (strcmp(argv[1], "cell") == 0) {
356 err = cell_management(argc, argv);
357 } else if (strcmp(argv[1], "--help") == 0) {