[virt-tools-list] guestfish question

Richard W.M. Jones rjones at redhat.com
Fri Nov 5 15:19:48 UTC 2010


On Fri, Nov 05, 2010 at 08:53:20AM -0600, Eric Blake wrote:
> On 11/05/2010 07:55 AM, Richard W.M. Jones wrote:
> > On Thu, Nov 04, 2010 at 11:55:28AM -0400, Kenneth Armstrong wrote:
> >> rootdisk=$(guestfish --ro -a $diskimage -i inspect-os)
> >> distribution=$(guestfish --ro -a $diskimage -i inspect-get-distro $rootdisk)
> >> echo $distribution
> > 
> > By the way it's quicker to do:
> > 
> >   eval $(guestfish --ro -a $diskimage -i --listen)
> 
> You should probably get in the habit of using:
> 
> eval "$(guestfish ... --listen)"
> 
> so that if guestfish ever changes its output to include shell globbing
> characters, the result won't cause inadvertent file name matching (for a
> more concrete example, dircolors outputs literal *, so you should always
> use eval "$(dircolors)" rather than eval $(dircolors) in startup
> scripts; otherwise the existence of a file that matches the glob and
> contains a semicolon could cause the eval to perform arbitrary commands).
> 
> Also, I noticed that guestfish --listen assumes a POSIX shell, in that
> it currently outputs 'export GUESTFISH_PID=15404'.  But this is not
> portable to Solaris /bin/sh; it would be more portable to follow
> dircolors' lead of using 'GUESTFISH_PID=15404; export GUESTFISH_PID'.
> 
> It might also be worth copying dircolors' lead by implementing
> 'guestfish --listen --csh' which caters to the csh-family of shells by
> producing 'setenv GUESTFISH_PID 15404'.

Thanks.  I have take up all these suggestions.  Would you mind
looking over the small patches attached?

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
New in Fedora 11: Fedora Windows cross-compiler. Compile Windows
programs, test, and build Windows installers. Over 70 libraries supprt'd
http://fedoraproject.org/wiki/MinGW http://www.annexia.org/fedora_mingw
-------------- next part --------------
>From 633ae13c80d40b0f30db73e520c08cf365d2f8a3 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 5 Nov 2010 15:04:08 +0000
Subject: [PATCH 1/3] fish: More portable export sh statment.

Don't depend on bash, but allow sh/dash/etc format:
GUESTFISH_PID=nn; export GUESTFISH_PID

(Thanks Eric Blake).
---
 fish/rc.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/fish/rc.c b/fish/rc.c
index e637fc6..2d2f227 100644
--- a/fish/rc.c
+++ b/fish/rc.c
@@ -198,7 +198,7 @@ rc_listen (void)
 
   if (pid > 0) {
     /* Parent process. */
-    printf ("export GUESTFISH_PID=%d\n", pid);
+    printf ("GUESTFISH_PID=%d; export GUESTFISH_PID\n", pid);
     fflush (stdout);
     _exit (0);
   }
-- 
1.7.3.2

-------------- next part --------------
>From c9b663852d401ecdb80b381447a9620a612e5978 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 5 Nov 2010 15:07:11 +0000
Subject: [PATCH 2/3] fish: Suggest safer form of eval.

eval "$(guestfish --listen)"

instead of various other forms.

(Thanks Eric Blake).
---
 fish/guestfish.pod |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/fish/guestfish.pod b/fish/guestfish.pod
index 7319bd7..6368cc9 100644
--- a/fish/guestfish.pod
+++ b/fish/guestfish.pod
@@ -140,7 +140,7 @@ To list what is available do:
 
 =head2 Remote control
 
- eval `guestfish --listen`
+ eval "$(guestfish --listen)"
  guestfish --remote add-ro disk.img
  guestfish --remote run
  guestfish --remote lvs
@@ -730,7 +730,7 @@ up a guestfish process each time.
 
 Start a guestfish server process using:
 
- eval `guestfish --listen`
+ eval "$(guestfish --listen)"
 
 and then send it commands by doing:
 
@@ -750,9 +750,9 @@ The C<eval> statement sets the environment variable C<$GUESTFISH_PID>,
 which is how the I<--remote> option knows where to send the commands.
 You can have several guestfish listener processes running using:
 
- eval `guestfish --listen`
+ eval "$(guestfish --listen)"
  pid1=$GUESTFISH_PID
- eval `guestfish --listen`
+ eval "$(guestfish --listen)"
  pid2=$GUESTFISH_PID
  ...
  guestfish --remote=$pid1 cmd
-- 
1.7.3.2

