[Libguestfs] [PATCH] cat: add -m option

Pino Toscano
2014-06-23T11:00:11Z

Implement the -m/--mount as available in guestfish to override the
automatic introspection and specify which partitions to mount instead.
---
 cat/cat.c        | 45 ++++++++++++++++++++++++++++++++++++++-------
 cat/virt-cat.pod | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 74 insertions(+), 7 deletions(-)

diff --git a/cat/cat.c b/cat/cat.c
index e86ecf3..72bf81b 100644
--- a/cat/cat.c
+++ b/cat/cat.c
@@ -46,6 +46,7 @@ const char *libvirt_uri = NULL;
 int inspector = 1;

 static int do_cat (int argc, char *argv[]);
+static int do_cat_simple (int argc, char *argv[]);
 static int is_windows (guestfs_h *g, const char *root);
 static char *windows_path (guestfs_h *g, const char *root, const char
*filename);

@@ -70,6 +71,8 @@ usage (int status)
              "  --format[=raw|..]    Force disk format for -a
option\n"
              "  --help               Display brief help\n"
              "  --keys-from-stdin    Read passphrases from stdin\n"
+             "  -m|--mount dev[:mnt[:opts[:fstype]]]\n"
+             "                       Mount dev on mnt (if omitted,
/)\n"
              "  -v|--verbose         Verbose messages\n"
              "  -V|--version         Display version and exit\n"
              "  -x                   Trace libguestfs API calls\n"
@@ -89,7 +92,7 @@ main (int argc, char *argv[])

   enum { HELP_OPTION = CHAR_MAX + 1 };

-  static const char *options = "a:c:d:vVx";
+  static const char *options = "a:c:d:m:vVx";
   static const struct option long_options[] = {
     { "add", 1, 0, 'a' },
     { "connect", 1, 0, 'c' },
@@ -99,12 +102,16 @@ main (int argc, char *argv[])
     { "help", 0, 0, HELP_OPTION },
     { "keys-from-stdin", 0, 0, 0 },
     { "long-options", 0, 0, 0 },
+    { "mount", 1, 0, 'm' },
     { "verbose", 0, 0, 'v' },
     { "version", 0, 0, 'V' },
     { 0, 0, 0, 0 }
   };
   struct drv *drvs = NULL;
   struct drv *drv;
+  struct mp *mps = NULL;
+  struct mp *mp;
+  char *p;
   const char *format = NULL;
   int c;
   int r;
@@ -152,6 +159,11 @@ main (int argc, char *argv[])
       OPTION_d;
       break;

+    case 'm':
+      OPTION_m;
+      inspector = 0;
+      break;
+
     case 'v':
       OPTION_v;
       break;
@@ -214,7 +226,7 @@ main (int argc, char *argv[])
    * values.
    */
   assert (read_only == 1);
-  assert (inspector == 1);
+  assert (inspector == 1 || mps != NULL);
   assert (live == 0);

   /* User must specify at least one filename on the command line. */
@@ -225,20 +237,25 @@ main (int argc, char *argv[])
   if (drvs == NULL)
     usage (EXIT_FAILURE);

-  /* Add drives, inspect and mount.  Note that inspector is always true,
-   * and there is no -m option.
-   */
+  /* Add drives, inspect and mount. */
   add_drives (drvs, 'a');

   if (guestfs_launch (g) == -1)
     exit (EXIT_FAILURE);

-  inspect_mount ();
+  if (mps != NULL)
+    mount_mps (mps);
+  else
+    inspect_mount ();

   /* Free up data structures, no longer needed after this point. */
   free_drives (drvs);
+  free_mps (mps);

-  r = do_cat (argc - optind, &argv[optind]);
+  if (inspector)
+    r = do_cat (argc - optind, &argv[optind]);
+  else
+    r = do_cat_simple (argc - optind, &argv[optind]);

   guestfs_close (g);

@@ -283,6 +300,20 @@ do_cat (int argc, char *argv[])
 }

 static int
+do_cat_simple (int argc, char *argv[])
+{
+  unsigned errors = 0;
+  int i;
+
+  for (i = 0; i < argc; ++i) {
+    if (guestfs_download (g, argv[i], "/dev/stdout") == -1)
+      errors++;
+  }
+
+  return errors == 0 ? 0 : -1;
+}
+
+static int
 is_windows (guestfs_h *g, const char *root)
 {
   char *type;
diff --git a/cat/virt-cat.pod b/cat/virt-cat.pod
index 9824a83..e06a961 100644
--- a/cat/virt-cat.pod
+++ b/cat/virt-cat.pod
@@ -126,6 +126,42 @@ security problem with malicious guests (CVE-2010-3851).
 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<-m dev[:mountpoint[:options[:fstype]
+
+=item B<--mount dev[:mountpoint[:options[:fstype]
+
+Mount the named partition or logical volume on the given mountpoint.
+
+If the mountpoint is omitted, it defaults to C</>.
+
+Specifying any mountpoint disables the inspection of the guest and
+the mount of its root and all of its mountpoints, so make sure
+to mount all the mountpoints needed to work with the filenames
+gives as arguments.
+
+If you don't know what filesystems a disk image contains, you can
+either run guestfish without this option, then list the partitions,
+filesystems and LVs available (see L</list-partitions>,
+L</list-filesystems> and L</lvs> commands), or you can use the
+L<virt-filesystems(1)> program.
+
+The third (and rarely used) part of the mount parameter is the list of
+mount options used to mount the underlying filesystem.  If this is not
+given, then the mount options are either the empty string or C<ro>
+(the latter if the I<--ro> flag is used).  By specifying the mount
+options, you override this default choice.  Probably the only time you
+would use this is to enable ACLs and/or extended attributes if the
+filesystem can support them:
+
+ -m /dev/sda1:/:acl,user_xattr
+
+Using this flag is equivalent to using the C<mount-options> command.
+
+The fourth part of the parameter is the filesystem driver to use, such
+as C<ext3> or C<ntfs>. This is rarely needed, but can be useful if
+multiple drivers are valid for a filesystem (eg: C<ext2> and
C<ext3>),
+or if libguestfs misidentifies a filesystem.
+
 =item B<-v>

 =item B<--verbose>
--
1.9.3