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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 /* Currently open libguestfs handle. */
43 int keys_from_stdin = 0;
45 const char *libvirt_uri = NULL;
48 static int is_windows (guestfs_h *g, const char *root);
49 static char *windows_path (guestfs_h *g, const char *root, const char *filename);
52 bad_cast (char const *s)
57 static void __attribute__((noreturn))
60 if (status != EXIT_SUCCESS)
61 fprintf (stderr, _("Try `%s --help' for more information.\n"),
65 _("%s: display files in a virtual machine\n"
66 "Copyright (C) 2010 Red Hat Inc.\n"
68 " %s [--options] -d domname file [file ...]\n"
69 " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
71 " -a|--add image Add image\n"
72 " -c|--connect uri Specify libvirt URI for -d option\n"
73 " -d|--domain guest Add disks from libvirt guest\n"
74 " --echo-keys Don't turn off echo for passphrases\n"
75 " --format[=raw|..] Force disk format for -a option\n"
76 " --help Display brief help\n"
77 " --keys-from-stdin Read passphrases from stdin\n"
78 " -v|--verbose Verbose messages\n"
79 " -V|--version Display version and exit\n"
80 " -x Trace libguestfs API calls\n"
81 "For more information, see the manpage %s(1).\n"),
82 program_name, program_name, program_name,
89 main (int argc, char *argv[])
91 /* Set global program name that is not polluted with libtool artifacts. */
92 set_program_name (argv[0]);
94 setlocale (LC_ALL, "");
95 bindtextdomain (PACKAGE, LOCALEBASEDIR);
98 enum { HELP_OPTION = CHAR_MAX + 1 };
100 static const char *options = "a:c:d:vVx";
101 static const struct option long_options[] = {
102 { "add", 1, 0, 'a' },
103 { "connect", 1, 0, 'c' },
104 { "domain", 1, 0, 'd' },
105 { "echo-keys", 0, 0, 0 },
106 { "format", 2, 0, 0 },
107 { "help", 0, 0, HELP_OPTION },
108 { "keys-from-stdin", 0, 0, 0 },
109 { "verbose", 0, 0, 'v' },
110 { "version", 0, 0, 'V' },
113 struct drv *drvs = NULL;
115 const char *format = NULL;
119 g = guestfs_create ();
121 fprintf (stderr, _("guestfs_create: failed to create handle\n"));
125 argv[0] = bad_cast (program_name);
128 c = getopt_long (argc, argv, options, long_options, &option_index);
132 case 0: /* options which are long only */
133 if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
135 } else if (STREQ (long_options[option_index].name, "echo-keys")) {
137 } else if (STREQ (long_options[option_index].name, "format")) {
138 if (!optarg || STREQ (optarg, ""))
143 fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
144 program_name, long_options[option_index].name, option_index);
162 usage (EXIT_SUCCESS);
177 usage (EXIT_SUCCESS);
180 usage (EXIT_FAILURE);
184 /* Old-style syntax? There were no -a or -d options in the old
185 * virt-cat which is how we detect this.
188 /* argc - 1 because last parameter is the single filename. */
189 while (optind < argc - 1) {
190 if (strchr (argv[optind], '/') ||
191 access (argv[optind], F_OK) == 0) { /* simulate -a option */
192 drv = malloc (sizeof (struct drv));
198 drv->a.filename = argv[optind];
199 drv->a.format = NULL;
202 } else { /* simulate -d option */
203 drv = malloc (sizeof (struct drv));
209 drv->d.guest = argv[optind];
218 /* These are really constants, but they have to be variables for the
219 * options parsing code. Assert here that they have known-good
222 assert (read_only == 1);
223 assert (inspector == 1);
226 /* User must specify at least one filename on the command line. */
227 if (optind >= argc || argc - optind < 1)
228 usage (EXIT_FAILURE);
230 /* User must have specified some drives. */
232 usage (EXIT_FAILURE);
234 /* Add drives, inspect and mount. Note that inspector is always true,
235 * and there is no -m option.
237 add_drives (drvs, 'a');
239 if (guestfs_launch (g) == -1)
244 /* Free up data structures, no longer needed after this point. */
251 /* Get root mountpoint. See: fish/inspect.c:inspect_mount */
252 roots = guestfs_inspect_get_roots (g);
254 assert (roots[0] != NULL);
255 assert (roots[1] == NULL);
259 /* Windows? Special handling is required. */
260 windows = is_windows (g, root);
262 for (; optind < argc; optind++) {
263 char *filename_to_free = NULL;
264 const char *filename = argv[optind];
267 filename = filename_to_free = windows_path (g, root, filename);
268 if (filename == NULL) {
274 if (guestfs_download (g, filename, "/dev/stdout") == -1)
277 free (filename_to_free);
284 exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
288 is_windows (guestfs_h *g, const char *root)
293 type = guestfs_inspect_get_type (g, root);
297 w = STREQ (type, "windows");
302 static void mount_drive_letter_ro (char drive_letter, const char *root);
305 windows_path (guestfs_h *g, const char *root, const char *path)
310 /* If there is a drive letter, rewrite the path. */
311 if (c_isalpha (path[0]) && path[1] == ':') {
312 char drive_letter = c_tolower (path[0]);
313 /* This returns the newly allocated string. */
314 mount_drive_letter_ro (drive_letter, root);
315 ret = strdup (path + 2);
336 /* Blindly convert any backslashes into forward slashes. Is this good? */
337 for (i = 0; i < strlen (ret); ++i)
341 /* If this fails, we want to return NULL. */
342 char *t = guestfs_case_sensitive_path (g, ret);
350 mount_drive_letter_ro (char drive_letter, const char *root)
356 /* Resolve the drive letter using the drive mappings table. */
357 drives = guestfs_inspect_get_drive_mappings (g, root);
358 if (drives == NULL || drives[0] == NULL) {
359 fprintf (stderr, _("%s: to use Windows drive letters, this must be a Windows guest\n"),
365 for (i = 0; drives[i] != NULL; i += 2) {
366 if (c_tolower (drives[i][0]) == drive_letter && drives[i][1] == '\0') {
367 device = drives[i+1];
372 if (device == NULL) {
373 fprintf (stderr, _("%s: drive '%c:' not found.\n"),
374 program_name, drive_letter);
378 /* Unmount current disk and remount device. */
379 if (guestfs_umount_all (g) == -1)
382 if (guestfs_mount_ro (g, device, "/") == -1)
385 for (i = 0; drives[i] != NULL; ++i)
388 /* Don't need to free (device) because that string was in the