-------------- next part --------------
>From 8caea9d22f95673cba083877b4c34fb6f22e1921 Mon Sep 17 00:00:00 2001
From: Richard W.M. Jones <rjones at redhat.com>
Date: Fri, 5 Nov 2010 15:17:45 +0000
Subject: [PATCH 3/3] fish: Add --listen --csh to for csh, tcsh compatibility.

(Thanks Eric Blake).
---
 fish/fish.c        |    5 +++++
 fish/fish.h        |    1 +
 fish/guestfish.pod |   12 ++++++++++++
 fish/rc.c          |    7 ++++++-
 4 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/fish/fish.c b/fish/fish.c
index d9a92dd..9f20bba 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -67,6 +67,7 @@ int read_only = 0;
 int quit = 0;
 int verbose = 0;
 int remote_control_listen = 0;
+int remote_control_csh = 0;
 int remote_control = 0;
 int exit_on_error = 1;
 int command_num = 0;
@@ -105,6 +106,7 @@ usage (int status)
              "  -h|--cmd-help cmd    Display detailed help on 'cmd'\n"
              "  -a|--add image       Add image\n"
              "  -c|--connect uri     Specify libvirt URI for -d option\n"
+             "  --csh                Make --listen csh-compatible\n"
              "  -d|--domain guest    Add disks from libvirt guest\n"
              "  -D|--no-dest-paths   Don't tab-complete paths from guest fs\n"
              "  --echo-keys          Don't turn off echo for passphrases\n"
@@ -153,6 +155,7 @@ main (int argc, char *argv[])
     { "add", 1, 0, 'a' },
     { "cmd-help", 2, 0, 'h' },
     { "connect", 1, 0, 'c' },
+    { "csh", 0, 0, 0 },
     { "domain", 1, 0, 'd' },
     { "echo-keys", 0, 0, 0 },
     { "file", 1, 0, 'f' },
@@ -262,6 +265,8 @@ main (int argc, char *argv[])
           format = NULL;
         else
           format = optarg;
+      } else if (STREQ (long_options[option_index].name, "csh")) {
+        remote_control_csh = 1;
       } else {
         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
                  program_name, long_options[option_index].name, option_index);
diff --git a/fish/fish.h b/fish/fish.h
index 8fedf5d..3597805 100644
--- a/fish/fish.h
+++ b/fish/fish.h
@@ -61,6 +61,7 @@ extern int command_num;
 extern int utf8_mode;
 extern int have_terminfo;
 extern int progress_bars;
+extern int remote_control_csh;
 extern const char *libvirt_uri;
 extern int issue_command (const char *cmd, char *argv[], const char *pipe);
 extern void pod2text (const char *name, const char *shortdesc, const char *body);
diff --git a/fish/guestfish.pod b/fish/guestfish.pod
index 6368cc9..0ec7b3b 100644
--- a/fish/guestfish.pod
+++ b/fish/guestfish.pod
@@ -174,6 +174,11 @@ When used in conjunction with the I<-d> option, this specifies
 the libvirt URI to use.  The default is to use the default libvirt
 connection.
 
+=item B<--csh>
+
+If using the I<--listen> option and a csh-like shell, use this option.
+See section L</REMOTE CONTROL AND CSH> below.
+
 =item B<-d libvirt-domain> | B<--domain libvirt-domain>
 
 Add disks from the named libvirt domain.  If the I<--ro> option is
@@ -758,6 +763,13 @@ You can have several guestfish listener processes running using:
  guestfish --remote=$pid1 cmd
  guestfish --remote=$pid2 cmd
 
+=head2 REMOTE CONTROL AND CSH
+
+When using csh-like shells (csh, tcsh etc) you have to add the
+I<--csh> option:
+
+ eval `guestfish --listen --csh`
+
 =head2 REMOTE CONTROL DETAILS
 
 Remote control happens over a Unix domain socket called
diff --git a/fish/rc.c b/fish/rc.c
index 2d2f227..a2bde4a 100644
--- a/fish/rc.c
+++ b/fish/rc.c
@@ -198,7 +198,12 @@ rc_listen (void)
 
   if (pid > 0) {
     /* Parent process. */
-    printf ("GUESTFISH_PID=%d; export GUESTFISH_PID\n", pid);
+
+    if (!remote_control_csh)
+      printf ("GUESTFISH_PID=%d; export GUESTFISH_PID\n", pid);
+    else
+      printf ("setenv GUESTFISH_PID %d\n", pid);
+
     fflush (stdout);
     _exit (0);
   }
-- 
1.7.3.2



More information about the virt-tools-list mailing list