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.
34 /* Currently open libguestfs handle. */
39 int keys_from_stdin = 0;
41 const char *libvirt_uri = NULL;
45 bad_cast (char const *s)
50 static void __attribute__((noreturn))
53 if (status != EXIT_SUCCESS)
54 fprintf (stderr, _("Try `%s --help' for more information.\n"),
58 _("%s: display files in a virtual machine\n"
59 "Copyright (C) 2010 Red Hat Inc.\n"
61 " %s [--options] -d domname file [file ...]\n"
62 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
64 " -a|--add image Add image\n"
65 " -c|--connect uri Specify libvirt URI for -d option\n"
66 " -d|--domain guest Add disks from libvirt guest\n"
67 " --echo-keys Don't turn off echo for passphrases\n"
68 " --format[=raw|..] Force disk format for -a option\n"
69 " --help Display brief help\n"
70 " --keys-from-stdin Read passphrases from stdin\n"
71 " -v|--verbose Verbose messages\n"
72 " -V|--version Display version and exit\n"
73 " -x Trace libguestfs API calls\n"
74 "For more information, see the manpage %s(1).\n"),
75 program_name, program_name, program_name,
82 main (int argc, char *argv[])
84 /* Set global program name that is not polluted with libtool artifacts. */
85 set_program_name (argv[0]);
87 setlocale (LC_ALL, "");
88 bindtextdomain (PACKAGE, LOCALEBASEDIR);
91 enum { HELP_OPTION = CHAR_MAX + 1 };
93 static const char *options = "a:c:d:vVx";
94 static const struct option long_options[] = {
96 { "connect", 1, 0, 'c' },
97 { "domain", 1, 0, 'd' },
98 { "echo-keys", 0, 0, 0 },
99 { "format", 2, 0, 0 },
100 { "help", 0, 0, HELP_OPTION },
101 { "keys-from-stdin", 0, 0, 0 },
102 { "verbose", 0, 0, 'v' },
103 { "version", 0, 0, 'V' },
106 struct drv *drvs = NULL;
108 const char *format = NULL;
112 g = guestfs_create ();
114 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
118 argv[0] = bad_cast (program_name);
121 c = getopt_long (argc, argv, options, long_options, &option_index);
125 case 0: /* options which are long only */
126 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
128 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
130 } else if (STREQ (long_options[option_index].name, "format")) {
131 if (!optarg || STREQ (optarg, ""))
136 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
137 program_name, long_options[option_index].name, option_index);
155 usage (EXIT_SUCCESS);
170 usage (EXIT_SUCCESS);
173 usage (EXIT_FAILURE);
177 /* Old-style syntax? There were no -a or -d options in the old
178 * virt-cat which is how we detect this.
181 /* argc - 1 because last parameter is the single filename. */
182 while (optind < argc - 1) {
183 if (strchr (argv[optind], '/') ||
184 access (argv[optind], F_OK) == 0) { /* simulate -a option */
185 drv = malloc (sizeof (struct drv));
191 drv->a.filename = argv[optind];
192 drv->a.format = NULL;
195 } else { /* simulate -d option */
196 drv = malloc (sizeof (struct drv));
202 drv->d.guest = argv[optind];
211 /* These are really constants, but they have to be variables for the
212 * options parsing code. Assert here that they have known-good
215 assert (read_only == 1);
216 assert (inspector == 1);
218 /* User must specify at least one filename on the command line. */
219 if (optind >= argc || argc - optind < 1)
220 usage (EXIT_FAILURE);
222 /* User must have specified some drives. */
224 usage (EXIT_FAILURE);
226 /* Add drives, inspect and mount. Note that inspector is always true,
227 * and there is no -m option.
229 add_drives (drvs, 'a');
231 if (guestfs_launch (g) == -1)
236 /* Free up data structures, no longer needed after this point. */
241 while (optind < argc) {
242 if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
249 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);