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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
23 #include <jailhouse.h>
25 static void help(const char *progname)
27 printf("%s <command> <args>\n"
28 "\nAvailable commands:\n"
29 " enable CONFIGFILE\n"
31 " cell create CONFIGFILE IMAGE [-l ADDRESS] "
32 "[IMAGE [-l ADDRESS] ...]\n"
33 " cell destroy CONFIGFILE\n",
41 fd = open("/dev/jailhouse", O_RDWR);
43 perror("opening /dev/jailhouse");
49 static void *read_file(const char *name, size_t *size)
55 fd = open(name, O_RDONLY);
57 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
61 if (fstat(fd, &stat) < 0) {
66 buffer = malloc(stat.st_size);
68 fprintf(stderr, "insufficient memory\n");
72 if (read(fd, buffer, stat.st_size) < stat.st_size) {
73 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
85 static int enable(int argc, char *argv[])
95 config = read_file(argv[2], NULL);
99 err = ioctl(fd, JAILHOUSE_ENABLE, config);
101 perror("JAILHOUSE_ENABLE");
109 static int cell_create(int argc, char *argv[])
111 struct jailhouse_preload_image *image;
112 struct jailhouse_new_cell *cell;
113 unsigned int images, arg_num;
125 while (arg_num < argc) {
129 if (arg_num < argc && strcmp(argv[arg_num], "-l") == 0) {
130 if (arg_num + 1 >= argc) {
138 cell = malloc(sizeof(*cell) + sizeof(*image) * images);
140 fprintf(stderr, "insufficient memory\n");
144 cell->config_address = (unsigned long)read_file(argv[3], &size);
145 cell->config_size = size;
146 cell->num_preload_images = images;
152 image->source_address =
153 (unsigned long)read_file(argv[arg_num++], &size);
155 image->target_address = 0;
157 if (arg_num < argc && strcmp(argv[arg_num], "-l") == 0) {
159 image->target_address =
160 strtoll(argv[arg_num + 1], &endp, 0);
161 if (errno != 0 || *endp != 0) {
173 err = ioctl(fd, JAILHOUSE_CELL_CREATE, cell);
175 perror("JAILHOUSE_CELL_CREATE");
178 free((void *)(unsigned long)cell->config_address);
179 free((void *)(unsigned long)image->source_address);
185 static int cell_destroy(int argc, char *argv[])
187 struct jailhouse_cell cell;
196 cell.config_address = (unsigned long)read_file(argv[3], &size);
197 cell.config_size = size;
201 err = ioctl(fd, JAILHOUSE_CELL_DESTROY, &cell);
203 perror("JAILHOUSE_CELL_DESTROY");
206 free((void *)(unsigned long)cell.config_address);
211 static int cell_management(int argc, char *argv[])
220 if (strcmp(argv[2], "create") == 0)
221 err = cell_create(argc, argv);
222 else if (strcmp(argv[2], "destroy") == 0)
223 err = cell_destroy(argc, argv);
232 int main(int argc, char *argv[])
242 if (strcmp(argv[1], "enable") == 0) {
243 err = enable(argc, argv);
244 } else if (strcmp(argv[1], "disable") == 0) {
246 err = ioctl(fd, JAILHOUSE_DISABLE);
248 perror("JAILHOUSE_DISABLE");
250 } else if (strcmp(argv[1], "cell") == 0) {
251 err = cell_management(argc, argv);