summaryrefslogtreecommitdiffstats
path: root/cat
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2010-11-11 14:53:51 +0000
committerRichard W.M. Jones <rjones@redhat.com>2010-11-11 16:56:43 +0000
commitd29e9a552f5823d1d8cb8d4962cb1301bbf60b0e (patch)
tree02e375395b90ebc0f1485e336110defef8422ee2 /cat
parent4d900cdac8258daa2e99c6ceb2a4985154e94150 (diff)
downloadlibguestfs-d29e9a552f5823d1d8cb8d4962cb1301bbf60b0e.tar.gz
libguestfs-d29e9a552f5823d1d8cb8d4962cb1301bbf60b0e.tar.xz
libguestfs-d29e9a552f5823d1d8cb8d4962cb1301bbf60b0e.zip
Rewrite virt-cat in C.
With changes in the core API since 1.5, virt-cat was little more than a Perl wrapper which did some command line argument processing. Thus it could easily be rewritten in C. This version also shares core command line argument processing with guestfish and guestmount, so the options have changed slightly (old-style command line *is* supported). virt-cat -a disk.img file [file ...] virt-cat -d domname file [file ...] Several other guestfish options are supported including encryption, and with the new style multiple files can be downloaded. See the man page for details.
Diffstat (limited to 'cat')
-rw-r--r--cat/Makefile.am78
-rwxr-xr-xcat/run-cat-locally53
-rwxr-xr-xcat/test-virt-cat.sh14
-rw-r--r--cat/virt-cat.c245
-rwxr-xr-xcat/virt-cat.pod200
5 files changed, 590 insertions, 0 deletions
diff --git a/cat/Makefile.am b/cat/Makefile.am
new file mode 100644
index 00000000..a8e9b318
--- /dev/null
+++ b/cat/Makefile.am
@@ -0,0 +1,78 @@
+# libguestfs virt-cat.
+# Copyright (C) 2010 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+include $(top_srcdir)/subdir-rules.mk
+
+EXTRA_DIST = \
+ run-cat-locally \
+ test-virt-cat.sh \
+ virt-cat.pod
+
+bin_PROGRAMS = virt-cat
+
+SHARED_SOURCE_FILES = \
+ ../fish/inspect.c \
+ ../fish/keys.c \
+ ../fish/options.h \
+ ../fish/options.c \
+ ../fish/virt.c
+
+virt_cat_SOURCES = \
+ $(SHARED_SOURCE_FILES) \
+ virt-cat.c
+
+virt_cat_CFLAGS = \
+ -I$(top_srcdir)/src -I$(top_builddir)/src \
+ -I$(top_srcdir)/fish \
+ -I$(srcdir)/../gnulib/lib -I../gnulib/lib \
+ -DLOCALEBASEDIR=\""$(datadir)/locale"\" \
+ $(WARN_CFLAGS) $(WERROR_CFLAGS)
+
+virt_cat_LDADD = \
+ $(top_builddir)/src/libguestfs.la \
+ ../gnulib/lib/libgnu.la
+
+# Manual pages and HTML files for the website.
+man_MANS = virt-cat.1
+
+noinst_DATA = $(top_builddir)/html/virt-cat.1.html
+
+virt-cat.1: virt-cat.pod
+ $(POD2MAN) \
+ --section 1 \
+ -c "Virtualization Support" \
+ --release "$(PACKAGE_NAME)-$(PACKAGE_VERSION)" \
+ $< > $@-t && mv $@-t $@
+
+$(top_builddir)/html/virt-cat.1.html: virt-cat.pod
+ mkdir -p $(top_builddir)/html
+ cd $(top_builddir) && pod2html \
+ --css 'pod.css' \
+ --htmldir html \
+ --outfile html/$@ \
+ $(abs_srcdir)/$<
+
+# Tests.
+
+random_val := $(shell awk 'BEGIN{srand(); print 1+int(255*rand())}' < /dev/null)
+
+TESTS_ENVIRONMENT = \
+ MALLOC_PERTURB_=$(random_val) \
+ LD_LIBRARY_PATH=$(top_builddir)/src/.libs \
+ LIBGUESTFS_PATH=$(top_builddir)/appliance
+
+TESTS = test-virt-cat.sh
diff --git a/cat/run-cat-locally b/cat/run-cat-locally
new file mode 100755
index 00000000..7945463c
--- /dev/null
+++ b/cat/run-cat-locally
@@ -0,0 +1,53 @@
+#!/usr/bin/perl
+# Copyright (C) 2009 Red Hat Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+# This script sets up the environment so you can run virt-* tools in
+# place without needing to do 'make install' first. You can also run
+# the tools by creating a symlink to this script and putting it in
+# your path.
+#
+# Use it like this:
+# ./run-cat-locally [usual virt-cat args ...]
+
+use strict;
+use warnings;
+
+use File::Basename qw(dirname);
+use File::Spec;
+use Cwd qw(abs_path);
+
+my $path = $0;
+my $tool = "cat";
+
+# Follow symlinks until we get to the real file
+while(-l $path) {
+ my $link = readlink($path) or die "readlink: $path: $!";
+ if(File::Spec->file_name_is_absolute($link)) {
+ $path = $link;
+ } else {
+ $path = File::Spec->catfile(dirname($path), $link);
+ }
+}
+
+# Get the absolute path of the parent directory
+$path = abs_path(dirname($path).'/..');
+
+$ENV{LD_LIBRARY_PATH} = $path.'/src/.libs';
+$ENV{LIBGUESTFS_PATH} = $path.'/appliance';
+
+#print (join " ", ("$path/$tool/virt-$tool", @ARGV), "\n");
+exec('perl', "$path/$tool/virt-$tool", @ARGV);
diff --git a/cat/test-virt-cat.sh b/cat/test-virt-cat.sh
new file mode 100755
index 00000000..19b8d875
--- /dev/null
+++ b/cat/test-virt-cat.sh
@@ -0,0 +1,14 @@
+#!/bin/bash -
+
+export LANG=C
+set -e
+
+# Read out the test files from the image using virt-cat.
+if [ "$(./virt-cat ../images/fedora.img /etc/test1)" != "abcdefg" ]; then
+ echo "$0: error: mismatch in file test1"
+ exit 1
+fi
+if [ "$(./virt-cat ../images/fedora.img /etc/test2)" != "" ]; then
+ echo "$0: error: mismatch in file test2"
+ exit 1
+fi
diff --git a/cat/virt-cat.c b/cat/virt-cat.c
new file mode 100644
index 00000000..58ebd8ec
--- /dev/null
+++ b/cat/virt-cat.c
@@ -0,0 +1,245 @@
+/* virt-cat
+ * Copyright (C) 2010 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <assert.h>
+
+#include "progname.h"
+
+#include "guestfs.h"
+#include "options.h"
+
+/* Currently open libguestfs handle. */
+guestfs_h *g;
+
+int read_only = 1;
+int verbose = 0;
+int keys_from_stdin = 0;
+int echo_keys = 0;
+const char *libvirt_uri = NULL;
+int inspector = 1;
+
+static inline char *
+bad_cast (char const *s)
+{
+ return (char *) s;
+}
+
+static void __attribute__((noreturn))
+usage (int status)
+{
+ if (status != EXIT_SUCCESS)
+ fprintf (stderr, _("Try `%s --help' for more information.\n"),
+ program_name);
+ else {
+ fprintf (stdout,
+ _("%s: display files in a virtual machine\n"
+ "Copyright (C) 2010 Red Hat Inc.\n"
+ "Usage:\n"
+ " %s [--options] -d domname file [file ...]\n"
+ " %s [--options] -a disk.img [-a disk.img ...] file [file ...]\n"
+ "Options:\n"
+ " -a|--add image Add image\n"
+ " -c|--connect uri Specify libvirt URI for -d option\n"
+ " -d|--domain guest Add disks from libvirt guest\n"
+ " --echo-keys Don't turn off echo for passphrases\n"
+ " --format[=raw|..] Force disk format for -a option\n"
+ " --help Display brief help\n"
+ " --keys-from-stdin Read passphrases from stdin\n"
+ " -v|--verbose Verbose messages\n"
+ " -V|--version Display version and exit\n"
+ " -x Echo each command before executing it\n"
+ "For more information, see the manpage %s(1).\n"),
+ program_name, program_name, program_name,
+ program_name);
+ }
+ exit (status);
+}
+
+int
+main (int argc, char *argv[])
+{
+ /* Set global program name that is not polluted with libtool artifacts. */
+ set_program_name (argv[0]);
+
+ setlocale (LC_ALL, "");
+ bindtextdomain (PACKAGE, LOCALEBASEDIR);
+ textdomain (PACKAGE);
+
+ enum { HELP_OPTION = CHAR_MAX + 1 };
+
+ static const char *options = "a:c:d:vVx";
+ static const struct option long_options[] = {
+ { "add", 1, 0, 'a' },
+ { "connect", 1, 0, 'c' },
+ { "domain", 1, 0, 'd' },
+ { "echo-keys", 0, 0, 0 },
+ { "format", 2, 0, 0 },
+ { "help", 0, 0, HELP_OPTION },
+ { "keys-from-stdin", 0, 0, 0 },
+ { "verbose", 0, 0, 'v' },
+ { "version", 0, 0, 'V' },
+ { 0, 0, 0, 0 }
+ };
+ struct drv *drvs = NULL;
+ struct drv *drv;
+ char *p, *file = NULL;
+ const char *format = NULL;
+ int c;
+ int option_index;
+ int next_prepared_drive = 1;
+
+ g = guestfs_create ();
+ if (g == NULL) {
+ fprintf (stderr, _("guestfs_create: failed to create handle\n"));
+ exit (EXIT_FAILURE);
+ }
+
+ argv[0] = bad_cast (program_name);
+
+ for (;;) {
+ c = getopt_long (argc, argv, options, long_options, &option_index);
+ if (c == -1) break;
+
+ switch (c) {
+ case 0: /* options which are long only */
+ if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
+ keys_from_stdin = 1;
+ } else if (STREQ (long_options[option_index].name, "echo-keys")) {
+ echo_keys = 1;
+ } else if (STREQ (long_options[option_index].name, "format")) {
+ if (!optarg || STREQ (optarg, ""))
+ format = NULL;
+ else
+ format = optarg;
+ } else {
+ fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
+ program_name, long_options[option_index].name, option_index);
+ exit (EXIT_FAILURE);
+ }
+ break;
+
+ case 'a':
+ OPTION_a;
+ break;
+
+ case 'c':
+ OPTION_c;
+ break;
+
+ case 'd':
+ OPTION_d;
+ break;
+
+ case 'h':
+ usage (EXIT_SUCCESS);
+
+ case 'v':
+ OPTION_v;
+ break;
+
+ case 'V':
+ OPTION_V;
+ break;
+
+ case 'x':
+ OPTION_x;
+ break;
+
+ case HELP_OPTION:
+ usage (EXIT_SUCCESS);
+
+ default:
+ usage (EXIT_FAILURE);
+ }
+ }
+
+ /* Old-style syntax? There were no -a or -d options in the old
+ * virt-cat which is how we detect this.
+ */
+ if (drvs == NULL) {
+ /* argc - 1 because last parameter is the single filename. */
+ while (optind < argc - 1) {
+ if (strchr (argv[optind], '/') ||
+ access (argv[optind], F_OK) == 0) { /* simulate -a option */
+ drv = malloc (sizeof (struct drv));
+ if (!drv) {
+ perror ("malloc");
+ exit (EXIT_FAILURE);
+ }
+ drv->type = drv_a;
+ drv->a.filename = argv[optind];
+ drv->a.format = NULL;
+ drv->next = drvs;
+ drvs = drv;
+ } else { /* simulate -d option */
+ drv = malloc (sizeof (struct drv));
+ if (!drv) {
+ perror ("malloc");
+ exit (EXIT_FAILURE);
+ }
+ drv->type = drv_d;
+ drv->d.guest = argv[optind];
+ drv->next = drvs;
+ drvs = drv;
+ }
+
+ optind++;
+ }
+ }
+
+ /* These are really constants, but they have to be variables for the
+ * options parsing code. Assert here that they have known-good
+ * values.
+ */
+ assert (read_only == 1);
+ assert (inspector == 1);
+
+ /* User must specify at least one filename on the command line. */
+ if (optind >= argc || argc - optind < 1)
+ usage (EXIT_FAILURE);
+
+ /* Add drives, inspect and mount. Note that inspector is always true,
+ * and there is no -m option.
+ */
+ add_drives (drvs, 'a');
+
+ if (guestfs_launch (g) == -1)
+ exit (EXIT_FAILURE);
+
+ inspect_mount ();
+
+ /* Free up data structures, no longer needed after this point. */
+ free_drives (drvs);
+
+ while (optind < argc) {
+ if (guestfs_download (g, argv[optind], "/dev/stdout") == -1)
+ exit (EXIT_FAILURE);
+ optind++;
+ }
+
+ guestfs_close (g);
+
+ exit (EXIT_SUCCESS);
+}
diff --git a/cat/virt-cat.pod b/cat/virt-cat.pod
new file mode 100755
index 00000000..b203bbfe
--- /dev/null
+++ b/cat/virt-cat.pod
@@ -0,0 +1,200 @@
+=encoding utf8
+
+=head1 NAME
+
+virt-cat - Display files in a virtual machine
+
+=head1 SYNOPSIS
+
+ virt-cat [--options] -d domname file [file ...]
+
+ virt-cat [--options] -a disk.img [-a disk.img ...] file [file ...]
+
+Old-style:
+
+ virt-cat domname file
+
+ virt-cat disk.img file
+
+=head1 DESCRIPTION
+
+C<virt-cat> is a command line tool to display the contents of C<file>
+where C<file> exists in the named virtual machine (or disk image).
+
+Multiple filenames can be given, in which case they are concatenated
+together. Each filename must be a full path, starting at the root
+directory (starting with '/').
+
+C<virt-cat> can be used to quickly view a file. To edit a file, use
+C<virt-edit>. For more complex cases you should look at the
+L<guestfish(1)> tool.
+
+=head1 EXAMPLES
+
+Display C</etc/fstab> file from inside the libvirt VM called
+C<mydomain>:
+
+ virt-cat -d mydomain /etc/fstab
+
+List syslog messages from a VM disk image file:
+
+ virt-cat -a disk.img /var/log/messages | tail
+
+Find out what DHCP IP address a VM acquired:
+
+ virt-cat -d mydomain /var/log/messages | \
+ grep 'dhclient: bound to' | tail
+
+Find out what packages were recently installed:
+
+ virt-cat -d mydomain /var/log/yum.log | tail
+
+Find out who is logged on inside a virtual machine:
+
+ virt-cat -d mydomain /var/run/utmp > /tmp/utmp
+ who /tmp/utmp
+
+or who was logged on:
+
+ virt-cat -d mydomain /var/log/wtmp > /tmp/wtmp
+ last -f /tmp/wtmp
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<--help>
+
+Display brief help.
+
+=item B<-a> file
+
+=item B<--add> file
+
+Add I<file> which should be a disk image from a virtual machine. If
+the virtual machine has multiple block devices, you must supply all of
+them with separate I<-a> options.
+
+The format of the disk image is auto-detected. To override this and
+force a particular format use the I<--format=..> option.
+
+=item B<-c> URI
+
+=item B<--connect> URI
+
+If using libvirt, connect to the given I<URI>. If omitted, then we
+connect to the default libvirt hypervisor.
+
+If you specify guest block devices directly (I<-a>), then libvirt is
+not used at all.
+
+=item B<-d> guest
+
+=item B<--domain> guest
+
+Add all the disks from the named libvirt guest.
+
+=item B<--echo-keys>
+
+When prompting for keys and passphrases, virt-cat normally turns
+echoing off so you cannot see what you are typing. If you are not
+worried about Tempest attacks and there is no one else in the room you
+can specify this flag to see what you are typing.
+
+=item B<--format=raw|qcow2|..> | B<--format>
+
+The default for the I<-a> option is to auto-detect the format of the
+disk image. Using this forces the disk format for I<-a> options which
+follow on the command line. Using I<--format> with no argument
+switches back to auto-detection for subsequent I<-a> options.
+
+For example:
+
+ virt-cat --format=raw -a disk.img
+
+forces raw format (no auto-detection) for C<disk.img>.
+
+ virt-cat --format=raw -a disk.img --format -a another.img
+
+forces raw format (no auto-detection) for C<disk.img> and reverts to
+auto-detection for C<another.img>.
+
+If you have untrusted raw-format guest disk images, you should use
+this option to specify the disk format. This avoids a possible
+security problem with malicious guests (CVE-2010-3851). See also
+L</add-drive-opts>.
+
+=item B<--keys-from-stdin>
+
+Read key or passphrase parameters from stdin. The default is
+to try to read passphrases from the user by opening C</dev/tty>.
+
+=item B<-v>
+
+=item B<--verbose>
+
+Enable verbose messages for debugging.
+
+=item B<-V>
+
+=item B<--version>
+
+Display version number and exit.
+
+=item B<-x>
+
+Enable tracing of libguestfs API calls.
+
+=back
+
+=head1 OLD-STYLE COMMAND LINE ARGUMENTS
+
+Previous versions of virt-cat allowed you to write either:
+
+ virt-cat disk.img [disk.img ...] file
+
+or
+
+ virt-cat guestname file
+
+whereas in this version you should use I<-a> or I<-d> respectively
+to avoid the confusing case where a disk image might have the same
+name as a guest.
+
+For compatibility the old style is still supported.
+
+=head1 SHELL QUOTING
+
+Libvirt guest names can contain arbitrary characters, some of which
+have meaning to the shell such as C<#> and space. You may need to
+quote or escape these characters on the command line. See the shell
+manual page L<sh(1)> for details.
+
+=head1 SEE ALSO
+
+L<guestfs(3)>,
+L<guestfish(1)>,
+L<virt-edit(1)>,
+L<http://libguestfs.org/>.
+
+=head1 AUTHOR
+
+Richard W.M. Jones L<http://people.redhat.com/~rjones/>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2010 Red Hat Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.