+/* 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));
+}
+