2 * Copyright (C) 2010-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
36 /* Currently open libguestfs handle. */
42 int keys_from_stdin = 0;
44 const char *libvirt_uri = NULL;
48 bad_cast (char const *s)
53 static void __attribute__((noreturn))
56 if (status != EXIT_SUCCESS)
57 fprintf (stderr, _("Try `%s --help' for more information.\n"),
61 _("%s: display files in a virtual machine\n"
62 "Copyright (C) 2010 Red Hat Inc.\n"
64 " %s [--options] -d domname file [file ...]\n"
65 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
67 " -a|--add image Add image\n"
68 " -c|--connect uri Specify libvirt URI for -d option\n"
69 " -d|--domain guest Add disks from libvirt guest\n"
70 " --echo-keys Don't turn off echo for passphrases\n"
71 " --format[=raw|..] Force disk format for -a option\n"
72 " --help Display brief help\n"
73 " --keys-from-stdin Read passphrases from stdin\n"
74 " -v|--verbose Verbose messages\n"
75 " -V|--version Display version and exit\n"
76 " -x Trace libguestfs API calls\n"
77 "For more information, see the manpage %s(1).\n"),
78 program_name, program_name, program_name,
85 main (int argc, char *argv[])
87 /* Set global program name that is not polluted with libtool artifacts. */
88 set_program_name (argv[0]);
90 setlocale (LC_ALL, "");
91 bindtextdomain (PACKAGE, LOCALEBASEDIR);
94 enum { HELP_OPTION = CHAR_MAX + 1 };
96 static const char *options = "a:c:d:vVx";
97 static const struct option long_options[] = {
99 { "connect", 1, 0, 'c' },
100 { "domain", 1, 0, 'd' },
101 { "echo-keys", 0, 0, 0 },
102 { "format", 2, 0, 0 },
103 { "help", 0, 0, HELP_OPTION },
104 { "keys-from-stdin", 0, 0, 0 },
105 { "verbose", 0, 0, 'v' },
106 { "version", 0, 0, 'V' },
109 struct drv *drvs = NULL;
111 const char *format = NULL;
115 g = guestfs_create ();
117 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
121 argv[0] = bad_cast (program_name);
124 c = getopt_long (argc, argv, options, long_options, &option_index);
128 case 0: /* options which are long only */
129 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
131 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
133 } else if (STREQ (long_options[option_index].name, "format")) {
134 if (!optarg || STREQ (optarg, ""))
139 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
140 program_name, long_options[option_index].name, option_index);
158 usage (EXIT_SUCCESS);
173 usage (EXIT_SUCCESS);
176 usage (EXIT_FAILURE);
180 /* Old-style syntax? There were no -a or -d options in the old
181 * virt-cat which is how we detect this.
184 /* argc - 1 because last parameter is the single filename. */
185 while (optind < argc - 1) {
186 if (strchr (argv[optind], '/') ||
187 access (argv[optind], F_OK) == 0) { /* simulate -a option */
188 drv = malloc (sizeof (struct drv));
194 drv->a.filename = argv[optind];
195 drv->a.format = NULL;
198 } else { /* simulate -d option */
199 drv = malloc (sizeof (struct drv));
205 drv->d.guest = argv[optind];
214 /* These are really constants, but they have to be variables for the
215 * options parsing code. Assert here that they have known-good
218 assert (read_only == 1);
219 assert (inspector == 1);
222 /* User must specify at least one filename on the command line. */
223 if (optind >= argc || argc - optind < 1)
224 usage (EXIT_FAILURE);
226 /* User must have specified some drives. */
228 usage (EXIT_FAILURE);
230 /* Add drives, inspect and mount. Note that inspector is always true,
231 * and there is no -m option.
233 add_drives (drvs, 'a');
235 if (guestfs_launch (g) == -1)
240 /* Free up data structures, no longer needed after this point. */
245 while (optind < argc) {
246 if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
253 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);