summaryrefslogtreecommitdiffstats
path: root/fish
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2011-03-30 17:03:11 +0100
committerRichard W.M. Jones <rjones@redhat.com>2011-03-31 15:42:13 +0100
commitb8e1dee73a1deef1bfd5937e2abfbe9afef7b1ef (patch)
tree2c523527cf6d52c719722c2400df1bcbf6bd3699 /fish
parent4155d20014b24b2203c4dd8b00e2e19450a7e3bb (diff)
downloadlibguestfs-b8e1dee73a1deef1bfd5937e2abfbe9afef7b1ef.tar.gz
libguestfs-b8e1dee73a1deef1bfd5937e2abfbe9afef7b1ef.tar.xz
libguestfs-b8e1dee73a1deef1bfd5937e2abfbe9afef7b1ef.zip
Add /etc/libguestfs-tools.conf configuration file.
This allows the default for --ro or --rw to be controlled for the three tools guestfish, guestmount and virt-rescue.
Diffstat (limited to 'fish')
-rw-r--r--fish/Makefile.am15
-rw-r--r--fish/config.c142
-rw-r--r--fish/fish.c2
-rw-r--r--fish/guestfish.pod48
-rw-r--r--fish/libguestfs-tools.conf24
-rw-r--r--fish/options.h3
6 files changed, 214 insertions, 20 deletions
diff --git a/fish/Makefile.am b/fish/Makefile.am
index 311da896..3eae8e47 100644
--- a/fish/Makefile.am
+++ b/fish/Makefile.am
@@ -1,5 +1,5 @@
# libguestfs
-# Copyright (C) 2009-2010 Red Hat Inc.
+# Copyright (C) 2009-2011 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
@@ -60,6 +60,7 @@ EXTRA_DIST = \
# so we know which ones are shared. These files must not include
# other guestfish files.
SHARED_SOURCE_FILES = \
+ config.c \
inspect.c \
keys.c \
options.h \
@@ -118,10 +119,13 @@ guestfish_CFLAGS = \
-DGUESTFS_DEFAULT_PATH='"$(libdir)/guestfs"' \
-DLOCALEBASEDIR=\""$(datadir)/locale"\" \
-I$(srcdir)/../gnulib/lib -I../gnulib/lib \
- $(WARN_CFLAGS) $(WERROR_CFLAGS)
+ $(WARN_CFLAGS) $(WERROR_CFLAGS) \
+ $(LIBCONFIG_CFLAGS)
guestfish_LDADD = \
- $(top_builddir)/src/libguestfs.la $(LIBREADLINE) -lm
+ $(LIBCONFIG_LIBS) \
+ $(LIBREADLINE) \
+ $(top_builddir)/src/libguestfs.la -lm
# Make guestfish use the convenience libraries.
noinst_LTLIBRARIES = libcmds.la librc_protocol.la
@@ -207,6 +211,11 @@ stamp-virt-tar-out.pod: virt-tar-out.pod
$<
touch $@
+# libguestfs tools config file.
+
+toolsconfdir = $(sysconfdir)
+toolsconf_DATA = libguestfs-tools.conf
+
# Bash completion script.
bashcompletiondir = $(sysconfdir)/bash_completion.d
diff --git a/fish/config.c b/fish/config.c
new file mode 100644
index 00000000..c92bc4e2
--- /dev/null
+++ b/fish/config.c
@@ -0,0 +1,142 @@
+/* libguestfs - guestfish and guestmount shared option parsing
+ * Copyright (C) 2011 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#ifdef HAVE_LIBCONFIG
+#include <libconfig.h>
+#endif
+
+#include "guestfs.h"
+
+#include "options.h"
+
+static const char *home_filename = /* $HOME/ */ ".libguestfs-tools.rc";
+static const char *etc_filename = "/etc/libguestfs-tools.conf";
+
+#ifdef HAVE_LIBCONFIG
+
+/* Note that parse_config is called very early, before command line
+ * parsing and before the verbose flag has been set.
+ */
+
+void
+parse_config (void)
+{
+ const char *home;
+ size_t len;
+ char *path;
+ FILE *fp;
+ config_t conf;
+
+ config_init (&conf);
+
+ /* Try $HOME first. */
+ home = getenv ("HOME");
+ if (home != NULL) {
+ len = strlen (home) + 1 + strlen (home_filename) + 1;
+ path = malloc (len);
+ if (path == NULL) {
+ perror ("malloc");
+ exit (EXIT_FAILURE);
+ }
+ snprintf (path, len, "%s/%s", home, home_filename);
+
+ fp = fopen (path, "r");
+ if (fp != NULL) {
+ /*
+ if (verbose)
+ fprintf (stderr, "%s: reading configuration from %s\n",
+ program_name, path);
+ */
+
+ if (config_read (&conf, fp) == CONFIG_FALSE) {
+ fprintf (stderr,
+ _("%s: %s: line %d: error parsing configuration file: %s\n"),
+ program_name, path, config_error_line (&conf),
+ config_error_text (&conf));
+ exit (EXIT_FAILURE);
+ }
+
+ if (fclose (fp) == -1) {
+ perror (path);
+ exit (EXIT_FAILURE);
+ }
+
+ /* Notes:
+ *
+ * (1) It's not obvious from the documentation, that config_read
+ * completely resets the 'conf' structure. This means we cannot
+ * call config_read twice on the two possible configuration
+ * files, but instead have to copy out settings into our
+ * variables between calls.
+ *
+ * (2) If the next call fails then 'read_only' variable is not
+ * updated. Failure could happen just because the setting is
+ * missing from the configuration file, so we ignore it here.
+ */
+ config_lookup_bool (&conf, "read_only", &read_only);
+ }
+
+ free (path);
+ }
+
+ fp = fopen (etc_filename, "r");
+ if (fp != NULL) {
+ /*
+ if (verbose)
+ fprintf (stderr, "%s: reading configuration from %s\n",
+ program_name, etc_filename);
+ */
+
+ if (config_read (&conf, fp) == CONFIG_FALSE) {
+ fprintf (stderr,
+ _("%s: %s: line %d: error parsing configuration file: %s\n"),
+ program_name, etc_filename, config_error_line (&conf),
+ config_error_text (&conf));
+ exit (EXIT_FAILURE);
+ }
+
+ if (fclose (fp) == -1) {
+ perror (path);
+ exit (EXIT_FAILURE);
+ }
+
+ config_lookup_bool (&conf, "read_only", &read_only);
+ }
+
+ config_destroy (&conf);
+}
+
+#else /* !HAVE_LIBCONFIG */
+
+void
+parse_config (void)
+{
+ /*
+ if (verbose)
+ fprintf (stderr,
+ _("%s: compiled without libconfig, guestfish configuration file ignored\n"),
+ program_name);
+ */
+}
+
+#endif /* !HAVE_LIBCONFIG */
diff --git a/fish/fish.c b/fish/fish.c
index 65a0c1dd..c4fdf799 100644
--- a/fish/fish.c
+++ b/fish/fish.c
@@ -158,6 +158,8 @@ main (int argc, char *argv[])
bindtextdomain (PACKAGE, LOCALEBASEDIR);
textdomain (PACKAGE);
+ parse_config ();
+
set_up_terminal ();
enum { HELP_OPTION = CHAR_MAX + 1 };
diff --git a/fish/guestfish.pod b/fish/guestfish.pod
index 98286d18..58f0bd9d 100644
--- a/fish/guestfish.pod
+++ b/fish/guestfish.pod
@@ -402,7 +402,9 @@ Display the guestfish / libguestfs version number and exit.
=item B<--rw>
-This option does nothing at the moment.
+This changes the I<-a>, I<-d> and I<-m> options so that disks are
+added and mounts are done read-write.
+
See L</OPENING DISKS FOR READ AND WRITE> below.
=item B<-x>
@@ -466,27 +468,30 @@ asked for without doing this.
=head1 OPENING DISKS FOR READ AND WRITE
-The guestfish (and L<guestmount(1)>) options I<--ro> and I<--rw>
-affect whether the other command line options I<-a>, I<-c>, I<-d>,
-I<-i> and I<-m> open disk images read-only or for writing.
+The guestfish, L<guestmount(1)> and L<virt-rescue(1)> options I<--ro>
+and I<--rw> affect whether the other command line options I<-a>,
+I<-c>, I<-d>, I<-i> and I<-m> open disk images read-only or for
+writing.
-In libguestfs E<lt> 1.6.2, guestfish and guestmount defaulted to
-opening disk images supplied on the command line for write. To open a
-disk image read-only you have to do I<-a image --ro>.
+In libguestfs E<le> 1.8, guestfish, guestmount and virt-rescue
+defaulted to opening disk images supplied on the command line for
+write. To open a disk image read-only you have to do I<-a image --ro>.
This matters: If you accidentally open a live VM disk image writable
then you will cause irreversible disk corruption.
-By libguestfs 1.10 we intend to change the default the other way. Disk
-images will be opened read-only. You will have to either specify
-I<guestfish --rw> or change a configuration file in order to get write
-access for disk images specified by those other command line options.
+By libguestfs 1.10 we intend to change the default the other way.
+Disk images will be opened read-only. You will have to either specify
+I<guestfish --rw>, I<guestmount --rw>, I<virt-rescue --rw>, or change
+the configuration file C</etc/libguestfs-tools.conf> in order to get
+write access for disk images specified by those other command line
+options.
-This version of guestfish has a I<--rw> option which does nothing (it
-is already the default). However it is highly recommended that you
-use this option to indicate that guestfish needs write access, and to
-prepare your scripts for the day when this option will be required for
-write access.
+This version of guestfish, guestmount and virt-rescue has a I<--rw>
+option which does nothing (it is already the default). However it is
+highly recommended that you use this option to indicate that you need
+write access, and prepare your scripts for the day when this option
+will be required for write access.
B<Note:> This does I<not> affect commands like L</add> and L</mount>,
or any other libguestfs program apart from guestfish and guestmount.
@@ -1081,6 +1086,15 @@ enough.
=over 4
+=item $HOME/.libguestfs-tools.rc
+
+=item /etc/libguestfs-tools.conf
+
+This configuration file controls the default read-only or read-write
+mode (I<--ro> or I<--rw>).
+
+See L</OPENING DISKS FOR READ AND WRITE>.
+
=item $HOME/.guestfish
If compiled with GNU readline support, then the command history
@@ -1156,7 +1170,7 @@ Richard W.M. Jones (C<rjones at redhat dot com>)
=head1 COPYRIGHT
-Copyright (C) 2009-2010 Red Hat Inc.
+Copyright (C) 2009-2011 Red Hat Inc.
L<http://libguestfs.org/>
This program is free software; you can redistribute it and/or modify
diff --git a/fish/libguestfs-tools.conf b/fish/libguestfs-tools.conf
new file mode 100644
index 00000000..7e604684
--- /dev/null
+++ b/fish/libguestfs-tools.conf
@@ -0,0 +1,24 @@
+# /etc/libguestfs-tools.conf or .libguestfs-tools.rc
+#
+# This configuration file changes the defaults for the following programs:
+# * guestfish
+# * guestmount
+# * virt-rescue
+# For more information on these programs, read the manual pages
+# (guestfish(1) etc).
+#
+# /etc/libguestfs-tools.conf is the global configuration file for all
+# of the above programs. Local users can override the global
+# configuration by copying this file into '.libguestfs-tools.rc' in
+# their home directory and modifying it accordingly.
+#
+# This file is parsed by the libconfig library. For more information
+# about the format, see
+# http://www.hyperrealm.com/libconfig/libconfig_manual.html
+
+# Uncomment the following line to make guestfish, guestmount,
+# virt-rescue be read-only by default (as if the --ro flag was given).
+# If it is commented out then the default is read-write. Note that
+# the semi-colon (;) is required.
+
+#read_only = true;
diff --git a/fish/options.h b/fish/options.h
index e0cad013..9b9aee5e 100644
--- a/fish/options.h
+++ b/fish/options.h
@@ -112,6 +112,9 @@ struct mp {
char *options;
};
+/* in config.c */
+extern void parse_config (void);
+
/* in inspect.c */
extern void inspect_mount (void);
extern void print_inspect_prompt (void);