2 * Copyright (C) 2010 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.
33 /* Currently open libguestfs handle. */
38 int keys_from_stdin = 0;
40 const char *libvirt_uri = NULL;
44 bad_cast (char const *s)
49 static void __attribute__((noreturn))
52 if (status != EXIT_SUCCESS)
53 fprintf (stderr, _("Try `%s --help' for more information.\n"),
57 _("%s: display files in a virtual machine\n"
58 "Copyright (C) 2010 Red Hat Inc.\n"
60 " %s [--options] -d domname file [file ...]\n"
61 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
63 " -a|--add image Add image\n"
64 " -c|--connect uri Specify libvirt URI for -d option\n"
65 " -d|--domain guest Add disks from libvirt guest\n"
66 " --echo-keys Don't turn off echo for passphrases\n"
67 " --format[=raw|..] Force disk format for -a option\n"
68 " --help Display brief help\n"
69 " --keys-from-stdin Read passphrases from stdin\n"
70 " -v|--verbose Verbose messages\n"
71 " -V|--version Display version and exit\n"
72 " -x Trace libguestfs API calls\n"
73 "For more information, see the manpage %s(1).\n"),
74 program_name, program_name, program_name,
81 main (int argc, char *argv[])
83 /* Set global program name that is not polluted with libtool artifacts. */
84 set_program_name (argv[0]);
86 setlocale (LC_ALL, "");
87 bindtextdomain (PACKAGE, LOCALEBASEDIR);
90 enum { HELP_OPTION = CHAR_MAX + 1 };
92 static const char *options = "a:c:d:vVx";
93 static const struct option long_options[] = {
95 { "connect", 1, 0, 'c' },
96 { "domain", 1, 0, 'd' },
97 { "echo-keys", 0, 0, 0 },
98 { "format", 2, 0, 0 },
99 { "help", 0, 0, HELP_OPTION },
100 { "keys-from-stdin", 0, 0, 0 },
101 { "verbose", 0, 0, 'v' },
102 { "version", 0, 0, 'V' },
105 struct drv *drvs = NULL;
107 const char *format = NULL;
111 g = guestfs_create ();
113 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
117 argv[0] = bad_cast (program_name);
120 c = getopt_long (argc, argv, options, long_options, &option_index);
124 case 0: /* options which are long only */
125 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
127 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
129 } else if (STREQ (long_options[option_index].name, "format")) {
130 if (!optarg || STREQ (optarg, ""))
135 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
136 program_name, long_options[option_index].name, option_index);
154 usage (EXIT_SUCCESS);
169 usage (EXIT_SUCCESS);
172 usage (EXIT_FAILURE);
176 /* Old-style syntax? There were no -a or -d options in the old
177 * virt-cat which is how we detect this.
180 /* argc - 1 because last parameter is the single filename. */
181 while (optind < argc - 1) {
182 if (strchr (argv[optind], '/') ||
183 access (argv[optind], F_OK) == 0) { /* simulate -a option */
184 drv = malloc (sizeof (struct drv));
190 drv->a.filename = argv[optind];
191 drv->a.format = NULL;
194 } else { /* simulate -d option */
195 drv = malloc (sizeof (struct drv));
201 drv->d.guest = argv[optind];
210 /* These are really constants, but they have to be variables for the
211 * options parsing code. Assert here that they have known-good
214 assert (read_only == 1);
215 assert (inspector == 1);
217 /* User must specify at least one filename on the command line. */
218 if (optind >= argc || argc - optind < 1)
219 usage (EXIT_FAILURE);
221 /* User must have specified some drives. */
223 usage (EXIT_FAILURE);
225 /* Add drives, inspect and mount. Note that inspector is always true,
226 * and there is no -m option.
228 add_drives (drvs, 'a');
230 if (guestfs_launch (g) == -1)
235 /* Free up data structures, no longer needed after this point. */
240 while (optind < argc) {
241 if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
248 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);