2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 #include <readline/readline.h>
23 #include <readline/history.h>
28 * show usage information (program arguments)
33 printf(" --dir|-d <path> set working dir to <path>\n");
34 printf(" --help|-h show this help\n");
38 * help for dumm root shell
42 printf("start name=<name> [options] start a guest named <name>\n");
43 printf(" additional options:\n");
44 printf(" kernel=<uml-kernel>\n");
45 printf(" master=<read-only root files>\n");
46 printf(" memory=<guest memory in MB>\n");
47 printf("list list running guests\n");
48 printf("guest <name> open guest menu for <name>\n");
49 printf("help show this help\n");
50 printf("quit kill quests and exit\n");
55 * help for guest shell
57 static void help_guest()
59 printf("addif <name> add an interface to the guest\n");
60 printf("delif <name> remove the interface\n");
61 printf("listif list guests interfaces\n");
62 printf("help show this help\n");
63 printf("quit quit the guest menu\n");
69 static void start(dumm_t
*dumm
, char *line
)
77 char *const opts
[] = {
92 switch (getsubopt(&line
, opts
, &value
))
116 printf("option 'name' is required.\n");
133 if (dumm
->start_guest(dumm
, name
, kernel
, master
, mem
))
135 printf("starting guest '%s'\n", name
);
139 printf("starting guest '%s' failed\n", name
);
144 * add an iface to a guest
146 static void add_if(guest_t
*guest
, char *name
)
150 iface
= guest
->create_iface(guest
, name
);
153 printf("created guest interface '%s' connected to '%s'\n",
154 iface
->get_guest(iface
), iface
->get_host(iface
));
158 printf("failed to create guest interface\n");
163 * delete an iface from a guest
165 static void del_if(guest_t
*guest
, char *name
)
168 iterator_t
*iterator
;
171 iterator
= guest
->create_iface_iterator(guest
);
172 while (iterator
->iterate(iterator
, (void**)&iface
))
174 if (streq(name
, iface
->get_guest(iface
)))
176 iterator
->remove(iterator
);
177 printf("removing interface '%s' ('%s') from %s\n",
178 iface
->get_guest(iface
), iface
->get_host(iface
),
179 guest
->get_name(guest
));
180 iface
->destroy(iface
);
185 iterator
->destroy(iterator
);
188 printf("guest '%s' has no interface named '%s'\n",
189 guest
->get_name(guest
), name
);
194 * list interfaces on a guest
196 static void list_if(guest_t
*guest
)
199 iterator_t
*iterator
;
201 iterator
= guest
->create_iface_iterator(guest
);
202 while (iterator
->iterate(iterator
, (void**)&iface
))
204 printf("'%s' => '%s'\n", iface
->get_guest(iface
), iface
->get_host(iface
));
207 iterator
->destroy(iterator
);
211 * subshell for guests
213 static void guest(dumm_t
*dumm
, char *name
)
218 iterator_t
*iterator
;
222 iterator
= dumm
->create_guest_iterator(dumm
);
223 while (iterator
->iterate(iterator
, (void**)&guest
))
225 if (streq(name
, guest
->get_name(guest
)))
230 iterator
->destroy(iterator
);
233 printf("guest '%s' not found\n", name
);
237 len
= snprintf(prompt
, sizeof(prompt
), "dumm@%s# ", name
);
238 if (len
< 0 || len
>= sizeof(prompt
))
252 char *const opts
[] = {
263 line
= readline(prompt
);
264 if (line
== NULL
|| *line
== '\0')
279 switch (getsubopt(&pos
, opts
, &value
))
297 printf("command unknown: '%s'\n", line
);
305 * list running UML guests
307 static void list(dumm_t
*dumm
)
309 iterator_t
*guests
, *ifaces
;
313 guests
= dumm
->create_guest_iterator(dumm
);
314 while (guests
->iterate(guests
, (void**)&guest
))
316 printf("%s\n", guest
->get_name(guest
));
317 ifaces
= guest
->create_iface_iterator(guest
);
318 while (ifaces
->iterate(ifaces
, (void**)&iface
))
320 printf(" '%s' => '%s'\n", iface
->get_guest(iface
), iface
->get_host(iface
));
322 ifaces
->destroy(ifaces
);
324 guests
->destroy(guests
);
328 * main routine, parses args and reads from console
330 int main(int argc
, char *argv
[])
337 struct option options
[] = {
343 switch (getopt_long(argc
, argv
, "d:h", options
, NULL
))
350 printf("changing to directory '%s' failed.\n", optarg
);
364 dumm
= dumm_create();
375 char *const opts
[] = {
386 line
= readline("dumm# ");
387 if (line
== NULL
|| *line
== '\0')
403 switch (getsubopt(&pos
, opts
, &value
))
421 printf("command unknown: '%s'\n", line
);