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 PRELOADIMAGE [-l ADDRESS]\n"
32 " cell destroy CONFIGFILE\n",
40 fd = open("/dev/jailhouse", O_RDWR);
42 perror("opening /dev/jailhouse");
48 static void *read_file(const char *name, size_t *size)
54 fd = open(name, O_RDONLY);
56 fprintf(stderr, "opening %s: %s\n", name, strerror(errno));
60 if (fstat(fd, &stat) < 0) {
65 buffer = malloc(stat.st_size);
67 fprintf(stderr, "insufficient memory\n");
71 if (read(fd, buffer, stat.st_size) < stat.st_size) {
72 fprintf(stderr, "reading %s: %s\n", name, strerror(errno));
84 static int enable(int argc, char *argv[])
94 config = read_file(argv[2], NULL);
98 err = ioctl(fd, JAILHOUSE_ENABLE, config);
100 perror("JAILHOUSE_ENABLE");
108 static int cell_create(int argc, char *argv[])
111 struct jailhouse_new_cell cell;
112 struct jailhouse_preload_image image;
114 struct jailhouse_new_cell *cell = ¶ms.cell;
115 struct jailhouse_preload_image *image = params.cell.image;
120 if (argc != 5 && argc != 7) {
125 cell->config_address = (unsigned long)read_file(argv[3], &size);
126 cell->config_size = size;
127 cell->num_preload_images = 1;
129 image->source_address = (unsigned long)read_file(argv[4], &size);
131 image->target_address = 0;
135 image->target_address = strtoll(argv[6], &endp, 0);
136 if (errno != 0 || *endp != 0 || strcmp(argv[5], "-l") != 0) {
144 err = ioctl(fd, JAILHOUSE_CELL_CREATE, ¶ms);
146 perror("JAILHOUSE_CELL_CREATE");
149 free((void *)(unsigned long)cell->config_address);
150 free((void *)(unsigned long)image->source_address);
155 static int cell_destroy(int argc, char *argv[])
157 struct jailhouse_cell cell;
166 cell.config_address = (unsigned long)read_file(argv[3], &size);
167 cell.config_size = size;
171 err = ioctl(fd, JAILHOUSE_CELL_DESTROY, &cell);
173 perror("JAILHOUSE_CELL_DESTROY");
176 free((void *)(unsigned long)cell.config_address);
181 static int cell_management(int argc, char *argv[])
190 if (strcmp(argv[2], "create") == 0)
191 err = cell_create(argc, argv);
192 else if (strcmp(argv[2], "destroy") == 0)
193 err = cell_destroy(argc, argv);
202 int main(int argc, char *argv[])
212 if (strcmp(argv[1], "enable") == 0) {
213 err = enable(argc, argv);
214 } else if (strcmp(argv[1], "disable") == 0) {
216 err = ioctl(fd, JAILHOUSE_DISABLE);
218 perror("JAILHOUSE_DISABLE");
220 } else if (strcmp(argv[1], "cell") == 0) {
221 err = cell_management(argc, argv);