Update FSF address.
[libguestfs.git] / daemon / lvm-filter.c
index e487c6b..07d872b 100644 (file)
@@ -1,5 +1,5 @@
 /* libguestfs - the guestfsd daemon
- * Copyright (C) 2010 Red Hat Inc.
+ * Copyright (C) 2010-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
@@ -13,7 +13,7 @@
  *
  * 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.h>
 #include <inttypes.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
-#include "daemon.h"
 #include "c-ctype.h"
+#include "ignore-value.h"
+
+#include "daemon.h"
 #include "actions.h"
 
+/* This runs during daemon start up and creates a complete copy of
+ * /etc/lvm so that we can modify it as we desire.  We set
+ * LVM_SYSTEM_DIR to point to the copy.
+ */
+static char lvm_system_dir[] = "/tmp/lvmXXXXXX";
+
+static void rm_lvm_system_dir (void);
+
+void
+copy_lvm (void)
+{
+  struct stat statbuf;
+  char cmd[64];
+  int r;
+
+  /* If /etc/lvm directory doesn't exist (or isn't a directory) assume
+   * that this system doesn't support LVM and do nothing.
+   */
+  r = stat ("/etc/lvm", &statbuf);
+  if (r == -1) {
+    perror ("copy_lvm: stat: /etc/lvm");
+    return;
+  }
+  if (! S_ISDIR (statbuf.st_mode)) {
+    fprintf (stderr, "copy_lvm: warning: /etc/lvm is not a directory\n");
+    return;
+  }
+
+  if (mkdtemp (lvm_system_dir) == NULL) {
+    perror (lvm_system_dir);
+    exit (EXIT_FAILURE);
+  }
+
+  /* Hopefully no dotfiles in there ... XXX */
+  snprintf (cmd, sizeof cmd, "cp -a /etc/lvm/* %s", lvm_system_dir);
+  r = system (cmd);
+  if (r == -1) {
+    perror (cmd);
+    rmdir (lvm_system_dir);
+    exit (EXIT_FAILURE);
+  }
+
+  if (WEXITSTATUS (r) != 0) {
+    fprintf (stderr, "cp command failed with return code %d\n",
+             WEXITSTATUS (r));
+    rmdir (lvm_system_dir);
+    exit (EXIT_FAILURE);
+  }
+
+  /* Set environment variable so we use the copy. */
+  setenv ("LVM_SYSTEM_DIR", lvm_system_dir, 1);
+
+  /* Set a handler to remove the temporary directory at exit. */
+  atexit (rm_lvm_system_dir);
+}
+
+static void
+rm_lvm_system_dir (void)
+{
+  char cmd[64];
+
+  snprintf (cmd, sizeof cmd, "rm -rf %s", lvm_system_dir);
+  ignore_value (system (cmd));
+}
+
 /* Does the current line match the regexp /^\s*filter\s*=/ */
 static int
 is_filter_line (const char *line)
@@ -52,21 +120,24 @@ is_filter_line (const char *line)
   return 1;
 }
 
-/* Rewrite the 'filter = [ ... ]' line in /etc/lvm/lvm.conf. */
+/* Rewrite the 'filter = [ ... ]' line in lvm.conf. */
 static int
 set_filter (const char *filter)
 {
-  if (verbose)
-    fprintf (stderr, "LVM: setting device filter to %s\n", filter);
+  char lvm_conf[64];
+  snprintf (lvm_conf, sizeof lvm_conf, "%s/lvm.conf", lvm_system_dir);
 
-  FILE *ifp = fopen ("/etc/lvm/lvm.conf", "r");
+  char lvm_conf_new[64];
+  snprintf (lvm_conf_new, sizeof lvm_conf, "%s/lvm.conf.new", lvm_system_dir);
+
+  FILE *ifp = fopen (lvm_conf, "r");
   if (ifp == NULL) {
-    reply_with_perror ("open: /etc/lvm/lvm.conf");
+    reply_with_perror ("open: %s", lvm_conf);
     return -1;
   }
-  FILE *ofp = fopen ("/etc/lvm/lvm.conf.new", "w");
+  FILE *ofp = fopen (lvm_conf_new, "w");
   if (ofp == NULL) {
-    reply_with_perror ("open: /etc/lvm/lvm.conf.new");
+    reply_with_perror ("open: %s", lvm_conf_new);
     fclose (ifp);
     return -1;
   }
@@ -76,19 +147,17 @@ set_filter (const char *filter)
   while (getline (&line, &len, ifp) != -1) {
     int r;
     if (is_filter_line (line)) {
-      if (verbose)
-        fprintf (stderr, "LVM: replacing config line:\n%s", line);
       r = fprintf (ofp, "    filter = [ %s ]\n", filter);
     } else {
       r = fprintf (ofp, "%s", line);
     }
     if (r < 0) {
       /* NB. fprintf doesn't set errno on error. */
-      reply_with_error ("/etc/lvm/lvm.conf.new: write failed");
+      reply_with_error ("%s: write failed", lvm_conf_new);
       fclose (ifp);
       fclose (ofp);
       free (line);
-      unlink ("/etc/lvm/lvm.conf.new");
+      unlink (lvm_conf_new);
       return -1;
     }
   }
@@ -96,20 +165,20 @@ set_filter (const char *filter)
   free (line);
 
   if (fclose (ifp) == EOF) {
-    reply_with_perror ("/etc/lvm/lvm.conf.new");
-    unlink ("/etc/lvm/lvm.conf.new");
+    reply_with_perror ("close: %s", lvm_conf);
+    unlink (lvm_conf_new);
     fclose (ofp);
     return -1;
   }
   if (fclose (ofp) == EOF) {
-    reply_with_perror ("/etc/lvm/lvm.conf.new");
-    unlink ("/etc/lvm/lvm.conf.new");
+    reply_with_perror ("close: %s", lvm_conf_new);
+    unlink (lvm_conf_new);
     return -1;
   }
 
-  if (rename ("/etc/lvm/lvm.conf.new", "/etc/lvm/lvm.conf") == -1) {
-    reply_with_perror ("rename: /etc/lvm/lvm.conf");
-    unlink ("/etc/lvm/lvm.conf.new");
+  if (rename (lvm_conf_new, lvm_conf) == -1) {
+    reply_with_perror ("rename: %s", lvm_conf);
+    unlink (lvm_conf_new);
     return -1;
   }
 
@@ -122,7 +191,7 @@ vgchange (const char *vgchange_flag)
   char *err;
   int r = command (NULL, &err, "lvm", "vgchange", vgchange_flag, NULL);
   if (r == -1) {
-    reply_with_error ("vgscan: %s", err);
+    reply_with_error ("vgchange: %s", err);
     free (err);
     return -1;
   }
@@ -149,7 +218,10 @@ reactivate (void)
 static int
 rescan (void)
 {
-  unlink ("/etc/lvm/cache/.cache");
+  char lvm_cache[64];
+  snprintf (lvm_cache, sizeof lvm_cache, "%s/cache/.cache", lvm_system_dir);
+
+  unlink (lvm_cache);
 
   char *err;
   int r = command (NULL, &err, "lvm", "vgscan", NULL);